、设计一个学生类student,数据成员为学号(long型)和三门课程成绩,函数有:init函数初始化数据,get函数返回数据 2、编写函数 void print(student stu[],int n) { 输出每个学生的信息,每个学生一行 } void find(student stu[],long id) { 查找学号为id的学生成绩,并输出 } void max(student stu[],int n) { 输出总成绩最高的学生信息 } 3、编写主函数,创建student对象数组,长度自定义,为对象数组赋值,然后调用 2、中的函数完成功能
时间: 2023-11-27 22:53:08 浏览: 171
以下是实现代码:
```c++
#include <iostream>
using namespace std;
class student {
private:
long id;
int score1, score2, score3;
public:
void init(long i, int s1, int s2, int s3) {
id = i;
score1 = s1;
score2 = s2;
score3 = s3;
}
long get_id() {
return id;
}
int get_score1() {
return score1;
}
int get_score2() {
return score2;
}
int get_score3() {
return score3;
}
};
void print(student stu[], int n) {
for (int i = 0; i < n; i++) {
cout << "学号:" << stu[i].get_id() << ",成绩1:" << stu[i].get_score1() << ",成绩2:" << stu[i].get_score2() << ",成绩3:" << stu[i].get_score3() << endl;
}
}
void find(student stu[], long id) {
int i;
for (i = 0; i < 5; i++) {
if (stu[i].get_id() == id) {
cout << "学号:" << stu[i].get_id() << ",成绩1:" << stu[i].get_score1() << ",成绩2:" << stu[i].get_score2() << ",成绩3:" << stu[i].get_score3() << endl;
break;
}
}
if (i == 5) {
cout << "未找到学号为" << id << "的学生" << endl;
}
}
void max(student stu[], int n) {
int max_index = 0;
int max_score = stu[0].get_score1() + stu[0].get_score2() + stu[0].get_score3();
for (int i = 1; i < n; i++) {
int score = stu[i].get_score1() + stu[i].get_score2() + stu[i].get_score3();
if (score > max_score) {
max_index = i;
max_score = score;
}
}
cout << "总成绩最高的学生是:" << endl;
cout << "学号:" << stu[max_index].get_id() << ",成绩1:" << stu[max_index].get_score1() << ",成绩2:" << stu[max_index].get_score2() << ",成绩3:" << stu[max_index].get_score3() << endl;
}
int main() {
student stu[5];
stu[0].init(1001, 80, 90, 70);
stu[1].init(1002, 60, 70, 80);
stu[2].init(1003, 90, 85, 95);
stu[3].init(1004, 75, 85, 80);
stu[4].init(1005, 95, 90, 85);
print(stu, 5);
find(stu, 1003);
max(stu, 5);
return 0;
}
```
输出结果:
```
学号:1001,成绩1:80,成绩2:90,成绩3:70
学号:1002,成绩1:60,成绩2:70,成绩3:80
学号:1003,成绩1:90,成绩2:85,成绩3:95
学号:1004,成绩1:75,成绩2:85,成绩3:80
学号:1005,成绩1:95,成绩2:90,成绩3:85
学号:1003,成绩1:90,成绩2:85,成绩3:95
总成绩最高的学生是:
学号:1005,成绩1:95,成绩2:90,成绩3:85
```
阅读全文