/* 8.3 */#include<stdio.h>#include<stdlib.h>intmain(){int ch;
FILE * fp;char fname[50];printf("Enter the name of the file: ");scanf("%s",fname);
fp =fopen(fname,"r");if(fp ==NULL){printf("Failed to open file. Bye\n");exit(1);}while((ch =getc(fp))!=EOF)putchar(ch);fclose(fp);return0;}
/* 8.4 */#include<stdio.h>intmain(void){int guess =1;printf("Pick an integer from 1 to 100. I will try to guess ");printf("it.\nRespond with 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 know i could do it!\n");return0;}
/* 8.5 */#include<stdio.h>voiddisplay(char cr,int lines,int width);intmain(void){int ch;int rows, cols;printf("Enter a character and two integers:\n");while((ch =getchar())!='\n'){scanf("%d %d",&rows,&cols);display(ch, rows, cols);printf("Enter another character and two integers:\n");printf("Enter a newline to quit.\n");}printf("Bye.\n");return0;}voiddisplay(char cr,int lines,int width){int row, col;for(row =1; row <= lines; row++){for(col =1; col <= width; col++)putchar(cr);putchar('\n');}}
/* 8.6 */#include<stdio.h>voiddisplay(char cr,int lines,int width);intmain(void){int ch;int rows, cols;printf("Enter a character and two integers:\n");while((ch =getchar())!='\n'){if(scanf("%d %d",&rows,&cols)!=2)break;display(ch, rows, cols);while(getchar()!='\n')continue;printf("Enter another character and two integers:\n");printf("Enter a newline to quit.\n");}printf("Bye.\n");return0;}voiddisplay(char cr,int lines,int width){int row, col;for(row =1; row <= lines; row++){for(col =1; col <= width; col++)putchar(cr);putchar('\n');}}
/* 8.7 */#include<stdio.h>#include<stdbool.h>//验证输入是一个整数 longget_long(void);//验证范围的上下限是否有效
bool bad_limits(long begin,long end,long low,long high);//计算a~b之间的整数平方和 doublesum_square(long a,long b);intmain(void){constlong MIN =-10000000L;constlong MAX =+10000000L;long start;long stop;double answer;printf("This program computes the sum of the squares of ""integers in a range.\nThe lower bound should not ""be less than -10000000 and\nthe upper bound ""should not be more than +10000000.\nEnter the ""limits (enter 0 for both limits to quit):\n""lower limit: ");
start =get_long();printf("upper limit: ");
stop =get_long();while(start !=0|| stop !=0){if(bad_limits(start, stop, MIN, MAX))printf("Please try again.\n");else{
answer =sum_square(start, stop);printf("The sum of the squares of the integers ");printf("from %ld to %ld is %g\n",
start, stop, answer);}printf("Enter the limits (enter 0 for both ""limits to quit):\n");printf("lower limit: ");
start =get_long();printf("upper limit: ");
stop =get_long();}printf("Done.\n");return0;}longget_long(void){long input;char ch;while(scanf("%ld",&input)!=1){while((ch =getchar())!='\n')putchar(ch);printf(" is not an integer.\nPlease enter an ");printf("integer value, such as 25, -178, or 3: ");}return input;}doublesum_square(long a,long b){double total =0;long i;for(i = a; i <= b; i++)
total +=(double) i *(double) i;return total;}
bool bad_limits(long begin,long end,long low,long high){
bool not_good = false;if(begin > end){printf("%ld isn't smaller than %ld.\n",begin, end);
not_good = true;}if(begin < low || end <low){printf("Values must be %ld or greater.\n",low);
not_good = true;}if(begin > high || end > high){printf("Values must be %ld or less.\n",high);
not_good = true;}return not_good;}
/* 8.8 */#include<stdio.h>charget_choice(void);charget_first(void);intget_int(void);voidcount(void);intmain(void){int choice;voidcount(void);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;}}printf("Bye.\n");return0;}voidcount(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;}charget_choice(void){int ch;printf("Enter the letter of your choice:\n");printf("a. advic 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;}charget_first(void){int ch;
ch =getchar();while(getchar()!='\n')continue;return ch;}intget_int(void){int input;char ch;while(scanf("%d",&input)!=1){while((ch =getchar())!='\n')putchar(ch);printf(" is not an integer.\nPlease enter an");printf("integer value, such as 25, -178, or 3: ");}return input;}