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

7.12.1

#include <stdio.h>
int main() {
	int score1 = 0, score2 = 0, score3 = 0;
	char ch;

	printf("Please enter strings(enter'#'to quit): ");
	scanf_s("%c", &ch);
	while (ch != '#') {
		if (ch == ' ')
			score1++;
		else if (ch == '\n')
			score2++;
		else
			score3++;
		scanf_s("%c", &ch);
	}
	printf("\nDone.\n");
	printf("There %d blank %d enter and %d chars", score1, score2, score3);

	return 0;
}

 7.12.2

#include <stdio.h>
int main() {
	char ch;
	int score = 0;
	printf("Please enter the strings(# to quit):");
	scanf_s("%c", &ch);
	while (ch != '#') {
		printf("%c %3d ", ch, ch);
		score++;
		if (score % 8 == 0)
			printf("\n");
		scanf_s("%c", &ch);
	}

	return 0;
}

 7.12.3

#include <stdio.h>
int main() {
	int num, onum = 0, evnum = 0;  //onum 是奇数的个数,evnum是偶数的个数 
	printf("Please enter integer(enter o to quit): ");
	scanf_s("%d", &num);
	while (num != 0) {
		if (num % 2 == 0)
			evnum++;
		else
			onum++;
		scanf_s("%d", &num);
	}
	printf("There are %d odd number, %d even number.", onum, evnum);

	return 0;
}

7.12.4

#include <stdio.h>
int main() {
	char ch;
	int score = 0;

	printf("Please enter string(# to quit):");
	scanf_s("%c", &ch);
	while(ch != '#'){
		if (ch == '!') {
			printf("!!");
			score++;
		}
		else if (ch == '.') {
			printf("!");
			score++;
		}
		else
			printf("%c", ch);
		scanf_s("%c", &ch);
	}
	printf("There are %d replace", score);
	return 0;
}

7.12.5 

 

#include <stdio.h>
int main() {
	char ch;
	int score = 0;

	printf("Please enter string(# to quit):");
	while((ch = getchar()) != '#') {
		switch (ch) {
			case'!':
				printf("!!");
				score++;
				break;

			case'.':
				printf("!");
				score++;
				break;
			default:
				printf("%c", ch);
		}
	}
	printf("\nThere are %d replace", score);
	return 0;
}

7.12.6

#include <stdio.h>
int charge(char CH) {
	int SCORE = 0;
	if (CH = 'e') {
		CH = getchar();
		if (CH == '#')
			return -1;
		else if (CH == 'i')
			SCORE++;
		else if (CH == 'e')
			SCORE += charge(CH);
	}
	return SCORE;
}
int main() {
	char ch;
	int score = 0, result;

	printf("Please enter string(# to quit):");
	while((ch = getchar()) != '#') {
		result = charge(ch);
		if (result == -1)
			break;
		score += result;
	}
	printf("\nThere are %d \'ei\'", score);
	return 0;
}

 7.12.7

#include <stdio.h>
#define MONEY 1000
#define OVERTIME 40
#define OVERWORK 1.5
#define TAX1 0.15
#define TAX2 0.2
#define TAX3 0.25
int main() {
	float salary, hour;
	printf("Please enter your work hours: ");
	scanf_s("%d", &hour);
	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);

	return 0;
}

7.12.8

#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("1) $8.75/hr                          2) $9.33/hr\n");
	printf("3) $10.00/hr                         4) $11.20/hr\n");
	printf("5) quit\n");
	printf("******************************************************************\n");
	scanf_s("%d", &num);

	if (num < 5 && num > 0) {
		printf("Please enter your work time(hour): ");
		scanf_s("%f", &hour);
		
		switch (num) {
			case 1:
				money = 8.75;
				break;
			case 2:
				money = 9.33;
				break;
			case 3:
				money = 10.00;
				break;
			case 4:
				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 == 5)
		printf("Done.");
	else {
		printf("Please enter right number!\n");
		goto UI;
	}

    return 0;
}

7.12.9

#include <stdio.h>
int main() {
	int num, temp_num, dov, judge = 1;
	printf("Please enter a postive number: ");
	scanf_s("%d", &num);

	while (num <= 0) {
		printf("Please enter right number!\n");
		printf("Please enter a postive number: ");
		scanf_s("%d", &num);
	}
	if (num == 1)
		printf("There is no prime number.");

	else
		for (temp_num = 2; temp_num <= num; temp_num++) {

			for (dov = 2; dov < temp_num; dov++) {
				if (temp_num % dov == 0) {
					judge = 0;
					break;
				}
				else
					judge = 1;
			}
			if (judge)
				printf("%d is prime number.\n", temp_num);
		}

	return 0;
}

7.12.10

