Coding/[EdX] CS 50

[Week 2] Array

๊น€์œ ๋‹ˆ์ฝ˜ 2021. 12. 5. 22:45

 

- make ๋Œ€์‹  ์“ธ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ - 

clang : ์ปดํŒŒ์ผ๋Ÿฌ

ํด๋žญ์€ ์กฐ๊ธˆ ๋” ๋งŽ์€ ๊ฒƒ์„ ์š”๊ตฌํ•œ๋‹ค. 

clang์„ ์‚ฌ์šฉํ•ด๋ณด์ž.

clang hello.c

์—”ํ„ฐ๋ฅผ ์น˜๋ฉด ์•”๊ฑฐ๋„ ์•ˆ์ผ์–ด๋‚˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ด๋ฉด ์ปดํŒŒ์ผ ์ž˜ ๋œ๊ฒƒ.

ls ํ•˜๋ฉด ์ด์ƒํ•œ a.out์„ ๋ณด๊ฒŒ๋  ๊ฒƒ

 

์ด์ƒํ•œ๋ฐ? 

a.out ์ง€์šด๋‹ค. 

 

clang -o hello hello.c

์ปค๋งจ๋“œ๋ผ์ธ ์•„๊ทœ๋จผํŠธ 

 

 

==> ๊ฒฐ๋ก  : ์ปดํŒŒ์ผํ•˜๋ฉด clang ์–ด์ฉŒ๊ตฌ ํ•˜๋ฉด์„œ ๋‹ค clang์„ ์‚ฌ์šฉํ•ด์„œ ์ปดํŒŒ์ผํ•ด์ค€๋‹ค.

๋„ˆ๋Š” clang ์‚ฌ์šฉํ•ด์„œ ์Šค์Šค๋กœ ์ปดํŒŒ์ผํ•  ์ˆ˜๋„ ์žˆ์–ด. 

 

์ปดํŒŒ์ผ์˜ ๋‹จ๊ณ„: (Lower-level detail) 

1) ํ”„๋กœ์„ธ์‹ฑ(Source code)

- ํ”„๋ฆฌํ”„๋กœ์„ธ์‹ฑ ์Šคํ…: ์™œ ํ•˜๋Š”๊ฑฐ์ง€? #์ด ๋“ค์–ด๊ฐ„ ๋ฌธ์žฅ ์™œ ์จ์•ผํ•˜๋‹ˆ? ==> defining function / ์ปดํŒŒ์ผ๋Ÿฌ ํŠธ๋ ˆ์ด๋‹ ์‹œํ‚ค๋Š” ๊ฒƒ

header file : hint๋ฅผ ์ฃผ๋Š” ๊ฒƒ์ด์•ผ. 

2) ์ปดํŒŒ์ผ๋ง(Assembly code)

- ์„ธ์ƒ์—๋Š” ๋‹ค์–‘ํ•œ cpu๊ฐ€ ์žˆ๋Š”๋ฐ, ์–ด์…ˆ๋ธ”๋ฆฌ ์ฝ”๋“œ, ์ค‘๊ฐ„์— main, get_string, printf --> ์žˆ์ž–์•„. 

์–ด์…ˆ๋ธ”๋ฆฌ ์ฝ”๋“œ๋กœ ๋ฐ”๊ฟˆ 

์‚ฌ๋žŒ๋“ค์ด ์˜›๋‚ ์—๋Š” ์–ด์…ˆ๋ธ”๋ฆฌ ์ผ๋Š”๋ฐ ์ด์ œ๋Š” C, ํŒŒ์ด์ฌ ๋“ฑ์„ ์“ด๋‹ค. 

3) ์–ด์Ž”๋ธ”๋ง(Machine code) 

์–ด์…ˆ๋ธ”๋ง ์ฝ”๋“œ๋ฅผ ๋จธ์‹ ์ฝ”๋“œ(0,1)๋กœ ๋ฐ”๊ฟˆ

