题解 | #个人所得税计算程序#
个人所得税计算程序
https://www.nowcoder.com/practice/afd6c29943c54453b2b5e893653c627e
思路是,先在类中写好自定义比较函数,再用针对容器的算法对容器内的内容实现自定义排序
注意类的成员是private,需要调用成员函数来访问,另外结果保留一位小数
#include <iostream> #include <vector> #include <algorithm> // write your code here...... using namespace std; class Employee { private: string name; double salary; // write your code here...... public: void set_name(string name){ this->name = name; } void set_salary(double salary){ this->salary = salary; } double get_salary(){ return this->salary; } string get_name(){ return this->name; } Employee(string a, double b) :name(a), salary(b) {} }; bool mycompare_salary(Employee &emp1,Employee &emp2){ if(emp1.get_salary()>emp2.get_salary()){ return true; } else return false; } int main() { // write your code here...... Employee zhangsan("张三",6500.00); Employee lisi("李四",8000.00); Employee wangwu("王五",100000.00); vector<Employee> emp; cout.setf(ios::fixed); cout.precision(1); emp.push_back(zhangsan); emp.push_back(lisi); emp.push_back(wangwu); sort(emp.begin(),emp.end(),mycompare_salary); for(auto iter = emp.begin();iter!=emp.end();++iter){ if((iter->get_salary()-3500.00)<=1500){ cout<<iter->get_name()<<"应该缴纳的个人所得税是:"<<(iter->get_salary()-3500)*0.03<<endl; } else if((iter->get_salary()-3500.00)>1500&&(iter->get_salary()-3500.00)<=4500){ cout<<iter->get_name()<<"应该缴纳的个人所得税是:"<<(iter->get_salary()-3500)*0.10-105<<endl; } else if((iter->get_salary()-3500.00)>4500&&(iter->get_salary()-3500.00)<=9000){ cout<<iter->get_name()<<"应该缴纳的个人所得税是;"<<(iter->get_salary()-3500)*0.20-555<<endl; } else if((iter->get_salary()-3500.00)>9000&&(iter->get_salary()-3500.00)<=35000){ cout<<iter->get_name()<<"应该缴纳的个人所得税是;"<<(iter->get_salary()-3500)*0.25-1005<<endl; } else if((iter->get_salary()-3500.00)>35000&&(iter->get_salary()-3500.00)<=55000){ cout<<iter->get_name()<<"应该缴纳的个人所得税是;"<<(iter->get_salary()-3500)*0.30-2755<<endl; } else if((iter->get_salary()-3500.00)>55000&&(iter->get_salary()-3500.00)<=80000){ cout<<iter->get_name()<<"应该缴纳的个人所得税是;"<<(iter->get_salary()-3500)*0.35-5505<<endl; } else{ cout<<iter->get_name()<<"应该缴纳的个人所得税是;"<<(iter->get_salary()-3500)*0.45-13505<<endl; } } return 0; }