C/C++
Table of contents 1. 2. getchar() putchar() 3. (Buffer) 4. 5. 6. 7. 8. 1
2
3
1 // pseudo code 2 read a character 3 while there is more input 4 increment character count 5 if a line has been read, increment line count 6 if a word has been read, increment word count 7 read next character 4
// while (( ch = getchar ())!= STOP ) {... } 5
// while (( ch = getchar ())!= STOP ) {... } 5
getchar() STOP 6
7
7
c!= && c!= \n && c!= \t! isspace (c) // # include <ctype.h> c == c == \n c == \t isspace (c) // # include <ctype.h> 8
( inword ) 1 inword 1 inword 0 1 // pseudo code 2 if c is not a whitespace and inword is false 3 set inword to true and count the word 4 if c is a white space and inword is true 5 set inword to false 9
i // wordcnt.c: # include <stdio.h> # include <ctype.h> # include < stdbool.h> # define STOP int main ( void ) { char c, prev ; long n_chars = 0L; int n_lines = 0, n_words = 0, p_lines = 0; bool inword = false ; printf (" Enter text ( to quit ):\n"); prev = \n ; while ((c = getchar ())!= STOP ) { n_chars ++; if ( c == \ n ) n_lines ++; 10
ii } if (! isspace (c) &&! inword ) { inword = true ; n_words ++; } if ( isspace ( c) && inword ) inword = false ; prev = c; } if ( prev!= \ n ) p_lines = 1; printf (" characters = % ld, words = %d, lines = %d, ", n_chars, n_words, n_lines ); printf (" partial lines = %d\n", p_lines ); return 0; 11
Enter text ( to quit ): Reason is a powerful servant but an inadequate master. characters = 56, words = 9, lines = 3, partial lines = 0 12
getchar() putchar()
getchar() putchar() 1 // echo.c: 2 # include <stdio.h> 3 int main ( void ) 4 { 5 char ch; 6 while (( ch = getchar ())!= # ) 7 putchar (ch); 8 return 0; 9 } 13
getchar() putchar() 1 // echo.c: 2 # include <stdio.h> 3 int main ( void ) 4 { 5 char ch; 6 while (( ch = getchar ())!= # ) 7 putchar (ch); 8 return 0; 9 } Hello world Hello world I am happy I am happy 13
(Buffer)
(Buffer) HHeelllloo wwoorrlldd [ enter ] II aamm hhaappppyy [ enter ] 14
(Buffer) 15
echo.c # # 16
EOF C getchar EOF (End Of File ) scanf() EOF EOF stdio.h # define EOF ( -1) 17
EOF -1 getchar() 0-127 0-255 -1 18
EOF // echo_eof.c # include <stdio.h> int main ( void ) { int ch; while (( ch = getchar ())!= EOF ) putchar (ch); return 0; } 19
EOF Hello world [ enter ] Hello world [ Ctrl +D] 20
EOF EOF Unix Ctrl+D Ctrl+Z Ctrl+Z 21
EOF // Linux or Mac OS Hello world [ enter ] Hello world [ Ctrl +D] 22
8.4
8.4 23
1-100 1 2 3 y yes n no 24
i // guess. c -- an inefficient and faulty number - guesser # include <stdio.h> int main ( void ) { int guess = 1; printf (" Pick an integer from 1 to 100. I will "); printf (" try to guess it.\ nrespond with "); printf (" a y if my guess is right and with "); printf ("\ nan n if it is wrong.\n"); printf ("Uh... is your number %d?\n", guess ); while ( getchar ()!= y ) printf ("Well, then, is it %d?\n", ++ guess ); printf ("I knew I could do it!\n"); return 0; } 25
Pick an integer from 1 to 100. I will try to guess it. Respond with a y if my guess is right and with an n if it is wrong. Uh... is your number 1? n Well, then, is it 2? Well, then, is it 3? n Well, then, is it 4? Well, then, is it 5? y I knew I could do it! 26
n Why? 27
n Why? 27
n Why? n n!= y Well, then, is it 2? \n \n!= y Well, then, is it 3? 27
while while ( getchar ()!= y ) { printf ("Well, then, is it %d?\n", ++ guess ); while ( getchar ()!= \n ) continue ; // skip rest of input line } no no way n 28
Pick an integer from 1 to 100. I will try to guess it. Respond with a y if my guess is right and with an n if it is wrong. Uh... is your number 1? n Well, then, is it 2? no Well, then, is it 3? no sir Well, then, is it 4? forget it Well, then, is it 5? y I knew I could do it! 29
30
1 int n; scanf ("%d", &n); // get first value while ( n >= 0) // detect out - of - range value { // process n scanf ("%d", &n); // get next value } 31
2 q scanf() scanf ("%d", &n) == 1 32
int n; while ( scanf ("%d", &n) == 1 && n >= 0) { // process n } while 33
scanf() getchar() 34
1000 1000 35
i /* checking. c -- validating input */ # include <stdio.h> # include < stdbool.h> // validate that input is an integer int get_int ( void ); // validate that range limits are valid bool bad_ limits ( int begin, int end, int low, int high ); // calculate the sum of the squares of the integer a through b double sum_squares ( int a, int b); int main ( void ) { const int MIN = -1000; 36
ii const int MAX = +1000; int start ; int stop ; double answer ; printf (" This program computes the sum of the " " squares of integers in a range.\ n" " The lower bound should not be less than " " -1000 and \ nthe upper bound should not " " be more than +1000.\ nenter the limits " "( enter 0 for both limits to quit ):\ n" " lower limit : "); start = get_ int (); 37
iii printf (" upper limit : "); stop = get_int (); while ( start!=0 stop!= 0) { if ( bad_limits ( start, stop, MIN, MAX )) printf (" Please try again.\n"); else { answer = sum_ squares ( start, stop ); printf (" The sum of the squares of the integers "); printf (" from %d to %d is %g\n", start, stop, answer ); } printf (" Enter the limits ( enter 0 for both " " limits to quit ):\n"); printf (" lower limit : "); 38
iv start = get_ int (); printf (" upper limit : "); stop = get_int (); } printf (" Done.\n"); return 0; } int get_int ( void ) { int input ; char ch; while ( scanf ("%d", & input )!= 1) { while (( ch = getchar ())!= \n ) putchar ( ch); // dispose of bad input printf (" is not an integer.\n"); 39
v printf (" Please enter an integer value, "); printf (" such as 25, -178, or 3: "); } return input ; } double sum_ squares ( int a, int b) { double total = 0; int i; for (i = a; i <= b; i ++) total += i * i; return total ; } bool bad_ limits ( int begin, int end, int low, int high ) { 40
vi bool not_ good = false ; if ( begin > end ) { printf ("%d isn t smaller than %d.\n", begin, end ); not_ good = true ; } if ( begin < low end < low ) { printf (" Values must be %d or greater.\n", low ); not_ good = true ; } if ( begin > high end > high ) { printf (" Values must be %d or less.\n", high ); 41
vii } not_ good = true ; } return not_ good ; 42
get_int() int input while while 43
bad_limits() 44
i This program computes the sum of the squares of integers in a range. The lower bound should not be less than -1000 and the upper bound should not be more than +1000. Enter the limits ( enter 0 for both limits to quit ): lower limit : 1q upper limit : q is not an integer. Please enter an integer value, such as 25, -178, or 3: 3 The sum of the squares of the integers from 1 to 3 is 14 Enter the limits ( enter 0 for both limits to quit ): lower limit : q q is not an integer. Please enter an integer value, such as 25, -178, or 3: 3 45
ii upper limit : 5 The sum of the squares of the integers from 3 to 5 is 50 Enter the limits ( enter 0 for both limits to quit ): lower limit : 4 upper limit : 3q 4 isn t smaller than 3. Please try again. Enter the limits ( enter 0 for both limits to quit ): lower limit : q is not an integer. Please enter an integer value, such as 25, -178, or 3: 0 upper limit : 0 Done. 46
main() get_int() badlimits() sum_squares() 47
C is 28 12. 4 C 1 i 2 s 3 4 2... 48
C getchar() while (( ch = getchar ())!= \n ) putchar (ch); 49
C 42 scanf() 50
C %c 4 char %s 4 2 %d 4 10 +2 = 42 int %f 42 float 51
Enter the letter of your choice : a. advice b. bell c. count q. quit 52
53
Enter the letter of your choice : a. advice b. bell c. count q. quit a[ enter ] Buy low, sell high. Enter the letter of your choice : a. advice b. bell c. count q. quit b[ enter ] Enter the letter of your choice : a. advice b. bell c. count q. quit c[ enter ] Count how far? Enter an integer : two [ enter ] two is not an integer. 54
Please enter an integer value, such as 25, -178, or 3: 5[ enter ] 1 2 3 4 5 Enter the letter of your choice : a. advice b. bell c. count q. quit q Bye. 55
i /* menu. c -- menu techniques */ # include <stdio.h> char get_choice ( void ); char get_first ( void ); int get_int ( void ); void count ( void ); int main ( void ) { int choice ; while ( ( choice = get_choice ())!= q ) { switch ( choice ) { case a : printf (" Buy low, sell high.\n"); break ; case b : putchar ( \a ); break ; case c : count (); break ; default : printf (" Program error!\n"); break ; } 56
ii } } printf (" Bye.\n"); return 0; void count ( void ) { int n, i; printf (" Count how far? Enter an integer :\n"); n = get_int (); for ( i = 1; i <= n; i ++) printf ("%d\n", i); while ( getchar ()!= \ n ) continue ; } char get_choice ( void ) 57
iii { } int ch; printf (" Enter the letter of your choice :\ n"); printf ("a. advice b. bell \n"); printf ("c. count q. quit \n"); ch = get_first (); while ( (ch < a ch > c ) && ch!= q ) { printf (" Please respond with a, b, c, or q.\n"); ch = get_first (); } return ch; char get_first ( void ) { int ch; 58
iv } ch = getchar (); while ( getchar ()!= \ n ) continue ; return ch; int get_int ( void ) { int input ; char ch; while ( scanf ("%d", & input )!= 1) { while (( ch = getchar ())!= \n ) putchar ( ch); // dispose of bad input printf (" is not an integer.\n"); printf (" Enter an integer value,\n"); printf (" such as 25, -178, or 3: "); } 59
v } return input ; 60
EOF 61
1-100 z 50 z 50 50 100 75 75 50 75 62 62
float 0 63
Enter the operation of your choice : a. add b. substract c. multiply d. divide q. quit a Enter first number : 22. 4 Enter second number : one one is not a number. Please enter a number, such as 2.5, -1.78 E8, or 3: 1 22. 40 + 1. 00 = 23. 40. 64
Enter the operation of your choice : a. add b. substract c. multiply d. divide q. quit d Enter first number : 1 Enter second number : 0 Enter a number other than 0: 0.2 1.00 / 0.20 = 5.00. Enter the operation of your choice : a. add b. substract c. multiply d. divide q. quit q Bye. 65