首页 > 试题广场 >

个人所得税计算程序

[编程题]个人所得税计算程序
  • 热度指数:14414 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解

个人所得税是国家对本国公民、居住在本国境内的个人的所得和境外个人来源于本国的所得征收的一种所得税。假设某地区的起征点为3500元(即月工资低于3500时不需要缴纳个人所得税),个人所得税的计算公式为:应纳税额 =(工资薪金所得-扣除数)× 适用税率-速算扣除数。其中,扣除数为3500元,适用税率以及速算扣除数如下表所示。

表-1 个人所得税缴纳标准

  全月应纳税所得额

   税率   

   速算扣除数(元)   

  不超过1500元

   3%

      0

  超过1500元至4500元

   10%

     105

 超过4500元至9000元

   20%

     555

  超过9000元至35000元

   25%

     1005

 超过35000元至55000元      

   30%

     2755

  超过55000元至80000元  

   35%

     5505

  超过80000元

   45%

     13505

上表中的全月应纳税所得额 = 工资薪金所得-扣除数。

现在请你补全代码中的 Employee 类,新建三个 Employee 对象,姓名分别是张三,李四和王五,他们的月工资分别为 6500,8000,100000。并将他们存入一个 STL 容器中,要求按照月工资由高到底的顺序排序,遍历容器并计算他们应缴纳的个人所得税(个人所得税为 double 类型,保留一位小数)。


输入描述:


输出描述:
王五应该缴纳的个人所得税是:xxx
李四应该缴纳的个人所得税是:xxx
张三应该缴纳的个人所得税是:xxx
注意:月薪6500的张三应缴45块,题目给出的答案195实际应为月薪7250所得,张三同志背着我们悄悄努力然后升职加薪惊艳所有人啦!
发表于 2024-07-10 14:05:01 回复(0)
题给的数,和答案的不一致
发表于 2023-02-21 16:57:57 回复(1)
#include <iostream>
// write your code here......
#include<vector>
#include<algorithm>
using namespace std;

class Employee {

    private:
        string name;
        double salary;
    // write your code here......
    public:
    friend bool salarydiv(Employee A,Employee B);
    friend double salarytax(Employee A);
    Employee(){};
    Employee(string name,double salary){
        this->name=name;
        this->salary=salary;
    }

};
bool salarydiv(Employee A,Employee B){
    return A.salary>B.salary;
}
double salarytax(Employee A){
    double tax=0;
    double temp=A.salary-3500;
    if(temp<=0){tax=0;}
    else if(temp>0&&temp<=1500){tax=temp*0.03;}
    else if(temp>1500&&temp<=4500){tax=temp*0.1-105;}
    else if(temp>4500&&temp<=9000){tax=temp*0.2-555;}
    else if(temp>9000&&temp<=35000){tax=temp*0.25-1005;}
    else if(temp>35000&&temp<=55000){tax=temp*0.3-2755;}
    else if(temp>55000&&temp<=80000){tax=temp*0.35-5505;}
    else if(temp>80000){tax=temp*0.45-13505;}
    cout<<A.name<<"应该缴纳的个人所得税是:"<<tax<<endl;
    return tax;
}


int main() {

    // write your code here......
    Employee A("张三",6500),B("李四",8000),C("王五",100000);
    vector<Employee>emp;
    emp.push_back(A);
    emp.push_back(B);
    emp.push_back(C);
    sort(emp.begin(),emp.end(),salarydiv);
    vector<Employee>::iterator it=emp.begin();
    for(;it!=emp.end();it++){
        salarytax(*it);
    }

   不明白为什么提交不通过
发表于 2022-05-12 09:17:34 回复(3)
#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;
}

发表于 2022-01-15 16:09:24 回复(2)
#include <iomanip>
#include <ios>
#include <iostream>
#include<map>
#include<algorithm>
#include<string>
#include<vector>
// write your code here......

using namespace std;

class Employee {

public:
    string name;
    double salary;
    // write your code here......

    Employee(string n, double s)
    {
        this->name = n;
        this->salary = s;
    }


};

