的#include
// Eric Brooks //第10章税务计划 // 04 /二千零十一分之一十九
int main() { FILE * tax; //声明税是文件
int holdScreen, taxpayerId, grossIncome, taxExemptions;
char fillingStatus;
float taxableIncome,taxRate,taxAmount;
tax = fopen("tax.txt","r");// open tax for reading
//tax = fopen("txttemp.txt.txt","r");// open tax for reading
if (tax == NULL)
{
printf("Failed to open file. \n");
scanf("%d",&holdScreen);
exit(1);
}
printf("\n\t\t\t\tTax Table");
printf("\n\n Taxpayer ID \t Taxable Income \tTax Rate \tTax Amount");// print heading for output
printf("\n ----------- \t -------------- \t-------- \t----------");// print heading for output
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);
}
}
fclose(tax);// close file tax
scanf("%d",&holdScreen);
return 0;
}
输出应该看起来像这样,但它不能正常工作。我认为它与if语句和's'||没有关系's''''|| 'M'|| 'j'|| 'J'。
Tax Table
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