自学C第10天

C Primer Plus

第10题:这题和第8题差不多。

#include <stdio.h>

#define single 17850

#define host 23900

#define married_t 29750

#define married_s 14875

void count(int a, float b);

void show_menus(void);

int main(void)

{

        float income = 0;

        float net_income = 0;

        float tax = 0;

        int enter;

        show_menus();

        while (scanf("%d", &enter) == 1 && enter != 5)

        {

                switch (enter)

                {

                case 1:

                printf("Please enter how much you earn this year:\n");

                scanf("%f", &income);

                count(single, income);

                show_menus();

                break;

                case 2:

                printf("Please enter how much you earn this year:\n");

                scanf("%f", &income);

                count(host, income);

                show_menus();

                break;

                case 3:

                printf("Please enter how much you earn this year:\n");

                scanf("%f", &income);

                count(married_t, income);

                show_menus();

                break;

                case 4:

                printf("Please enter how much you earn this year:\n");

                scanf("%f", &income);

                count(married_s, income);

                show_menus();

                break;

                default:

                printf("Please enter number between 1~5:\n");

                show_menus();

                break;

                }

        }

        printf("Done!");

        return 0;

}

void show_menus(void)

{

        printf("**************************************************\n\n");

        printf("Enter the number corresponding to your status:\n");

        printf("1) Single                        2) House Hold\n");

        printf("3) Married                       4) Seprated\n");

        printf("5) quit\n");

        printf("**************************************************\n\n");

}

void count(int a, float b)

{

        float tax;

        tax = 0;

        if (b < a && b > 0)

        {

        tax = b * 0.15;

        printf("The tax you have to pay is $%g, and your net income is $%g\n", tax, b - tax);

        }

        else

        {

        tax = a * 0.15 + (b - a) * 0.28;

        printf("The tax you have to pay is $%g, and your net income is $%g\n", tax, b - tax);

        }

return;

}

第11题:这题和上一题差不多 但复杂度更高

#include <stdio.h>

#define ART 2.05

#define BEET 1.15

#define CARR 1.09

void show_menus(void);

float pack_fee(float a, float b, float c); #注:如果要定义函数并且需要用到函数的返回值,前面一定要声明数据类型,如果不  声明会默认返回整数型。

float total_p(float a, float b, float c);

float discount(float a, float b, float c);

int main(void)

{

        float a = 0, b = 0 ,c = 0;

        float i, j, k;

        int enter;

        float tp, pf, dis;

        show_menus();

        while (scanf("%d", &enter) == 1 && enter != 4) #注:其实这里用getchar用字符型会更好,方便操做。

        {

                switch (enter)

                {

                case 1:

                printf("Enter the weight of Artichoke:\n");

                scanf("%f", &i);

                a += i;   #注:一开始没弄清楚一直用 a+=a

                show_menus();

                break;

                case 2:

                printf("Enter the weight of Beet:\n");

                scanf("%f", &j);

                b += j;

                show_menus();

                break;

                case 3:

                printf("Enter the weight of Carrot:\n");

                scanf("%f", &k);

                c += k;

                show_menus();

                break;

                default:

                printf("Please enter number between 1~4\n");

                show_menus();

                break;

                }

        }

        pf = pack_fee(a, b, c); #注:需要声明一个变量去接受函数返回的值

        tp = total_p(a, b, c);

        dis = discount(a, b, c);

        printf("Artichoke: 2.05 $/bl   Beet: 1.15 $/bl   Carrot: 1.09 $/bl\n");

        printf("Order detial:\n\tArtichoke\tBeet\tCarrot\n");

        printf("Weight:  \t%g lb   \t%g lb   \t%g lb\n", a, b, c);

        printf("Total weight: %.2f\n", a + b + c);

        printf("Price of the vegetables is %.2f\n", tp);

        printf("Price include the package fee is %.2f\n", pf + tp);

        printf("Discount: %%%g", dis);

        return 0;

}

void show_menus(void)

{

        printf("***********************************************************\n\n");

        printf("Enter the number corresponding to the vegetable:\n");

        printf("1) Artichoke    2) Beet    3) Carrot    4) Quit and Count\n");

        printf("***********************************************************\n\n");

}

float pack_fee(float a, float b, float c)

{

        float weight, p_fee;

        weight = a + b + c;

        if (weight <= 5)

        p_fee = 6.5; #其实6.5,14等数值在头文件下面直接定义成#define会更好,方便理解和维护

        else if (weight > 5 && weight <= 20)

        p_fee = 14;

        else

        p_fee = 14 + (weight - 20) * 0.5;

        return p_fee;

}

float total_p(float a, float b, float c)

{

        float cost;

        cost = a * ART + b * BEET + c * CARR;

        if (cost >= 100)

        cost = cost * 0.95;

        return cost;

}

float discount(float a, float b,float c)

{

        float cost;

        float dis;

        cost = a * ART + b * BEET + c * CARR;

        if (cost >= 100)

        dis = 5;

        else

        dis = 0;

        return dis;

}

第7章终于结束了,明天开始第八章!

加油!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值