bool func_sort(const pair<string, double>&a, const pair<string, double>&b)
{
    return a.second > b.second;
}
int main()
{

    // write your code here.....
    Employee p1("张三", 6500), p2("李四", 8000), p3("王五", 100000);
    map<string, double> ma = { {p1.name,p1.salary},{p2.name,p2.salary},{p3.name,p3.salary} };
    vector<pair<string, double>> vec(ma.begin(), ma.end());
    sort(vec.begin(), vec.end(), func_sort);
    for (auto it = vec.begin(); it != vec.end(); it++)
    {
        double money = (it->second - 3500);
        double tax = 0;
        if (money < 1500)
        {
            tax = money * 0.03;
        }
        else if (money > 1500 && money <= 4500)
        {
            tax = money * 0.1 - 105;
        }
        else if (money > 4500 && money <= 9000)
        {
            tax = money * 0.2 - 555;
        }
        else if (money > 9000 && money <= 35000)
        {
            tax = money * 0.25 - 1005;
        }
        else if (money > 35000 && money <= 55000)
        {
            tax = money * 0.3 - 2755;
        }
        else if (money > 55000 && money <= 80000)
        {
            tax = money * 0.35 - 5505;
        }
        else
        {
            tax = money * 0.45 - 13505;
        }
        cout << it->first << "应该缴纳的个人所得税是:" <<fixed<<setprecision(1)<< tax << endl;
    }
    return 0;
}
发表于 2025-01-27 11:19:37 回复(0)
#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;
}

发表于 2025-01-20 12:34:36 回复(0)
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
// write your code here......

using namespace std;

class Employee {

    private:
        string name;
        double salary;
    // write your code here......
    public:
        Employee(string na,double sa){
        this->name=na;
        this->salary=sa;
        }
        void setname(string str){
        this->name=str;
        }
        void setsalary(int sala){
        this->salary=sala;    
        }
        string getname(){
        return name;
        }
        double getsalary(){
        return salary;
        }

};

int main() {
    Employee ww("张三",6500);Employee ls("李四",8000);Employee zs("王五",100000);
    vector<Employee> ve;
    ve.push_back(ww);
    ve.push_back(ls);
    ve.push_back(zs);
    for (int j=0; j<ve.size(); j++) {
        for (int i=0; i<ve.size()-1; i++) {
        Employee aa("张三",6500);
        if (ve[i].getsalary()<ve[i+1].getsalary()) {
        aa=ve[i];
        ve[i]=ve[i+1];ve[i+1]=aa;
        }
        }
    }
    vector<Employee>::iterator t;
    double salll;
    for(t = ve.begin(); t != ve.end(); t++){
        if ((*t).getsalary()<=4500) {
        salll=0.0;
        }
        else if ((*t).getsalary()>4500&&(*t).getsalary()<=8000) {
        salll=((*t).getsalary()-3500)*0.1-105;
        }
        else if ((*t).getsalary()>8000&&(*t).getsalary()<=12500) {
        salll=((*t).getsalary()-3500)*0.2-555;
        }
        else if ((*t).getsalary()>12500&&(*t).getsalary()<=38500) {
        salll=((*t).getsalary()-3500)*0.25-1005;
        }
        else if ((*t).getsalary()>38500&&(*t).getsalary()<=58500) {
        salll=((*t).getsalary()-3500)*0.3-2755;
        }
        else if ((*t).getsalary()>58500&&(*t).getsalary()<=83500) {
        salll=((*t).getsalary()-3500)*0.35-5505;
        }
        else if ((*t).getsalary()>83500) {
        salll=((*t).getsalary()-3500)*0.45-13505;
        }

        cout << (*t).getname()<< "应该缴纳的个人所得税是:"<<setiosflags(ios::fixed)<<setprecision(1)<< salll << endl;
       
    }

    return 0;
}
发表于 2024-12-08 16:47:28 回复(0)
#include <algorithm>
//#include <functional>
#include <iomanip>
//#include <ios>
#include <iostream>
//#include <string>
#include <vector>
// write your code here......

using namespace std;

class Employee {

