程序无法正确打印 [英] Program isn't printing correctly

查看:78
本文介绍了程序无法正确打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为我的程序在被声明为无效之后仍在通过if语句.它应该在与ID相同的行上打印无效的填充状态或无效的豁免状态.但是,它会在ID上方打印有效语句,并仍显示taxableIncome和taxRate和TaxAmount.while(fscanf(tax,"\ n%d%c%d%d",& taxpayerId,& fillingStatus,& grossIncome,& taxExemptions)!= EOF){

I believe my program is still going through the if statements after it is declared invalid. It should print invalid filling status or invalid exemption status on the same line with the id. Buts its printing the valid statement above the the id and still displaying the taxableIncome and taxRate and TaxAmount. while(fscanf(tax,"\n%d %c %d %d ",&taxpayerId, &fillingStatus, &grossIncome, &taxExemptions) != EOF) {

                    fillingStatus = toupper(fillingStatus);

                    if ( fillingStatus == 'S')
                    {
                        taxableIncome = grossIncome - 3000 - 1000 * taxExemptions;

                    }
                    else if ( fillingStatus == 'M')
                    {
                        taxableIncome = grossIncome - 3000 - 1000 * taxExemptions;

                    }   
                    else if ( fillingStatus == 'J')
                    {
                        taxableIncome = grossIncome - 6000 - 1000 * taxExemptions;

                    }

                    if (taxableIncome < 0)
                    {
                        taxableIncome = 0;
                    }


                    if ( fillingStatus == 'S')
                    {
                            if (taxableIncome < 5000)
                            {
                                taxRate = 0.15;
                            }
                            if (taxableIncome  > 20000)
                            {
                                taxRate = 0.31;
                            }
                            else
                            {
                                taxRate = 0.22;
                            }
                    }     
                    else if ( fillingStatus == 'M')
                    {
                            if (taxableIncome < 10000)
                            {
                                taxRate = 0.15;

                            }   
                            if (taxableIncome > 40000)
                            {
                                taxRate = 0.31;
                            }
                            else
                            {
                                taxRate = 0.22;
                            }
                    }
                    else if ( fillingStatus == 'J')
                    {
                            if (taxableIncome < 7000)
                            {
                                taxRate = 0.17;
                            }
                            if (taxableIncome > 25000)
                            {    
                                taxRate = 0.33;
                            }
                            else
                            {
                                taxRate = 0.24;
                            } 
                    }
                    else
                    {   
                            printf("\n %d     **** Invalid filling status ****",taxpayerId);
                            continue;
                    }

                    if (taxExemptions > 12  || taxExemptions < 0) 
                    {
                        printf("\n %d     **** Invalid number of exemptions ****", taxpayerId);
                    } 
                    else 
                    {
                        taxAmount = taxableIncome * taxRate;
                        printf("\n %d %15.2f  %15.2f  %18.2f",taxpayerId,taxableIncome,taxRate,taxAmount);
                    }  

输出看起来像这样:

Taxpayer ID    Taxable Income    Tax Rate        Tax Amount
-----------    --------------    --------        ----------

111111111           4000.00        0.15            600.00
222222222          36500.00        0.22           8030.00
333333333          19152.00        0.24           4596.48
444444444    **** Invalid filing status ****
555555555          67000.00        0.31          20770.00
666666666          53197.00        0.33          17555.01
777777777          19000.00        0.22           4180.00
888888888    **** Invalid number of exemptions ****
999999999          46308.00        0.33          15281.64
101010101          91602.00        0.31          28396.62
121212121           9525.00        0.15           1428.75
131313131              0.00        0.17              0.00

我的输出看起来像这样:

My out put look like this:

纳税人ID应税收入
税率税额----------- -------------- -------- ----------

Taxpayer ID Taxable Income
Tax Rate Tax Amount ----------- -------------- -------- ----------

111111111           4000.00        0.22            880.00
222222222          36500.00        0.31          11315.00
333333333          19152.00        0.24           4596.48
444444444          **** Invalid filing status ****           
555555555          67000.00        0.31          20770.00
666666666          53197.00        0.24          12767.28
777777777          **** Invalid number of exemptions ****        
888888888           3543.00        0.22
999999999          46308.00        0.24            779.46
101010101          91602.00        0.22          11113.92
121212121           9525.00        0.31           2952.75
131313131              0.00        0.24              0.00

推荐答案

fillingStatus == 's' || 'S'

上面的条件并没有按照您认为的做.试试:

The above condition does not do what you appear to think it does. Try:

fillingStatus == 's' || fillingStatus == 'S'

原因是 == 运算符一次只比较一对值.因此您的表情类似:

The reason is that the == operator only compares one pair of values at a time. So your expression reads something like:

如果(fillingStatus为's')或('S'为非零)

if (fillingStatus is 's') or ('S' is nonzero)

这始终为真(因为'S'始终为非零).校正后的表达式如下:

which is always true (since 'S' is always nonzero). The corrected expression reads like:

如果(fillingStatus为's')或(fillingStatus为'S')

if (fillingStatus is 's') or (fillingStatus is 'S')

可能值得注意的是,与上述C ++代码相比,某些其他语言在表达此条件方面也没有那么冗长的方式.例如,在Python中,您可以编写:

It might be worth noting that some other languages have less verbose ways of expressing this condition than the above C++ code. For example, in Python you can write:

if fillingStatus in ('s', 'S'):

测试 fillingStatus 's 'S'.

这篇关于程序无法正确打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