题解 | #个人所得税计算程序#
个人所得税计算程序
https://www.nowcoder.com/practice/afd6c29943c54453b2b5e893653c627e
#include <iostream>
// write your code here......
#include <vector>
#include <algorithm>
#include <iomanip>
using namespace std;
class Employee {
private:
string name;
double salary;
// write your code here......
public:
Employee(string sn, double sal): name(sn), salary(sal) {
}
double cal_tax(double sal);
double get_sal();
string get_nam();
};
double Employee::get_sal() {
return this->salary;
}
string Employee::get_nam() {
return this->name;
}
double Employee::cal_tax(double sal){
double tax = 0;
if(sal > 80000+3500){
tax = (sal-3500)*0.45-13505;
}else if (sal>55000+3500 && salary <= 80000+3500) {
tax = (sal-3500)*0.35-5505;
}else if (sal>35000+3500 && salary <= 55000+3500) {
tax = (sal-3500)*0.3-2755;
}else if (sal>9000+3500 && sal <= 35000+3500) {
tax = (sal-3500)*0.25-1005;
}else if (sal>4500+3500 && sal <= 9000+3500) {
tax = (sal-3500)*0.2-555;
}else if (sal>1500+3500 && sal <= 4500+3500) {
tax = (sal-3500)*0.1-105;
}else if(sal>0+3500 && sal <= 1500+3500){
tax = (sal-3500)*0.03;
}else{
tax = 0;
}
return tax;
}
void out(Employee& p) {
cout << p.get_nam() << "应该缴纳的个人所得税是:"<<setprecision(1)<<setiosflags(ios::fixed) << p.cal_tax(
p.get_sal()) << endl;
}
int main() {
// write your code here......
Employee zs("张三", 6500);
Employee ls("李四", 8000);
Employee ww("王五", 100000);
vector<Employee> vi;
Employee* pe[] = {&zs, &ls, &ww};
for (int i = 0; i < 3; i++) {
bool swapped = false;
for (int j = 0; j < 3 - i - 1; j++) {
if (pe[i]->get_sal() > pe[j]->get_sal()) {
swap(pe[i], pe[j]);
swapped = true;
}
}
if (!swapped) break;
}
for (int i = (int)(sizeof (pe) / sizeof (*pe)) - 1; i >= 0; --i) {
vi.push_back(*(pe[i]));
}
for_each(vi.begin(), vi.end(), out);
return 0;
}