4) Linking

๋ชจ๋“  0101010๋“ค(๋„ˆ์˜ #, ์ˆซ์ž, ๋ฌธ์ž ๋“ฑ๋“ฑ)์„ ํ•˜๋‚˜์˜ ๋ธ”๋Ÿญ์œผ๋กœ ๋ชจ์•„์„œ --> ๋„ˆ์˜ ์ฝ”๋“œ ๋ธ”๋Ÿญ๋“ค๋กœ ์‚ฌ์šฉํ•˜๋Š”๊ฑฐ์•ผ

 

 

์šฐ๋ฆฌ๊ฐ€ ์ฒ˜์Œ ์‹œ์Šคํ…œ์„ ๋‹ค์šด๋ฐ›์„๋•Œ ์ปดํŒŒ์ผ๋Ÿฌ, ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋“ฑ์ด .h ํŒŒ์ผ์˜ ํ˜•ํƒœ๋กœ ์ €์žฅ์ด ๋œ๋‹ค. 

 

 

์—๋Ÿฌ ๊ดœ์ฐฎ์•„ 

20% ์ •๋„๋Š” ๋‚˜๋„ ์—๋Ÿฌ ์ƒ๊ฒจ

์‚ฌ๋žŒ๋“ค์€ ์‹ค์ˆ˜ ํ•˜์ž–์•„. 

 

์–ด๋–ป๊ฒŒ ๋””๋ฒ„๊ทธํ•  ์ˆ˜ ์žˆ์„๊นŒ? 

 

์–ด๋–ค ํ•˜๋ฒ„๋“œ ์ปดํ“จํ„ฐ ๊ด€๋ จ ์‚ฌ๋žŒ์ด ์ปดํ“จํ„ฐ๊ฐ€ ๋ฌธ์ œ์žˆ์—ˆ๋Š”๋ฐ, ์ง„์งœ ๋ฒŒ๋ ˆ๊ฐ€ ์žˆ์–ด์„œ ๋ฒ„๊ทธ ๋ผ๊ณ  ํ•จ. 

๋ฒŒ๋ ˆ ๋…ธํŠธ์— ๋ถ™์—ฌ๋‘ 

 

style50: ์‚ฌ๋žŒ์ด ์ฝ๊ธฐ ์ข‹๊ฒŒ ์–‘์‹ ๋ฐ”๊ฟ”์คŒ

 

๋‹ค๋ฅธ ํˆด๋„ ์ข‹์€๋ฐ

ํ•˜๋‚˜๋Š” ์„ธ๊ณ„์ ์ธ ๋””๋ฒ„๊น… ํ‚ค๊ฐ€ ์žˆ์–ด : printf

๋‚˜ # 10๊ฐœ ํ”„๋ฆฐํŠธํ•˜๊ณ  ์‹ถ์€๋ฐ ์ž˜ ํ•˜๊ณ  ์žˆ๋‚˜ ๋ณด๊ณ  ์‹ถ์„๋•Œ, ์•„๋ž˜์ฒ˜๋Ÿผ ์ฝ”๋“œ ์น  ์ˆ˜ ์žˆ์Œ 

 

 

 

์ข€ ๋” ํŒŒ์›Œํ’€ํ•œ ๋””๋ฒ„๊น… ํˆด์„ ์ฐพ์•„๋ณด๋„๋ก ํ•˜์ž. debug50

์ด๊ฑด ์Šคํ… ๋ฐ”์ด ์Šคํ…์œผ๋กœ ๋„ˆ์˜ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰์‹œํ‚ฌ๊ฑฐ์•ผ. 

์ฒœ์ฒœํžˆ. ๊ทธ๋ž˜์„œ ์–ด๋–ค ์ผ์ด ๋ฒŒ์–ด์ง€๋Š”์ง€ ๋ณผ ์ˆ˜ ์žˆ์ง€.

debug50์„ ์‹คํ–‰์‹œํ‚ค๋ฉด break point ์ƒ๊ธฐ๊ณ , ํ•˜๋‚˜ํ•˜๋‚˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ. 

* ํ„ฐ๋ฏธ๋„์—์„œ ๋ฐฉํ–ฅํ‚ค ์‚ฌ์šฉํ•˜๋ฉด ๋„ˆ๊ฐ€ ์‚ฌ์šฉํ•œ ๋ช…๋ น์–ด๋“ค ๋ณผ ์ˆ˜ ์žˆ์–ด. 

 

 

get_negative_int():

debug50 ./buggy1

 

step into๋ฅผ ๋ˆŒ๋Ÿฌ๋ณด์ž.

function ์†์œผ๋กœ ๋“ค์–ด๊ฐ„๋‹ค. 

๊ทธ๋‹ค์Œ์— step over์„ ๋ˆ„๋ฅด๋ฉด, 

๋””๋ฒ„๊ฑฐ๋Š” ์œ— ๋ผ์ธ์œผ๋กœ ๋„˜์–ด๊ฐ„๋‹ค. 

 

 


 

 

๊ณผ์ œ

Lab2:  scrabble.c
๋ฌธ์ œ ๋ฐ”๋กœ๊ฐ€๊ธฐ: 

 

Lab 2: Scrabble - CS50x 2021

Harvard University's introduction to the intellectual enterprises of computer science and the art of programming.

cs50.harvard.edu

#include <ctype.h>
#include <cs50.h>
#include <stdio.h>
#include <string.h>

// Points assigned to each letter of the alphabet
int POINTS[] = {1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10};

int compute_score(string word)
{
    //keep track of score
    int score = 0;

    // Compute score for each charcter
    for (int i = 0, len = strlen(word); i < len; i++)
    {
        if (isupper(word[i]))
        {
            score += POINTS[word[i] - 'A'];
        }
        else if (islower(word[i]))
        {
            score += POINTS[word[i] - 'a'];
        }

    }
    return score;
}

;

int main(void)
{
    // Get input words from both players
    string word1 = get_string("Player 1: ");
    string word2 = get_string("Player 2: ");

    // Score both words
    int score1 = compute_score(word1);
    int score2 = compute_score(word2);

    // TODO: Print the winner
    if (score1 > score2)
    {
        printf("Player 1 wins!");
    }
    else if (score1 < score2)
    {
        printf("Player 2 wins!");
    }
    else
    {
        printf("Tie!");
    }
}

 

 

Readability.c

์™€ ์ด ๊ณผ์ œ ์ข€ ์žฌ๋ฐŒ๋‹ค...! ๊ณผ์ œ ๋„ˆ๋ฌด ์นœ์ ˆํ•˜๊ณ  ์ตœ๊ณ ๋„ค ์ง„์งœ 

์˜์–ด ์ง€๋ฌธ๋“ค์ด ๊ณผ์—ฐ ๋ช‡ ํ•™๋…„์ด ์ฝ๊ธฐ์— ์ ํ•ฉํ•œ์ง€ ์•Œ๋ ค์ฃผ๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“ค์–ด๋ณด์ž!

์•ž์œผ๋กœ ์˜์–ด ๋ฌธ์žฅ ์ฝ๊ธฐ ์ „์— ํ”„๋กœ๊ทธ๋žจ ๋งŒ๋“ค์–ด๋‘๊ณ  ์ด๊ฑฐ ๋Œ๋ ค๋ณด๊ณ  ์ฝ์–ด์•ผ๊ฒ ๋‹ค.  

 

#include <stdio.h>
#include <cs50.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
int main(void)
{
    string text = get_string("Text: ");
    
    //string length
    int i = strlen(text);
    int letters = 0; 
    int words = 0; 
    int sentences = 0;
    
    //Loop
    for (int a = 0; a < i ; a++)
    {
        char c = text[a];
        
        //count letters 
        if (isalpha(c) != 0)
        {
            letters++;
        }
        
        //count words
        if (c == ' ')
        {
            words++;
        }
        
        //count sentencs
        if (c == '.' || c == '!' || c == '?')
        {
            sentences++;
        }
    
      
    }
    
    // ๋„์–ด์“ฐ๊ธฐ์—๋Š” ๋‹จ์–ด ๊ฐœ์ˆ˜ ์…€ ๋•Œ +1 ํ•ด์ฃผ์–ด์•ผ ํ•จ์œผ๋กœ 
    words = words + 1;
    
    float L = ((float)letters / (float)words) * 100;
    float S = ((float)sentences / (float)words) * 100;
    float subindex = 0.0588 * L - 0.296 * S - 15.8;
    int index = round(subindex);
   
    
    // PRINT THE RESULT 
    if (index < 1)
    {
        printf("Before Grade 1\n");
    }
    else if (index >= 16)
    {
        printf("Grade 16+\n");
    }
    else 
    {
        printf("Grade %i\n", index);
    }

}

 

 

Caesar.c

๋ฌธ์ œ ๋ฐ”๋กœ๊ฐ€๊ธฐ 

 

Caesar - CS50x 2021

Harvard University's introduction to the intellectual enterprises of computer science and the art of programming.

cs50.harvard.edu

 

์นด์ด์‚ฌ๋ฅด์˜ ์•”ํ˜ธ ํ•ด๋… ํ”„๋กœ๊ทธ๋žจ 

์ƒˆ๋กœ์šด ์˜๋‹จ์–ด๋„ ๋ฐฐ์› ๋‹ค.

cipher : ์•”ํ˜ธ์˜ 

#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, string argv[])
{
    // if arc doesn't has 2 argument prints error
    if (argc != 2)
    {
        printf("Usage: ./caesar key\n");
        return 1;
    }
    else
    {
        // if there are 2 arguments checks if all the second argument contains digits
        string k = argv[1];
        for (int i = 0; i < strlen(k); i++)
        {
            if (isdigit(k[i]) == 0)
            {
                printf("Usage: ./caesar key\n");
                return 1;

            }
        }
        // print commands below for testing purpose

        // printf("Success\n");
        // printf("%s\n",k);

        // converts second argument to int
        int myKey = atoi(k);

        // asks users for plaintext to be ciphered
        string plaintext = get_string("Plaintext: ");

        // For loop to convert to ciphertext
        for (int i = 0; i < strlen(plaintext); i++)
        {
            if (isupper(plaintext[i]))
            {
                // by given formula need to change base so that A is 0
                // when you minus capital A ASCII value by 65 it becomes 0
                plaintext[i] = (plaintext[i] - 65);
                plaintext[i] = (plaintext[i] + myKey) % 26;
                plaintext[i] = (plaintext[i] + 65);
                // printf("%c",plaintext[i]);
            }
            if (islower(plaintext[i]))
            {
                plaintext[i] = (plaintext[i] - 97);
                plaintext[i] = (plaintext[i] + myKey) % 26;
                plaintext[i] = (plaintext[i] + 97);
                // printf("%c",plaintext[i]);

            }
            if (isalpha(plaintext[i]) == 0)
            {
                plaintext[i] = plaintext[i];
                // printf("%c",plaintext[i]);
            }
        }
        printf("Ciphertext: %s", plaintext);
        printf("\n");

    }

}

'Coding > [EdX] CS 50' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Week 4] Memory  (0) 2021.12.06
[Week 3] Algorithms  (0) 2021.12.05
[Week1] C  (0) 2021.11.29
[Week 0] Scratch  (0) 2021.11.23
Introduction to Mathematical Thinking ๋กœ ๋ณธ๊ฒฉ ์‹œ์ž‘  (0) 2021.10.08