我应该把什么放在套装中,我的条件应该是什么?

时间:2016-04-20 20:17:01

标签: loops if-statement while-loop

计算应付所得税应纳税所得税。请务必包含错误检查,以确保用户不输入负数。还有一张图表

http://imgur.com/E5AqNxQ

让我陷入困境的是我的条件应该是什么,我宣布正确的事情应该是什么? 我有 声明incometax,taxdue,as float

来电输入

通话集

致电输出

输入

写"输入应税收入"

输入应税收入

结束输入

我不知道。

输出

帮帮我

1 个答案:

答案 0 :(得分:0)

我不确定你用什么语言写这个,但这是你在C中的解决方案。请记住这是使用整数,而不是浮点数。所以答案将被截断。教科书中的问题是假设整数条目,因此为什么我这样做了。

    int main()
{
    printf("Please enter your income: ");
    int iIncome = 0;
    int iTaxAmount = 0;
    char iChecking = 0;

    //use a while loop with our scanf to make sure a valid number is input.
    while (iChecking == 0)
    {
        scanf("%d", &iIncome);
        if (iIncome <= 0)
        {
            printf("Please enter a positive, nonzero number\n");
            printf("Please enter your income: ");
        }
        else
        {
            iChecking = 1;
        }
    }


    if (iIncome < 50000)
    {

        iTaxAmount = iIncome * 0.05;
    }
    else if (iIncome < 100000 && iIncome >= 50000)
    {
        //set base amount
        iTaxAmount = 2500;
        int iTaxableOver50k = 0;
        //calculate taxable amount over 50k
        iTaxableOver50k = iIncome - 50000;
        //add base tax to percent value
        iTaxAmount = iTaxAmount + iTaxableOver50k * 0.07;
    }
    else if (iIncome >= 100000)
    {
        //set base tax amount
        iTaxAmount = 6000;
        int iTaxableOver100k = 0;
        //calculate taxable amount over 100k
        iTaxableOver100k = iIncome - 100000;
        //add base tax to percent value
        iTaxAmount = iTaxAmount + iTaxableOver100k*0.09;
    }

    printf("\nYou must pay: $%d in tax\n", iTaxAmount);

    return 0;
}

以下是使用浮点计算得出最终答案的解决方案。

#include <stdio.h>

int main()
{
    printf("Please enter your income: ");
    int iIncome = 0;
    float iTaxAmount = 0.0f;
    char iChecking = 0;

    //use a while loop with our scanf to make sure a valid number is input.
    while (iChecking == 0)
    {
        scanf("%d", &iIncome);
        if (iIncome <= 0)
        {
            printf("Please enter a positive, nonzero number\n");
            printf("Please enter your income: ");
        }
        else
        {
            iChecking = 1;
        }
    }


    if (iIncome < 50000)
    {

        iTaxAmount = (float)iIncome * 0.05f;
    }
    else if (iIncome < 100000 && iIncome >= 50000)
    {
        //set base amount
        iTaxAmount = 2500;
        int iTaxableOver50k = 0;
        //calculate taxable amount over 50k
        iTaxableOver50k = iIncome - 50000;
        //add base tax to percent value
        iTaxAmount = iTaxAmount + (float)iTaxableOver50k * 0.07f;
    }
    else if (iIncome >= 100000)
    {
        //set base tax amount
        iTaxAmount = 6000;
        int iTaxableOver100k = 0;
        //calculate taxable amount over 100k
        iTaxableOver100k = iIncome - 100000;
        //add base tax to percent value
        iTaxAmount = iTaxAmount + (float)iTaxableOver100k*0.09f;
    }


    printf("\nYou must pay: $%.2f in tax\n", iTaxAmount);

    return 0;
}