#include<stdio.h>
#define YANG 2.05
#define TIAN 1.15
#define HU 1.09
int main(void)
{
char ch;
int a, b, c, total;
int sum_a = 0;
int sum_b = 0;
int sum_c = 0;
double money_a = 0;
double money_b = 0;
double money_c = 0;
double money_total;
double money_supress;
printf("a) 洋蓟2.05美元/磅\n");
printf("b) 甜菜1.15美元/磅\n");
printf("c) 胡萝卜1.09美元/磅\n");
printf("Please select a vegetables or q to quit: \n");
while ((ch = getchar()) && ch != 'q')
{
while (getchar() != '\n')
continue;
switch (ch)
{
case 'a':printf("Please input a number of how much do you want to buy: \n");
scanf("%d", &a);
money_a += a * YANG;
sum_a += a;
break;
case 'b':printf("Please input a number of how much do you want to buy: \n");
scanf("%d", &b);
money_b += b * TIAN;
sum_b += b;
break;
case 'c':printf("Please input a number of how much do you want to buy: \n");
scanf("%d", &c);
money_c += c * HU;
sum_c += c;
break;
default:printf("Sorry, Please input again.\n");
break;
}
printf("What others do you want to buy: \n");
}
printf("Sorry, this is not valid selection.\n");
total = sum_a + sum_b + sum_c;
money_total = money_a + money_b + money_c;
money_total = (money_total >= 100) ? (money_total * 0.95) : (money_total);
if (total > 20)
money_supress = 14 + (total - 20) * 0.5;
else if (total > 5 && total < 20)
money_supress = 14;
else
money_supress = 6.5;
printf("This is all your items:\n");
printf("你一共买了%d洋蓟,共消费%.2lf美元.\n", sum_a, money_a);
printf("你一共买了%d甜菜,共消费%.2lf美元.\n", sum_b, money_b);
printf("你一共买了%d胡萝卜,共消费%.2lf美元.\n", sum_c, money_c);
printf("全部共计%d,合计消费%.2lf,运费及包装费合计%.2lf.\n", total, money_total, money_supress);
return 0;
}
自己编写的,假设输入a,之后输入10,根据提示再输入a,就会显示Sorry,Please input again. What others do you want to buy:接着再输入a,才可以正常运行,看了半天也没发现哪里出问题,求指教。我的原意是继续输入a就可以反复添加。