C Primer Plus编程练习答案(Chapter8)

8.11.1

 

#include <stdio.h>
int main() {

	int ch;
	int num = 0;

	while ((ch = getchar()) != EOF) {
		num++;
		//putchar(ch);		   此两行为文件输出验证项
		//printf("%d \n", num);
	}

	printf("There are %d strings in the file.", num);

	return 0;
}

8.11.2

 

#include <stdio.h>
int main() {

	int ch;
	int n = 0;

	while ((ch = getchar()) != EOF) {
		if (ch == '\n') {
			printf("\\n");
		}
		else if (ch == ' ') {
			printf("\' \' -  0  ");
			n++;
		}
		else {
			putchar('\'');
			putchar(ch);
			putchar('\'');
			printf(" - %d  ", ch);
			n++;
		}
		if (n == 10 || ch == '\n') {
			printf("\n");
			n = 0;
		}
	}
	printf("^Z - %d  ", ch);

	return 0;
}

8.11.3

 

#include <stdio.h>
int main() {

	int ch;
	int num_Clt = 0, num_Slt = 0;

	while ((ch = getchar()) != EOF) {
		if (ch >= 65 && ch <=  90)
			num_Clt++;
		else if (ch >= 97 && ch <= 122)
			num_Slt++;
	}

	printf("There are %d Captial letters.", num_Clt);
	printf("There are %d Small letters.", num_Slt);

	return 0;
}
	

8.11.4

 

本题书中提示的ctype.h头文件中的ispunct函数存在一定局限性。ispunct(ch)为判定ch是否为字符,ch是特殊字符则返回非零数,ch若是数字和普通字符则返回0。但在实操中发现ispunct(ch)判定的特殊字符不包括空白字符。因此应该同时选用isspace()函数进行空白字符的筛选。

#include <stdio.h>
#include <ctype.h>
int main() {

	int ch, temp_ch = 0;
	int word = 0, letter = 0;
	int avg_letter, sum = 0;

	while ((ch = getchar()) != EOF) {
		if (ispunct(ch) == 0 && ch != ' ') {
			letter++;
		}
		if ((ispunct(ch) || isspace(ch)) && ispunct(temp_ch) == 0 && isspace(temp_ch) == 0)
		{
			sum += letter;
			word++;
			// 验证项
			//printf("letter = %d sum = %d word = %d\n", letter, sum, word);
			letter = 0;

		}
		temp_ch = ch;

	}

	printf("On averagyae,There are %d letters one per word.", sum / word);

	return 0;
}

8.11.5

 程序设计略显臃肿,并不优越。

#include <stdio.h>
int main() {

	int max = 100, min = 1;
	int guess = 50, ch;
	printf("Pick an integer from 1 to 100. I will try to guess it.\n");
	printf("Respond with \'l\' if my guess is large.\n");
	printf("And respond with \'s\' if my guess is small.\n");
	printf("Please respond with \'y\' if my guess is right\n");
	printf("Uh...is your number is %d?\n", (max + min) / 2);

	while (ch = getchar()) {
		if (ch == 's' || ch == 'l') {
			while (getchar() != '\n')					//
				continue;				//输入正确后清除缓冲区直到下次输入
			if (ch == 's') {
				min = guess;
				guess = (max + min) / 2;
			}
			else if (ch == 'l') {
				max = guess;
				guess = (max + min) / 2;
			}
			if (max - min == 2) {
				printf("Aha! your number must be %d!", guess);
				break;
			}
			if (max < min || max == min) {
				printf("Are you joking now?\n");
				printf("You fucking forget your number! Try it again!");
				max = 100;
				min = 0;
				guess = 50;
				printf("\nUh...is your number is %d?\n", guess);
				continue;
			}
			printf("Uh...is your number is %d?\n", guess);
		}
		else if (ch == 'y') {					//猜测正确的反馈输入
			printf("Aha! I get you!");
			break;
		}
		else {				//不合法的输入,提示后重新输入
			while (getchar() != '\n')
				continue;
			printf("Your enter is wrong. Please enter again!\n");
			printf("Uh...is your number is %d?\n", guess);
		}

	}

	return 0;
}

8.11.6

 

