TIL_21.01.26 ๐Ÿƒ๐Ÿฝโ€โ™‚๏ธ๐Ÿƒ๐Ÿฝโ€โ™‚๏ธ

23894 ๋‹จ์–ด TILTIL

CS50

C์—๋Š” ์—ฌ๋Ÿฌ ์ž๋ฃŒํ˜•์ด ์žˆ๊ณ  ๊ฐ๊ฐ์˜ ์ž๋ฃŒํ˜•์€ ์„œ๋กœ ๋‹ค๋ฅธ ํฌ๊ธฐ์˜ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ฐจ์ง€ํ•œ๋‹ค. char ํƒ€์ž…์˜ ๋ณ€์ˆ˜๋ฅผ ์ƒ์„ฑํ•˜๋ฉด ๋ฉ”๋ชจ๋ฆฌ์˜ ํ•œ ์‚ฌ๊ฐํ˜• ์•ˆ์— ๊ทธ ๋ณ€์ˆ˜์˜ ๊ฐ’์ด ์ €์žฅ๋œ๋‹ค.

Array

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

int main(void)
{
    int score1 = 72;
    int score2 = 73;
    int score3 = 33;

    printf("Average: %i\n", (score1 + score2 + score3)/3);
}

์„ธ ๊ฐœ์˜ ์ ์ˆ˜๋ฅผ ์ €์žฅํ•˜๊ณ  ํ‰๊ท ์„ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์ด๋‹ค.
ํ•˜์ง€๋งŒ ์ด ์ฝ”๋“œ๊ฐ€ ์ตœ์„ ์˜ ๋””์ž์ธ์ด ์•„๋‹ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” ์ ์ˆ˜๊ฐ€ 3๊ฐ€์ง€๋กœ ๊ณ ์ •์ ์ด๊ณ  ์†Œ์ˆ˜์ ์— ๋Œ€ํ•œ ๋Œ€์‘๋„ ์—†๋‹ค. ๋ฐ˜๋ณต์ ์ธ ์ž‘์—…์ด ๋ˆˆ์— ๋ณด์ธ๋‹ค.

๋”ฐ๋ผ์„œ ๊ฐœ์„ ์˜ ์—ฌ์ง€๊ฐ€ ์žˆ๋Š” ํ”„๋กœ๊ทธ๋žจ์ธ ๊ฒƒ์ด๋‹ค.

์ผ๋‹จ ์ ์ˆ˜์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋” ๋งŽ์•„์งˆ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด ๋ฐฐ์—ด์ด๋‹ค.

๋ฐฐ์—ด์€ ๋™์ผํ•œ ์ž๋ฃŒํ˜•์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฉ”๋ชจ๋ฆฌ์ƒ์— ์—ฐ์ด์–ด์„œ ์ €์žฅํ•˜๊ณ  ์ด๋ฅผ ํ•˜๋‚˜์˜ ๋ณ€์ˆ˜๋กœ ๊ด€๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋œ๋‹ค.

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

int main(void)
{
    int scores[3];  // ํฌํ‚ค๊ฐ€ 3์ธ ๋ฐฐ์—ด์„ ๋งŒ๋“ค๊ณ  
    scores[0] = 72; // ํ•ด๋‹น index์— ๊ฐ’์„ ํ• ๋‹น
    scores[1] = 73;
    scores[2] = 33;

    printf("Average: %i\n", (scores[0] + scores[1] + scores[2])/3);
}

ํ•˜์ง€๋งŒ ์ด ์ฝ”๋“œ์—์„œ๋„ ์ ์ˆ˜์˜ ๊ฐœ์ˆ˜๊ฐ€ ๊ณ ์ •๋œ๋‹ค๋Š” ๋ฌธ์ œ์ ์ด ์—ฌ์ „ํžˆ ์กด์žฌํ•œ๋‹ค.
๋˜ ์ ์ˆ˜๊ฐ€ ์ž…๋ ฅ์„ ๋ฐ›๋Š”๊ฒŒ ์•„๋‹Œ ์ฝ”๋“œ ๋‚ด๋ถ€์— ์ด๋ฏธ ์ง€์ •์ด ๋˜์–ด์žˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ฐœ์„  ๊ฐ€๋Šฅํ•œ ๋ฐ˜๋ณต๋“ค๋„ ์—ฌ์ „ํžˆ ์กด์žฌํ•œ๋‹ค.

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

