0
点赞
收藏
分享

微信扫一扫

C PRIMER PLUS第七章11题

m逆光生长 2022-02-09 阅读 98

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是退出订购。程序要记录累计的重量。即,如果用户输入4磅的甜菜,然后输入5磅的甜菜,程序应该报告9磅的甜菜。然后,该程序要计算货物总价、折扣(涂过油的话)、运费和包装费。随后,程序应显示所有的购买信息:武平售价、订购的重量(单位:磅)、订购的蔬菜费用、订单的总费用、折扣(如果有的哈)、运费和宝篆非、以及所有的费用总额。


#include <stdio.h>
#define BREAK1 5 //磅数第一个判断点 
#define BREAK2 20 //磅数第二个判断点 
#define Break1 100 //蔬菜价格总额判断点 
#define discount 0.05 //折扣 
#define charge1 6.5 //一阶运费和包装费 
#define charge2 14 //二阶运费和包装费 
#define charge3_per 0.5 //三阶运费和包装费(每磅) 
#define price1 2.05 //洋蓟单价 
#define price2 1.15 //甜菜单价 
#define price3 1.09 //胡萝卜单价 

int main(void)
{
	double pound = 0.0; //用户输入磅数 
	double pound1 = 0.0; //洋蓟磅数 
	double pound2 = 0.0; //甜菜磅数 
	double pound3 = 0.0; //胡萝卜磅数 
	double totalpound = 0.0; //总磅数 
	char choice; //用户选择的字母 
	double Discount = 0.0; //折扣 
	double veg_fee = 0.0; //蔬菜总价 
	double trans_fee = 0.0;  //运费和包装费 
	
	printf("Enter the number corresponding to the vegetable you want to purchase:\n");
	printf("a) 洋蓟                            b) 甜菜\n");
	printf("c) 胡萝卜                          q) quit\n");
	while((choice = getchar()) != 'q') //q退出订购 
	{
		if(choice < 'a' || choice > 'c') //判断有效输入 
		{
			printf("Enter the right letter\n");
			getchar();
			continue;
		}
		
		printf("Enter how much do you want to purchase(pounds):");
		scanf("%lf", &pound);
		totalpound += pound; //加总磅数 
	
		switch (choice){ //加各个菜的磅数 
			case 'a': veg_fee += pound * price1;
			pound1 += pound;
			break;
			case 'b': veg_fee += pound * price2;
			pound2 += pound;
		    break;
		    case 'c': veg_fee += pound * price3;
		    pound3 += pound;
		    break;
		}
	printf("Enter the number corresponding to the vegetable you want to purchase:\n");
	printf("a) 洋蓟                            b) 甜菜\n");
	printf("c) 胡萝卜                          q) quit\n");
	getchar();
	}
	
	if(totalpound <= BREAK1) //判断运费和包装费 
	    trans_fee = charge1;
	else if(totalpound > BREAK1 && totalpound <= BREAK2)
		trans_fee = charge2;
	else 
		trans_fee = charge2 + (totalpound - BREAK2) * charge3_per;
			
	if(veg_fee >= Break1) //判断折扣情况 
	{
		Discount = discount * veg_fee;
	}
	
	printf("your purchase information is as the following:\n");
	printf("洋蓟: %.2lf pound(s), $%.2lf/pound\n", pound1, price1);
	printf("甜菜: %.2lf pound(s), $%.2lf/pound\n", pound2, price2);
	printf("胡萝卜: %.2lf pound(s), $%.2lf/pound\n", pound3, price3);
	printf("your vegetable fee is $%.2lf, transport and wrapper fee is $%.2lf, fee before discount(if you have) is $%.2lf\n", veg_fee, trans_fee, veg_fee + trans_fee);	
	if(Discount > 0)
	printf("your discount is $%.2lf for your purchase is above $100\n", Discount);
	printf("you should pay $%.2lf in the end", veg_fee + trans_fee - Discount);
	return 0;
 } 
举报

相关推荐

0 条评论