C Primer Plus 6th(中文版)第七章编程练习答案(只有第十一题,由于在其它地方找不到答案或者蔬菜磅数没有累加,因此来分享一下欢迎指正)
#include<stdio.h>//7.11
#define ARTICHOKE 2.05
#define BEET 1.15
#define CARROT 1.09
#define BREAK1 5
#define BREAK2 20
int main(void)
{
char ch,ch_1;
int pounds[2000];
int i=0;
int all = 0;
double discount, total, add;
printf(“please enter option of your order:\n “);
printf(“a.artichoke b.beet c.carrot q.quit\n”);
scanf_s(”%c”, &ch);
switch (ch)
{
case ‘a’:
printf(“please enter the pounds of artichokes:”);
do//do while 用来累加用户输入的磅数,当输入为换行时跳出循环。
{
scanf("%d", £s[i]);//
all += pounds[i];
i++;
}
while ((ch_1 = getchar()) != ‘\n’);
total = all * ARTICHOKE;
break;
case ‘b’:
printf(“please enter the pounds of beet:”);
do//do while 用来累加用户输入的磅数,当输入为换行时跳出循环。
{
scanf("%d", £s[i]);//
all += pounds[i];
i++;
} while ((ch_1 = getchar()) != ‘\n’);
total = all * BEET;
break;
case ‘c’:
printf(“please enter the pounds of carrot:”);
do//do while 用来累加用户输入的磅数,当输入为换行时跳出循环。
{
scanf("%d", £s[i]);//
all += pounds[i];
i++;
} while ((ch_1 = getchar()) != ‘\n’);
total = all * CARROT;
break;
case ‘q’:
return 0;
default:printf(“please enter the correct leter:”);
scanf_s("%c", &ch);
}
if (total <= 100)
{
total = total;
discount = 0;
}
else
{
discount = total * 0.05;
total = total - discount;
}
if (all <= BREAK1)
add = 6.5;
else if (all <= BREAK2)
add = 14;
else
add = 14 + (all - 20) * 0.5;
total += add;
printf(“the pounds is:%d\n”, all);
printf(“the toaal price of the goods is:%.2f\n”,total);
printf(“the discount of the goods is:%.2f\n”,discount);
printf(“the add prince of the goods is:%.2f\n”,add);
return 0;
}
运行示例