c primer plus 第八章答案

#include<stdio.h>   //第四题
#include<ctype.h>
#include<stdbool.h>
int main(void)

{
bool inword=false;
char ch;
int words=0,letters=0,count=0;
float average;

while((ch=getchar())!=EOF)
{    
     if(!isspace(ch)&&!ispunct(ch))
      letters++;


	if(!isspace(ch)&&!inword)
	{
	inword=true;
	words++;
}
	if(isspace(ch)&&inword)
	inword=false;
	
}
printf("单词%d个\n",words);
printf("%d个字母",letters); 
printf("平均%f",(float)letters/(float)words);





}
#include<stdio.h>//第五题
int main(void)
{
	int guess=50;
	int big=100,small=0;
	char ch;
	printf("is your number 50?or too big,or too small?\n");
	printf("enter b or s,y to end\n");
	while((ch=getchar())!='y')
	{
		switch(ch)
	{
		case 'b':
		big=guess;
		guess=guess-(guess-small)/2;
		
		printf("is your number %d?or too big,or too small?\n",guess);
			
			break;
			case 's':
			small=guess;
			guess=guess+(big-guess)/2;
			
			printf("is your number %d?or too big,or too small?\n",guess);
				
				break;
				
			}
			while(getchar()!='\n')
			continue;
		
	}
	printf("I knew I could do it");
} 

第八题未完成版,还无法跳过空字符

#include<stdio.h>
#include<ctype.h>
int get_int(void);
int main(void)
{
	float ans;
	float num1,num2;
	char cho;

	printf("Enter the operation of your choice:");
	printf("a.add        s.subtract\n");
	printf("m.multiply     d.divide\n");
	printf("q.quit\n");
	while((cho=getchar())!='q'&&islower(cho))
{   
	
	printf("Enter first number\n");
    num1=get_int();

    printf("Enter second number\n");
    num2=get_int();
    
	switch(cho)
	{
		
	
		case 'a':
			ans=num1+num2;
			printf("%f+%f=%f\n",num1,num2,ans);
			break;
			case 's':
				ans=num1-num2;
				printf("%f-%f=%f\n",num1,num2,ans);
				case 'm':
					ans=num1*num2;
					printf("%f*%f=%f\n",num1,num2,ans);
					break;
					case 'd':
						
					     while(num2==0){
					     	printf("Please enter a number than 0:");
						  num2=get_int();
					     }
                           
						    
					    ans=num1/num2;
					    printf("%f/%f=%f\n",num1,num2,ans);
					    break;
	}
	printf("Enter the operation of your choice:");
	printf("a.add        s.subtract\n");
	printf("m.multiply     d.divide\n");
	printf("q.quit\n");
		while(getchar()!='\n')
		continue;
}
	
	printf("Bye.\n");
	
	
	return 0;
	
	
	
}
int get_int(void)
{
	float input;
	char ch;
	while(scanf("%f",&input)!=1)
	{
	while((ch=getchar())!='\n') 
	putchar(ch);
	printf(" is not an integer.\nPlease enter an");
	printf("integer value,sach as 25,-178,or 3:");
    }
	return input;
	
}





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值