Introduction to Programming ( 數 ) Lecture 3 Spring 2005 March 4, 2005 Lecture 2 Outline 數 料 If if 狀 if 2
(Standard Output, stdout): 料. ((Standard Input, stdin): 料. 類 數 數 數 說 printf 見 數 puts 串 數 putchar 數 putch 數 scanf 見 數 gets 串讀 數 getchar 讀 數 getche 讀 數 getch 讀 數 stdio.h stdio.h stdio.h conio.h stdio.h stdio.h stdio.h conio.h conio.h 3 (printf()) printf() 數,: printf( 串, 參數列 ) 料. 參數列 : 料 數, 數 例 : num%d printf( I have %d dogs.,num); printf( This is number two: %d.,2); printf( %d,1/3); 0 num=3, I have 3 dogs. This is number two: 2. 4
料 printf() 數 %c %s %d %e %f %g %u %x %o %ld %lf %lu %lx %lo %le %lg %lle 料 串 數 數 ( 數 e) 數 ( 數 ) 數 (%f%e) 數 六 數 數 數 數 數 數 數 數 數 六 數 數 數 精 度 ( 數 ) 精 度 ( 精 ) 精 度 數 5 printf( ) 數 欄 度 欄 度 C, 料 行 數, % 列 度. 例 : 數 ch= $, 度 5,, 令 : printf( %5c,ch); : 數 ch= $, 度 5,, 令 : printf( %-5c,ch); : $ $ 數 數 year=44 printf( Years is %2d.,year); printf( Years is %4d.,year); Y e a r s i s 4 4. Y e a r s i s 4 4. 6
printf( ) 數 欄 度 cont.1 數 料 數, 數 例 : f=3.14159 printf( %7.4f,f); : 3. 1 4 1 6 數 料 例 : f=1.2345e+05 printf( %12.6e,f); : 1. 2 3 4 5 0 e + 0 5 12 數 6 1 數 5 數 7 printf( ) 數 Carriage control character (Escape Sequences), 來. 例 : printf( Hi\n ); /* Hi 行 */ printf( pi character=\xe3 ); /* π () */ printf( Wake up!\a\n ); 8
Escape Sequence Characters Character Escape Sequence " \" ' \'? \? \ \\ BEL \a BS \b FF \f NL \n CR \r HT \t VT \v 9 printf()scanf() printf() 數 %c scanf() 數 %c 料 %s %d %s %d 串 數 %e %f ( 數 ) %f %e ( 數 ) %g ( 精 ) %u %x %o %u %x %o 數 六 數 數 %ld %lf %lu %lf %le 數 數 數 數 數 數 %lx %lo %D %U 數 六 數 數 數 %le %X 精 度 ( 數 ) %lg %lle %O %lle 精 度 ( 精 ) 精 度 數 10
不 數 數 %f %e 數 來 數 數 11 行 5 行, 數 數 數 7 行, %f 數 number, 數 數 9 行 %e, 數 number 12
#include <stdio.h> #include <stdlib.h> int main(void) int num; num=2; printf( Value=%d, Address=%d\n, num, &num); system("pause"); return 0; 13 scanf() 數 料, 數, : scanf( 串, 參數列 ). (scanf( )) 參數列 : 料 數. 數 = & 數 例 : int x,y; scanf( %d %d, &x, &y); 兩, 數 ( 行 Tab ),Enter,, 行. 14
A Simple Example #include <stdio.h> #include <stdlib.h> int main() int x, y; printf( x y Enter: "); scanf("%d %d", &x, &y); printf("x= %d, y= %d", x, y); system("pause"); return 0; 15 數 料 數 getch( ) getche( ) getchar( ) Enter 不 不 # include 不 不 conio.h conio.h stdio.h 數 數 =getch(); 數 =getche(); 數 =getchar(); 16
練 2-1 #include<stdio.h> #include<conio.h> #include <stdlib.h> int main(void) char ch; printf( Type any character: ); ch=getche(); printf( \nthe character you typed was %c.,ch); system("pause"); return 0; 17 練 2-2 #include<stdio.h> #include <stdlib.h> int main(void) char ch; printf( Type any character: ); ch=getchar(); printf( \nthe character you typed was %c.\n, ch); system("pause"); return 0; 18
數 數 putchar(), 數 數 (stdio.h) 數 putchar( 數 ); putch(), 數 數 (conio.h) 數 putch( 數 ); 19 /* 數 */ #include <stdio.h> #include <stdlib.h> #include<conio.h> 練 3 int main(void) char ch1,ch2,ch3; printf("please enter two characters\n==>"); ch1=getche(); ch2=getche(); printf(" \nthe first character is\n==>"); putchar(ch1); printf("\nthe second character is\n==>"); putchar(ch2); printf( \nplease input a character\n==> ); ch3=getchar(); printf("\nthe third character is\n==>"); putchar(ch3); printf("\n\n\n"); system("pause"); return 0; 20
---- 例 1 / * */ #include <stdio.h> #include <stdlib.h> 串 int main() int A=7,B=3; /* 數 */ /* 行 */ printf("a=7,b=3\n"); printf("a == B is %d\n", A==B); printf("a > B is %d\n", A>B); printf("a < B is %d\n", A<B); printf("a >= B is %d\n", A>=B); printf("a <= B is %d\n", A<=B); printf("a!= B is %d\n\n\n", A!=B); system("pause"); return 0; 21 ---- 例 1 行 : A=7,B=3 A == B is 0 A > B is 1 A < B is 0 A >= B is 1 A <= B is 0 A!= B is 1 C 林 0false0true 22
---- 例 2 /* */ #include <stdio.h> #include <stdlib.h> int main() int Radius; /* 數 數 */ float PI, Area; /* 兩 數 數 */ PI=3.1415; /* 率 */ printf( Please input the radius: "); scanf( %d, &Radius); Area= PI* Radius* Radius; /* */ printf( The area of radius %d is %f\n\n\n", Radius, Area); return 0; /* */ 23 ---- 例 2 行 : Please input the radius: 34 The area of radius 34 is : 3631.573975 24
---- 例 3 /* 兩 */ #include <stdio.h> #include <stdlib.h> 串 int main() char ch1, ch2; /* 兩 數 */ /* 兩 */ printf( Please input 2 characters: ); scanf( %c %c, &ch1, &ch2); /* ASCII */ printf( \ndisplay input characters \n ); printf( ch1=%c, %d\n, ch1, ch1); printf( ch2=%c, %d\n, ch2, ch2); return 0; /* */ 25 ---- 例 3 行 : Please input 2 characters: a b Display input characters ch1=a,97 ch2=b,98 26
(++, --) ++ 數 1, -- 數 1, x==1, ++x; x12 --x; x10 不 x++; ++x; 不 : 不 見 例 27 ---- 例 4 /* ++ii++ */ #include <stdio.h> #include <stdlib.h> int main() int i=0; /* 數 數 i0 */ /*,1 */ printf( i=%d\n,,i); /* i=0 */ printf( i++=%d\n,,i++); /*,1 */ printf( after i=%d\n\n,i); /* 1 */ i=0; /* i */ /* 1*/ printf( i=%d\n,,i); /* i=0 */ printf( ++i=%d\n,,++i); /* 1 */ printf( after i=%d\n\n,i); /* 1 */ return 0; /* */ 28
---- 例 4 行 : i=0 i++=0 after i=1 i=0 ++i=1 after i=1 29 ---- 例 5 /* 數 */ #include<stdio.h> #include <stdlib.h> int main(void) int i ; I = 418; printf( /%d/\n,i); printf( /%2d/\n,i); printf( /%5d/\n,i); printf( /%-5d/\n,i); return 0; 30
---- 例 5 行 : /418/ /418/ / 418/ /418 / 31 數 數 數 float f; int i =10; f = i; // 10.0 C 數 數 數 不 float f = 2.5; int i; i = f; // 2 數 trunc round 數 (<math.h>) float f = 2.5; int i; i = trunc(f); C(type cast) () float f = 2.5; int i; i = (int) f; // 2 數 (mixed mode arithmetic expression) 3 * 1.5 3.0 * 1.5 i + f i 數 f Printf 串 32
---- 例 6 /* 料 */ #include<stdio.h> void main(void) float x = 5.3; int y = 9; int z = 4; char ch1= d ; //ASCII code of d is 100 x = y/z; // 數 printf( The result1 is %6.2f\n,x); x = (float) y / (float) z; printf( The result2 is %6.2f\n,x); printf( (1).The ch1 is %c\n,ch1); ch1+=5; // ch1 = ch1 + 5; printf( (2).The ch1 is %d\n,ch1); 33 ---- 例 6 行 : The result1 is 2.00 The result2 is 2.25 (1).The ch1 is d (2).The ch1 is 105 ASCII code of i is 105 34
35 行 數 i 10 %d %f 數 數 i 數 %d 數 %f, 行, 數 數 i 10 數 0.000000, 不 36
37 行, 數 %f 數, 數 數 數, 說 數 38
料 類 度 sizeof () 料 C 度, 料, 度 39 /* 料 度 */ #include<stdio.h> ---- 例 7 void main(void) printf( 1.The size of char is %d\n",sizeof(char)); printf( 2.The size of int is %d\n",sizeof(int)); printf( 3.The size of long int is %d\n",sizeof(long int)); printf( 4.The size of float is %d\n",sizeof(float)); printf( 5.The size of double is %d\n",sizeof(double)); 40
---- 例 7 行 : 1.The size of char is 1 2.The size of int is 2 3.The size of long int is 4 4.The size of float is 4 5.The size of double is 8 41 /* */ #include<stdio.h> void main(void) int a,b,z; ---- 例 8 printf( Input 2 number to get the larger number.\n\n ); printf( First number: ); scanf( %d,&a); printf( Second number: ); scanf( %d,&b); z = a - b printf("their difference is %d\n\n\n",z); 42
---- 例 8 行 : Input 2 number to get their difference. First number: 17 Second number:22 The larger number is -5 43 (if statement)
if, 都 1 行, 行 行, 行 流, 論 行, 流 都 Sequential flow 45 if, 不, 不, 不 論, 便 不 路, 流 46
if 利 狀 來 行,, 說,, 47 if (if statement) if if ( expr ) statement1; if 流 expr true Statement 1 48
49 行 ASCII, 數 65~90, 97~122 來 50
if (if-else statements) if-else if-else 流 if (expr) Statement1; else Statement2; false Statement2 expr true Statement2 51 if ---- 例 例 1 兩數 #include<stdio.h> void main() int a, b; printf("please input two numbers : "); scanf("%d%d", &a,&b); if (a>b) printf("%d is bigger than %d\n", a,b); else printf("%d is less than %d\n", a,b); 兩 數 數 /*a>b*/ /*a<=b*/ 52
if ---- 例 例 2 數 #include<stdio.h> main() int a, b; printf("please input a number for a : "); scanf("%d", &a); if (a>0) printf("%d is a positive number\n", a); b=a; else printf("%d is not a positive number\n", a); b=-a; printf("the absolute value of a is %d\n", b); 兩 數 數 數 數 a 53 if ---- 例 例 流 數 數 數 數 number 流 說 rem =number%2 理 / 流 number 數 False rem =1? True number 數 54
if ---- 例 例 #include<stdio.h> void main() int number, rem; printf("\1: Please input a number for testing ==> "); scanf("%d",&number); rem = number % 2; if ( rem == 1 ) printf("\2: %d is odd \n",number); else printf("\2: %d is even \n",number); % 數 55 if ---- 例 例 4 Obtain a "yes" or "no" answer from the user. #include <stdio.h> void main(void) char ch; int ans; printf("type yes or no?(y/n)"); ch = getchar(); if ((ch == Y') (ch =='y')) ans = 1; else if((ch =='N') (ch =='n')) ans = 0; printf("the value of answer=%d\n",ans); 數 ch 數 數 讀 數 ch 56
邏 例 說 && a && b a,b 兩, 1(),0(). a b a,b 兩 1(),a,b 都 0().!!a a,0(), a,1(). 57 邏 a==2, b==6, : (! ( a >= b ) ) && ( a == b ) 1 && 0 0 1 2 3!() &&() () 58
if ---- 例 例 流 里 車 里 不 里 數 True i<=1500? False diff =i -1500 cost =70 diff 500 數? False True cost =70+(diff/500)*50 cost =70+(diff/500+1)*50 cost 59 if---- 例 例 里 不 #include <stdio.h> int main(void) int i,diff,cost; printf(" 里 ():"); scanf("%d",&i); if (i<=1500) /* 里 數 1500*/ cost=70; else ~ 60
diff = i-1500; /* 里 數 1500 里 率 */ if (diff%500==0) /* 里 數不 500500 率 */ cost=70+(diff/500)*5; else cost=70+((diff/500)+1)*5; printf(" 車 : %d ",cost); printf("\n"); return 0; 61 if (else if statements) else if if ( expr1) statement1; else if (expr2) statement2; else if (expr3) statement3; else statement4; staement5; else if 流 Statement1 Statement2 Statement3 true expr1 false true expr2 false true expr3 false Statement4 62
例 Nested if if (nested if) ---- 例 兩 數 ch,ans 讀 數 ch 63 例 Nested if if (nested if) ---- 例 #include<stdio.h> void main() int a, b; printf(" 數 a : "); scanf("%d", &a); if (a<10) printf(" %d 10 數 \n", a); else if (a>10) printf(" %d 10 數 \n", a); else printf(" %d 10 數 \n", a); 64
if (nested if) ---- 例 例 3 To determine what kind of character it is. void main() char ch; printf("\1: Please input a character ==> "); ch = getche() printf("\n"); if (( ch >= 'A' ) && ( ch <= 'Z' )) printf("\2: It is an upper case character.\n"); else if (( ch >= 'a' ) && ( ch <= 'z' )) printf("\2: It is a lower case character.\n"); else if (( ch >= '0' ) && ( ch <= '9' )) printf("\2: It is a digit.\n"); else printf("\2: It is a special character.\n"); printf("\1: Please input a character ==> "); 65 例 3 行 : if (nested if) ---- 例 : Please input a character ==> g : It is a lower case character. : Please input a character ==> G : It is an upper case character. : Please input a character ==> 8 : It is a digit. : Please input a character ==> % : It is a special character. : Please input a character ==> 66
if (nested if) ---- 練 1 練 列 類 00~59E 60~69D 70~79C 80~89B 90~100A #include<stdio.h> void main() int score; printf("\1:please input the score of a student ==> "); scanf("%d",&score); if ( score >= 90 ) printf("\1:the score is in A grade.\n"); else if ( (score >= 80) && (score<=89) ) printf("\1:the score is in B grade.\n"); ~ 67 if (nested if) ----( 練 1 )cont. 1 else if ( (score >= 70) && (score<=79)) printf("\1:the score is in C grade.\n"); else if ( (score >= 60) && (score<=69)) printf("\1:the score is in D grade.\n"); else printf("\1:the score is in E grade.\n"); 68