用struct(C语言)或class(C++语言)实现以下程序: 定义数据结构(struct或class)存储老师们的评教成绩(至少10人吧,不然这个学院太可怜了…),包括: (1)姓名:字符串 (2)课程名称:字符串数组(为什么是数组呢?因为会有多门课?) (3)课程评教成绩:整数(百分值:0-100)或字符(等级制:A-E)数组,成绩的顺序和课程名称顺序对应 完成以下函数 (1)计算年终奖:根据平均评教成绩确定今年的年终奖(公式自己定,别忘记写在设计文档里) (2)查询:输入姓名查询某名老师的所有评教成绩和今年的年终奖,警告那些年终奖为负数的老师(什么?你说年终奖不可能为负数?那是你的问题) PS. 鼓励用C++ class和成员函数,这样可以更好地理解现代程序的架构
时间: 2023-12-28 19:04:44 浏览: 75
以下是用C++ class实现以上程序的示例代码:
```cpp
#include <iostream>
#include <string>
using namespace std;
class Teacher {
public:
Teacher(string name, string* courses, int* scores, int num_courses) :
name(name), num_courses(num_courses) {
this->courses = new string[num_courses];
for (int i = 0; i < num_courses; i++) {
this->courses[i] = courses[i];
}
this->scores = new int[num_courses];
for (int i = 0; i < num_courses; i++) {
this->scores[i] = scores[i];
}
}
~Teacher() {
delete[] courses;
delete[] scores;
}
void print() {
cout << "姓名:" << name << endl;
cout << "课程名称及成绩:" << endl;
for (int i = 0; i < num_courses; i++) {
cout << courses[i] << ":" << scores[i] << endl;
}
cout << "年终奖:" << computeBonus() << endl;
}
int computeBonus() {
int sum = 0;
for (int i = 0; i < num_courses; i++) {
sum += scores[i];
}
int avg_score = sum / num_courses;
int bonus = 0;
if (avg_score >= 90) {
bonus = 10000;
} else if (avg_score >= 80) {
bonus = 8000;
} else if (avg_score >= 70) {
bonus = 5000;
} else if (avg_score >= 60) {
bonus = 3000;
} else {
bonus = 0;
}
return bonus;
}
private:
string name;
string* courses;
int* scores;
int num_courses;
};
int main() {
string courses[] = {"语文", "数学", "英语"};
int scores[] = {85, 90, 95};
Teacher t("张三", courses, scores, 3);
t.print();
return 0;
}
```
注:此代码仅为示例,实际应用中还需要根据实际情况进行修改和完善。
阅读全文