10 5 2 dangling else problem dangling else
10 5. 2 택일문 • dangling else problem /* dangling else problem */ if (basic_math >= 90) if (math >= 90) printf(“n. Excellent studentn”); else printf(“n. Good studentn”); [해 설] - dangling else problem (질문) else문이 2개의 if 중 어느 것과 쌍을 이루는가? (답) C언어에서는 가장 안쪽의 if(즉 둘째 if)와 쌍을 이룸
11 5. 2 택일문 • dangling else problem(계속) /* dangling else problem */ if (basic_math >= 90) { if (math >= 90) printf(“n. Excellent studentn”); else printf(“n. Good studentn”); } [해 설] - C 언어에서는 {와 }를 생략해도 그 의미는 위와 같다. - 즉 C 언어의 dangling else problem은 항상 else와 가장 가까운 (즉 가장 안쪽의) if와 짝을 이루는 것으로 처리된다.
16 5. 2 택일문 • switch-case 문(break사용) switch (수식) { case 정수형수식 1: 문장리스트 1; break; case 정수형수식 2: 문장리스트 2; break; ⋮ case 정수형수식n: 문장리스트n; break; default: 문장리스트n+1; } int s; char gr; ⋮ switch (s / 10) { case 10: gr = ‘A’; break; case 9: gr = ‘A’; break; case 8: gr = ‘B’; break; case 7: gr = ‘C’; break; case 6: gr = ‘D’; break; default: gr = ‘F’; } - s값이 87이라면 s/10의 결과가 8이므로 gr에 ‘B’가 배정된 후 break 수행으로 즉시 switch 문장을 빠져나간다. [예 5. 5] switch-case (break사용)
22 5. 3 반복문 • (예 5. 7 계속) /* (2) */ int n, i, factorial; n = 5; i = 1; factorial = 1; while (i <= n) { factorial = factorial * i; i++; } - n의 factorial을 구한다. 가령 n이 5이면, 1부터 5까지 차례로 factorial에 곱해진다. - factorial = factorial * i; 대신 factorial *= i; 로 표기 가능
36 5. 4 분기문 • goto 문 – goto 문을 수행하면 해당 레이블 문으로 제어 흐름이 넘어간다. goto labl; . . . labl : /* 예 5. 13 goto 문의 사용 */ while (scanf(“%lf”, &x)) { /* lf는 long float 즉 double */ if (x < 0. 0) goto minus_warning; printf(“n. Square root of %f = %f” , x, sqrt(x)); }. . . minus_warning: printf( “n 음수값 발생”); (예 5. 13)
40 5. 6 예 제 1 2 3 4 5 6 7 8 9 8 10 11 /* File : ex 5 -1. c 문자를 입력받아 ‘a’이면 A를 출력하고, 아니면 not A를 출력 */ #include <stdio. h> int main(void) { char a; scanf(“%c”, &a); if (a == ‘a’) printf(“An”); else printf(“not An”); } 예제 5. 1 output not A
42 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 19 /* File : ex 5 -3. c 정수 3개를 각각 a, b c로 입력받아, b*b – 4*a*c가 양수이면 +, 0이면 0, 음수이면 –를 출력 */ #include <stdio. h> int main(void) { int a, b, c, d; 예제 5. 3 printf(“Enter three integers: ” ); scanf(“%d %d %d”, &a, &b, &c); d = b * b – 4 * a *c; if (d > 0) printf(“+n”); else if (d == 0) printf(“ 0n”); else printf(“-n”); } Enter three integers: - 5 3 4 output
43 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 /* File : ex 5 -4. c 정수, 연산자, 정수를 연속으로 입력받아 이를 수행하는 프로그램(연산자는 + - * / 제공) */ #include <stdio. h> int main(void) { char op; int a, b; 예제 5. 4 printf(“Enter an expression(e. g. 4*6) : ” ); scanf(“%d%c%d”, &a, &op, &b); if (op == ‘+’) printf(“%d+%d=%dn”, a, b, a + b); else if (op == ‘-’) printf(“%d-%d=%dn”, a, b, a - b); else if (op == ‘*’) printf(“%d*%d=%dn”, a, b, a * b); else if (op == ‘/’) printf(“%d/%d=%dn”, a, b, a / b); else printf(“No such operator %cn”, op); } output Enter an expression(e. g. 4*6) : 5*7=35
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 /* File : ex 5 -5. c 정수, 연산자, 정수를 연속으로 입력받아 처리 단, switch 문을 사용하시오. */ #include <stdio. h> int main(void) { char op; int a, b; printf(“수식을 입력하시오(예 4*6) : ”); scanf(“%d%c%d”, &a, &op, &b); switch (op) { case ‘+’: printf(“%d+%d=%dn”, a, b, a + b); break; case ‘-’: printf(“%d-%d=%dn”, a, b, a - b); break; case ‘*’: printf(“%d*%d=%dn”, a, b, a * b); break; case ‘/’: printf(“%d/%d=%dn”, a, b, a / b); break; default: printf(“No such operator %cn”, op); } } 44 예제 5. 5
45 1 2 3 4 5 6 7 8 9 10 11 C C C /* File : ex 5 -6. c i의 초기값 15에서 시작하여 3씩 감소하면서, i가 음수가 아닌한 C Programming이라 새 줄에 출력하는 프로그램을 for 문장을 이용하여 작성하시오. */ #include <stdio. h> int main(void) { int i; for (i = 15; i >= 0; i = i – 3) printf(“C Programmingn”); } Programming Programming 예제 5. 6 output
46 1 2 3 4 5 6 7 8 9 10 11 12 13 /* File : ex 5 -7. c While 문을 이용하여 2의 10승을 구하는 프로그램 */ #include <stdio. h> int main(void) { int i = 1; int ans = 1; while (i <= 10) { ans = ans * 2; i++; } printf(“ 2^10=%dn”, ans); } 예제 5. 7 output 2^10=1024
1 2 3 4 5 6 7 8 9 10 11 12 /* File : ex 5 -9. c 구구단을 9줄로 출력하시오. */ #include <stdio. h> int main(void) { int i, j; for (i = 1; i < 10; i++) { for (j = 1; j < 10; j++) printf(“ %2 d”, i*j); printf(“n”); } } - i값을 바꾸어 반복되는 for 문장 내에 2개의 문장을 수행한다. - 한 문장은 j를 바꾸어 반복되는 for문장이고, 다음 문장은 줄바꿈 문자를 출력한다. 1 2 3 4 5 6 7 8 9 2 4 6 8 10 12 14 16 18 3 6 9 12 15 18 21 24 27 4 8 12 16 20 24 28 32 36 5 10 15 20 25 30 35 40 45 6 12 18 24 30 36 42 48 54 7 14 21 28 35 42 49 56 63 8 16 24 32 40 48 56 64 72 9 18 27 36 45 54 63 72 81 48 예제 5. 9 [해 설] output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 /* File : ex 5 -10. c 다음 식을 만족하는 A, B, C, D를 구하시오. 단, A, B, C, D는 각각 0 -9 중 한 개의 수이다. A B C + C A C ------B D D */ #include <stdio. h> int main(void) { int a, b, c, d, sum; for (a = 1; a < 10; a++) for (b = 1; b < 10; b++) for (c = 1; c < 10; c++) for (d = 0; d < 10; d++) { sum = (100 * a + 10 * b + c) + (100 * c + 10 * a + c); if (sum == 100 * b + 10 * d + d) printf(“A=%d B=%d C=%d D=%dn”, a, b, c, d); } } 49 예제 5. 10 output A=1 A=2 A=5 B=3 B=6 B=9 B=7 C=2 C=4 C=6 C=1 D=4 D=8 D=2
1 2 3 4 5 6 /* File : ex 5 -14. c 다음 프로그램이 수행하는 일을 설명하시오. */ #include <stdio. h> int main(void) { int c, blank_cnt = 0, letter_cnt = 0, nl_cnt = 0, other_cnt = 0; 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 while ((c = getchar()) != EOF) /* while 몸체에 { } 불필요 */ if (c == ‘ ’ && c == ‘t’) blank_cnt++; else if (c >= ‘a’ && c <= ‘z’ || c >= ‘A’ && c <= ‘Z’) letter_cnt++; else if (c == ‘n’) nl_cnt++; else other_cnt++; printf(“n%12 s%12 s”, “blanks”, “letters”, “lines”, “others”, “total”); printf(“nn%12 d%12 d%12 dnn” , blank_cnt, letter_cnt, nl_cnt, other_cnt, blank_cnt + letter_cnt + nl_cnt + other_cnt); } 54 예제 5. 14
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 /* File : ex 5 -15. c 임의 개의 자료를 읽어 들여 점수와 letter grade를 출력 */ #include <stdio. h> int main(void) { int points; char let_gr; printf(“POINTS LETTER_GRADEn” ); printf(“ ----------n” ); while (scanf(“%d”, &points) == 1) { switch (points / 10) { case 10: case 9: let_gr = ‘A’; break; case 8: let_gr = ‘B’; break; case 7: let_gr = ‘C’; break; case 6: let_gr = ‘D’; break; default: let_gr = ‘F’; } printf(“ %d %cn”, points, let_gr); } } 56 예제 5. 15
- Slides: 57