#include<stdio.h>
#define ARTICHOKE_PRICE_PER_POUND 2.05
#define BEET_PRICE_PER_POUND 1.15
#define CARROT_PRICE_PER_POUND 1.09
#define DISCOUNT_100$ 0.05
#define WEIGHT_LEVEL1 5
#define WEIGHT_LEVEL2 20
#define CARRIAGE_LEVEL1 6.5
#define CARRIAGE_LEVEL2 14
#define CARRIAGE_LEVEL3_RATE 0.5
void show_menu(void);
int main(void)
{
char choice;
double current_weight=0;
double artichoke_weight=0;
double beet_weight = 0;
double carrot_weight = 0;
double all_weight = 0;
double all_price = 0;
double discount = 0;
double carriage = 0;
do
{
show_menu();
choice = getchar();
switch (choice)
{
case 'a':
{
printf("Enter the weight of artichoke you wanna buy in pounds:");
scanf_s("%lf", ¤t_weight);
while (getchar() != '\n')//清空缓冲区剩余字符
continue;
artichoke_weight += current_weight;
printf("You're now buying %f pounds of artichoke,%f pounds of beet,%f "
"pounds of carrot.\n", artichoke_weight, beet_weight, carrot_weight);
break;
}
case 'b':
{
printf("Enter the weight of beet you wanna buy in pounds:");
scanf_s("%lf", ¤t_weight);
while (getchar() != '\n')//清空缓冲区剩余字符
continue;
beet_weight += current_weight;
printf("You're now buying %f pounds of artichoke,%f pounds of beet,%f "
"pounds of carrot.\n", artichoke_weight, beet_weight, carrot_weight);
break;
}
case 'c':
{
printf("Enter the weight of carrot you wanna buy in pounds:");
scanf_s("%lf", ¤t_weight);
while (getchar() != '\n')//清空缓冲区剩余字符
continue;
carrot_weight += current_weight;
printf("You're now buying %f pounds of artichoke,%f pounds of beet,%f "
"pounds of carrot.\n", artichoke_weight, beet_weight, carrot_weight);
break;
}
case 'q':break;
default:
{
printf("Enter correct letter.\n");
while (getchar() != '\n');//清空缓冲区剩余字符
break;
}
}
} while (choice != 'q');
all_price=ARTICHOKE_PRICE_PER_POUND* artichoke_weight+
BEET_PRICE_PER_POUND* beet_weight+
CARROT_PRICE_PER_POUND* carrot_weight;
all_weight = artichoke_weight + beet_weight + carrot_weight;
if (all_weight <= WEIGHT_LEVEL1)
carriage = CARRIAGE_LEVEL1;
else if (all_weight <= WEIGHT_LEVEL2)
carriage = CARRIAGE_LEVEL2;
else
carriage = 14 + (all_weight - WEIGHT_LEVEL2) * CARRIAGE_LEVEL3_RATE;
printf("The price of the artichoke is $2.05 per pound.\nThe price of the "
"beet is $1.15 per pound.\nThe price of the carrot is $1.09 per pound.\n"
"You have just ordered %.2f pounds of artichokes,%.2f pounds of beets and "
"%.2f pounds of carrots.\nThe pice of artichoke is $%.2f.\nThe pice of"
"beet is $%.2f.\nThe price of carrot is $%.2f.\nAll price is $%.2f.\n"
, artichoke_weight, beet_weight, carrot_weight
, ARTICHOKE_PRICE_PER_POUND * artichoke_weight
, BEET_PRICE_PER_POUND * beet_weight
, CARROT_PRICE_PER_POUND * carrot_weight, all_price);
if (all_price >= 100)
{
discount = all_price * DISCOUNT_100$;
printf("All price is over $100,so you can get $%.2f discount.\n"
,discount);
}
else
printf("All price is under $100,so you can get no discount.\n");
printf("The fee for package and carriage is $%.2f.\n"
"You should pay $%.2f in total.", carriage
,all_price-discount+carriage);
return 0;
}
void show_menu(void)
{
printf("Choose the goods you want:\n"
"a artichoke\nb beet\nc carrot\nq to quit\n");
return 0;
}
C Primer Plus 第七章 第11题
最新推荐文章于 2024-08-14 07:21:13 发布