计算个税(定义一个薪水类) (起征点800): #include <iostream> using namespace std; #define TAX_THRESHOLD 800 struct Tax { double standard; double tax_rate; }; class Salary { double income; public: static Tax tax_array[]; Salary(int m = 0) { income = m; } void operator - (int payout) { income -= payout; cout << "工资余额:" << income << endl; } void CalculateSalary(void); }; Tax Salary::tax_array[] = { {0,0.05}, {500, 0.10}, {2000, 0.15}, {5000,0.20}, {20000,0.25}, {40000,0.30}, {60000,0.35}, {80000,0.40}, {100000,0.45} }; void Salary::CalculateSalary(void) { double tax = 0; double x = income - TAX_THRESHOLD; if(x > 0) { for(int i = sizeof(tax_array) / sizeof(*tax_array) - 1; i >= 0; i--) { if(x > tax_array[i].standard) { tax += (x - tax_array[i].standard) * tax_array[i].tax_rate; x = tax_array[i].standard; } } } cout << "税前工资为:" << income << endl; cout << "个人所得税为:" << tax << endl; income -= tax; cout << "实发工资为:" << income << endl; } int main(void) { double myincome; double mypayout; while(cout << "您的月薪为:", cin >> myincome) { Salary mysalary(myincome); mysalary.CalculateSalary(); cout << "取款金额:"; cin >> mypayout; mysalary - mypayout; // 硬性重载减号 cout << endl; } return 0; } 运行结果: 要修改的话很方便哦!(我也注意了魔数的问题)