记录——《C Primer Plus (第五版)》第七章编程练习第十一题

40 篇文章 0 订阅

ABC Mail Order Grocery朝鲜蓟的售价是1.25美元/磅,甜菜的售价是0.65美元/磅,胡萝卜的售价是0.89美元/磅。在添加运输费用之前,他们为100美元的订单提供5%的打折优惠。对5磅或以下的定单收取3.50美元的运输和装卸费用;超过5磅而不足20磅的定单收取1O.OO美元的运输和装卸费用:20磅或以上的运输,在8美元基础上每磅加收0.1美元。编写程序,在循环中使用switch语句,以便对输入的响应是让用户输入所需的a朝鲜蓟磅数,b甜菜的磅数,c胡萝卜的磅数,而q允许用户退出订购过程。然后程序计算总费用、折扣和运输费用(如果有运输费的话),以及总数。随后程序应该显示所有的购买信息:每磅的费用、订购的磅数、该订单每种蔬菜的费用、订单的总费用、折扣,如果有的话加上运输费用,以及所有费用的总数。

# include <stdio.h>

# define PRICEA 1.25     //a物品的单价
# define PRICEB 0.65
# define PRICEC 0.89
# define COST_POUND_LV1 3.50   //一级运费
# define COST_POUND_LV2 10.00
# define COST_POUND_LV3 8.00
# define COST_POUND_LV3_ADD 0.10
# define DISCOUNT 0.05


double getweight(double);
void clear();

int main(void)
{
    char ch;
    double pounds[3] = {0};  //物品重量
    double cost_total=0;   //  总费用
    double cost_transport=0;  //总运输费用
    double cost_discount=0;  //折扣
    double pound_total = 0;   //总磅数
    double order = 0;   //总订单
    double cost_a_total=0;  //朝鲜蓟的总价
    double cost_b_total=0;
    double cost_c_total=0;


    printf("请输入要选择的商品的序号:\n");
    printf("a 朝鲜蓟, b 甜菜, c 胡萝卜\n");
    while(printf("\n请选择您要订购的货品:"),(ch=getchar())!='q'){  
        switch(ch){  
            case 'a':  
                pounds[0]=getweight(pounds[0]);  
                break;  

            case 'b': 
                pounds[1]=getweight(pounds[1]);  
                break;  

            case 'c':
                pounds[2]=getweight(pounds[2]);  
                break;

            default:
                printf("输入有误,请重新选择货品。\n");  
                clear();  
                break;  

        }  
    } 

    pound_total = pounds[0] + pounds[1] + pounds[2];      //总磅数
    cost_a_total = pounds[0] * PRICEA;
    cost_b_total = pounds[1] * PRICEB;
    cost_c_total = pounds[2] * PRICEC;
    order = cost_a_total + cost_b_total + cost_c_total;//  总订单钱数

    if(pound_total >= 20.00)
    {
        cost_transport = COST_POUND_LV3 + COST_POUND_LV3_ADD * pound_total;
    }
    else if(pound_total >5)
        cost_transport = COST_POUND_LV2;
    else if(pound_total >=0)
        cost_transport = COST_POUND_LV1;

    if(order >= 100.00)
    {
        cost_discount = order * DISCOUNT; 
    //  order = order - cost_discount ;
    }

    cost_total = order + cost_transport - cost_discount;

    printf(" 朝鲜蓟每磅%.3lf,订购了%.3lf磅 \n甜菜每磅%.3lf,订购了%.3lf磅 \n胡萝卜每磅%.3lf订购了%.3lf磅\n",PRICEA, pounds[0], PRICEB, pounds[1], PRICEC, pounds[2]);
    printf("订购朝鲜蓟花费 %.3lf 订购甜菜花费 %.3lf 订购胡萝卜花费%.3lf\n",cost_a_total, cost_b_total, cost_c_total);
    printf("你总共订购花费了%.3lf 打折后费用为%.3lf\n", order, cost_discount);
    printf("你花费的运费为%.3lf\n", cost_transport);
    printf("你总共花费了%.3lf\n", cost_total);
    return 0;
}

double getweight(double pounds)
{
    double weight;
    printf("请输入所需磅数:");
    scanf("%lf", &weight);
    pounds += weight;
    printf("你已购买 %.3lf 磅!\n", pounds);
    clear();                      //清空输入的字符
    return pounds;
}

void clear()
{
    while(getchar() != '\n')
        continue;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值