#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; }