Outline
-
Upload
sharon-kelly -
Category
Documents
-
view
20 -
download
0
description
Transcript of Outline
Outline Symbolic Constants Character Input and Output if … else switch…case
Symbol constants
#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 name(Meaningful, easy to read)
Symbol value
Replace this symbol nameat 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)
I/O devices are also takenas files
輸入stdin
輸出stdout
Example: File copying
#include <stdio.h>
/* echo, version 1 */main(){ int c;
c=getchar(); while( c != EOF ){ putchar(c); c = getchar(); }}
not equal to
End Of File
A constant definedin stdio.hNOT the same asany char values
Example: File copying EOF
Print the value of EOF
End of file
OS Keyboardterminationsignal(ctrl-Z)
#include <stdio.h>
main(){ printf("EOF = %d\n", EOF);}
Example: File copyingAssignment
c= getchar(); Assignment is an expression and has a valu
e, 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 namingConventionFor variables
nc = nc+1;
32-bitinteger
Example : Character counting 2
#include <stdio.h>
main(){ double nc;
for(nc = 0; getchar()!= EOF; ++nc) ; printf("%.0f\n",nc);}
null statement
Increaserange
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);}
Test condition
is equal to
條件測試characterconstant
ASCII valueof input char.
“\n” ?
if statement
if( expression ){ statement 1; }else{ statement 2; }
TestYES
Statement 1
NO
if
statement 3;Statement 2
Statement 3
else
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
switch(expression){ case const-expr : statements case const-expr : statements … default: statements}
optional
integer-valued constant
* All case expressions must be different
SwitchTest
expression
switch
case
expr1 statements 1
expr2
case
…
defaultstatements N
statements 2break;
(fall-through)
#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 areMultiple labels for a single computation