天天看点

C Primer Plus 第七章:分支和跳转 编程答案

7.16 编程练习

  1. 编写一个程序读取输入,读到#字符停止,然后报告读取的空格数、换行数和其他所有字符的数量。
#include <stdio.h>
int main()
{
	int num_n = 0;
	int num_space = 0;
	int num_else = 0;
	char ch;
	while (scanf("%c", &ch) == 1)
	{
		if (ch == '#')
			break;
		else if (ch == ' ')
			num_space++;
		else if (ch == '\n')
			num_n++;
		else
			num_else++;
	}
	printf("num_n:%3d\num_else:%3d\nnum_space:%3d\n", \
	num_n, num_else, num_space);
	return 0;
}
           
  1. 编写一个程序读取输入,读到#字符停止。程序要打印每个输入的字符以及对应的ASCII码(十进制)。一行打印8个字符。建议:使用字符计数和求模运算符(%)在每8个循环周期时打印一个换行符。
#include <stdio.h>
#define SIZE 8
int main()
{
	char ch;
	int num = 1;
	while (scanf("%c", &ch) == 1)
	{
		if (ch == '#')
		{
			break;
		}
		else
		{
			if (num % SIZE == 0)
				printf("%-5d\n",ch);
			else
				printf("%-5d",ch);
			num++;
		}
	}
	return 0;
}
           
  1. 编写一个程序,读取整数直到用户输入 0。

    输入结束后,程序应报告用户输入的偶数(不包括 0)个数、

    这些偶数的平均值、输入的奇数个数及其奇数的平均值

#include <stdio.h>
int main()
{
	int num_even = 0;
	int num_odd = 0;
	int sum_even =0;
	int sum_odd = 0;
	int num;
	while (scanf("%d", &num) == 1)
	{
		if (num == 0)
		{
			break;
		}
		else if (num % 2 == 0)
		{
			num_even++;
			sum_even +=num;
		}
		else
		{
			num_odd++;
			sum_odd +=num;
		}
	}
	printf("num_even:%-5d,num_odd:%-5d\n", num_even, num_odd);
	printf("sum_even:%-5d,sum_odd:%-5d\n", sum_even, sum_odd);
	printf("ave_even:%-5d,ave_odd:%-5d\n",sum_even / num_even, \
	sum_odd / num_odd);
	return 0;
}
           
  1. 使用if else语句编写一个程序读取输入,读到#停止。用感叹号代替句号,用两个感叹号代替原来的感叹号,最后报告进行了多少次替代。
#include <stdio.h>
int main()
{
	int i = 0, j = 0;
	char ch;
	while ((ch = getchar()) != '#')
	{
		if (ch == '.')
		{
			putchar('!');
			i++;
		}
		else if (ch == '!')
		{
			putchar('!');
			putchar('!');
			j++;
			
		}
		else
		{
			putchar(ch);
		}
	}
	printf("\nthe times of '.', replaced:\n:%-5d\n", i);
	printf("the times of '!' replaced:\n%-5d\n", j);
	return 0;
}
           
  1. 用switch重写练习4。
#include <stdio.h>
int main()
{
	int i = 0, j = 0;
	char ch;
	while ((ch = getchar()) != '#')
	{
		switch (ch)
		{
		case '.':
			{
				putchar('!');
				i++;
				break;
			}
		case '!':
			{
				putchar('!');
				putchar('!');
				break;
				j++;
			}
		default:
			{
				putchar(ch);
			}
		}
	}
	printf("\nthe times of '.', replaced:\n:%-5d\n", i);
	printf("the times of '!' replaced:\n%-5d\n", j);
	return 0;
}
           
  1. 编写程序输入,读到#停止,报告ei出现的次数。
#include <stdio.h>
int main()
{
	int count = 0;
	char ch, ch_last;
	while ((ch = getchar()) != '\n')
	{
		if (ch == 'i' && ch_last == 'e')
		{
			count++;
			ch_last = ch;
		}
		else
		{
			ch_last = ch;
		}
	}
	printf("\nthe times of occurrences of 'ei' is:%-2d\n",count);
	return 0;
}
           
  1. 编写一个程序,提示用户输入一周工作的小时数,然后打印工资总额、税金和净收入 做如下假设:

    a.基本工资 = 1000美元 / 小时

    b.加班(超过40小时) = 1.5倍时间

    c.税率: 前300美元为15% 续150美元为20% 余下的为25%

    用#define定义符号常量,不用在意是否符合当前的税法

