7차시:
제어의 흐름 함수
• 프로그래밍 및 실험
• 제 7주
• 동국대학교 조영석
이 문서는 나눔글꼴로 작성되었습니다. 설치하기
2
3.10 수학함수
sqrt ( ) pow ( ) exp ( ) log ( ) sin ( ) cos ( ) tan ( ) etc..
(예) #include < math.h >
#include < stdio.h >
void main( ) { double x;
printf("\n%s", "the square root of x\n"
"x raised to the power of x\n");
while (1) {
printf("Input a real number : ");
if (scanf("%lf", &x) != 1) /* number of input fields */
break; /* successfully scanned, */
/* converted, and stored. */
if (x >= 0.0)
printf("\n%15s %22.15e\n"
"%15s %22.15e\n"
"%15s %22.15e\n\n", "x = ", x,
"sqrt(x) = ", sqrt(x),
"pow(x, x) = ", pow(x, x) );
else {
printf("The number must be nonnegative. \n");
break;
} } }
3
- abs( )와 fabs( )
• abs(x) : int형의 절대값을 int 형으로 return.
• fabs(x) : double형의 절대값을 double 형으로 return.
4
3.11 변환과 cast
- 자동변환(묵시적 변환, coercion, 승격, widening)
if 두 피연산자 중 하나가 long double,
다른 피연산자는 long double로 변환됨.
else if 두 피연산자 중 하나가 double,
다른 피연산자는 double로 변환됨.
else if 두 피연산자 중 하나가 float,
다른 피연산자는 float로 변환됨.
else 두 피연산자에 다음의 rule에 따라 정수적 승격이 일어난다.
5
< rule >
if 두 피연산자 중 하나가 unsigned long형,
다른 피연산자는 unsigned long형으로 변환됨.
else if 두 피연산자 중 하나가 long형, 다른 하나가 unsigned형, if long형이 unsigned형의 모든 값 표현가능,
unsigned형 → long형으로 변환됨.
else 두 피연산자는 unsigned long형으로 변환됨.
else if 두 피연산자 중 하나가 long형,
다른 피연산자는 long형으로 변환됨.
else if 두 피연산자 중 하나가 unsigned형,
다른 피연산자는 unsigned형으로 변환됨.
else 두 연산자 모두 int형.
6
(주의)
int i;
double d;
d = i; /* double로 승격, 문제없음 */
i = d; /* 소숫점이하 손실 */
선언
char c; short s; int i;
long l; unsigned u; unsigned long ul;
float f; double d; long double ld;
수 식 형 수 식 형
c - s / i int u * 7 - i unsigned u * 2.0 - i double f * 7 - i float
c + 3 int 7 * s * ul unsigned long c + 5.0 double ld + c long double d + s double u - ul unsigned long 2 * i / l long u - l system-dependent
7
- 캐스트 : 자동 변환에서 보다 programmer의 의도를 더 명확히 판단할 수 있음.
(예)
int i;
(double) i /* int형이 double형으로 변환됨 */
(long)('A' + 1.0)
x = (float) ((int)y + 1);
/* y의 값은 int형으로 변환되나 y의 형은 불변 */
(double) (x = 77)
/* 수식의 return값은 변환되나 x의 형은 불변 */
NOT (double) x = 77
/* x의 형은 변환되지 않으므로 error */
8
3.12 16진 상수와 8진 상수
- 16진 상수 : 0xnnn‥‥n , 0 ≤ n ≤ F - 8진 상수 : 0nnn‥‥n , 0 ≤ n ≤ 7
10진수 2진수 16진수 8진수
0 00000000 00 000
1 00000001 01 001
2 00000010 02 002
3 00000011 03 003
‥‥
31 00011111 1F 037
32 00100000 20 040
‥‥
188 10111100 BC 274
‥‥
254 11111110 FE 376
255 11111111 FF 377
9
- printf( ) 문에서 표현.
printf("%d %x %o", 54, 0x36, 066);
printf("%d %x %o", 0x36, 066, 54);
printf(“%#o %#x”, 54, 66);
%o: 8진수로 출력
%#o: 앞에 0을 숫자 앞에 붙여서 8진수로 출력
%x: 16진수로 출력
%#x: 앞에 0x를 숫자 앞에 붙여서 16진수로 출력
%X: 16진수를 영문자 대문자를 사용하여 출력
%#X: 앞에 0X를 숫자 앞에 붙여서 영문자 대문자를 사용 하여 16진수로 출력
출력결과:
54 36 66 54 36 66 066 0x42
10
Chapter 4. 제어의 흐름 4.1 관계, 등가, 논리연산자 - 관계 연산자 : < > <= >=
- 등가 연산자 : == !=
- 논리 연산자 : ! && ||
! : 단항 연산자, ! 이외에는 모두 이항 연산자.
논리연산자는 연산 후 정수값 0 또는 1을 생성.
0, 0.0, NULL, '0' : 거짓(false) 기타 : 참(true)
11
4.2 관계 연산자와 수식 (예)
a > 3 a < b
-1.3 >= (2.0 * x + 3.3)
a < b < c /* 구문은 올바르나 혼동의 위험 */
(주의)
3 < 7 < 5 : 참(true) - false를 예측
∵ (3 < 7) < 5
1 < 5 ∴ 참(true)
3 < 1 < 5도 true임 (3<1 == 0, 0<5 == 1, ∴ true)
12
선언 및 초기화
char c = 'w';
int i = 1, j = 2, k = -7;
double x = 7e+33, y = 0.001;
수식 동일한 수식 결과 값
'a' + 1 < c ('a' + 1) < c 1 - i - 5 * j >= k + 1 ((- i) - (5 * j)) >= (k + 1) 0 3 < j < 5 (3 < j) < 5 1 x - 3.333 <= x + y (x - 3.333) <= (x + y) 1
x < x + y x < (x + y) 0
x가 매우 큰 수이고 y는 아주 작은 경우 y는 0.0으로 표현되 며 (x+y)의 값은 x값과 같은 값으로 표현되는 경우가 있음.
13
4.3 등가연산자 (예)
c == 'A' k != -2
x + y == 3 * z -7 (주의)
if (a == 1) v.s. if(a = 1) /* 항상 참 */
.... ....
14
4.4 논리연산자와 수식 (예) !a
!(x + 7.7)
!(a < b || c < d)
(주의) !!x != x /* x가 3일 경우, !x는 0, !!x는 1, 1 != 3 */
선언 및 초기화
char c = 'A'; int i = 7, j = 7; double x = 0.0, y = 2.3;
수식 동일 수식 결과값
! c ! c 0
! (i - j) ! (i - j) 1
! i - j (! i) - j -7
! ! (x + y) ! (! (x + y)) 1 ! x * ! ! y (! x) * (! (! y)) 1
15
(예) a && b a || b
3 && (-2 * a + 7) 선언 및 초기화
char c = 'B'; int i = 3, j = 3, k = 3; double x = 0.0, y = 2.3;
수식 동일 수식 결과값
i && j && k (i && j) && k 1 x || i && j - 3 x || (i && (j - 3)) 0 i < j && x < y (i < j) && (x < y) 0 i < j || x < y (i < j) && (x < y) 1 'A' <= c && c <= 'Z' ('A' <= c) && (c <= 'Z') 1 c - 1 == 'A' || c + 1 == 'Z' ((c - 1) == 'A') || ((c + 1) == 'Z') 1
16
4.5 복합문 (예) {
a += b += c; /* a = a + (b = (b +c)) */
printf(....);
} {
int a, b, c;
a = 1;
{
b = 2;
c = 3;
} }
17
4.6 수식과 공백문장 a = b;
a = b + c;
;
printf("\n");;
if (a < b && !(c < d)); /* if ((a >= b) || (c < d)) */
else /* i++; */
i++;
/* if 문에 “;”가 없으면 errer */
18
4.7 if와 if-else문 - if 문
if ( expr ) statement - 복합문의 사용
if ( j < k ) min = j;
if ( j < k )
printf("j is smaller than k");
if ( j < k ) { /* 두개의 if문을 하나로 */
min = j;
printf("j is smaller than k");
}
19
- if-else 문
if ( expr )
statement1 else
ststement2
(예) if ( x < y ) min = x;
else
min = y;
20
if ( c >= 'a' && c <= 'z' ) lower_case_count ++;
else {
other_count ++;
printf("%c is not a lower case letter\n", c);
}
(주의)
if ( i != j ) { i ++;
j ++;
}; /* if 문의 끝 */
else /* ERROR */
i = i - j;
21
if ( a == 1 ) /* if (( a == 1 ) && ( b == 2 )) */
if ( b== 2 )
printf("***\n");
if ( a == 1 ) if ( b == 2 )
printf("***\n");
else
printf("###\n");
(주의)
if ( a == 1) if ( b == 2 )
printf("***\n");
else
printf("###\n");
22
4.8 while문
while ( expr ) statement (예)
while ( i ++ < n ) /* factorial */
factorial *= i;
while ( (c = getchar( )) != EOF ) { if ( c >= 'a' && c <= 'z' )
++ lowercase_count;
++ total_count;
}
23
(주의)
while ( -- n ) /* n이 음수이면 infinite loop */
...
while ( --n > 0) /* n이 음수이어도 문제 없음 */
...
- 공백문장의 사용
while ( (c = getchar( )) == ' ')
; /* programmer의 의도가 분명함 */
while ( (c = getchar( )) == ' ') ;
/* 실수일 가능성 있음 */
24
4.9 for문
for ( expr1 ; expr2 ; expr3 ) statement
next statement
- 같은 연산을 수행하는 while문.
expr1 ;
while ( expr2 ) { statement
expr3 ; }
next statement
25
(예)
for ( i = 1; i <= n; ++i ) /* factorial */
factorial *= i;
for ( j = 2; k % j == 0; ++j ) { /* k의 약수와 그 합 */
printf("%d is a divisor of %d\n", j, k);
sum += j;
}
26
- for문에서 expression의 생략.
i = 1;
sum = 0;
for ( ; i <= 10; ++ i ) sum = sum + i;
i = 1;
sum = 0;
for ( ; i <= 10; ) sum += i ++;
for ( ; ; ) /* 무한루프 */
...
27
- nested for문.
for ( ... ) for ( ... ) for ( ... ) statement (예)
for (i=1; i<=10; ++i) { for (j=1; j<=10; ++j) printf ("%5d", i+j);
printf ("\n");
}
28
실행결과
i j 1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10 11
2 3 4 5 6 7 8 9 10 11 12 3 4 5 6 7 8 9 10 11 12 13 4 5 6 7 8 9 10 11 12 13 14 5 6 7 8 9 10 11 12 13 14 15 6 7 8 9 10 11 12 13 14 15 16 7 8 9 10 11 12 13 14 15 16 17 8 9 10 11 12 13 14 15 16 17 18 9 10 11 12 13 14 15 16 17 18 19 10 11 12 13 14 15 16 17 18 19 20
이 문서는 나눔글꼴로 작성되었습니다. 설치하기