C Primer Plus 第七章 第11题

#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", &current_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", &current_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", &current_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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值