我是靠谱客的博主 坚定铃铛,最近开发中收集的这篇文章主要介绍《C Primer Plus》中文第六版 编程练习答案 第八章 字符输入/输出和输入验证,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

C Primer Plus 第8章 字符输入/输出和输入验证

      • 1.设计一个程序, 统计在读到文件结尾之前读取的字符数。
      • 2.编写一个程序, 在遇到 EOF 之前, 把输入作为字符流读取。 程序要打印每个输入的字符及其相应的ASCII十进制值。 注意, 在ASCII序列中, 空格字符前面的字符都是非打印字符, 要特殊处理这些字符。 如果非打印字符是换行符或制表符, 则分别打印n或t。 否则, 使用控制字符表示法。 例如, ASCII的1是Ctrl+A, 可显示为^A。 注意, A的ASCII值是Ctrl+A的值加上64。 其他非打印字符也有类似的关系。 除每次遇到换行符打印新的一行之外, 每行打印10对值。 (注意: 不同的操作系统其控制字符可能不同。)
      • 3.编写一个程序, 在遇到 EOF 之前, 把输入作为字符流读取。 该程序要报告输入中的大写字母和小写字母的个数。 假设大小写字母数值是连续的。 或者使用ctype.h库中合适的分类函数更方便。
      • 4.编写一个程序, 在遇到EOF之前, 把输入作为字符流读取。 该程序要报告平均每个单词的字母数。 不要把空白统计为单词的字母。 实际上, 标点符号也不应该统计, 但是现在暂时不同考虑这么多(如果你比较在意这点,考虑使用ctype.h系列中的ispunct()函数)。
      • 5.修改程序清单8.4的猜数字程序, 使用更智能的猜测策略。 例如, 程序最初猜50, 询问用户是猜大了、 猜小了还是猜对了。 如果猜小了, 那么下一次猜测的值应是50和100中值, 也就是75。 如果这次猜大了, 那么下一次猜测的值应是50和75的中值, 等等。 使用二分查找(binary search) 策略, 如果用户没有欺骗程序, 那么程序很快就会猜到正确的答案。
      • 6.修改程序清单8.8中的get_first()函数, 让该函数返回读取的第1个非空白字符, 并在一个简单的程序中测试。
      • 7.修改第7章的编程练习8, 用字符代替数字标记菜单的选项。 用q代替5作为结束输入的标记。
      • 8.编写一个程序,显示一个提供加法、减法、乘法、 除法的菜单。 获得用户选择的选项后,程序提示用户输入两个数字,然后执行用户刚才选择的操作。该程序只接受菜单提供的选项。程序使用float类型的变量储存用户输入的数字,如果用户输入失败,则允许再次输入。进行除法运算时,如果用户输入0作为第2个数(除数),程序应提示用户重新输入一个新值。该程序的一个运行示例如下:

1.设计一个程序, 统计在读到文件结尾之前读取的字符数。

#include <stdio.h>
int main(void)
{
char ch;
int cnt=0;
while((ch=getchar())!=EOF)
{
cnt++;
}
printf("字符数为%d",cnt);//包括空白符,换行符等
return 0;
}

2.编写一个程序, 在遇到 EOF 之前, 把输入作为字符流读取。 程序要打印每个输入的字符及其相应的ASCII十进制值。 注意, 在ASCII序列中, 空格字符前面的字符都是非打印字符, 要特殊处理这些字符。 如果非打印字符是换行符或制表符, 则分别打印n或t。 否则, 使用控制字符表示法。 例如, ASCII的1是Ctrl+A, 可显示为^A。 注意, A的ASCII值是Ctrl+A的值加上64。 其他非打印字符也有类似的关系。 除每次遇到换行符打印新的一行之外, 每行打印10对值。 (注意: 不同的操作系统其控制字符可能不同。)

