C Primier Plus(第六版)第7章 编程练习 VS2019测试

第1题

/*编程练习7.1读取输入统计空格换行和其他字符*/
#include<stdio.h>

int main(void)
{
	char c;
	int n_space = 0;
	int n_line = 0;
	long n_character = 0L;

	printf("Please enter some text;enter # to quit.\n");
	while ((c = getchar() )!= '#') {
		if (c == ' ')
			n_space++;
		else if (c == '\n')
			n_line++;
		else
			n_character++;
	}
	printf("Done!There are %d spaces, %d lines, and %ld another characters.\n", n_space, n_line, n_character);

	return 0;
}

第2题

/*编程练习7.2读取输入打印字符及对应的ASCII码,每行打印8个*/
#include<stdio.h>

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

	printf("Please enter some text to analysis,enter # to quite.\n");
	while ((ch = getchar()) != '#') {
		num++;
		printf("%c-%d\t", ch, ch);
		if (num % 8 == 0)
			printf("\n");
	}
	printf("\nDone!");

	return 0;
}

第3题

/*编写一个程序,读取整数直到用户输入0.输入结束后,程序应报告用户输入的偶数(不包括0)个数、这个偶数的平均值、输入的奇数个数及其平均值*/
#include<stdio.h>

int main(void)
{
	float sum_odd = 0, sum_even = 0;
	int num_odd = 0, num_even = 0, n;

	printf("Please enter some integers to analysis,0 to quite.\n");
	while((scanf_s("%d",&n))==1){
		if (n == 0)
			goto end;
		if (n % 2 == 1) {
			sum_odd += n;
			num_odd++;
		}
		if (n % 2 == 0) {
			sum_even += n;
			num_even++;
		}
	}
	end:printf("\nDone!");
	printf("There are %d odd numbers,the average of odd number is %.2f;there are %d even numbers,the average of even is %.2f.\n",
		num_odd, sum_odd / num_odd, num_even, sum_even / num_even);
	
	return 0;
}

第4题

/*使用if else 语句编写一个程序读取输入,读到#停止。用感叹号替换句号,
用两个感叹号替换原来的感叹号,最后报告进行了多少次替换。*/
#include<stdio.h>

int main(void)
{
	char ch;

	printf("Please input some text to revise, # to quite.\n");
	while ((ch = getchar()) != '#') {
		if (ch == '.')
			putchar('!');
		else if(ch == '!') {
			putchar('!');
			putchar('!');
		}
		else
			putchar(ch);
	}
	printf("\nDone!");

	return 0;
}

第5题

/*使用switch重写练习4*/
#include<stdio.h>

int main(void)
{
	char ch;

	printf("Please input some text to revise, # to quite.\n");
	while ((ch = getchar()) != '#') {
		switch (ch) {
		case '.':putchar('!');
			break;
		case '!':putchar('!');
			putchar('!');
			break;
		default:putchar(ch);
			break;
		}
	}

	return 0;
}

第6题

/*编写程序读取输入,读到#停止,报告ei出现的次数*/
#include<stdio.h>
#include<stdbool.h>

int main(void)
{
	char ch;
	_Bool ch_pre=false, ch_later=false;	//将ei拆分,出现'e',将ch_pre设为真,出现'i',将ch_later设为真
	int count=0;

	printf("Please input some text to analysis, # to quit.\n");
	while ((ch = getchar()) != '#') {
		switch (ch) {
		case 'e':ch_pre = true;
			break;
		case 'i':ch_later = true;
			if (ch_pre == true && ch_later == true)
				count++;
			ch_pre = false;
			break;
		default:ch_pre = false;
			ch_later = false;
			break;
		}
	}
	printf("There are %d 'ea'.\n", count);

	return 0;
}

第7题

/*编程练习7.7 编写一个程序,提示用户输入一周工作的小时数,然后打印工资总额、税金和净收入。
做如下假设:a.基本工资=10.00美元/小时  b.加班(超过40小时)=1.5倍的时间  c.税率:前300美元
为15%续150美元为20%  余下的为25%  用#define定义符号常量。*/
#include<stdio.h>
#define BASIC_WAGE 10.00
#define BASIC_HOUR 40
#define HOUR_RATE 1.5
#define TAX_RATE_1 0.15
#define TAX_RATE_2 0.20
#define TAX_RATE_3 0.25

int main(void)
{
	float hour,total, tax, income;

	printf("Please enter the number of hours worked in a week.\n");
	scanf_s("%f", &hour);
	if (hour > BASIC_HOUR)
		hour = BASIC_HOUR + (hour - BASIC_HOUR) * HOUR_RATE;
	else
		;
	total = BASIC_WAGE * hour;
	if (total <= 300)
		tax = total * TAX_RATE_1;
	else if (total > 300 && total <= 450)
		tax = 300 * TAX_RATE_1 + (total - 300) * TAX_RATE_2;
	else
		tax = 300 * TAX_RATE_1 + 150 * TAX_RATE_2 + (total - 450) * TAX_RATE_3;
	income = total - tax;
	printf("Your total salary is %.2f, the tax is %.2f, the income is %.2f.\n", total, tax, income);

	return 0;
}

