用struct(C语言)或class(C++语言)实现以下程序: 定义数据结构(struct或class)存储:至少10人的老师姓名(字符串),各个老师的课程名称(字符串数组,每个老师至少两门课程),各个老师的各门课程评教成绩:整数(百分值:0-100)数组,成绩的顺序和课程名称顺序对应 计算年终奖:年终奖=课程评教成绩均值*10-600 输入老师姓名(scanf),查询某名老师的所有评教成绩和今年的年终奖,警告那些年终奖为负数的老师
时间: 2023-12-30 17:05:35 浏览: 81
以下是基于C++语言的实现,使用class定义数据结构:
```cpp
#include <iostream>
#include <string>
using namespace std;
class Teacher {
public:
string name; //老师姓名
string courses[2]; //老师的两门课程
int scores[2]; //老师的两门课程评教成绩
float bonus; //年终奖
Teacher(string n, string c1, int s1, string c2, int s2) {
name = n;
courses[0] = c1;
scores[0] = s1;
courses[1] = c2;
scores[1] = s2;
bonus = calculateBonus();
}
float calculateBonus() {
float meanScore = (scores[0] + scores[1]) / 2.0; //计算两门课程平均成绩
float bonus = meanScore * 10 - 600; //计算年终奖
if (bonus < 0) { //判断年终奖是否为负数
cout << "Warning: " << name << "'s bonus is negative." << endl;
}
return bonus;
}
void printScores() {
cout << name << "'s scores:" << endl;
cout << courses[0] << ": " << scores[0] << endl;
cout << courses[1] << ": " << scores[1] << endl;
}
};
int main() {
Teacher teachers[10] = {
Teacher("teacher1", "course1", 80, "course2", 90),
Teacher("teacher2", "course1", 85, "course2", 95),
Teacher("teacher3", "course1", 90, "course2", 92),
Teacher("teacher4", "course1", 78, "course2", 85),
Teacher("teacher5", "course1", 92, "course2", 88),
Teacher("teacher6", "course1", 87, "course2", 93),
Teacher("teacher7", "course1", 91, "course2", 89),
Teacher("teacher8", "course1", 83, "course2", 82),
Teacher("teacher9", "course1", 79, "course2", 87),
Teacher("teacher10", "course1", 88, "course2", 90)
};
string name;
cout << "Enter teacher name: ";
cin >> name;
for (int i = 0; i < 10; i++) {
if (teachers[i].name == name) {
teachers[i].printScores();
cout << "Bonus: " << teachers[i].bonus << endl;
break;
}
}
return 0;
}
```
在main函数中,我们定义了一个Teacher类型的数组,存储了10个老师的数据。然后,用户输入老师姓名,程序遍历数组,找到对应老师的数据,打印评教成绩和年终奖。如果年终奖为负数,会输出警告信息。
阅读全文