    private:
        string name;
        double salary;
    // write your code here......

public:
    void savename (string s){
        name = s;
    }
    void savesalary (double num){
        salary = num;
    }
    string getname(){
        return name;
    }
    double getsalary(){
        return salary;
    }
    double calculate(double Sa, double taxper, double kou ){
        double result = Sa * taxper - kou;
        return result;
    }
    double Tax(){
        double tax;
        double standard = 3500;
        double Sa = salary - standard;
        if ( Sa <= 0 ) {
            tax = 0;
        }else if(Sa <= 1500 && Sa >0){
            tax = calculate(Sa, 0.03, 0);
        }else if(Sa <= 4500 && Sa >1500){
            tax = calculate(Sa, 0.1, 105);
        }else if (Sa <= 9000 && Sa >4500) {
            tax = calculate(Sa, 0.2, 555);
        }else if (Sa <= 35000 && Sa > 9000) {
            tax = calculate(Sa, 0.25, 1005);
        }else if(Sa <= 55000 && Sa > 35000) {
            tax = calculate(Sa, 0.3, 2755);
        }else if(Sa <= 80000 && Sa > 55000) {
            tax = calculate(Sa, 0.35, 5505);
        }else if(Sa > 80000) {
            tax = calculate(Sa, 0.45, 13505);
        }
        return tax;
    }
    bool operator>(const Employee& other) const{
        return salary > other.salary;
    }

};

int main() {

    // write your code here......
    Employee ep1;
    Employee ep2;
    Employee ep3;

    ep1.savename("张三");
    ep1.savesalary(6500);
    ep2.savename("李四");
    ep2.savesalary(8000);
    ep3.savename("王五");
    ep3.savesalary(100000);
   
    vector<Employee*> v;
    v.push_back(&ep1);
    v.push_back(&ep2);
    v.push_back(&ep3);

    sort(v.begin(), v.end() , [](Employee* a,Employee* b){
        return *a > *b;
    });//需要重装载
    cout << fixed << setprecision(1);
    for(const auto &iter : v){
        cout << iter->getname();
        cout<< "应该缴纳的个人所得税是:" ;
        cout << iter->Tax() << endl;
    }
    return 0;
}
发表于 2024-11-20 23:48:13 回复(0)
#include <csignal>
#include <functional>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

#define WORKER_NUM 3
#define E(x) Ex

unsigned int s[6] = {1500, 4500, 9000, 35000, 55000, 80000};
double e[7] = {0.0, 105.0, 555.0, 1005.0, 2755.0, 5505.0, 13505.0};
double x[7] = {0.03, 0.1, 0.2, 0.25, 0.3, 0.35, 0.45};

string name[WORKER_NUM] = {"张三", "李四", "王五"};
double sa[WORKER_NUM]= {6500.0, 8000.0, 100000.0};

class Employee {
    private:
        string name;
        double salary;
    public:
        Employee(string name, double salary);
        ~Employee();  
        double get_tax();
        double& get_salary();
        void cout_tax(Employee worker);
};

Employee::Employee(string name, double salary){
    this->name = name;
    this->salary = salary;
}

Employee::~Employee(){
    ;
}

double Employee::get_tax(){
    double tax = 0.0;
    double cash = salary;
    cash -= 3500;

    if(cash <= 0){
        tax = 0.0; 
    }else if(cash <= s[0]){
        tax = cash * x[0] - e[0];
    }else if(cash <= s[1]){
        tax = cash * x[1] - e[1];
    }else if(cash <= s[2]){
        tax = cash * x[2] - e[2];
    }else if(cash <= s[3]){
        tax = cash * x[3] - e[3];
    }else if(cash <= s[4]){
        tax = cash * x[4] - e[4];
    }else if(cash <= s[5]){
        tax = cash * x[5] - e[5];
    }else{
        tax = cash * x[6] - e[6];
    }

    return tax;
}

double& Employee::get_salary(){
    return this->salary;
}

void Employee::cout_tax(Employee worker){
    cout << worker.name << "应该缴纳的个人所得税是:" 
         << fixed << setprecision(1) << worker.get_tax() 
         << endl;
}

