Download presentation
Presentation is loading. Please wait.
1
Review: midterm #9 #include void main(void) { int c; c = getchar(); if(c>=48){ if(c<=57) printf("%c\n",c); else printf("not a digit\n"); } else printf("not a digit\n"); }
2
Logic operator: && (p. 5-16) #include void main(void) { int c; c = getchar(); if(c>= ‘ 0 ’ && c<= ‘ 9 ’ ) printf("%c\n",c); else printf("not a digit\n"); } 1 && 1 = 1 1 && 0 = 0
3
Review: 10 進位轉 2 進位 (decimal to binary) Binary notation 1101 2 =1x2 3 +1x2 2 +0x2 1 +1x2 0 = 13 10 Conversion from decimal to binary 13 -> 12 6 -> 0 3 -> 1 1 1101 Reminder 13 = 6*2+1 = (3*2+0)*2+1 = ((2*1+1)*2+0)*2+1 while(){ }
4
Modular operator: % #include void main(void) { int num, reminder; scanf("%d", &num); while(num>0){ // reminder = num-(num/2)*2; reminder = num % 2; printf("%d ", reminder); num = num/2; } printf("\n"); }
5
Array 陣列 Fall 2003, Jen-Chang Liu
6
應用:輸入多個數字 Ex. 輸入五個數字,存在變數裡,找出最大值 使用陣列變數 #include main(){ int c1, c2, c3, c4, c5; … } #include main(){ int c[100]; // 整數陣列 … } c[0] c[1] c[2] c[3] c[4]
7
Example: Count digits Store occurrence of each digit in an array #include /* count digits, white space, and others */ main() { int c, i, nwhite, nother; int ndigit[10]; /* initialize */ Declaration of array ndigit[0] ndigit[1] ndigit[2] ndigit[3] ndigit[4] ndigit[9] ?????? 16 bits
8
/* initialize */ nwhite = nother = 0; for(i=0; i<10; ++i) ndigit[i] = 0; while((c = getchar()) != EOF) if(c >= '0' && c <= '9') ++ndigit[ c-'0' ]++; else if(c == ' ' || c == '\n' || c == '\t') ++nwhite; else ++nother; printf("digit = "); for(i=0; i<10; ++i) printf(" %d", ndigit[i]); printf(", white space = %d, other = %d\n", nwhite, nother); } in the interval [0,9] /* || OR */ 1 or 1 =1 1 or 0 =1 0 or 0 =0
9
Else-If if( expression ) statement else if statement 2 … else statement N statement K Test YES Statement 1 NO IF Statement K Test YES Statement 2 else if … Test else NO Statement N YES Statement N-1 … None of the above Default case
10
++i 與 i++ 單獨一個時,兩者相同,都是 i=i+1 i++; ++i; 與其他算符合併時 i=2; j=i++; /* j=i; i++; */ i=2; j=++i; /* i++; j=i; */
11
#include /* switch-case version */ main() { int c, i, nwhite, nother, ndigit[10]; nwhite = nother = 0; for(i=0; i<10; i++) ndigit[i] = 0; while( (c=getchar()) != EOF){ switch(c){ case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': ndigit[c-'0']++; break; case ' ': case '\n': case '\t': nwhite++; break; default: nother++; break; } printf("digits="); for(i=0; i<10; i++) printf(" %d", ndigit[i]); printf(", white space = %d, other = %d\n", nwhite, nother); return 0; } Not necessary, but good
12
printf("digits="); for(i=0; i<10; i++) printf(" %d", ndigit[i]); printf(", white space = %d, other = %d\n", nwhite, nother); return 0; } *note: fall-throughs are necessary when there are Multiple labels for a single computation
13
Else-If: example /* binary search */ void main(void) { int v[5]={1, 3, 5, 10, 15}; //initialize int x = 3; // search target int low, high, mid; low = 0; high = 4; while( low <= high ){ mid = (low+high)/2; if ( x < v[mid]) high = mid – 1; else if( x > v[mid]) low = mid + 1; else{ printf( “ match=%d ”, mid); return; /* found match */ } printf( “ no match ” ); } Target: x 0 n-1 mid low high x 0 n-1 high low x mid v v
14
Character arrays Character arrays = strings Null character H e l l o \n \0 結尾字元 char s[10]; S[0]S[1]S[2]S[3] … …… S[9] 字串 char s[10]=“Hello\n”; printf(“%s”, s);
15
atoi: ASCII string to Integer Algorithmic procedure Skip white space, if any Skip sign, if any Get integer part and convert it Ex. char s[10]= “ – 200 ” ;
16
Example: atoi #include /* convert s to integer */ main() { char s[20]= “ -101 ” ; int i, n, sign; for (i=0; isspace(s[i]); i++) /* skip white space */ ; sign = (s[i] == ‘ - ’ ) ? – 1 : 1; if (s[i] == ‘ + ’ || s[i] == ‘ - ’ ) /* skip sign */ i++; for (n=0; isdigit(s[i]); i++) /* compute number */ n = 10*n + (s[i]- ’ 0 ’ ); printf("number is %d\n", sign*n); }
17
break Exit from the loop (for, while, do-while, switch) loop body break;... * Don ’ t have to wait until test condition is satisfied * Exit from the innermost loop only
18
break: example #include /* trim: remove trailing blanks, tabs, newlines */ main() { int n; char s[10]= “ good \t “ ; for (n= strlen(s)-1; n>=0; n--) if (s[n] != ‘ ‘ && s[n] != ‘ \t ’ && s[n] != ‘ \n ’ ) break; s[n+1]= ‘ \0 ’ ; printf("string=%s", s); }
19
Summary Array int digit[10]; char str[20]; Control flow if … else if … else Break Operation % ++
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.