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

#include <iostream>
#include <map>
#include <string>
#include <iomanip>
using namespace std;

struct TaxInfo {
    double rate;
    double deduction;
};

class Employee {
  public:
    Employee(string name, double salary) : name(name), salary(salary) {}

    double calcTax() {
        int income = salary - 3500;
        int grade = getGrade(income);
        const auto& tax_info = tax.at(grade);
        return income * tax_info.rate - tax_info.deduction;
    }

  private:
    int getGrade(int income) {
        static const int grade_boundaries[] = {1500, 4500, 9000, 35000, 55000, 80000};
        for (int i = 0; i < 6; i++) {
            if (income < grade_boundaries[i]) return i;
        }
        return 6;
    }

  public:
    const double& getSalary() {
        return salary;
    }
    const string& getName() {
        return name;
    }

  private:
    string name;
    double salary;
    static map<int, TaxInfo> tax;
};
map<int, TaxInfo > Employee::tax = {
    {0, {0.03, 0}},
    {1, {0.1, 105}},
    {2, {0.2, 555}},
    {3, {0.25, 1005}},
    {4, {0.3, 2775}},
    {5, {0.35, 5505}},
    {6, {0.45, 13505}}
};


int main() {
    std::map<double, Employee> m;
    m.emplace(6500, Employee("张三", 6500));
    m.emplace(8000, Employee("李四", 8000));
    m.emplace(100000, Employee("王五", 100000));

    for (auto it = m.rbegin(); it != m.rend(); it++) {
        cout << it->second.getName() << "应该缴纳的个人所得税是:"
             << fixed << setprecision(1) << it->second.calcTax() << endl;
    }

    return 0;
}

全部评论

相关推荐

03-19 10:07
已编辑
广东药科大学 golang
Yki_:你倒是进一个面啊
点赞 评论 收藏
分享
AAA专业长城贴瓷砖刘大爷:这样的简历我会直接丢进垃圾桶,花里胡哨的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务