bool cmp_up(Employee x, Employee y){
    return x.get_salary()>y.get_salary();
}

int main(){

    unsigned int n = 3;
    vector<Employee>worker;
    for(int i=0 ; i<WORKER_NUM ; i++){
        Employee E(i)(name[i], sa[i]);
        worker.push_back(E(i));
    }
    sort(worker.begin(), worker.end(), cmp_up);
    
    for(int i=0 ; i<WORKER_NUM ; i++){
        worker[i].cout_tax(worker[i]);
    }

    return 0;
}

发表于 2024-10-29 10:32:08 回复(0)
#include <algorithm>
#include <iostream>
#include <vector>
#include <iomanip>
#include <algorithm>
using namespace std;

class Employee {

    private:
        string name;
        double salary;
    public:
        Employee(string n, double s)
        {
            this->name=n;
            this->salary=s;
        }
        double tax()
        {
            if(this->salary<3500)
            {return 0;}
            else if(this->salary<5000)
            {return (salary-3500)*0.03;}
            else if(this->salary<8000)
            {return (salary-3500)*0.1-105;}
            else if(this->salary<12500)
            {return (salary-3500)*0.2-555;}
            else if(this->salary<38500)
            {return (salary-3500)*0.25-1005;}
            else if(this->salary<58500)
            {return (salary-3500)*0.3-2755;}
            else if(this->salary<83500)
            {return (salary-3500)*0.35-5505;}
            else 
            {return (salary-3500)*0.45-13505;}
        }
        string getname()
        {
            return this->name;
        }
        double getsalary()
        {
            return this->salary;
        }
};

bool cmp (Employee a, Employee b)
{
    return a.getsalary() > b.getsalary();
}

