3.编写一个1程序,在遇到EOF之前,把输入作为字符输入流读取。该程序要报告输入中的大写字母和小写字母的个数。假设大小写字母数值是连续的。或者使用ctype.h库中合适的分类函数更方便。
#include <stdio.h>
#include <ctype.h>
int main()
{
char ch;
unsigned int count_upper=0,count_lower=0,count_other=0;
while ((ch=getchar())!=EOF)
{
if (isupper(ch))
count_upper++;
else if (islower(ch))
count_lower++;
else
count_other++;
}
printf("大写字母有%d个,小写字母有%d个,非字母字符有%d个。\n",a,b,c);
return 0;
}
4.编写一个程序,在遇到EOF之前,把输入作为字符流读取。该程序要报告平均每个单词的字母数。不要把空白统计为单词字母。实际上,标点符号也不应该统计。(可以考虑使用ctype.h库中的ispunct()函数)
#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>
int main(void)
{
char ch;
bool inword=false;
float words=0,word=0,count;
while ((ch=getchar()) !=EOF)
{
if (isalpha(ch))
word++; //字母
if (!isspace(ch) && !inword)
{
inword=true;
words++; //单词
}
if (isspace(ch) && inword)
inword=false;
if(ispunct(ch))
continue;
}
count=word/words;
printf("平均每个单词有%.2f字母.\n",count);
return 0;
}
6.修改程序8.8中的get_first()函数,让该函数返回读取的第一个非空白字符,并在一个简单的程序中测试。
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
char get_first(void);
int main(void)
{
char ch;
ch = get_first();
printf("%c\n", ch);
return 0;
}
char get_first(void)
{
int ch;
ch = getchar();
while (isspace(ch))
{
ch = getchar();
}
while (getchar() != '\n')
{
continue;
}
return ch;
}
8.编写一个程序,显示一个提供加法、减法、乘法、除法的菜单。获取用户选择的选项后,程序使用float类型的变量存储用户输入的数字,如果用户输入失败,则允许再次输入。进行除法运算时,如果用户输入0作为第2个数(除数),程序应提示用户重新输入一个新值。该程序的一个运行示例如下:
Enter the operation of your choice:
a. add s. subtract
m. multiply d. divide
q. quit
a
Enter first number: 22 .4
Enter second number: one
one is not an number.
Please enter a number, such as 2.5, -1.78E8, or 3: 1
22.4 + 1 = 23.4
Enter the operation of your choice:
a. add s. subtract
m. multiply d. divide
q. quit
d
Enter first number: 18.4
Enter second number: 0
Enter a number other than 0 : 0.2
18.4 / 0.2 = 92
Enter the operation of your choice:
a. add s. subtract
m. multiply d. divide
q. quit
q
Bye.
#include <stdio.h>
int main()
{
float num1,num2,end;
char ch,oper;
printf("Enter the operation of your choice:\n");
printf("a. add s. subtract\n");
printf("m. multiply d. divide\n");
printf("q. quit\n");
while (scanf("%c",&ch)==1 && ch=='a' || ch=='s' || ch=='m' || ch=='d' && ch !='q')
{
//while (getchar() !='\n')
// continue;
printf("Enter first number: ");
while(scanf("%f",&num1)!=1)
{
while ((ch=getchar()) !='\n')
putchar(ch);
printf(" is not an number.\n");
printf("请输入一个数字,例如 2.5, -1.78E8,or 3:");
}
printf("Enter seconds number: ");
while(scanf("%f",&num2)!=1)
{
while ((ch=getchar()) !='\n')
putchar(ch);
printf(" is not an number.\n");
printf("请输入一个数字,例如 2.5, -1.78E8,or 3:");
}
switch(ch)
{
case 'a':
end=num1+num2;oper='+';break;
case 's':
end=num1-num2;oper='-';break;
case 'm':
end=num1*num2;oper='*';break;
case 'd':
while (num2==0)
{
printf("Enter a number other than 0: ");
scanf("%f",&num2);
}
end=num1/num2;oper='/';break;
default:printf("程序有错.\n");
}
printf("%g %c %g=%g\n",num1,oper,num2,end);
printf("Enter the operation of your choice:\n");
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;
}