float average (int length, int array[]);

int main(void)
{
    int n = get_int("Number of scores: ");
    int scores[n];

    for (int i = 0; i < n; i++)
    {
        scores[i] = get_int("Score %i: ", i + 1);
    }
    printf("Average: %.1f\n", average(n, scores));
}

float average (int length, int array[])
{
    int sum =0;
    for (int i = 0; i < length; i++)
    {
        sum += array[i];
    }
    return (float) sum / (float) length;
}

์ ์ˆ˜์˜ ๊ฐœ์ˆ˜๋ฅผ ์ž…๋ ฅ ๋ฐ›๊ณ  ๊ทธ ๊ฐ’์„ ์ด์šฉํ•ด n๊ฐœ์˜ ์‚ฌ์ด์ฆˆ๋ฅผ ๊ฐ€์ง„ ๋ฐฐ์—ด์„ ๋งŒ๋“ค๊ณ 
n๋ฒˆ ์ ์ˆ˜ ์ž…๋ ฅ์„ ๋ฐ›๋Š”๋‹ค. ๋˜ average ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์„œ ๋ฐฐ์—ด์˜ ๊ธธ์ด์™€ ๋ฐฐ์—ด์„ ์ธ์ž๊ฐ’์œผ๋กœ ์ „๋‹ฌํ•ด ํ‰๊ท  ๊ฐ’์„ ๊ตฌํ•ด์ค€๊ฑธ ์ถœ๋ ฅํ•œ๋‹ค.

๋ฌธ์ž์—ด๊ณผ ๋ฐฐ์—ด

string์€ char ์ž๋ฃŒํ˜•์˜ ๋ฐ์ดํ„ฐ๋“ค์˜ ๋ฐฐ์—ด์ด๋‹ค.
string = "HI!"; ๋Š” ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ์ €์žฅ๋œ๋‹ค.

๋ฌธ์ž์—ด์€ ๋”ฐ๋กœ ๊ธธ์ด๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์ปดํ“จํ„ฐ๊ฐ€ ๋ฌธ์ž์—ด์ด ์ข…๋ฃŒ๋˜๋Š” ์ง€์ ์„ ์•Œ๊ธฐ์œ„ํ•ด์„œ ๋ฌธ์ž์—ด์˜ ๋์€ ํ•ญ์ƒ null ์ข…๋‹จ ๋ฌธ์ž \0์ด ๋“ค์–ด๊ฐ„๋‹ค.

string names[4];

names[0] = "EMMA";
names[1] = "RODRIGO";
names[2] = "BRIAN";
names[3] = "DAVID";

printf("%s\n", names[0]);
printf("%c%c%c%c\n", names[0][0], names[0][1], names[0][2], names[0][3]);

์ด๋Ÿฌํ•œ ์ฝ”๋“œ๊ฐ€ ์žˆ๋‹ค๋ฉด ์‹ค์ œ ๋ฉ”๋ชจ๋ฆฌ ์ƒ์—๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์ €์žฅ๋œ๋‹ค.

๋ฌธ์ž์—ด์˜ ํ™œ์šฉ

๋ฌธ์ž์—ด ๊ธธ์ด ํƒ์ƒ‰

๋ฌธ์ž์—ด ๊ธธ์ด ํƒ์ƒ‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์ฒซ๋ฒˆ์งธ๋กœ ๋ฐ˜๋ณต๋ฌธ์„ ์ด์šฉํ•ด ๋ฌธ์ž์—ด์„ ์ˆœํšŒํ•˜๋ฉฐ \0๊ณผ ์ผ์น˜ํ•˜๋Š”์ง€ ๊ฒ€์‚ฌํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ํ•˜์ง€๋งŒ strlenํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜๋ฉด ์‰ฝ๊ฒŒ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.

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

