C primer plus 编程练习 7.12

本文详细介绍了C Primer Plus书籍中第7.12章节的编程练习,涵盖关键概念和解决方案步骤,帮助读者深入理解C语言。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.

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

int main(void)
{
	char ch;
	int sc = 0,nc = 0,oc = 0;

	printf("Eenter something you want to be statisticed(\"#\" to quit):");
	while ((ch = getchar()) != '#')
	{
		if (ch == ' ')
			sc++;
		else if (ch == '\n')
			nc++;
		else 
			oc++;
	}
	printf("There are %d space, %d newlines,%d others\n",sc,nc,oc);

	return 0;
}

2.

#include <stdio.h>

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

	printf("Enter something(# to quit: ");
	
	while ((ch = getchar()) != '#')
	{
		if ((n % 8 == 0) && n != 0)
			printf("\n");
		printf("%c-%d,",ch,ch);
		n++;
	}

	return 0;
}

3.

#include <stdio.h>

int main(void)
{
	int num;
	int ct_even = 0, ct_odd = 0;
	double sum_even = 0.0,sum_odd = 0.0;

	printf("Enter a integer: ");
	

	while (scanf("%d",&num) == 1 && num != 0)
	{
		if (num % 2 == 0)
		{
			ct_even++;
			sum_even += (float)num;
		}
		else
		{
			ct_odd++;
		    sum_odd += (float)num;
		}
        printf("Enter the next integer(0 to quit): ");
	}

	printf("Number of evens: %d",ct_even);
	if (ct_even > 0)
		printf("  average: %g",sum_even / ct_even);	
	printf("\n");

    printf("Number of odds: %d",ct_odd);
	if (ct_odd > 0)
		printf("  average: %g",sum_odd / ct_odd);	
	printf("\n");
	printf("done\n");

	return 0;
}

4.

#include <stdio.h>

int main(void)
{
	char ch;
	int i = 0;
	int j = 0;

	printf("Enter something to test the program(# to quit): ");

	while ( (ch = getchar()) != '#')
	{
		if ('.' == ch)
		{
			i++;
			putchar('!');
		}
		else if ('!' == ch)
		{
			putchar('!');
			putchar('!');
			j++;
		}
		else
		    putchar(ch);
	}
    printf("%d times",i + j);
	return 0;

5.

#include <stdio.h>

int main(void)
{
	char ch;
	int ct1 = 0;
	int ct2 = 0;

	printf("Enter something to test the program(# to quit): ");

	while ( (ch = getchar()) != '#')
	{
		switch (ch)
		{
		case '.':
			putchar('!');
		    ct1++;
			break;
	    case '!':
			putchar('!');
			putchar('!');
			ct2++;
			break;
		default:
		    putchar(ch);
		}
	}
    printf("%d times",ct1 + ct2);

	return 0;
}

6.

#include <s
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值