4.编写一个程序,在遇到EOF之前,把输入作为字符流读取。该程序要报告平均每个单词的字母数。不要把空白统计为单词的字母。实际上,标点符号也不应该统计,但是现在暂时不同考虑这么多(如果你比较在意这点,考虑使用ctype.h系列中的ispunct()函数)。
#include<stdio.h>
#include<ctype.h>
#include<stdbool.h>
int main(void){
char c;
int n_letters=0,n_words=0;//n_letters:字母数,n_words:单词数
bool inword=false;//一个flag,如果c在单词中,inword等于true
while((c=getchar())!=EOF){
if(islower(c)||isupper(c))n_letters++;//若是大写字母或者小写字母,则字母数加1
if(!isspace(c)&&!ispunct(c)&&!inword){//若c不为空格,不为标点符号,不在单词中
inword=true;
n_words++;
}
if(inword&&(isspace(c)||ispunct(c)))//若c不在单词中,是空格或者标点符号
inword=false;
}
printf("平均每个单词的字母数为%f",(double)n_letters/(double)n_words);
return 0;
}
5.编写一个更智能的猜数字程序。eg.程序最初猜50,询问用户是猜大了,猜小了还是猜对了。如果猜小了,那么下一次猜测的值应是50和100的中值。如果猜大了,那么下一次猜测的值应是50和75的中值,等等。使用二分查找策略。
#include<stdio.h>
#include<ctype.h>
int main(void){
int high=100;
int low=1;
int guess=(high+low)/2;
char response;
printf("Pick an integer from 1 to 100.I will try to guess ");
printf("it.\nRespond with a y if my guess is right,with\na h if is high,and with an l if it is low.\n");
printf("Uh...is your number %d?\n",guess);
while((response=getchar())!='y'){
/*while(response=='\n')
continue;*/
if(response!='h'&&response!='l'){
printf("I don't understand that response.Please enter h for\n");
printf("high,l for low,or y for correct.\n");
//continue;
}
if(response=='h')
high=guess-1;
if(response=='l')
low=guess+1;
while((response=getchar())!='\n')
continue;//另外加上去的
guess=(high+low)/2;
printf("Well,then,is it %d?\n",guess);
}
printf("I knew I could do it!\n");
return 0;
}
第五题是有参考答案的,我这个就是直接按照参考答案打上去的。
但是运行的时候发现有问题,即输入了一次之后,再也输不进数字了。然后把橙色的注释掉之后,能完整运行,但是输入换行符时仍然有些奇怪。之后再看一下吧。
(PS:发布之后发现调的颜色用css表示了,为了方便看,我再发多一张截图。如果要直接复制该段代码,注意要把css的代码删掉再运行。)
6.修改程序清单8.8中的get_first()函数,让该函数返回读取的第1个非空白字符,并在一个简单的程序中测试
#include<stdio.h>
#include<ctype.h>
char get_choice(void);
void count(void);
char get_first(void);
int get_int(void);
int main(void){
int choice;
void count(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");
return 0;
}
char get_choice(void){
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;
ch=getchar();
while(getchar()!='\n')
continue;
return ch;
*/
//注释掉的是8.8原get_fitst()函数
int ch;
ch=getchar();
while(isspace(ch)){
ch=getchar();
}//变的只有这5行。当ch是空白符时,isspace(ch)为非零值,进入while循环,ch读取下一个字符。直到ch不是空白字符时,退出该while循环。
while(getchar()!='\n')
continue;//当录入到了第一个非空白字符后,清除那一行后面的字符
return ch;
}
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;
}
int get_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 integer value,such as 25,-178 or 3:");
}
return input;
}
8.
#include<stdio.h>
#include<ctype.h>
void menu(void);
int get_first(void);
double get_double(void);
int main(void)
{
double a,b;
char response;
menu();
while((response=get_first())!='q'){
response=tolower(response);
printf("Enter first number: ");
scanf("%lf",&a);
printf("Enter second number: ");
scanf("%lf",&b);
switch(response){
case 'a':printf("%g + %g = %g\n",a,b,a+b);
break;
case 's':printf("%g - %g = %g\n",a,b,a-b);
break;
case 'm':printf("%g * %g = %g\n",a,b,a*b);
break;
case 'd':
{
if(b==0){printf("enter error!please enter another number\n");
break;}
else printf("%g / %g = %g\n",a,b,a/b);
break;
}
default:printf("Please enter a or s or m or d or q\n");
break;
}
menu();
}
printf("Bye.\n");
return 0;
}
void menu(void){
printf("Enter the operation of your choice:\n");
printf("a.add s.subtract\n");
printf("m.multiply d.divice\n");
printf("q.quit\n");
}
int get_first(void){
char ch;
ch=getchar();
while(isspace(ch))
ch=getchar();
while(getchar()!='\n')
continue;
return ch;
}
double get_double(void){
double input;
char ch;
while(scanf("%lf",&input)!=1){
while((ch=getchar())!='\n')
putchar(ch);
printf(" is not an integer.\nPlease enter an integer value,such as 25,-178 or 3:");
}
return input;
}