用c++写一个个人所得税计算器
时间: 2024-10-21 16:07:18 浏览: 30
在C++中编写一个简单的个人所得税计算器,我们可以创建一个结构体或类来表示个人的基本信息以及税收计算规则。这里是一个基本的示例:
```cpp
#include <iostream>
using namespace std;
class TaxCalculator {
private:
double income;
double tax_thresholds[] = {0, 3500, 14000, 30000, 42000}; // 税率分段点
double tax_rates[] = {0, 0.03, 0.1, 0.2, 0.25}; // 对应的税率
public:
TaxCalculator(double annual_income) : income(annual_income) {}
void calculateTax() {
if (income <= tax_thresholds[0]) {
tax = income * tax_rates[0];
} else {
int i;
for (i = 1; i < sizeof(tax_thresholds) / sizeof(tax_thresholds[0]); ++i) {
if (income > tax_thresholds[i - 1] && income <= tax_thresholds[i]) {
tax += (income - tax_thresholds[i - 1]) * tax_rates[i];
break;
}
}
}
cout << "个人年收入:" << income << " 元,应缴税额:" << tax << " 元" << endl;
}
double getTax() const { return tax; }
};
int main() {
double annual_income;
cout << "请输入您的年收入:";
cin >> annual_income;
TaxCalculator calculator(annual_income);
calculator.calculateTax();
return 0;
}
阅读全文