第8题

/*编程练习7.8修改练习7的假设a,让程序可以给出一个供选择的工资等级菜单。
使用swith完成工资登记选择。运行程序后,现实的菜单应该类似这样:
******************************************************************
Enter the number corresponding to the desired pay rate or 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 WAGE_LEVEL_1 8.75
#define WAGE_LEVEL_2 9.33
#define WAGE_LEVEL_3 10.00
#define WAGE_LEVEL_4 11.20
#define BASIC_HOUR 40
#define HOUR_RATE 1.5
#define TAX_RATE_1 0.15
#define TAX_RATE_2 0.20
#define TAX_RATE_3 0.25

int main(void)
{
	float wage,hour, total, tax, income;
	int level;

	printf("Please select your wage level(1 to 4, 5 to quit).\n");
	printf("******************************************************\n");
	printf("1)$%.2f/hr\t\t2)$%.2f/hr\n3)$%.2f/hr\t\t4)$%.2f/hr\n5)quit\n",
		WAGE_LEVEL_1, WAGE_LEVEL_2, WAGE_LEVEL_3, WAGE_LEVEL_4);
	printf("******************************************************\n");

	while ((scanf_s("%d", &level)) == 1) {	//读取整数,返回值为1
		switch (level) {
		case 1:wage = WAGE_LEVEL_1;
			goto work;	//结束循环,进入下一个模块运算
		case 2:wage = WAGE_LEVEL_2;
			goto work;
		case 3:wage = WAGE_LEVEL_3;
			goto work;
		case 4:wage = WAGE_LEVEL_4;
			goto work;
		case 5:goto end;	//读取5,结束程序
		default:	//读取1~5以外的整数,循环继续,提示输入工资等级
			printf("Please select your wage level(1 to 4, 5 to quit).\n");
			printf("******************************************************\n");
			printf("1)$%.2f/hr\t\t2)$%.2f/hr\n3)$%.2f/hr\t\t4)$%.2f/hr\n5)quit\n",
				WAGE_LEVEL_1, WAGE_LEVEL_2, WAGE_LEVEL_3, WAGE_LEVEL_4);
			printf("******************************************************\n");
		}
	}

work: {
	printf("Please enter the number of hours worked in a week.\n");
	scanf_s("%f", &hour);
	if (hour > BASIC_HOUR)
		hour = BASIC_HOUR + (hour - BASIC_HOUR) * HOUR_RATE;
	else
		;
	total = wage * hour;
	if (total <= 300)
		tax = total * TAX_RATE_1;
	else if (total > 300 && total <= 450)
		tax = 300 * TAX_RATE_1 + (total - 300) * TAX_RATE_2;
	else
		tax = 300 * TAX_RATE_1 + 150 * TAX_RATE_2 + (total - 450) * TAX_RATE_3;
	income = total - tax;
	printf("Your total wage is %.2f, the tax is %.2f, the income is %.2f.\n", total, tax, income);
	}
	
	end:printf("Done!");

	return 0;
}

第9题

/*编程练习7.9编写一个程序,只接受正整数输入,然后显示所有小于或等于该数的素数
质数(prime number)又称素数,除了1和它本身外,不能被其他自然数整除;与合数相对*/
#include<stdio.h>

int main(void)
{
	unsigned int max, num, div;	//输入max,定义非负整数(num和div)作为除数在循环中递减

	printf("Please input the max number:\n");
	if (scanf_s("%u", &max) != 1)
		printf("Error!\n");
	for (num = 2; num <= max; num++){
		for (div = 2; div < num; div++) 	//由于1既不是素数也不是合数,所以div=2,剔除1的影响
		{
			if (num % div == 0)
				break;
			else {
				printf("%u ", num);
				break;
			}
		}
	}

	return 0;
}

第10题

/*编程练习7.10 1988年的美国联邦税收是近代最简单的税收方案(略)。编写一个程序,让用
户指定缴纳税金的种类和应纳税收入,然后计算税金。程序应通过循环让用户可以多次输入。*/
#include<stdio.h>
#define RATE_1 0.15
#define RATE_2 0.28
#define BASE_POINT_1 17850
#define BASE_POINT_2 23900
#define BASE_POINT_3 29750
#define BASE_POINT_4 14875

