C primer plus 第六版 第八章 编程练习 答案

8.1

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

#include <stdio.h>	

int main(void)
{
	int count = 0;
	char ch;

	while ((ch = getchar()) != EOF)
		count++;
	printf("There are %d chars.", count);

	return 0;
}

8.2不懂

8.3

//把输入作为字符流读取,报告大写字母和小写字母的个数

#include <stdio.h>
#include <ctype.h>

int main(void)
{
	char ch = 0;
	int count_lower = 0;
	int count_upper = 0;

	while ((ch = getchar()) != EOF)
	{
		if (islower(ch))
			count_lower++;
		if (isupper(ch))
			count_upper++;
	}
	printf("The number of lower letter:%d , upper letter: %d.\n",
		count_lower, count_upper);

	return 0;
}

8.5

 #include <stdio.h>

char get_first(void);

int main(void)
{
	int guess = 50;
	int start = 1;
	int end = 100;
	char ch;

	printf("Please pick an integer from 1 to 100."
		"I will guess it.\n");
	printf("is it 50?\n");
	while ((ch = get_first()) != 'y')
	{
		printf("OK, is it biger or smaller than the guess? bigger or small?\n");
		if ('b' == (ch = get_first()))
		{
			end = guess;
			guess = (start + end) / 2;
		}
		else
		{
			start = guess;
			guess = (start + end) / 2;
		}
		printf("The number is %d,is it right?\n", guess);
	}

	printf("I guess it.");
}

char get_first(void) {
	int ch;
	ch = getchar();
	while (getchar() != '\n')
		continue;
	return ch;
}

8.6

有空再更

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值