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

7.1

//编写一个程序读取输入,读到#字符停止,然后报告读取的空格数
//换行符数和所有其他字符的数量

#include <stdio.h>
#define STOP '#'
#define SPACE ' '

int main(void)
{
	char ch;
	int lines=1;
	int spaces=0;
	int others=0;

	while ((ch = getchar()) != STOP)
	{
		if (ch == SPACE)
			spaces++;
		if (ch == '\n')
			lines++;
		else
			others++;
	}
	printf("There are %d lines, %d spaces, %d others.", lines, spaces, others);

	return 0;
}

7.2

//编写一个程序读取输入,遇到#停止
//程序要打印每个输入的字符以及对应的ASCII码(十进制),一行8个

#include <stdio.h>
#define NUM 8
#define STOP '#'

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

	while ((ch = getchar()) != 'STOP')
	{
		count++;
		putchar(ch);
		printf(":%d ", ch);
		if (count % 8 == 0)
			printf("\n");
	}

	printf("Job Down!");

	return 0;
}

7.3
程序错误,不知道哪里有问题。。。
先记录以后查找。。。

//编写一个程序,读取整数知道用户输入0
//输入结束后,程序应报告用户输入的偶数、平均数、奇数、平均数个数

#include <stdio.h>	

int main(void)
{
	char num;;
	int count_double = 0;
	int count_single = 0;
	int sum_double = 0;
	int sum_single = 0;
	//float ave_double, ave_single;

	while ((num = getchar()) != 0)
	{
		if (num % 2 == 0)
		{ 
			count_double++;
			sum_double = sum_double 
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值