int main() {

    Employee e1 = Employee("张三",6500);
    Employee e2 = Employee("李四",8000);
    Employee e3 = Employee("王五",100000);
    vector<Employee> mp;
        mp.push_back(e1);
        mp.push_back(e2);
        mp.push_back(e3);
        sort(mp.begin(),mp.end(),cmp);
        for(auto i : mp)
        {
            cout<<i.getname()<<"应该缴纳的个人所得税是:"<<fixed<<setprecision(1)<<i.tax()<<endl;
        }
    return 0;
发表于 2024-08-19 21:46:53 回复(0)
不知道用啥排序,private变public写着玩玩
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
// write your code here......

using namespace std;

class Employee {

    public:
        string name;
        double salary;
    // write your code here......
    public:
    Employee(string name,double salary)
    {
        this->name=name;
        this->salary=salary;
    }

};

void swop1(double &m,double &n)
{
    double temp1=m;
    m=n;
    n=temp1;
}
void swop2(string &p,string &q)
{
    string temp2=p;
    p=q;
    q=temp2;
}
void sorted(vector<Employee>VEC)
{
    for(int i=0;i<VEC.size()-1;i++)
    {
        for(int j=0;j<VEC.size()-1-i;j++)
        {
            if(VEC[j].salary>VEC[j+1].salary)
            {
                swop1(VEC[j].salary,VEC[j+1].salary);
                swop2(VEC[j].name,VEC[j+1].name);
            }
        }
    }
}

void cal(Employee E)
{
    double number=0;
    double sbtm=E.salary-3500;
    if(sbtm>80000)
    {
        number=sbtm*0.45-13505;
    }
    else if(E.salary>55000)
    {
        number=sbtm*0.35-5505;        
    }
    else if(sbtm>35000)
    {
        number=sbtm*0.3-2755;        
    }
    else if(sbtm>9000)
    {
        number=sbtm*0.25-1005;        
    }
    else if(sbtm>4500)
    {
        number=sbtm*0.2-555;        
    }
    else if(sbtm>1500)
    {
        number=sbtm*0.1-105;        
    }
    else if (sbtm<=1500) {
        number=sbtm*0.03;
    }
    //cout<<E.name<<"王五应该缴纳的个人所得税是:"<<number<<endl;
    cout<<E.name<<"应该缴纳的个人所得税是:"<<std::fixed << std::setprecision(1)<<number<<endl;
}

int main() {

    // write your code here......
    vector<Employee>vec;
    Employee em1("张三",6500);vec.push_back(em1);
    Employee em2("李四",8000);vec.push_back(em2);
    Employee em3("王五",100000);vec.push_back(em3);
    sorted(vec);
    reverse(vec.begin(), vec.end());
    for(auto i :vec)
    {
        cal (i);
    }
    return 0;
}

发表于 2024-06-12 17:03:29 回复(0)
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iomanip> 
using namespace std;

class Employee {
    friend double Calculatetax(Employee e);
    public:
        string m_name;
        double m_salary;
    Employee(string name, double salary)
    {
        this->m_name = name;
        this->m_salary = salary;
    }
};
bool compare(Employee& e1, Employee& e2)
{
    return e1.m_salary > e2.m_salary;
}
void Calculatetax(Employee& e)
{
    double tamp = e.m_salary - 3500;
    double tax = 0.0;
    if (tamp <= 1500) 
        tax = tamp * 0.03;
    else if (tamp <= 4500)  
        tax = tamp * 0.1 - 105;
    else if (tamp <= 9000) 
        tax = tamp * 0.2 - 555;
    else if (tamp <= 35000) 
        tax = tamp * 0.25 - 1005;
    else if (tamp <= 55000) 
        tax = tamp * 0.3 - 2755;
    else if (tamp <= 80000) 
        tax = tamp * 0.35 - 5505;
    else
        tax = tamp * 0.45 - 13505;
    cout << fixed << setprecision(1);
    cout << e.m_name << "应该缴纳的个人所得税是:" << tax << endl;
}
int main() 
{
    vector<Employee>d;
    Employee e1("张三", 6500);
    Employee e2("李四", 8000);
    Employee e3("王五", 100000);
    d.push_back(e1);
    d.push_back(e2);
    d.push_back(e3);
    sort(d.begin(), d.end(),compare);
    for_each(d.begin(), d.end(), Calculatetax);
    return 0;
}

发表于 2024-06-08 10:48:41 回复(0)
题目中张三的月薪给的和答案的不一致,按照答案的算法,张三的月薪改成7250代码就能通过了
#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......
        double resalary;
    public:
        Employee(string n,double sa):name(n),salary(sa){};
        void recount_salary(double rate,int multinum){
            this->resalary = (salary-3500)*rate-multinum;
        }
        double getResalary(){
            return resalary;
        }
        string getname(){
            return name;
        }
};
bool cmp(Employee &p1,Employee &p2){
    return p1.getResalary()>p2.getResalary();
}
void myprint(Employee &p){
    cout<<p.getname()<<"应该缴纳的个人所得税是:"<< fixed <<setprecision(1) <<p.getResalary()<<endl;
}
int main() {

    // write your code here......
    Employee p1("张三",7250);
    Employee p2("李四",8000);
    Employee p3("王五",100000);
    p1.recount_salary(0.2, 555);
    p2.recount_salary(0.2, 555);
    p3.recount_salary(0.45, 13505);
    vector<Employee> v;
    v.push_back(p1);
    v.push_back(p2);
    v.push_back(p3);
    sort(v.begin(),v.end(),cmp);
    for_each(v.begin(),v.end(),myprint);

    return 0;
}
发表于 2023-09-04 21:34:44 回复(1)
#include <iostream>
// write your code here......
#include <vector>
#include <iomanip>

using namespace std;

class Employee {

    private:
        string name;
        double salary;
    // write your code here......
    public:
    Employee(string name,double salary)
    {
        this->name=name;
        this->salary=salary;
    }

    double getsalary()
    {
        return salary;
    }

    string getname()
    {
        return name;
    }
    

};

