《C Primer Plus》第七章编程练习答案

这是一篇关于《C Primer Plus》第七章的编程练习答案,作者分享了自己的学习成果,包含多个编程题目,旨在帮助读者巩固C语言的知识,欢迎大家纠错交流。
摘要由CSDN通过智能技术生成

自己学习过程中编写的,仅供参考,如有错误,欢迎评论区指正!
01

#include <stdio.h>

int main(void)
{
   
	int space = 0, n = 0, other = 0;
	char ch;

	while ((ch = getchar()) != '#')
	{
   
		if (ch == ' ')
			space++;
		else if (ch == '\n')
			n++;
		else
			other++;
	}
	printf("space=%d, n=%d, other=%d\n", space, n, other);

	return 0;
}

02

#include <stdio.h>

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

	while ((ch = getchar()) != '#')
	{
   

		if (count++ % 9 == 0)
			putchar('\n');

		else if (ch == '\n')
			printf("'\\n',%-3d ", ch);

		else if (ch == '\t')
			printf("'\\t',%-3d ", ch);

		else
			printf("%c,%-3d ", ch, ch);
	}

	return 0;
}

03

#include <stdio.h>

int main(void)
{
   
	double ave_ou, ave_ji;
	int num, sum_ou, sum_ji, count_ou, count_ji;
	sum_ji = sum_ou = count_ji = count_ou = 0;

	printf("请输入数字:");
	while (scanf("%d", &num) == 1 && num)
	{
   
		if (num % 2 == 0)
		{
   
			count_ou++;
			sum_ou += num;
		}
		else
		{
   
			count_ji++;
			sum_ji += num;
		}
		printf("请继续输入整数(输入0退出程序):");
	}
	printf("偶数个数为:%d, 偶数平均值为:%.2lf\n", count_ou, ave_ou);
	printf("奇数个数为:%d, 奇数平均值为:%.2lf\n", count_ji, ave_ji);

	printf("程序结束\n");

	return 0;
}

04

#include <stdio.h>

int main(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值