#include <stdio.h>
int main(void)
{
char c;
int n_chars=0 ;
printf("Enter the text(EOF to terminate):n");
while((c=getchar())!=EOF)
{
if(n_chars++%8==0)
{
printf("n");
}
if(c>=32)
{
printf(" '%c'-%d ",c,c);
}
else if(c=='n')
{
printf(" '\n'-\n ");
n_chars=0;
}
else if(c=='t')
{
printf(" '\t'-\t ");
n_chars=0;
}
else
{
printf(" '%c'-^%c ",c,c+64);
}
}
return 0;
}

3.编写一个程序, 在遇到 EOF 之前, 把输入作为字符流读取。 该程序要报告输入中的大写字母和小写字母的个数。 假设大小写字母数值是连续的。 或者使用ctype.h库中合适的分类函数更方便。

#include <stdio.h>
#include <ctype.h>
int main(void)
{
int ch;
int upp=0,low=0;
while ((ch = getchar()) != EOF)
{
if(isupper(ch))
{
upp++;
}
if(islower(ch))
{
low++;
}
}
printf("大写字母有%d个,小写字母有%d个",upp,low);
return 0;
}

4.编写一个程序, 在遇到EOF之前, 把输入作为字符流读取。 该程序要报告平均每个单词的字母数。 不要把空白统计为单词的字母。 实际上, 标点符号也不应该统计, 但是现在暂时不同考虑这么多(如果你比较在意这点,考虑使用ctype.h系列中的ispunct()函数)。

#include <stdio.h>
#include <ctype.h>
int main(void)
{
int ch;
int n_words=0;//单词数 
int n_letters=0;//总字母数
float n_average;//平均 
int inword=0;
while ((ch = getchar()) != EOF)
{
if(ispunct(ch))
{
continue;
}
if(isalpha(ch))
{
n_letters++;
}
if(!isspace(ch)&&!inword)
{
inword=1;
n_words++;
}
if(isspace(ch)&&inword)
{
inword=0;
}
}
n_average=(float)n_letters/n_words;
printf("有%d个字母,%d个单词,平均一个单词有%0.2f个字母",n_letters,n_words,n_average);
return 0;
}

5.修改程序清单8.4的猜数字程序, 使用更智能的猜测策略。 例如, 程序最初猜50, 询问用户是猜大了、 猜小了还是猜对了。 如果猜小了, 那么下一次猜测的值应是50和100中值, 也就是75。 如果这次猜大了, 那么下一次猜测的值应是50和75的中值, 等等。 使用二分查找(binary search) 策略, 如果用户没有欺骗程序, 那么程序很快就会猜到正确的答案。

#include <stdio.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 it");
printf("Respond with a y if my guess is right andn");
printf("with an n if it is wrong.n");
printf("Uh...is your number %d?n",guess);
while((response=getchar())!='y')
{
if(response!='h'&&response!='l')
{
printf("Sorry,I understand only y or h or l.n");
}
if(response=='h')
{
high=guess;
}
if(response=='l')
{
low=guess;
}
guess=(high+low)/2;
while(getchar()!='n')
{
continue;
}
printf("Well,then,is it %d?n",guess);
}
printf("I knew I could do it!!!");
return 0;
}

6.修改程序清单8.8中的get_first()函数, 让该函数返回读取的第1个非空白字符, 并在一个简单的程序中测试。

#include <stdio.h>
#include <ctype.h>
char get_first(void);
int main(void)
{
putchar(get_first());
return 0;
}
char get_first(void)
{
int ch;
while(isspace(ch=getchar()))
{
continue;
}
while(getchar()!='n')
{
continue;
}
return ch;
}

7.修改第7章的编程练习8, 用字符代替数字标记菜单的选项。 用q代替5作为结束输入的标记。

