题解 | #个人所得税计算程序#

个人所得税计算程序

https://www.nowcoder.com/practice/afd6c29943c54453b2b5e893653c627e

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iomanip>
using namespace std;

class Employee {

  private:
    string name; //姓名
    int salary; //工资
  public:
    Employee(string name, int salary) { //构造函数
        this->name = name;
        this->salary = salary;
    }

    string getName() { //获取姓名
        return name;
    }
    int getSalary() { //获取工资
        return salary;
    }
};

bool cmp(Employee& a, Employee& b) { //重载比较
    return a.getSalary() > b.getSalary();
}

void print(Employee& e) {
    double tax = 0.0;
    double income = e.getSalary() -
                    3500; //工资-扣除数=全月应纳税所得额
    //找到所属区间
    if (income <= 1500)
        tax = income * 0.03;
    else if (income <= 4500)
        tax = income * 0.1 - 105;
    else if (income <= 9000)
        tax = income * 0.2 - 555;
    else if (income <= 35000)
        tax = income * 0.25 - 1005;
    else if (income <= 55000)
        tax = income * 0.3  - 2755;
    else if (income <= 80000)
        tax = income * 0.35 - 5505;
    else
        tax = income * 0.45 - 13505;
    cout << fixed << setprecision(1); //保留1位小数
    cout << e.getName() << "应该缴纳的个人所得税是:" << tax << endl;
}

int main() {
    //新建三个类
    Employee e1("张三", 6500);
    Employee e2("李四", 8000);
    Employee e3("王五", 100000);
    vector<Employee> v;//将信息加入vector容器
    v.push_back(e1);
    v.push_back(e2);
    v.push_back(e3);
    sort(v.begin(), v.end(), cmp); //按工资从大到小排序
    for_each(v.begin(), v.end(), print);
    return 0;
}

全部评论

相关推荐

如题
投递阿里巴巴集团等公司10个岗位 >
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
06-13 19:30
化身华黑&nbsp;今天询问对接人审批情况,结果被告知没HC了&nbsp;云计算&nbsp;
苦闷的柠檬精allin实习:主管面结束后hr每周保温一次,结果前几天和我说没hc了,我也化身华黑子了
点赞 评论 收藏
分享
评论
1
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务