题目要求: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磅的甜菜。然后,该程序要计算货物总价、折扣(如果有的话)、运费和包装费。随后,程序应显示所有的购买信息:物品售价、订购的重量(单位:磅)、订购的蔬菜费用、订单的总费用、折扣(如果有的话)、运费和包装费,以及所有的费用总额。
整体思路:采用循环结构,先采集用户购买的蔬菜数量,结算时,在计算总价格。
代码:
/*
name: test11.c
author: AFei2019
date: 2020.3.16
function: Create a online shopping platform
*/
#include <stdio.h>
#include <stdlib.h>
const float artichoke = 2.05;
const float beet = 1.15;
const float carrot = 1.09;
float w_artichoke = 0;
float w_beet = 0;
float w_carrot = 0;
float money = 0;
void bill(void);
int main(void){
char selection;
while(1){
getchar();
printf("welcome to the wehsite, choose the function\n");
printf("a: artichoke $2.05/P\nb: beet $1.15/P\nc: carrot $1.09/P\nq: quit\n");
printf("your choose:");
scanf("%c", &selection);
switch (selection)
{
case 'a':{
float a = 0;
printf("How much artichoke do you want to buy?");
scanf("%f", &a);
w_artichoke += a;
break;
}
case 'b':{
float b;
printf("How much beet do you want to buy?");
scanf("%f", &b);
w_beet += b;
break;
}
case 'c':{
float c;
printf("How much carrot do you want to buy?");
scanf("%f", &c);
w_carrot += c;
break;
}
case 'q':{
bill();
break;
}
default:
break;
}
if(selection == 'q') break;
}
system("pause");
return 0;
}
void bill(void){
float sum = 0;
sum = w_artichoke + w_beet + w_carrot;
money = w_artichoke * artichoke + w_beet * beet + w_carrot * carrot;
if(sum <= 5.0 && sum != 0) money = money + 6.5;
if(sum > 5.0 && sum <= 20.0) money = money + 14;
if(sum > 100) money = money + (sum - 20.0) * 0.5 + 14;
if(money >= 100.0) money = money * 0.95;
printf("Thank you for your shopping\n");
printf("You bought %.3f pound artichoke, %.3f pound beet, %.3f pound carrot\n", w_artichoke, w_beet, w_carrot);
printf("artichoke spend %.3f dollors, beet spend %.3f dollors, carrot spend %.3f dollors\n", w_artichoke * artichoke, w_beet * beet, w_carrot * carrot);
printf("A total of %.3f dollors", money);
}
运行结果
如果解决了你的问题,请帮忙点个赞,谢谢大家哦~~
博主QQ:2240172425,可以加博主QQ一起交流学习经验
若是出现错误,还请大家海涵。