c primer puls 7.10

本文介绍了一个使用C语言实现的简单税收分类计算器程序。该程序根据用户输入的税务分类(如单身、已婚等)及总收入,计算并显示相应的税后收入。文章详细展示了程序从初始版本到最终版本的迭代过程,包括对错误的修正和功能改进。

4个税收分类。

第一次写完版本:

#include <stdio.h>
#define first_rate 0.15
#define over_rate 0.28
#define single_boundary 17850;
#define household_boundary 23900;
#define married_boundary 29750;
#define divorce_boundary 14875;
void caculate(long n);
int main(void)
{
    long sum_money,get_money;
    char type;

    printf("please enter your type (single/household/married/divorce) :\n");
    while (scanf("%s",type) == 1)
    {
        switch (type)
        {
            case 'single':
                n =single_boundary;
                caculate(n);
                break;
            case 'household':
                n = (long)household_boundary;
                caculate(n);
                break;
            case 'married':
                n = (long)married_boundary;
                caculate(n);
                break;
            case 'divorce':
                n = (long)divorce_boundary;
                caculate(n);
                break;
            default:
                printf("your enter is wrong please enter again: \n");
                break;
        }
    printf("done!\n");

    return 0;
    }

}
void caculate(long n)
{
    printf("please enter the sum_money: ");
    scanf("%ld",sum_money);

    if (sum_money > n)
        get_money = first_rate *n + over_rate*(sum_money - n);
    else
        get_money = first_rate * sum_money;

    printf("the total money of you is %ld and you  can get is %ld",sum_money,get_money);

}


很多错误: 1. 首先 case 后面的便签必须是:整数值!(包括char 类型),而我使用了字符串。2.子目标函数中 没有声明sum_money 与get_money,而在主函数中声明,错误很严重,在那里用就在那里声明,我在函数调用那么就在子函数中声明。 改看之后的版本:

#include <stdio.h>
#define first_rate 0.15
#define over_rate 0.28
#define single_boundary 17850;
#define household_boundary 23900;
#define married_boundary 29750;
#define divorce_boundary 14875;
void caculate(long n);
int main(void)
{
    long n,sum_money,get_money;
    char type;

    printf("please enter your type (single/household/married/divorce) :\n");
    while (scanf("%c",&type) == 1)
    {
        switch (type)
        {
            case 's':
                n =single_boundary;
                caculate(n);
                break;
            case 'h':
                n = (long)household_boundary;
                caculate(n);
                break;
            case 'm':
                n = (long)married_boundary;
                caculate(n);
                break;
            case 'd':
                n = (long)divorce_boundary;
                caculate(n);
                break;
            default:
                printf("your enter is wrong please enter again: \n");
                break;
        }
    printf("done!\n");

    return 0;
    }

}
void caculate(long n)
{
     long sum_money,get_money;
    printf("please enter the sum_money: ");
    scanf("%ld",&sum_money);

    if (sum_money > n)
        get_money = first_rate *n + over_rate*(sum_money - n);
    else
        get_money = first_rate * sum_money;

    printf("the total money of you is %ld and you  can get is %ld",sum_money,get_money);

}


虽然可以运行,但是还是有问题 1.当输入分类的类型为s(single)时候,应该输出的是3279.5,而结果是3279。这里还是初学的时候浮点数没有判断,这种税率问题可能会出现小数的结果,而我声明和打印时候都是用的整型!,这点错误出现很多次了 下次注意!3.函数没有办法循环输入,结果是把  return 0; 放在了while 循环的里面这样就导致了循环只会输出一次,



修改后运行还是出现一点小问题,就是循环里面如果不加scanf()语句会使得输入一次s 然后输入20000,之后能得出结果:
please enter the sum_money: 20000
the total money of you is 20000.0 and you  can get is 3279.5
please enter your type (single/household/married/divorce) :
your enter is wrong please enter again. please enter your type (single/household/married/divorce) :

循环自动跳过一次 进入swith 的default,加上scanf()后没有这个问题,尚未想通这个问题,是不是之后输入的20000存在了栈区被下一次类型输入读取 了,导致跳过一次输入直接进入default 的输入,而加了scanf()后将栈区里面的值刷新了就没有这个问题,个人想法,后面的学习中记得解决这个问题!


#include <stdio.h>
#define first_rate 0.15
#define over_rate 0.28
#define single_boundary 17850;
#define household_boundary 23900;
#define married_boundary 29750;
#define divorce_boundary 14875;
void caculate(long n);
int main(void)
{
    long n;
    char type;

    printf("please enter your type (single/household/married/divorce) q to quit:\n");
    while (scanf("%c",&type) == 1 && type != 'q')
    {
        switch (type)
        {
            case 's':
                n =single_boundary;
                caculate(n);
                break;
            case 'h':
                n = (long)household_boundary;
                caculate(n);
                break;
            case 'm':
                n = (long)married_boundary;
                caculate(n);
                break;
            case 'd':
                n = (long)divorce_boundary;
                caculate(n);
                break;
            default:
                printf("your enter is wrong please enter again. ");
                break;
        }
    printf("please enter your type (single/household/married/divorce) :\n");
    scanf("%c",&type);
    }

    printf("done!");
return 0;
}
void caculate(long n)
{
    double  sum_money,get_money;
    printf("please enter the sum_money: ");
    scanf("%lf",&sum_money);

    if (sum_money > n)
        get_money = first_rate *n + over_rate*(sum_money - n);
    else
        get_money = first_rate * sum_money;

    printf("the total money of you is %.1f and you  can get is %.1f\n",sum_money,get_money);

}
但还是有一些问题,比如一来就输入1000,或者在输入钱的数量是输入ww,很多问题。之后学习再来解决。20171209. 11:22.




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值