#include <stdio.h>
#define HOUR1 30
#define HOUR2 40
#define add_salary BASE_salary*1.5 
#define PAY1 8.75
#define PAY2 9.33
#define PAY3 10.00
#define PAY4 11.20
#define RATE1 0.15
#define RATE2 0.20
#define RATE3 0.25
char get_first(void);
int main(void)
{
char ch;
float hours,total_salary,tax,salary,BASE_salary;
showprintf:
printf("*****************************************************************n");
printf("Enter the number corresponding to the desired pay rate or action:n");
printf("a) $8.75/hr
b) $9.33/hrn");
printf("c) $10.00/hr
d) $11.20/hrn");
printf("q) quitn");
printf("******************************************************************n");
printf("Please choose your ch:");
while(ch=get_first())
{
switch(ch)
{
case 'a': BASE_salary= PAY1; break;
case 'b': BASE_salary= PAY2; break;
case 'c': BASE_salary= PAY3; break;
case 'd': BASE_salary= PAY4; break;
case 'q': return 0;
default:printf("Please enter the valid number:n");
goto showprintf;
}
printf("Enter your working hours:") ;
while ((!scanf("%f", &hours)) || (hours < 0))
{
while (getchar() != 'n')//在输无效字符后,gerchar()一直读取,直到摁了Enter键,执行下一句
{
continue;
}
printf("Enter a positive number:");
}
if(hours<=HOUR1)
{
total_salary=hours*BASE_salary;
tax=total_salary*RATE1;
}
else if(hours<=HOUR2)
{
total_salary=hours*BASE_salary;
tax=300*RATE1+(total_salary - 300) *RATE2;
}
else
{
total_salary=HOUR2*BASE_salary+(hours-HOUR2)*add_salary;
if(total_salary<=450)
{
tax=300*RATE1+(total_salary - 300) *RATE2;
}
else
{
tax=300*RATE1+(450 - 300) *RATE2+(total_salary - 450) *RATE3;
}
}
salary=total_salary-tax;
printf("工作时间是%gh,工资总额为$%gn",hours,total_salary);
printf("税金为$%g,净收入为$%gn",tax,salary);
goto showprintf;
}
return 0;
}
char get_first(void)
{
char ch;
while(isspace(ch=getchar()))
{
continue;
}
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. quita
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 559
q. quitd
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>
float figure_number(void);
char get_first(void);
int main(void)
{
char choice;
float num1,num2;
showprintf:
printf("Enter the operation of your choice:n");
printf("a. add
s. subtractn");
printf("m. multiply
d. dividen");
printf("q. quitn");
while((choice=get_first())!='q')
{
printf("Enter the first number:");
num1=figure_number();
printf("Enter second number:");
num2=figure_number();
switch(choice)
{
case 'a': printf("%g+%g = %gn", num1,num2,num1+num2);break;
case 's': printf("%g-%g = %gn", num1,num2,num1-num2);break;
case 'm': printf("%g*%g = %gn", num1,num2,num1*num2);break;
case 'd': if(num2==0)
{
printf("Enter a number other than 0: ");
num2 = figure_number();
}
printf("%g/%g = %gn", num1,num2,num1/num2);break;
default : printf("Please enter the valid choice:n");
goto showprintf;
break;
}
goto showprintf;
}
printf("Bye");
return 0;
}
char get_first(void)
{
char ch;
while(isspace(ch=getchar()))
{
continue;
}
while(getchar()!='n')
{
continue;
}
return ch;
}
float figure_number(void)
{
float num;
char ch;
while(scanf("%f",&num)!=1)
{
while((ch = getchar()) != 'n')
{
putchar(ch);
}
printf(" is not an number.n");
printf("Please enter a number, such as 2.5, -1.78E8, or 3: ");
}
while(getchar() != 'n')
{
continue;
}
return num;
}

最后

以上就是坚定铃铛为你收集整理的《C Primer Plus》中文第六版 编程练习答案 第八章 字符输入/输出和输入验证的全部内容,希望文章能够帮你解决《C Primer Plus》中文第六版 编程练习答案 第八章 字符输入/输出和输入验证所遇到的程序开发问题。

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

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

评论列表共有 0 条评论

立即
投稿
返回
顶部