8.11编程练习
Chapter 8 Programming Exercises
1.
/*
PE 8-1
设计一个程序,统计在读到文件结尾之前读取的字符数
*/
#include <stdio.h>
int main(void)
{
int count = 0;
while (getchar() != EOF)
count++;
printf("%d", count); //换行符也算在内
return 0;
}
这题到底是什么意思啊 = = 至今看不懂,看懂的大佬麻烦评论下
/*
PE 8-2
编写一个程序,在遇到EOF之前,把输入作为字符流读取。
程序要打印每个输入的字符及其相应的ASCII十进制值。
注意,在ASCII序列中,空格字符前面的字符都是非打印字符,要特殊处理这些字符。
如果非打印字符时换行符或制表符,则分别打印\n或\t。否则,使用控制字符表示法。
例如ASCII的1是Ctrl+A,可显示为^A。注意A的ASCII码是Ctrl+A的值加上64。
其他非打印字符也有类似的关系。除每次遇到换行符打印新的一行之外,每行打印10对值。
(注意:不同的操作系统其控制字符可能不同。)
*/
/*
PE 8-3
编写一个程序,在遇到EOF之前,把输入作为字符流读取。
该程序要报告输入的大写字母和小写字母的个数。假设大小写字母数是连续的。
或者使用ctype.c库中的分类函数更方便。
*/
#include <stdio.h>
int main(void)
{
int count_upper = 0;
int count_lower = 0;
int ch;
while ((ch = getchar()) != EOF)
if (isupper(ch))
count_upper++;
else if (islower(ch))
count_lower++;
printf("The number of uppercase letter %d, Lower case letters%d. \n ",
count_upper, count_lower);
return 0;
}
/*
PE 8-5
修改LIsting 8.4 的猜数程序,使用更智能的猜测策略。
例如,程序最初猜 50,询问用户是猜大了、猜小了还是猜对了。
如果猜小了,那么下一次猜测的值是 50和 100的中值,也就是 25,等等。
使用二分查找(binary search)策略,如果用户没有欺骗程序,
那么程序程序很快就会猜到正确的答案。
*/
#include <stdio.h>
#include <ctype.h>
char get_first(void);
char get_choice(void);
int main(void)
{
int guess = 50;
int start = 1;
int end = 100;
char choice;
printf("Pick an integer form 1 to 100. I will try to guess it. \n");
printf("Uh...is your number 50?\n");
while ((choice = get_choice()) != 'y')
{
ch