用c++写一个代码计算教师工资,工资包括固定工资和课时补贴,教授每课时五十元,副教授每课时三十元。一级教授固定工资八千元,二级教授固定工资七千五,副教授一级工资七千,二级六千
时间: 2023-06-04 17:03:58 浏览: 96
元,编写程序计算一个教师的工资。
以下是C语言代码:
#include <stdio.h>
int main() {
int level, hours;
float salary;
printf("请输入教师级别(1表示一级教授,2表示二级教授,3表示副教授):");
scanf("%d", &level);
printf("请输入教师课时数:");
scanf("%d", &hours);
switch (level) {
case 1:
salary = 8000 + hours * 50;
break;
case 2:
salary = 7500 + hours * 50;
break;
case 3:
salary = 7000 + hours * 30;
break;
default:
printf("输入有误!");
return 0;
}
printf("该教师的工资为:%.2f元\n", salary);
return 0;
}
在运行程序时,输入教师级别和课时数,程序会自动计算并输出教师的工资。如果输入的级别不是1、2、3中的任意一个,程序会提示“输入有误!”并退出。
相关问题
题目描述:某学校对教师每个月工资的计算规定如下:固定工资加课时补贴,教授的固定工资为5000元,每个课时补贴为50元;副教授的固定工资为3000元,每个课时补贴为30元;讲师的固定工资为2000元,每个课时补贴为20元。定义教师抽象类,派生教授、副教授和讲师类,编写程序求若干个教师的月工资。用C++
以下是实现该题目的示例代码:
```c++
#include <iostream>
using namespace std;
// 教师抽象类
class Teacher {
public:
virtual void calculateSalary() = 0;
virtual ~Teacher() {}
protected:
double fixedSalary; // 固定工资
double hourCost; // 每个课时补贴
int hourCount; // 课时数
};
// 教授类
class Professor : public Teacher {
public:
Professor(int hourCount) {
fixedSalary = 5000;
hourCost = 50;
this->hourCount = hourCount;
}
void calculateSalary() {
double salary = fixedSalary + hourCost * hourCount;
cout << "教授的月工资为:" << salary << "元" << endl;
}
};
// 副教授类
class AssociateProfessor : public Teacher {
public:
AssociateProfessor(int hourCount) {
fixedSalary = 3000;
hourCost = 30;
this->hourCount = hourCount;
}
void calculateSalary() {
double salary = fixedSalary + hourCost * hourCount;
cout << "副教授的月工资为:" << salary << "元" << endl;
}
};
// 讲师类
class Lecturer : public Teacher {
public:
Lecturer(int hourCount) {
fixedSalary = 2000;
hourCost = 20;
this->hourCount = hourCount;
}
void calculateSalary() {
double salary = fixedSalary + hourCost * hourCount;
cout << "讲师的月工资为:" << salary << "元" << endl;
}
};
int main() {
// 创建教授对象,计算月工资
Professor p(80);
p.calculateSalary();
// 创建副教授对象,计算月工资
AssociateProfessor ap(60);
ap.calculateSalary();
// 创建讲师对象,计算月工资
Lecturer l(40);
l.calculateSalary();
return 0;
}
```
输出结果如下:
```
教授的月工资为:9000元
副教授的月工资为:4800元
讲师的月工资为:3000元
```
某学校对教师每月工资的计算公式如下:固定工资+课时补贴。教授的固定工资为5000元,每个课时补贴50元;副教授的固定工资为3000元, 每个课时补贴30元;讲师的固定工资为2000元,每个课时补贴20元。定义教师抽象类,派生不同职称的教师类,编写程序求若干教师的月工资。c++
好的,以下是用 C++ 实现该程序的代码示例:
```cpp
#include <iostream>
using namespace std;
// 定义教师抽象类
class Teacher {
public:
virtual double get_salary(int course_num) = 0;
};
// 定义不同职称的教师类
class Professor : public Teacher {
public:
Professor(double fixed_salary, double course_salary) {
this->fixed_salary = fixed_salary;
this->course_salary = course_salary;
}
virtual double get_salary(int course_num) {
return this->fixed_salary + this->course_salary * course_num;
}
private:
double fixed_salary;
double course_salary;
};
class AssociateProfessor : public Teacher {
public:
AssociateProfessor(double fixed_salary, double course_salary) {
this->fixed_salary = fixed_salary;
this->course_salary = course_salary;
}
virtual double get_salary(int course_num) {
return this->fixed_salary + this->course_salary * course_num;
}
private:
double fixed_salary;
double course_salary;
};
class Lecturer : public Teacher {
public:
Lecturer(double fixed_salary, double course_salary) {
this->fixed_salary = fixed_salary;
this->course_salary = course_salary;
}
virtual double get_salary(int course_num) {
return this->fixed_salary + this->course_salary * course_num;
}
private:
double fixed_salary;
double course_salary;
};
// 计算若干教师的月工资
int main() {
Teacher* teacher_list[5] = {
new Professor(5000, 50),
new AssociateProfessor(3000, 30),
new Lecturer(2000, 20),
new Professor(5000, 50),
new Lecturer(2000, 20)
};
for (int i = 0; i < 5; i++) {
int course_num;
cout << "请输入" << typeid(*teacher_list[i]).name() << "授课的课时数:";
cin >> course_num;
double salary = teacher_list[i]->get_salary(course_num);
cout << typeid(*teacher_list[i]).name() << "的月工资为:" << salary << "元" << endl;
}
return 0;
}
```
程序运行结果示例:
```
请输入class Professor的月工资为:50授课的课时数:20
class Professor的月工资为:6000元
请输入class AssociateProfessor的月工资为:30授课的课时数:15
class AssociateProfessor的月工资为:4500元
请输入class Lecturer的月工资为:20授课的课时数:10
class Lecturer的月工资为:2200元
请输入class Professor的月工资为:50授课的课时数:30
class Professor的月工资为:6500元
请输入class Lecturer的月工资为:20授课的课时数:12
class Lecturer的月工资为:2440元
```
阅读全文