int main() {

    // write your code here......
    Employee e1("王五",100000);
    Employee e2("李四",8000);
    Employee e3("张三",6500);
    vector<Employee> evec;
    evec.push_back(e1);
    evec.push_back(e2);
    evec.push_back(e3);
    double tax=0.0;
    for(int i=0;i<3;i++)
    {
        if(evec[i].getsalary()>83500)
        {
            tax=(evec[i].getsalary()-3500)*0.45-13505;
        }
        else if(evec[i].getsalary()>58500&&evec[i].getsalary()<=83500)
        {
            tax=(evec[i].getsalary()-3500)*0.35-5505;
        }
        else if(evec[i].getsalary()>38500&&evec[i].getsalary()<=58500)
        {
            tax=(evec[i].getsalary()-3500)*0.3-2755;
        }
        else if(evec[i].getsalary()>12500&&evec[i].getsalary()<=38500)
        {
            tax=(evec[i].getsalary()-3500)*0.25-1005;
        }
        else if(evec[i].getsalary()>8000&&evec[i].getsalary()<=12500)
        {
            tax=(evec[i].getsalary()-3500)*0.2-555;
        }
        else if(evec[i].getsalary()>5000&&evec[i].getsalary()<=8000)
        {
            tax=(evec[i].getsalary()-3500)*0.1-105;
        }
        else if(evec[i].getsalary()>3500&&evec[i].getsalary()<=5000)
        {
            tax=(evec[i].getsalary()-3500)*0.1-105;
        }
        else{
            tax=0;
        }
        cout<<fixed<<setprecision(1);
        cout<<evec[i].getname()<<"应该缴纳的个人所得税是: "<<tax<<endl;
    }


    

    return 0;
}
为什么我的代码通过不了呢?
发表于 2023-06-01 00:44:21 回复(0)
#include <asm-generic/errno-base.h>
#include <functional>
#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
// write your code here......

using namespace std;

class Employee {

    private:
        string name;
        double salary;
    // write your code here......
    public:
        Employee(string name, double salary){
            this->name = name;
            this->salary = salary;
        }
        double calAx(){
            double num = salary - 3500.0;
            if(num>80000){
                return num*0.45 - 13505.0;
            }else if(num>55000){
                return num*0.35 - 5505.0;
            }else if(num>35000){
                return num*0.3 - 2755.0;
            }else if(num>9000){
                return num*0.25 - 1005.0;
            }else if(num>4500){
                return num*0.2 - 555.0;
            }else if(num>1500){
                return num*0.1 - 105.0;
            }else {
                return num*0.03;
            }
        }
        void showAx(){
            cout<<fixed<<setprecision(1);//保留一位小数
            cout<<name<<"应该缴纳的个人所得税是:"<<calAx()<<endl;
        }
        double getSalary(){
            return salary;
        }
};
bool com(Employee x, Employee y){
    return x.getSalary() > y.getSalary();
}
int main() {

    // write your code here......
    Employee zhangSan = Employee("张三", 6500);
    Employee liSi = Employee("李四", 8000);
    Employee wangWu = Employee("王五", 100000);

    vector<Employee> v;
    v.push_back(zhangSan);
    v.push_back(liSi);
    v.push_back(wangWu);
    sort(v.begin(), v.end(), com);
    for(auto i : v){
        i.showAx();
    }
   

    return 0;
}
发表于 2023-03-21 15:33:48 回复(0)
#include<iostream>
using namespace std;
#include <string>
#include<vector>
#include<iomanip>
#include<algorithm>
class Employee
{
	friend void calculateTax(Employee& p);//全局函数作友元
	friend void mySort(vector<Employee>& v);
	friend bool cmp(Employee& a, Employee& b);
private:
	string Name;
	double Income;
public:
	Employee(string name, double income)
	{
		this->Name = name;
		this->Income = income;
	}
};
//计算个人所得税
void calculateTax(Employee& p)
{
	double shouldTax = p.Income - 3500;//shouldTax为全月应纳税所得额
	double Tax;//Tax为应纳税额
	if (shouldTax <= 1500)
		Tax = shouldTax * 0.03;
	else if (shouldTax > 1500 && shouldTax <= 4500)
		Tax = shouldTax * 0.1 - 105;
	else if (shouldTax > 4500 && shouldTax <= 9000)
		Tax = shouldTax * 0.2 - 555;
	else if (shouldTax > 9000 && shouldTax <= 35000)
		Tax = shouldTax * 0.25 - 1005;
	else if (shouldTax > 35000 && shouldTax <= 55000)
		Tax = shouldTax * 0.3 - 2755;
	else if (shouldTax > 55000 && shouldTax <= 80000)
		Tax = shouldTax * 0.35 - 5505;
	else
		Tax = shouldTax * 0.45 - 13505;
	cout << fixed << setprecision(1);
	cout << p.Name << "应该缴纳的个人所得税是:" << Tax << endl;
}
//重载比较
bool cmp(Employee& a, Employee& b)
{
	return a.Income > b.Income;
}
//新建三个Employee
void test01()
{
	vector<Employee> v;
	Employee p1("张三", 6500), p2("李四", 8000), p3("王五", 100000);
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	sort(v.begin(), v.end(), cmp);
	for (int i = 0; i < 3; i++)
	{
		calculateTax(v[i]);
	}
}
int main()
{
	test01();
	return 0;
}


