我是靠谱客的博主 想人陪小懒猪,最近开发中收集的这篇文章主要介绍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 endn");
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.subtractn");
printf("m.multiply
d.dividen");
printf("q.quitn");
while((cho=getchar())!='q'&&islower(cho))
{
printf("Enter first numbern");
num1=get_int();
printf("Enter second numbern");
num2=get_int();
switch(cho)
{
case 'a':
ans=num1+num2;
printf("%f+%f=%fn",num1,num2,ans);
break;
case 's':
ans=num1-num2;
printf("%f-%f=%fn",num1,num2,ans);
case 'm':
ans=num1*num2;
printf("%f*%f=%fn",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=%fn",num1,num2,ans);
break;
}
printf("Enter the operation of your choice:");
printf("a.add
s.subtractn");
printf("m.multiply
d.dividen");
printf("q.quitn");
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;
}





最后

以上就是想人陪小懒猪为你收集整理的c primer plus 第八章答案的全部内容,希望文章能够帮你解决c primer plus 第八章答案所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(45)

评论列表共有 0 条评论

立即
投稿
返回
顶部