int main(void)
{
  string s = get_string("input: ");
  printf("Output:\n");
  for (int i =0, n = strlen(s); i < n; i++)
  {
    printf("%c\n",s[i]);
  }
}

๋ฌธ์ž์—ด ํƒ์ƒ‰ ๋ฐ ์ˆ˜์ •

์ž…๋ ฅ๋ฐ›์€ ๋ฌธ์ž์—ด์„ ๋Œ€๋ฌธ์ž๋กœ ๋ฐ”๊ฟ”์ฃผ๋Š” ๋ฐฉ๋ฒ•์€ ํฌ๊ฒŒ 2๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค.
์•„์Šคํ‚ค ์ฝ”๋“œ๋ฅผ ์ฐธ๊ณ ํ•ด ๋ณ€๊ฒฝํ•ด์ฃผ๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค.

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

int main(void)
{
  string s = get_string("Before: ");
  printf("After:  ");
  for (int i = 0, n = strlen(s); i < n; i++)
  {
    if (s[i] >= 'a' && s[i] <= 'z')
    {
      printf("%c", s[i] - 32);
    }
    else
    {
      printf("%c", s[i]);
    }
  }
  printf("\n");
}

๋˜ ctype ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์— ์žˆ๋Š” toupper ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

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

int main(void)
{
    string s = get_string("Before: ");
    printf("After:  ");
    for (int i = 0, n = strlen(s); i < n; i++)
    {
        printf("%c", toupper(s[i]));
    }
    printf("\n");
}

CS50 Manual Pages ๋ฅผ ์ฐธ์กฐํ•ด์„œ ๋‹ค์–‘ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ํ•จ์ˆ˜๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

๋ช…๋ นํ–‰ ์ธ์ž

์ปดํŒŒ์ผ ํ›„ ์ €์žฅํ•˜๊ณ ์ž ํ•˜๋Š” ํŒŒ์ผ๋ช…๊ณผ ๊ฐ™์ด ์ถ”๊ฐ€์ ์ธ ์ •๋ณด๋ฅผ ํ•จ๊ป˜ ์ค„ ์ˆ˜ ์žˆ๋‹ค.
์ด๋Ÿฌํ•œ ์ •๋ณด๋ฅผ ๋ช…๋ นํ–‰ ์ธ์ž๋ผ๊ณ  ํ•œ๋‹ค.

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

int main(int argc, string argv[])
{
    if (argc == 2)
    {
        printf("hello, %s\n", argv[1]);
    }
    else
    {
        printf("hello, world\n");
    }
}

์ฒซ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜ argc๋Š” main ํ•จ์ˆ˜๊ฐ€ ๋ฐ›๊ฒŒ ๋  ์ž…๋ ฅ์˜ ๊ฐœ์ˆ˜์ด๋‹ค. ๊ทธ๋ฆฌ๊ณ  argv[]๋Š” ๊ทธ ์ž…๋ ฅ์ด ํฌํ•จ๋˜์–ด ์žˆ๋Š” ๋ฐฐ์—ด์ด๋‹ค. argv[0] ์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ํ”„๋กœ๊ทธ๋žจ ์ด๋ฆ„์œผ๋กœ ์ €์žฅ๋œ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด ์œ„ ํ”„๋กœ๊ทธ๋žจ์„ arg.c ๋กœ ์ €์žฅํ•˜๊ณ  ์ปดํŒŒ์ผ ํ•œ ํ›„์— ./argc Kim ์œผ๋กœ ์‹คํ–‰ํ•˜๋ฉด hello, Kim์˜ ๊ฐ’์ด ์ถœ๋ ฅ์ด ๋œ๋‹ค.
argc == 2, argv[1] == Kim

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