#include<stdio.h>
int main()
{
const float price_y = 2.05;
const float price_t = 1.15;
const float price_h = 1.09; //规定三种菜的单价
const float discount_rate = 0.05; //折扣率
const float under5 = 6.50; //没超过5的运费
const float under20 = 14.00; //5至20的运费
const float base20 = 14.00; //20以上的运费
const float extra = 0.50; //20以上1磅的运费
float y,t,h,y_s=0,t_s=0,h_s=0,total;//定义三个蔬菜的重量
char c; //用来选择哪一种菜
float cost_y; //定义买洋芋的钱
float cost_t; //定义买甜菜的钱
float cost_h; //定义买胡萝卜的钱
float cost_total; //买菜的总费用
float final_total; //总花费
float discount; //折扣的钱
float shipping; //运费
printf("input kind :");
while((c=getchar())!='\n') //输入一个字符选择一种菜
{
while(getchar()!='\n') //这个循环什么意思?
continue;
switch(c)
{
case 'a':printf("input pounds of yj:");
scanf("%d",&y);
y_s+=y; //计算洋芋的重量
break;
case 'b':printf("input pounds of tc:");
scanf("%d",&t);
t_s+=t; //计算甜菜的重量
break;
case 'c':printf("input pounds of hlb:");
scanf("%d",&h);
h_s+=h; //计算胡萝卜的重量
break;
case 'q':printf("to quit");
c='\n'; //q退出
break;
}
}
total=y_s+t_s+h_s; //计算三个的总重量
if(total <= 5.0)
shipping=under5;
else if(total <= 20.0)
shipping=under20;
else
shipping=base20+(total-20.0)*extra;//用if判断运费多少
cost_y=y_s*price_y;
cost_t=t_s*price_t;
cost_h=h_s*price_h;
cost_total=cost_y+cost_t+cost_h; //买菜的总费用
if(cost_total>100.0)
discount=cost_total*discount_rate;
else
discount=0; //用if判断优惠
final_total=cost_total+shipping+-discount;//总费用
printf("%.2f lbs of artichokes at $%.2f per pound:$ %.2f\n",
y_s, price_y, cost_y); //打印洋芋的重量,单价,花费
printf("%.2f lbs of beets at $%.2f per pound: $%.2f\n",
t_s, price_t, cost_t); //打印甜菜的重量,单价,花费
printf("%.2f lbs of carrots at $%.2f per pound: $%.2f\n",
h_s, price_h, cost_h); //打印胡萝卜的重量,单价,花费
printf("Total cost of vegetables: $%.2f\n", cost_total);//打印买菜的钱
printf("discount:%.5f",discount);//打印优惠
printf("shipping:%.5f",shipping);//打印运费
printf("final_total:%.5f",final_total);//打印总费用
return 0;
}
int main()
{
const float price_y = 2.05;
const float price_t = 1.15;
const float price_h = 1.09; //规定三种菜的单价
const float discount_rate = 0.05; //折扣率
const float under5 = 6.50; //没超过5的运费
const float under20 = 14.00; //5至20的运费
const float base20 = 14.00; //20以上的运费
const float extra = 0.50; //20以上1磅的运费
float y,t,h,y_s=0,t_s=0,h_s=0,total;//定义三个蔬菜的重量
char c; //用来选择哪一种菜
float cost_y; //定义买洋芋的钱
float cost_t; //定义买甜菜的钱
float cost_h; //定义买胡萝卜的钱
float cost_total; //买菜的总费用
float final_total; //总花费
float discount; //折扣的钱
float shipping; //运费
printf("input kind :");
while((c=getchar())!='\n') //输入一个字符选择一种菜
{
while(getchar()!='\n') //这个循环什么意思?
continue;
switch(c)
{
case 'a':printf("input pounds of yj:");
scanf("%d",&y);
y_s+=y; //计算洋芋的重量
break;
case 'b':printf("input pounds of tc:");
scanf("%d",&t);
t_s+=t; //计算甜菜的重量
break;
case 'c':printf("input pounds of hlb:");
scanf("%d",&h);
h_s+=h; //计算胡萝卜的重量
break;
case 'q':printf("to quit");
c='\n'; //q退出
break;
}
}
total=y_s+t_s+h_s; //计算三个的总重量
if(total <= 5.0)
shipping=under5;
else if(total <= 20.0)
shipping=under20;
else
shipping=base20+(total-20.0)*extra;//用if判断运费多少
cost_y=y_s*price_y;
cost_t=t_s*price_t;
cost_h=h_s*price_h;
cost_total=cost_y+cost_t+cost_h; //买菜的总费用
if(cost_total>100.0)
discount=cost_total*discount_rate;
else
discount=0; //用if判断优惠
final_total=cost_total+shipping+-discount;//总费用
printf("%.2f lbs of artichokes at $%.2f per pound:$ %.2f\n",
y_s, price_y, cost_y); //打印洋芋的重量,单价,花费
printf("%.2f lbs of beets at $%.2f per pound: $%.2f\n",
t_s, price_t, cost_t); //打印甜菜的重量,单价,花费
printf("%.2f lbs of carrots at $%.2f per pound: $%.2f\n",
h_s, price_h, cost_h); //打印胡萝卜的重量,单价,花费
printf("Total cost of vegetables: $%.2f\n", cost_total);//打印买菜的钱
printf("discount:%.5f",discount);//打印优惠
printf("shipping:%.5f",shipping);//打印运费
printf("final_total:%.5f",final_total);//打印总费用
return 0;
}