2020-08-06

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", &pounds[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", &pounds[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", &pounds[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;
}
运行示例
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值