#include<stdio.h>
#define TIME_LIMIT 40
#define PAY_HOUR 10
int main()
{
	float workhours, pre_tax, after_tax;
	printf("please enter your working hours:");
	while(scanf("%f", &workhours) == 1)
	{
		if (workhours <= TIME_LIMIT)
		{
			pre_tax = PAY_HOUR * workhours;
		}
		else
		{
			pre_tax = PAY_HOUR * (TIME_LIMIT + (workhours - TIME_LIMIT) * 1.5);
		}

		if (pre_tax <=300)
		{
			after_tax = pre_tax - pre_tax * 0.15;
		}
		else if (pre_tax > 300 && pre_tax <=450)
		{
			after_tax = pre_tax - 300 * 0.15 - (pre_tax - 300) * 0.2;
		}
		else
		{
			after_tax = pre_tax - 300 * 0.15 - 150 * 0.2 - (pre_tax - 450) * 0.25;
		}
		printf("your salary before tax is:%-5.2f\n", pre_tax);
		printf("your salary after tax is:%-5.2f\n", after_tax);
		printf("please enter your working hours again or enter 'q' to quit:");
	}
	return 0;
}
           
  1. 修改练习7的假设a,让程序可以给出一个供选择的工资等级菜单

    使用 switch 完成工资等级的选择。

    运行程序后,显示的菜单应该类似这样:

    *******************************************************************************

    Enter the number corresponding to the desired pay rate of action:

    1) $8.75/hr 2) $9.33/hr

    3) $10.00/hr 4) $11.20/hr

    5) quit

    *******************************************************************************

    如果选择一个1~4其中的一个数字,程序应该询问用户工作的小时数。

    程序要通过循环运行,除非用户输入 5。

    如果输入1~5 以外的数字,程序应该提醒用户输入正确的选项,

    然后再重复显示菜单提示用户输入。

    使用#define创建符号常量表示各工资等级和税率

#include<stdio.h>
#define TIME_LIMIT 40
int main()
{
	int i, pay_level;
	float workhours, pre_tax, after_tax, pay_hour;
	for (i = 1; i <= 65; i++)
		putchar('*');
	printf("\nEnter the number corresponding to the desired pay rate or action:\n");
	printf("%d) $8.75/hr%-20d) $9.33/hr\n%d) $10.00/hr%-20d) $11.20/hr\n%d) quit)\n", 1, 2, 3, 4, 5);
	for (i = 1; i <= 65; i++)
		putchar('*');
	printf("\n");
	while(scanf("%d", &pay_level) == 1)
	{
		if (pay_level >=0 && pay_level <=4)
		{
			printf("Enter your working hours:");
			scanf("%f", &workhours);
			switch (pay_level)
			{
			case 1:
				{
					pay_hour = 8.75;
					break;
				}
			case 2:
				{
					pay_hour = 9.33;
					break;
				}
			case 3:
				{
					pay_hour = 10.00;
					break;
				}
			case 4:
				{
					pay_hour = 11.20;
					break;
				}
			default:
				/*do nothing*/;
			}
		}
		else if (pay_level == 5)
		{
			break;
		}
		else
		{
			printf("Enter the correct number, only 1-5 is availble:\n");
		}
		if (workhours <= TIME_LIMIT)
		{
			pre_tax = pay_hour * workhours;
		}
		else
		{
			pre_tax = pay_hour * (TIME_LIMIT + (workhours - TIME_LIMIT) * 1.5);
		}

		if (pre_tax <=300)
		{
			after_tax = pre_tax - pre_tax * 0.15;
		}
		else if (pre_tax > 300 && pre_tax <=450)
		{
			after_tax = pre_tax - 300 * 0.15 - (pre_tax - 300) * 0.2;
		}
		else
		{
			after_tax = pre_tax - 300 * 0.15 - 150 * 0.2 - (pre_tax - 450) * 0.25;
		}
		printf("your salary before tax is:%-5.2f\n", pre_tax);
		printf("your salary after tax is:%-5.2f\n", after_tax);
		printf("please enter the number again or enter 5 to quit:");
	}
	return 0;
}
           
  1. 编写一个程序,只接受正整数输入,然后显示所有小于等于该数的素数。
#include<stdio.h>
int main()
{
	int i, j, counter, num;
	printf("please enter the integer:");
	scanf("%d", &num);
	while (num > 0)
	{
		for (i = 1; i <= num; i++)
		{
			for (j = 1, counter = 0; j <= i; j++)
			{
				if (i % j == 0)
				{
					counter++;
					if (counter > 2)
					{
						break;
					}
					else
					{
						/*do nothing*/
					}
				}
				else
				{
					/*do nothing*/
				}
			}
			if (counter == 2)
			{
				printf("%-5d\n", i);
			}
			else
			{
				/*do nothing*/
			}
		}
		break;
	}
	return 0;
}
           
  1. 1988年的美国联邦税收计划是近代最简单的税收方案。它分为4个类别,每个类别有两个等级。下面是税收计划的摘要(美元数为应征税的收入):

    类别 税金

    单身------------------------17850美元按15%计,超出部分按28%计

    户主------------------------23900美元按15%计,超出部分按28%计

    已婚,共有---------------29750美元按15%计,超出部分按28%计

    已婚,离异---------------14875美元按15%计,超出部分按28%计

    例如:一位工资为20000美元的单身纳税人,应缴纳税费 0.15x17850+0.28x(20000-17850)美元。编写一个程序,让用户指定缴纳税金的种类和应纳税收入,然后计算税金。程序应通过循环让用户可以多次输入。

