此程序将通过输入您的收入和婚姻状况来计算您的税级,s 表示单身,m 表示已婚。
该程序将提示您输入收入,然后输入婚姻状况。但它不计算所欠所得税,始终显示 $0.00。任何帮助,将不胜感激。非常感谢!
这是迄今为止我的代码:
int taxBracket (double income, char maritalStatus);
double calculateTax(double income, int taxBracket);
int main() {
double income;
char maritalStatus;
int taxBracketValue;
printf("Enter income: ");
scanf("%le", &income);
printf("Enter marital status (s for single, m for married): ");
scanf(" %c", &maritalStatus);
taxBracketValue = taxBracket(income, maritalStatus);
if(taxBracketValue == -1) {
printf("Invalid marital status.\n");
return 1;
}
double tax = calculateTax(income, taxBracketValue);
printf("Income tax is $%.2f\n", tax);
return 0;
}
int taxBracket(double income, char maritalStatus) {
if (maritalStatus == 's') {
if(income < 15000)
return 0;
else if (income >= 15000 && income < 50000)
return 1;
else if (income >= 50000 && income < 90000)
return 2;
else if (income >=90000 && income < 160000)
return 3;
else
return 4;
}
else if (maritalStatus == 'm') {
if (income < 30000)
return 0;
else if (income>= 30000 && income < 80000)
return 1;
else if (income>= 80000 && income < 140000)
return 2;
else if (income >= 140000 && income < 220000)
return 3;
else
return 4;
}
return -1;
}
double calculateTax(double income, int taXBracket) {
double tax = 0.0;
int taxBracket;
switch(taxBracket) {
case 0:
tax = 0.0;
break;
case 1:
tax = income * 0.10;
break;
case 2:
tax = income * 0.15;
break;
case 3:
tax = income * 0.23;
break;
case 4:
tax = income * 0.33;
break;
default:;
break;
}
return tax;
}
calculateTax()
中,局部变量 taxBracket
未初始化,但您想使用大写不同的参数。这是您的主要问题,您的编译器应该为未初始化的变量生成警告。#include <stdio.h>
int taxbracket(double income, char maritalstatus) {
if (maritalstatus == 's') {
if(income < 15000)
return 0;
else if (income >= 15000 && income < 50000)
return 1;
else if (income >= 50000 && income < 90000)
return 2;
else if (income >=90000 && income < 160000)
return 3;
else
return 4;
}
else if (maritalstatus == 'm') {
if (income < 30000)
return 0;
else if (income>= 30000 && income < 80000)
return 1;
else if (income>= 80000 && income < 140000)
return 2;
else if (income >= 140000 && income < 220000)
return 3;
else
return 4;
}
return -1;
}
double calculatetax(double income, int taxbracket) {
double tax = 0.0;
switch(taxbracket) {
case 0:
tax = 0.0;
break;
case 1:
tax = income * 0.10;
break;
case 2:
tax = income * 0.15;
break;
case 3:
tax = income * 0.23;
break;
case 4:
tax = income * 0.33;
break;
default:;
break;
}
return tax;
}
int main() {
double income;
char maritalstatus;
int taxbracketvalue;
printf("enter income: ");
scanf("%le", &income);
printf("enter marital status (s for single, m for married): ");
scanf(" %c", &maritalstatus);
taxbracketvalue = taxbracket(income, maritalstatus);
if(taxbracketvalue == -1) {
printf("invalid marital status.\n");
return 1;
}
double tax = calculatetax(income, taxbracketvalue);
printf("income tax is $%.2f\n", tax);
return 0;
}
和示例运行:
Enter income: 30000
Enter marital status (s for single, m for married): s
Income tax is $3000.00