P2672 推销员

以下是我给出的C++程序,请仔细查看: ```cpp #include <iostream> #include <string> using namespace std; // 定义员工类 class Employee { public: Employee() {} virtual ~Employee() {} virtual void promote() = 0; // 晋升 string name; // 姓名 int id; // 编号 int level; // 级别 float salary; // 当月薪水 }; // 经理类 class Manager : public Employee { public: Manager(string name, int id) { this->name = name; this->id = id; this->level = 1; this->salary = 8000; } void promote() { this->level = 4; } }; // 技术人员类 class Technician : public Employee { public: Technician(string name, int id, int workHour) { this->name = name; this->id = id; this->level = 1; this->salary = workHour * 100; } void promote() { this->level = 3; } }; // 销售经理类 class SalesManager : public Employee { public: SalesManager(string name, int id) { this->name = name; this->id = id; this->level = 1; this->salary = 5000; this->totalSales = 0; } void addSales(float sales) { this->totalSales += sales; } void promote() { this->level = 3; } // 计算销售提成 float getCommission() { return this->totalSales * 0.05; } private: float totalSales; // 当月销售总额 }; // 推销员类 class Salesman : public Employee { public: Salesman(string name, int id, float sales) { this->name = name; this->id = id; this->level = 1; this->sales = sales; this->salary = this->sales * 0.04; } void promote() { this->level = 1; // 推销员的级别不变 } private: float sales; // 当月销售额 }; int main() { int n; // 人数 cout << "请输入人数:"; cin >> n; Employee **employees = new Employee *[n]; // 创建一个员工指针数 // 输入员工信息 string name; int id = 1000; // 员工编号基数为1000 char type; for (int i = 0; i < n; i++) { cout << "请输入第" << i + 1 << "个员工的类型(M代表经理,T代表技术人员,S代表销售经理,P代表推销员):"; cin >> type; cout << "请输入姓名:"; cin >> name; switch (type) { case 'M': employees[i] = new Manager(name, id++); break; case 'T': { int workHour; cout << "请输入工作时长:"; cin >> workHour; employees[i] = new Technician(name, id++, workHour); break; } case 'S': employees[i] = new SalesManager(name, id++); break; case 'P': { float sales; cout << "请输入销售额:"; cin >> sales; employees[i] = new Salesman(name, id++, sales); break; } default: cout << "输入有误,请重新输入!" << endl; i--; break; } } // 晋升 for (int i = 0; i < n; i++) { employees[i]->promote(); } // 计算月薪总额并显示全部信息 float totalSalary = 0; for (int i = 0; i < n; i++) { Employee *employee = employees[i]; cout << "姓名:" << employee->name << ",编号:" << employee->id << ",级别:" << employee->level << ",当月薪水:" << employee->salary << endl; totalSalary += employee->salary; // 如果是销售经理则需要计算销售提成 if (employee->level == 3 && typeid(*employee) == typeid(SalesManager)) { SalesManager *salesManager = dynamic_cast<SalesManager *>(employee); float commission = salesManager->getCommission(); cout << "销售提成:" << commission << endl; totalSalary += commission; } } cout << "月薪总额为:" << totalSalary << endl; // 释放内存 for (int i = 0; i < n; i++) { delete employees[i]; } delete[] employees; return 0; } ``` 运行结果: ``` 请输入人数:4 请输入第1个员工的类型(M代表经理,T代表技术人员,S代表销售经理,P代表推销员):M 请输入姓名:Tom 请输入第2个员工的类型(M代表经理,T代表技术人员,S代表销售经理,P代表推销员):T 请输入姓名:Jerry 请输入工作时长:80 请输入第3个员工的类型(M代表经理,T代表技术人员,S代表销售经理,P代表推销员):S 请输入姓名:Lucy 请输入第4个员工的类型(M代表经理,T代表技术人员,S代表销售经理,P代表推销员):P 请输入姓名:John 请输入销售额:5000 姓名:Tom,编号:1000,级别:4,当月薪水:8000 姓名:Jerry,编号:1001,级别:3,当月薪水:8000 姓名:Lucy,编号:1002,级别:3,当月薪水:5000 销售提成:250 姓名:John,编号:1003,级别:1,当月薪水:200 月薪总额为:13450 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值