#include<stdio.h>
#define RATE1 0.15
#define RATE2 0.28
#define LEVEL1 17850
#define LEVEL2 23900
#define LEVEL3 29750
#define LEVEL4 14875

int main()
{
	int category;
	float pre_tax, after_tax, tax_limit;
	printf("%d)单身%23d)户主\n%d)已婚、共有%17d已婚、离异\nplease enter the number corresponding your category correctly as above:", 1, 2, 3, 4);
	while (scanf("%d", &category) == 1)
	{
		if (category >= 1 && category <= 4)
		{
			switch (category)
			{
			case 1:
				{
					tax_limit = LEVEL1;
					break;
				}
			case 2:
				{
					tax_limit = LEVEL2;
					break;
				}
			case 3:
				{
					tax_limit = LEVEL3;
					break;
				}
			case 4:
				{
					tax_limit = LEVEL4;
					break;
				}
			}
		}
		else
		{
			printf("the number is error, enter 'q' to quit or enter number again:");
			continue;
		}
		printf("please enter your salary:");
		scanf("%f", &pre_tax);
		if (pre_tax <= tax_limit)
		{
			after_tax = pre_tax * (1 - RATE1);
		}
		else
		{
			after_tax = pre_tax - tax_limit *RATE1 - (pre_tax - tax_limit) * RATE2;
		}
		printf("tax_limit:%-5.2f\nsalary before tax:%-5.2f\nsalary after tax:%-5.2f\n", tax_limit, pre_tax, after_tax);
		printf("please continue to enter the number corresponding your category:");
	}
	printf("your input is not a number, THX!");
	return 0;
}
           
  1. ABC邮政杂货店出售的洋蓟售价为2.05美元/磅,甜菜售价为1.15美元/磅,胡萝卜售价为1.09美元/磅。添加运费之前,100美元的订单有5%的打折优惠。少于或等于5磅的订单收取6.5美元的运费和包装费,5磅~20磅的订单收取14美元的运费和包装费,超过20磅的订单在14美元的基础上每续重1磅增加0.5美元。编写一个程序,在循环中用switch语句实现用户输入不同的字母时有不同的响应,即输入a响应是让用户输入洋蓟的磅数,b是甜菜的磅数,c是胡萝卜的磅数,q退出订购。程序要记录累计的重量。然后,该程序要计算货物总价、折扣(如果有的话)、运费和包装费。随后,程序要显示所有的购买信息:物品售价、订购的重量(磅)、订购的蔬菜费用、订单的总费用、折扣(如果有的话)、运费和包装费、以及所有的费用总额。
#include<stdio.h>
#define ARTICHOKE 2.05
#define BEET 1.15
#define CARROT 1.09
#define DISCOUNT 0.05
#define LEVEL1 6.5
#define LEVEL2 14
#define RATE 0.5

int main()
{
	char name_veg;
	float weight, price_unit, price_total, pay, freight;
	printf("%7d)a means artichoke\n%7d)b means beet\n%7d)c means carrot\n%7d)q means quit\n", 1, 2, 3, 4);
	printf("Please enter initials of the vegetable name you want to buy:");
	while (scanf("%c", &name_veg) == 1)
	{
		switch (name_veg)
		{
		case 'a':
			{
				price_unit = ARTICHOKE;
				break;
			}
		case 'b':
			{
				price_unit = BEET;
				break;
			}
		case 'c':
			{
				price_unit = CARROT;
				break;
			}
		case 'q':
			{
				break;
			}

		}
		printf("enter how many pounds you need:");
		scanf("%f", &weight);
		price_total = price_unit * weight;
		if (price_total >= 100)
		{
			pay = price_total * (1 - DISCOUNT);
		}
		else
		{
			pay = price_total;
		}
		if (weight <= 5)
		{
			freight = LEVEL1;
		}
		else if (weight >5 && weight <=20)
		{
			freight = LEVEL2;
		}
		else
		{
			freight = LEVEL2 + (weight - 20) * RATE;
		}
		printf("the money you should pay is:%-5.2f", pay + freight);
		break;
	}
	return 0;
}