#include <stdio.h>
#define INCOME1 17850
#define INCOME2 23900
#define INCOME3 29750
#define INCOME4 14875
#define RATE1 0.15
#define RATE2 0.28
int main() {
	int num;
	float income, tax;
	
	UI:
	printf("******************************************************************\n");
	printf("Enter the number corresponding to your physical truth: \n");
	printf("1) Single                        2) Head of Household\n");
	printf("3) Married,Joint                 4) Married,Sparate\n");
	printf("5) quit\n");
	printf("******************************************************************\n");
	scanf_s("%d", &num);
	if (num > 0 && num < 5) {
		printf("Please enter your taxable income:");
		scanf_s("%f", &income);

		switch (num) {
		case 1: if (income < INCOME1)
					tax = income * RATE1;
				else
					tax = INCOME1 * RATE1 + (income - INCOME1) * RATE2;
				break;

		case 2: if (income < INCOME2)
					tax = income * RATE1;
				else
					tax = INCOME2 * RATE1 + (income - INCOME2) * RATE2;
				break;

		case 3: if (income < INCOME3)
					tax = income * RATE1;
				else
					tax = INCOME3 * RATE1 + (income - INCOME3) * RATE2;
				break;
		
		case 4: if (income < INCOME4)
					tax = income * RATE1;
				else
					tax = INCOME4 * RATE1 + (income - INCOME4) * RATE2;
				break;
		}
		printf("Your final tax is: %.2f\n\n", tax);
		goto UI;
	}
	
	else if (num == 5) 
		printf("The program has done.");

	else {
		printf("Please enter the right number!\n");
		goto UI;
	}
}

7.12.11

#include <stdio.h>
int main() {
	char ch;
	float a_pound = 0,b_pound = 0, c_pound = 0, all_pound, temp_pound;	//a,b,c为每种蔬菜的磅数,all为总磅数,temp由于累加磅数的暂存磅数
	float cost, discount = 0, ship_cost, last_cost;							//蔬菜花费,折扣,包装费,最终价格
	
	UI: 
	printf("******************************************************************\n");
	printf("Enter the letter corresponding to your desire to vegetable: \n");				//交互界面
	printf("a) Artichoke                    b) Beet\n");
	printf("c) Carrot                       q) Quit\n");
	printf("******************************************************************\n");
	ch = getchar();
	while(ch == 'a' || ch =='b'|| ch =='c') {
		switch (ch) {													//switch语句用于重复添加每种蔬菜的重量
			case 'a':
				printf("Please enter the pounds of artichokes: ");
				scanf_s("%f", &temp_pound);
				a_pound += temp_pound;
				break;

			case 'b':
				printf("Please enter the pounds of beets: ");
				scanf_s("%f", &temp_pound);
				b_pound += temp_pound;
				break;

			case 'c':
				printf("Please enter the pounds of carrots: ");
				scanf_s("%f", &temp_pound);
				c_pound += temp_pound;
				break;
		}
		printf("Enter the letter corresponding to your desire to vegetable: ");
		ch = getchar();
		ch = getchar();

	}
	if (ch == 'q') {													//if语句收到中止字符'q',开始计算各类价格
		printf("\nStop entering!\n\n");
		if (a_pound || b_pound || c_pound) {							//if语句判断,若并非全无输入,开始计算价格
			printf("Here are the cost per pound you bought:\n");
		
			if (a_pound)												//根据购买的蔬菜种类开始输出蔬菜售价
				printf("Artichoke: $2.05 /lb\n");
			if (b_pound)
				printf("Beet:      $1.15 /lb\n");
			if (c_pound)
				printf("Carrot:    $1.09 /lb\n");

			all_pound = a_pound + b_pound + c_pound;
			printf("The pounds ordered is: %.2f pounds\n", all_pound);	//计算总磅数并输出

			cost = a_pound * 2.05 + b_pound * 1.15 + c_pound * 1.09;	//计算总菜价并输出
			printf("The cost for that order for each vegetable is: $%.2f\n", cost);

			if (cost >= 100) {											//计算折扣,若有,输出
				discount = cost * 0.05;
				printf("The discount is: $%.2f\n", discount);
			}
            
			if (all_pound <= 5)											//计算包装费并输出
				ship_cost = 6.5;
			else if (all_pound > 5 && all_pound <= 20)
				ship_cost = 14;
			else
				ship_cost = 14 + (all_pound - 20.0) * 0.5;
			printf("The shipping charge is: $%.2f\n", ship_cost);

			last_cost = cost - discount + ship_cost;					//计算最终价格并输出
			printf("The grand total of all the charges is: $%.2f", last_cost);

		}

		else															//无输入,执行语句
			printf("You buy nothing!");
	}
	else {
		printf("Please enter right letter!\n");
		goto UI;
	}
		
}

(吐槽一下这个11题,真的又臭又长,写了半个小时,第一个while循环采用两次getchar()是无奈之举,希望下一章能理解字符输入和输出的原理。最后几道涉及界面的程序,为了实现输入非法字符后能够重新载入原始界面用到了goto,并不是什么好的写法。但是目前能想到的代替goto的方法就是再次使用while循环,如此的话不仅要重复界面的输出,同时所有语句将会缩进一个制表符,略显臃肿。相比之下goto显得简洁许多,因此依然沿用goto,是知识储备与思维的局限。)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值