#include <stdio.h>
#include <ctype.h> 
char get_first(void);
int main() { 
	char ch;
	
	printf("Please enter a sentence!\n");
	ch = get_first();
	if (ch != 0)
		printf("The first nonspace character is %c", ch);
	else
		printf("There no nonspce character.");
}
char get_first(void) {

	int ch = 0;
	while (isspace(ch = getchar())) {		//判断第一个字符是否是非空白字符。若是,继续获取直到不是为止。
		if (ch == '\n')						
			return 0;						//若到输入结束全部为非空白字符,返回0给主函数
		else
			continue;			
	}
	while (getchar() != '\n')
		continue;

	return ch;
	
}

8.11.7

 

#include <stdio.h>
#define OVERTIME 40
#define OVERWORK 1.5
#define TAX1 0.15
#define TAX2 0.2
#define TAX3 0.25
int main() {
	int num;
	float salary, hour, money;
	//交互界面
UI:
	printf("******************************************************************\n");
	printf("Enter the number corresponding to the desired pay rate or action: \n");
	printf("a) $8.75/hr                          b) $9.33/hr\n");
	printf("c) $10.00/hr                         d) $11.20/hr\n");
	printf("q) quit\n");
	printf("******************************************************************\n");
	num = getchar();
	while (getchar() != '\n')
		continue;

	if (num < 'e' && num >= 'a') {
		printf("Please enter your work time(hour): ");
		scanf_s("%f", &hour);

		switch (num) {
		case 'a':
			money = 8.75;
			break;
		case 'b':
			money = 9.33;
			break;
		case 'c':
			money = 10.00;
			break;
		case 'd':
			money = 11.20;
			break;
		}
		if (hour > OVERTIME)
			hour = OVERTIME + (hour - OVERTIME) * OVERWORK;
		salary = hour * money;
		if (salary < 300)
			salary -= salary * TAX1;
		else if (salary < 450)
			salary -= 300 * TAX1 + (salary - 300) * TAX2;
		else if (salary > 450)
			salary -= 300 * TAX1 + 150 * TAX2 + (salary - 450) * TAX3;

		printf("Your last salary is :%.2f", salary);

	}

	else if (num == 'q')
		printf("Done.");
	else {
		printf("Please enter right number!\n");
		goto UI;
	}

	return 0;
}

8.11.8

 

#include<stdio.h>
float get_float(void);
void UI();
int main() {

	int ch;
	float fst_num = 0, scd_num = 0;

	UI();

	ch = getchar();
	while (getchar() != '\n')
		continue;

	while (ch != 'q') {

		while (ch != 'a' && ch != 's' && ch != 'd' && ch != 'm') {
			printf("Please enter right character!\n");

			ch = getchar();
			while (getchar() != '\n')
				continue;
		}

		printf("Enter first number: ");
		fst_num = get_float();
		printf("Enter second number: ");
		scd_num = get_float();

		switch (ch) {
		case 'a':
			printf("%.1f + %.1f = %.1f\n", fst_num, scd_num, fst_num + scd_num);
			break;
		case 's':
			printf("%.1f - %.1f = %.1f\n", fst_num, scd_num, fst_num - scd_num);
			break;
		case 'm':
			printf("%.1f * %.1f = %.1f\n", fst_num, scd_num, fst_num * scd_num);
			break;
		case 'd':
			while (scd_num == 0) {
				printf("Enter enter a number more than 0:");
				scd_num = get_float();
			}
			printf("%.1f / %.1f = %.1f\n", fst_num, scd_num, fst_num / scd_num);
			break;

		}

		UI();
		ch = getchar();
		while (getchar() != '\n')
			continue;

	}

	printf("Bye.");

}

void UI() {
	printf("***********************************\n");
	printf("Enter the operation of your choice:\n");
	printf("a. add                s. subtract\n");
	printf("m. multiply           d. divide\n");
	printf("q. quit\n");
}
float get_float(void) {
	float input;
	char ch;

	while (scanf_s("%f", &input) != 1) {
		while ((ch = getchar()) != '\n')
			putchar(ch);
		printf(" is not an number.\n Please enter an number,");
		printf("such as 2.5, -1.78E8, or 3: ");
	}
	while (getchar() != '\n')
		continue;
	
	return input;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值