列表

详情


CPP53. 个人所得税计算程序

描述

个人所得税是国家对本国公民、居住在本国境内的个人的所得和境外个人来源于本国的所得征收的一种所得税。假设某地区的起征点为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

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++ 解法, 执行用时: 2ms, 内存消耗: 276KB, 提交时间: 2021-12-30

#include <iostream>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <string>
// write your code here......

using namespace std;

class Employee {

    private:
        string name;
        double salary;
    public:
        Employee(string name,double salary){
            this->name=name;
            this->salary=salary;
        }
        
        string getName(){
            return name;
        }
    
        double getSalary(){
            return salary;
        }
};

bool cmp(Employee& i,Employee& j){
        return i.getSalary()>j.getSalary();
    }


void print(Employee& g){
        double tax=0.0;
        double sshe=g.getSalary()-3500;
        
        if(sshe<0)
            tax=0.0;
        else if(sshe<=1500)
            tax=sshe*0.03;
        else if(sshe<=4500)
            tax=sshe*0.1-105;
        else if(sshe<=9000)
            tax=sshe*0.2-555;
        else if(sshe<=35000)
            tax=sshe*0.25-1005;
        else if(sshe<=55000)
            tax=sshe*0.3-2755;
        else if(sshe<=80000)
            tax=sshe*0.35-5505;
        else 
            tax=sshe*0.45-13505;
        
        cout<<fixed<<setprecision(1);
        cout<<g.getName()<<"应该缴纳的个人所得税是:"<<tax<<endl;
}
    
    // write your code here......


int main() {

    Employee g1("张三",6500);
    Employee g2("李四",8000);
    Employee g3("王五",100000);
    
    vector<Employee> v;
    v.push_back(g1);
    v.push_back(g2);
    v.push_back(g3);
    
    sort(v.begin(),v.end(),cmp);
    
    for_each(v.begin(),v.end(),print);
    // write your code here......
    

    return 0;
}

C++ 解法, 执行用时: 2ms, 内存消耗: 276KB, 提交时间: 2021-11-21

#include <iostream>
// write your code here.....
#include <string>
#include <vector>
#include <algorithm>
#include <iomanip>
using namespace std;
  
class Employee {
  
    private:
        string name;
        double salary;
    // write your code here......
    public:
    Employee(string nam,double sal)
    {
        name=nam;
        salary=sal;
    }
    double persal(double sal)
    {
        if((sal-3500)>80000)
            return (sal-3500)*0.45-13505;
        else if((sal-3500)>55000)
            return (sal-3500)*0.35-5505;
        else if((sal-3500)>35000)
            return (sal-3500)*0.3-2755;
        else if((sal-3500)>9000)
            return (sal-3500)*0.25-1005;
        else if((sal-3500)>4500)
            return (sal-3500)*0.2-555;
        else if((sal-3500)>1500)
            return (sal-3500)*0.1-105;
        else
            return (sal-3500)*0.03-0;
}
    friend void print(Employee &g);
      
     friend bool pre(const Employee &e,const Employee &f);
      
  
};
 bool pre(const Employee &e,const Employee &f)
 {
     return e.salary>f.salary;
 }
 void print(Employee &g)
    {
     cout<<fixed<<setprecision(1);
        cout<<g.name<<"应该缴纳的个人所得税是:"<<g.persal(g.salary)<<endl;
    }
  
int main() {
  
    // write your code here......
    Employee e1("张三",6500);
    Employee e2("李四",8000);
    Employee e3("王五",100000);
    vector<Employee> v;
    vector<Employee>::iterator its;
    v.push_back(e1);
    v.push_back(e2);
    v.push_back(e3);
    sort(v.begin(),v.end(),pre);
    for_each(v.begin(),  v.end(),print);
  
    return 0;
}

C++ 解法, 执行用时: 2ms, 内存消耗: 276KB, 提交时间: 2021-11-05

#include <iostream>
// write your code here......
#include<vector>
#include<algorithm>
using namespace std;
#include <iomanip>
class Employee {

    private:
        string name;
        double salary;
    // write your code here......
    public:
        Employee(string n,double sa):name(n),salary(sa) {}
        double getSalary()
        {
            return this->salary;
        }
        string getName()
        {
            return this->name;
        }

};
void print(Employee& e){
    //个人所得税
    double tax=0.0;
    //全月应纳税所得额
    double t=e.getSalary()-3500;
    //按表中所给的范围,分别进行计算
    if(t<=0){
        //小于0,不需要扣税
    }
    //其它情况,按所给公式及表中数据计算
    else if(t>0&&t<=1500){
        tax=t*0.03-0;
    }
    else if(t>1500&&t<=4500){
        tax=t*0.10-105;
    }
    else if(t>4500&&t<=9000){
        tax=t*0.20-555;
    }
    else if(t>9000&&t<=35000){
        tax=t*0.25-1005;
    }
    else if(t>35000&&t<=55000){
        tax=t*0.30-2755;
    }
    else if(t>55000&&t<=80000){
        tax=t*0.35-5505;
    }
    else{
        tax=t*0.45-13505;
    }
    //设置精度
    cout<<fixed<<setprecision(1);
    cout<<e.getName()<<"应该缴纳的个人所得税是:"<<tax<<endl;
}
bool comp(Employee& e1,Employee& e2)
{
    return e1.getSalary()>e2.getSalary();
}
int main() {

    // write your code here......
    Employee p1("张三",6500);
    Employee p2("李四",8000);
    Employee p3("王五",100000);
    vector<Employee>v{p1,p2,p3};
    sort(v.begin(),v.end(),comp);
    for_each(v.begin(), v.end(), print);
    
    return 0;
}

C++ 解法, 执行用时: 2ms, 内存消耗: 280KB, 提交时间: 2022-05-17

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

C++ 解法, 执行用时: 2ms, 内存消耗: 280KB, 提交时间: 2022-03-31

#include <iostream>
// write your code here......
#include <bits/stdc++.h>
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 cmp(Employee& e1, Employee& e2) {
    return e1.getSalary() > e2.getSalary();
}

void print(Employee& e) {
    double tax = 0.0;
    double t = e.getSalary() - 3500;
    if (t < 0) {}
    else if (t > 0 && t <= 1500) {
        tax = t * 0.03 - 0;
    } else if (t > 1500 && t <= 4500) {
        tax = t * 0.10 - 105;
    } else if (t > 4500 && t <= 9000) {
        tax = t * 0.20 - 555;
    } else if (t > 9000 && t <= 35000) {
        tax = t * 0.25 - 1005;
    } else if (t > 35000 && t <= 55000) {
        tax = t * 0.30 - 2755;
    } else if (t > 55000 && t <= 80000) {
        tax = t * 0.35 - 5505;
    } else {
        tax = t * 0.45 - 13505;
    }

    cout << fixed << setprecision(1);
    cout << e.getName() << "应该缴纳的个人所得税是:" << tax << endl;
}
int main() {

    // write your code here......
    Employee e1("张三", 6500);
    Employee e2("李四", 8000);
    Employee e3("王五", 100000);

    vector<Employee>vec;
    vec.push_back(e1);
    vec.push_back(e2);
    vec.push_back(e3);

    sort(vec.begin(), vec.end(), cmp);
    for_each(vec.begin(), vec.end(), print);
    return 0;
}

上一题