190 likes | 338 Views
Outline. Symbolic Constants Character Input and Output if … else switch…case. Symbol constants. Symbol name (Meaningful, easy to read). #include <stdio.h> #define LOWER 0 #define UPPER 300 #define STEP 20 /* print F-S table */ main() { int fahr;
E N D
Outline • Symbolic Constants • Character Input and Output • if … else • switch…case
Symbol constants Symbol name (Meaningful, easy to read) #include <stdio.h> #define LOWER 0 #define UPPER 300 #define STEP 20 /* print F-S table */ main() { int fahr; for(fahr=LOWER; fahr <= UPPER; fahr = fahr+STEP) printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32)); } Symbol value Replace this symbol name at compile time
Character I/O • A text stream is a sequence of characters getchar() getch() getche() getc() putchar(c) putch(c,stdout) putc(c,stdout) 輸入 stdin I/O devices are also taken as files 輸出 stdout
Example: File copying #include <stdio.h> /* echo, version 1 */ main() { int c; c=getchar(); while( c != EOF ){ putchar(c); c = getchar(); } } End Of File not equal to A constant defined in stdio.h NOT the same as any char values
Example: File copying End of file • EOF • Print the value of EOF OS Keyboard termination signal(ctrl-Z) #include <stdio.h> main() { printf("EOF = %d\n", EOF); }
Example: File copyingAssignment • c= getchar(); • Assignment is an expression and has a value, which is the value of the left hand side after assignment. #include <stdio.h> main() { int c; while( (c=getchar()) != EOF ){ putchar(c); } } Precedence of = and != Textbook p. 5-25
Example: Character counting #include <stdio.h> main() { long nc; /* number of character */ nc = 0; while(getchar() != EOF) ++nc; printf("%ld\n",nc); } Good naming Convention For variables 32-bit integer nc = nc+1;
Example : Character counting 2 #include <stdio.h> main() { double nc; for(nc = 0; getchar()!= EOF; ++nc) ; printf("%.0f\n",nc); } Increase range null statement
Outline • Symbolic Constants • Character Input and Output • if … else • switch … case
Example: Line counting #include <stdio.h> /* count lines */ main() { int c, nl; nl = 0; while( (c=getchar()) != EOF) if(c == '\n') ++nl; printf("%d\n", nl); } “\n” ? 條件測試 Test condition character constant ASCII value of input char. is equal to
if statement if if( expression ){ statement 1; } else{ statement 2; } YES Test Statement 1 NO else Statement 2 statement 3; Statement 3
if-else: some notes • if (expression) • Nested if • Else is associating with the closest previous else-less if if (expression != 0) if (n > 0){ if (a > b) z = a; } else z = b; if (n > 0) if (a > b) z = a; else z = b; if (n > 0) if (a > b) z = a; else z = b;
Example • 李伯伯 example 1.1-3
Outline • Symbolic Constants • Character Input and Output • if … else • switch … case
Switch • Multi-way decision integer-valued constant switch(expression){ caseconst-expr : statements caseconst-expr : statements … default: statements } optional * All case expressions must be different
switch Switch Test expression case expr1 statements 1 (fall-through) case expr2 statements 2 break; … default statements N
#include <stdio.h> 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
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