#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;
}