2.某学校教职工的工资计算方法为:
所有教职工都有基本工资。
·教师月工资等于固定工资+课时补贴,课时补贴根据职称和课时数计算。例如,每课时教授补贴 50元,副教授补贴 30 元,讲师补贴 20 元。
·管理人员月薪等于基本工资+职务工资。
·实验室人员月薪等于基本工资+工作日补贴,工作日补贴等于日补贴x月工作日数。
定义教职工抽象类,派生教师类、管理人员类和实验室人员类,编写程序测试这个类体系。
3.使用第2题中定义的教职工类体系,编写程序,输入某月各种职称教师的工资信息,建立异质链表输出每位教师的工资条,统计当月的总工资、平均工资、最高工资和最低工资。
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
class Faculty
{
public:
Faculty* next;
Faculty() :Name("0"), Age(0), Base_Salary(0), next(nullptr)
{
}
virtual void Set_Pay() = 0;
virtual double Calculate_Pay() = 0;
virtual void Print_Pay() = 0;
protected:
string Name;
int Age;
double Base_Salary;
};
class Teacher :public Faculty
{
public:
Teacher()
{
Title_Allowance = 0;
Lesson_Hours = 0;
Lesson_Hours_Allowance = 0;
job_Title = "0";
}
virtual void Set_Pay()
{
string name;
int age;
int option;
double base_salary;
double lesson_hours;
cout << endl << "录入新教师" << endl;
cout << "输入姓名:" << endl;
cin >> name;
Name = name;
cout << "输入年龄:" << endl;
cin >> age;
Age = age;
cout << "选择教师职称(1.教授,2.副教授,3.讲师)" << endl;
cin >> option;
if (option == 1)
{
Title_Allowance = 50;
job_Title = "教授";
}
else if (option == 2)
{
Title_Allowance = 30;
job_Title = "副教授";
}
else if (option == 3)
{
Title_Allowance = 20;
job_Title = "讲师";
}
else
{
cout << "error!" << endl;
exit(-1);
}
cout << "输入基本工资:" << endl;
cin >> base_salary;
Base_Salary = base_salary;
cout << "输入课时数:" << endl;
cin >> lesson_hours;
Lesson_Hours = lesson_hours;
Lesson_Hours_Allowance = Lesson_Hours * Title_Allowance;
}
virtual double Calculate_Pay()
{
double pay = 0;
pay = Base_Salary + Lesson_Hours_Allowance;
return pay;
}
virtual void Print_Pay()
{
cout << endl;
cout << "教师" << endl;
cout << "职称:" << job_Title << endl;
cout << "姓名:" << Name << endl;
cout << "年龄:" << Age << endl;
cout << "基本工资:" << Base_Salary << endl;
cout << "课时补贴:" << Lesson_Hours_Allowance << endl;
cout << "实发工资:" << Calculate_Pay() << endl;
}
protected:
int Title_Allowance;
double Lesson_Hours;
double Lesson_Hours_Allowance;
string job_Title;
};
class Manager :public Faculty
{
public:
Manager()
{
Job_Salary = 0;
}
virtual void Set_Pay()
{
cout << endl << "录入新管理人员" << endl;
string name;
int age;
double base_salary;
double job_salary;
cout << "输入姓名:" << endl;
cin >> name;
Name = name;
cout << "输入年龄:" << endl;
cin >> age;
Age = age;
cout << "输入基本工资:" << endl;
cin >> base_salary;
Base_Salary = base_salary;
cout << "输入职务工资:" << endl;
cin >> job_salary;
Job_Salary = job_salary;
}
virtual double Calculate_Pay()
{
double pay = 0;
pay = Base_Salary + Job_Salary;
return pay;
}
virtual void Print_Pay()
{
cout << endl;
cout << "管理人员" << endl;
cout << "姓名:" << Name << endl;
cout << "年龄:" << Age << endl;
cout << "基本工资:" << Base_Salary << endl;
cout << "职务工资:" << Job_Salary << endl;
cout << "实发工资:" << Calculate_Pay() << endl;
}
protected:
double Job_Salary;
};
class Laborator :public Faculty
{
public:
Laborator()
{
Day_Allowance = 0;
Month_Working_Day = 0;
Workday_Allowance = 0;
}
virtual void Set_Pay()
{
cout << endl << "录入新实验室人员" << endl;
string name;
int age;
double base_salary;
double day_allowance;
int month_working_day;
cout << "输入姓名:" << endl;
cin >> name;
Name = name;
cout << "输入年龄:" << endl;
cin >> age;
Age = age;
cout << "输入基本工资:" << endl;
cin >> base_salary;
Base_Salary = base_salary;
cout << "输入工作日补贴:" << endl;
cin >> day_allowance;
Day_Allowance = day_allowance;
cout << "输入月工作日数:" << endl;
cin >> month_working_day;
Month_Working_Day = month_working_day;
Workday_Allowance = Day_Allowance * Month_Working_Day;
}
virtual double Calculate_Pay()
{
double pay = 0;
pay = Base_Salary + Day_Allowance * Month_Working_Day;
return pay;
}
virtual void Print_Pay()
{
cout << endl;
cout << "实验室人员" << endl;
cout << "姓名:" << Name << endl;
cout << "年龄:" << Age << endl;
cout << "基本工资:" << Base_Salary << endl;
cout << "工作日补贴:" << Workday_Allowance << endl;
cout << "实发工资:" << Calculate_Pay() << endl;
}
protected:
double Day_Allowance;
int Month_Working_Day;
double Workday_Allowance;
};
void InsertNext(Faculty*& head, Faculty*& n)
{
Faculty* p = head, * q = head->next;
while (q)
{
p = q;
q = q->next;
}
p->next = n;
}
void Traverse_Linkedlist(Faculty*& first)
{
Faculty* q = first;
while (q)
{
q->Print_Pay();
cout << endl;
q = q->next;
}
}
void Monthly_Salary_Data(Faculty*& first)
{
Faculty* qallpay = first;
double Allpay = 0;
while (qallpay)
{
Allpay += qallpay->Calculate_Pay();
qallpay = qallpay->next;
}
cout << "当月总工资:" << Allpay << endl;
Faculty* average = first;
Allpay = 0;
int count = 0;
while (average)
{
Allpay += average->Calculate_Pay();
count++;
average = average->next;
}
cout << "当月平均工资:" << Allpay / count << endl;
Faculty* higest = first;
vector<double> v;
while (higest)
{
v.push_back(higest->Calculate_Pay());
higest = higest->next;
}
vector<double>::iterator biggest = max_element(begin(v), end(v));
cout << "当月最高工资:" << *biggest << endl;
vector<double>::iterator smallest = min_element(begin(v), end(v));
cout << "当月最低工资:" << *smallest << endl;
}
void Delete_Linkedlist(Faculty*& head)
{
Faculty* p = head, * q = head->next;
while (q)
{
delete p;
p = q;
q = q->next;
}
delete p;
}
int main()
{
Faculty* fac;
Faculty* nfac;
fac = new Teacher();
fac->Set_Pay();
fac->Calculate_Pay();
nfac = new Teacher();
nfac->Set_Pay();
nfac->Calculate_Pay();
InsertNext(fac, nfac);
nfac = new Teacher();
nfac->Set_Pay();
nfac->Calculate_Pay();
InsertNext(fac, nfac);
nfac = new Manager();
nfac->Set_Pay();
nfac->Calculate_Pay();
InsertNext(fac, nfac);
nfac = new Laborator();
nfac->Set_Pay();
nfac->Calculate_Pay();
InsertNext(fac, nfac);
Traverse_Linkedlist(fac);
Monthly_Salary_Data(fac);
Delete_Linkedlist(fac);
}
杂谈:还没系统学习链表,链表的建立不够规范