发表于 2023-02-07 16:34:02 回复(0)
#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 name,double salary)
    {
        this->name=name;
        this->salary=salary;
    }
    string getname()
    {
        return name;
    }
    double getsalary()
    {
        return salary;
    }
    bool operator>(Employee e1)  //重载比较
    {
        return this->salary>e1.salary;
    }
};

void print(Employee& e)
{
    double tax=0.0;
    double s=e.getsalary()-3500;
    if(s>80000)
    {
        tax=s*0.45-13505;
    }
    if(s>55000&&s<=80000)
    {
       tax=s*0.35-5505; 
    }
    if(s>35000&&s<=55000)    
    {
          tax=s*0.30-2755;
    } 
    if(s>9000&&s<=35000)
    {
        tax=s*0.25-1005;
    }      
    if(s>4500&&s<=9000)
    {
        tax=s*0.20-555;
    }   
    if(s>1500&&s<=4500)
    {
        tax=s*0.1-105;
    }   
    if(s<=1500&&s>0)
    {
         tax=s*0.03;
    }  
    if(s<=0)
    {
         tax=0.0;
    }
    cout<<fixed<<setprecision(1); //保留一位小数
    cout<<e.getname() <<"应该缴纳的个人所得税是:"<<tax<<endl;
}
int main() {

    // write your code here......
    //创建三个对象
    Employee p1("张三",6500),
    p2("李四",8000),
    p3("王五",100000);
    //将对象放入容器
    vector<Employee>vec;
    vec.push_back(p1);
    vec.push_back(p2);
    vec.push_back(p3);
    vector<Employee>::iterator it=vec.begin();
    sort(it,vec.end(),greater<>()); //从大到小排序
    for_each(it,vec.end(),print); //遍历输出
    return 0;
}
发表于 2022-08-27 00:00:23 回复(0)
一定要把类的定义放前面,不然系统识别不了,血泪
代码如下:
#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
using namespace std;

class Employee{
private:
    string name;
    double money;
public:
    Employee(string name,double money):name(name),money(money){}
    string get1(){
        return name;
    }
    double get2(){
        return money;
    }
};


bool cmp(Employee& x,Employee& y){
    return x.get2() > y.get2();
}

void dd(Employee& sum){
    cout << sum.get1() << "应该缴纳的个人所得税是:";
    double num = sum.get2() - 3500;
    if (num > 1500 && num <= 4500)
        cout<< fixed <<setprecision(1) << num * 0.1 - 105;
    else if (num <= 1500)
        cout<< fixed <<setprecision(1) <<  num * 0.03;
    else if (num > 80000)
        cout<< fixed <<setprecision(1) << num * 0.45 - 13505;
    cout << endl;
}

int main(){
    vector <Employee> pmc;
    Employee a("张三",6500);
    Employee b("李四",8000);
    Employee c("王五",100000);
    pmc.push_back(a);
    pmc.push_back(b);
    pmc.push_back(c);
    sort(pmc.begin(),pmc.end(),cmp);
    for_each(pmc.begin(),pmc.end(),dd);
    return 0;
}

发表于 2022-06-16 22:49:38 回复(0)