int main(void)
{
	int category;	//提示用户输入类别
	double tax, income,base_point;

	printf("Please select your tax category( 1~4, q to quit).\n");
	printf("***********************************************\n");
	printf("1.single                 2.head of household\n");
	printf("3.married and share      4.married but divorced\n");
	printf("***********************************************\n");

	while (scanf_s("%d", &category)) {
		switch (category) {
		case 1:base_point = BASE_POINT_1;
				break;
		case 2:base_point = BASE_POINT_2;
			break;
		case 3:base_point = BASE_POINT_3;
			break;
		case 4:base_point = BASE_POINT_4;
			break;
		default:
			printf("Please select your tax category( 1~4, q to quit).\n");
			printf("***********************************************\n");
			printf("1.single                 2.head of household\n");
			printf("3.married and share      4.married but divorced\n");
			printf("***********************************************\n");
			continue;
		}
		printf("Please input your income.\n");
		scanf_s("%lf", &income);
		if (income <= base_point)
			tax = income * RATE_1;
		else
			tax = base_point * RATE_1 + (income - base_point) * RATE_2;
		printf("The taxes you should pay are $%.2lf.\n", tax);
		printf("Please select your tax category( 1~4, q to quit).\n");
		printf("***********************************************\n");
		printf("1.single                 2.head of household\n");
		printf("3.married and share      4.married but divorced\n");
		printf("***********************************************\n");
	}
	printf("Done!");

	return 0;
}

第11题

/*编程练习7.11 ABC优购杂货店出售的洋薊售价为2.05美元/磅……(略)*/
#include<stdio.h>
#define PRICE_ARTICHOKE 2.05   //洋蓟单价
#define PRICE_BEET 1.15		//甜菜单价
#define PRICE_CARROT 1.09	//胡萝卜单价
#define DISCOUNT 0.05	//折扣5%
#define S_A_P_FEE_1   6.5	//包装运输费(Shipping and packaging fees)
#define S_A_P_FEE_2   14
#define S_A_P_FEE_3   0.5
#define FIRST_WEIGHT 5	//首重5磅,续重20磅
#define CONTINUED_WEIGHT 20



int main(void)
{
	double weight_artichoke, weight_beet, weight_carrot, weight_total;	//定义三种蔬菜重量和订单总重量
	double cost_artichoke, cost_beet, cost_carrot, cost_shopping, cost_s_a_p, cost_total,discount;
	/*定义三种蔬菜费用、蔬菜采购费用、包装运输费用、订单总费用、折扣*/
	char ch;	//定义字符变量,用于选择蔬菜种类

	printf("Please select the type of vegetables you want to purchase('!' to calculate the price, 'q' to quit).\n");
	printf("a)artichoke $2.05/lb       b)beet $1.15/lb       c)carrot $1.09/lb\n");
	while ((ch = getchar()) != 'q') {
		switch (ch) {
		case 'a':
			printf("Please enter the weight of the vegetables you want to buy(lb).\n");
			scanf_s("%lf", &weight_artichoke);
			continue;
		case 'b':
			printf("Please enter the weight of the vegetables you want to buy(lb).\n");
			scanf_s("%lf", &weight_beet);
			continue;
		case 'c':
			printf("Please enter the weight of the vegetables you want to buy(lb).\n");
			scanf_s("%lf", &weight_carrot);
			continue;
		case '!':break;	//'!'结束购买,并计价
		default:
			printf("Please select the type of vegetables you want to purchase(q to quit).\n");
			printf("a)artichoke $2.05/lb       b)beet $1.15/lb       c)carrot $1.09/lb\n");
			continue;
		}
		cost_artichoke = PRICE_ARTICHOKE * weight_artichoke;
		cost_beet = PRICE_BEET * weight_beet;
		cost_carrot = PRICE_CARROT * weight_carrot;
		cost_shopping = cost_artichoke + cost_beet + cost_carrot;
		if (cost_shopping >= 100) {
			discount = DISCOUNT * cost_shopping;
			cost_shopping -= discount;
		}	//计算蔬菜购置费用和折扣

		weight_total = weight_artichoke + weight_beet + weight_carrot;
		if (weight_total <= FIRST_WEIGHT)
			cost_s_a_p = S_A_P_FEE_1;
		else if (weight_total > FIRST_WEIGHT && weight_total <= CONTINUED_WEIGHT)
			cost_s_a_p = S_A_P_FEE_2;
		else
			cost_s_a_p = S_A_P_FEE_2 + (weight_total - CONTINUED_WEIGHT) * S_A_P_FEE_3;
		//计算蔬菜的运费和包装费

		cost_total = cost_shopping + cost_s_a_p;	//计算订单总费用

		printf("***************************************************************  ********\n");
		printf("Vegetable types    Selling price(dollar/lb)    weight(lb)    cost(dollar)\n");
		printf("   Artichoke              %.2lf                   %4.2lf          %6.2lf\n",
			PRICE_ARTICHOKE, weight_artichoke, cost_artichoke);
		printf("     Beet                 %.2lf                   %4.2lf          %6.2lf\n",
			PRICE_BEET, weight_artichoke, cost_beet);
		printf("    Carrot                %.2lf                   %4.2lf          %6.2lf\n",
			PRICE_CARROT, weight_artichoke, cost_carrot);
		printf("Total                                            %4.2lf          %6.2lf\n",
			weight_total, cost_total);
		printf("Discount                                                         %6.2lf\n", discount);
		printf("*************************************************************************\n");

	}
	printf("Done!\n");

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值