C Primer Plus(第六版)第八章编程题答案参考

8-1

#include <stdio.h>
int main(void) {
	int count = 0;
	while (getchar() != EOF) {
		count++;
	}
	printf("%d ", count);
	return 0;
}

8-2

#include <stdio.h>
int main(void){
	char ch;
	int count = 0;
	printf("请输入字符串(Ctrl+z作为结尾):");
	while ((ch = getchar()) != EOF){
		count++;
		if (ch < ' '){//空格前的字符为非打印字符,要特殊处理
			if (ch == '\t'){
				putchar('\\');
				putchar('t');
				printf(":%d\t", ch);
			}
			else if (ch == '\n'){
				count = 0;
				putchar('\\');
				putchar('n');
				printf(":%d\n", ch);
				printf("请输入字符串(Ctrl+z作为结尾):");
			}
			else{
				putchar('^');
				putchar(ch + 64);
				printf(":%d\t", ch);
			}
		}
		else{//不是非打印字符的直接输出
			putchar(ch);
			printf(":%d ", ch);
		}
		if (count % 10 == 0){//count计数,每十对换行
			count = 0;
			printf("\n");
		}
	}
	return 0;
}

8-3

#include <stdio.h>
#include <ctype.h>
int main(void) {
	char ch;
	int count_lower = 0;
	int count_upper = 0;
	printf("请输入字符串:");
	while ((ch = getchar()) != EOF) {
		if (islower(ch)) {
			count_lower++;
		}
		else if (isupper(ch)) {
			count_upper++;
		}
	}
	printf("大写字母:%d\n", count_upper);
	printf("小写字母:%d\n", count_lower);
	return 0;
}

8-4

#include <stdio.h>
#include <ctype.h>
int main(void) {
	char ch;
	int count_letter = 0;
	int count_word = 1;
	int average_letter = 0;
	printf("请输入一系列单词or输入整个句子:");
	while ((ch = getchar()) != EOF) {
		if (ch != ' ' && !ispunct(ch) && ch != '\n') {
			count_letter++;
		}
		else if (ch == ' ') {
			count_word++;
		}
		else if (ch == '\n') {
			printf("请继续输入(输入Ctrl+z结束程序):");
		}
	}
	average_letter = count_letter / count_word;
	printf("总共有%d个字母,%d个单词,平均每个单词的字母数为:%d", count_letter, count_word, average_letter);

	return 0;
}

8-5

#include <stdio.h>
int main(void) {
	int guess = 50;
	int lower_limit = 1;
	int upper_limit = 100;
	char response;
	printf("Pick an integer from 1 to  100, I will try to guess it.\n");
	printf("Respond with a y if my guess is right and with a l if it is larger and with a s if it is smaller.\n");
	printf("Uh…is your number %d?\n", guess);
	while ((response = getchar()) != 'y') {
		if (response == 'l') {//猜的太大了
			upper_limit = guess;
			guess = (lower_limit + upper_limit) / 2;
			printf("Well, then, is it %d?\n", guess);
		}
		else if (response == 's') {//猜的太小了
			lower_limit = guess;
			guess = (lower_limit + upper_limit) / 2;
			printf("Well, then, is it %d?\n", guess);
		}
		while (getchar() != '\n') {
			continue;
		}
	}
	printf("I knew I can do it!\n");
	return 0;
}

8-8

#include <stdio.h>
void menu();
double add(float x, float y);
double subtract(float x, float y);
double multiply(float x, float y);
double divide(float x, float y);
int main(void) {
	menu();
	float n1, n2;
	char choice;
	double result;
	while ((choice = getchar()) != 'q') {
		if (choice == 'a' || choice == 's' || choice == 'm' || choice == 'd') {//如果输入了指定的字母,则接下来输入需要计算的数字
			printf("Enter first number:");
			while (scanf("%f", &n1) != 1) {//若输入的不是数字,则不断提醒用户输入数字,并清空scanf缓存
				printf("Please enter a number, such as 2.5, -1.78E8, or 3:");
				while (getchar() != '\n') {
					continue;
				}
			}
			printf("Enter second number:");
			while (scanf("%f", &n2) != 1) {//若输入的不是数字,则不断提醒用户输入数字,并清空scanf缓存
				printf("Please enter a number, such as 2.5, -1.78E8, or 3:");
				while (getchar() != '\n') {
					continue;
				}
			}
		}
		else {
			printf("Please enter the right choice:\n");
		}
		switch (choice) {
		case 'a':
			result = add(n1, n2);
			printf("%.3lf\n", result);
			break;
		case 's':
			result = subtract(n1, n2);
			printf("%.3lf\n", result);
			break;
		case 'm':
			result = multiply(n1, n2);
			printf("%.3lf\n", result);
			break;
		case 'd':
			result = divide(n1, n2);
			printf("%.3lf\n", result);
			break;
		default:
			break;
		}
		while (getchar() != '\n') {
			continue;
		}
		menu();
	}
	printf("Bye!");
	return 0;
}
void menu() {
	printf("Enter the operation of your choice:\n");
	printf("a. add         s. subtract\n");
	printf("m. multiply    d. divide\n");
	printf("q. quit\n");
	return;
}
double add(float x, float y) {
	return x + y;
}
double subtract(float x, float y) {
	return x - y;
}
double multiply(float x, float y) {
	return x * y;
}
double divide(float x, float y) {
	while (y == 0) {
		printf("Enter a number other than 0:");
		scanf("%f", &y);
	}
	return x / y;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值