1、设计一个学生类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、中的函数完成功能
以下是根据您的要求设计的C++代码:
cpp
Copy code
class Student {
private:
long studentId;
double score1;
double score2;
double score3;
public:
// 构造函数初始化数据
Student(long id, double s1, double s2, double s3) {
studentId = id;
score1 = s1;
score2 = s2;
score3 = s3;
}
// 返回学生数据
long getStudentId() {
return studentId;
}
double getScore1() {
return score1;
}
double getScore2() {
return score2;
}
double getScore3() {
return score3;
}
};
void print(Student stu[], int n) {
for (int i = 0; i < n; i++) {
std::cout << "学号: " << stu[i].getStudentId() << std::endl;
std::cout << "成绩1: " << stu[i].getScore1() << std::endl;
std::cout << "成绩2: " << stu[i].getScore2() << std::endl;
std::cout << "成绩3: " << stu[i].getScore3() << std::endl;
std::cout << std::endl;
}
}
void find(Student stu[], long id, int n) {
for (int i = 0; i < n; i++) {
if (stu[i].getStudentId() == id) {
std::cout << "学号: " << stu[i].getStudentId() << std::endl;
std::cout << "成绩1: " << stu[i].getScore1() << std::endl;
std::cout << "成绩2: " << stu[i].getScore2() << std::endl;
std::cout << "成绩3: " << stu[i].getScore3() << std::endl;
return;
}
}
std::cout << "未找到学号为" << id << "的学生" << std::endl;
}
void max(Student stu[], int n) {
double maxTotalScore = 0.0;
int maxIndex = 0;
for (int i = 0; i < n; i++) {
double totalScore = stu[i].getScore1() + stu[i].getScore2() + stu[i].getScore3();
if (totalScore > maxTotalScore) {
maxTotalScore = totalScore;
maxIndex = i;
}
}
std::cout << "总成绩最高的学生信息:" << std::endl;
std::cout << "学号: " << stu[maxIndex].getStudentId() << std::endl;
std::cout << "成绩1: " << stu[maxIndex].getScore1() << std::endl;
std::cout << "成绩2: " << stu[maxIndex].getScore2() << std::endl;
std::cout << "成绩3: " << stu[maxIndex].getScore3() << std::endl;
}
int main() {
// 创建student对象数组并赋值
const int numStudents = 3;
Student students[numStudents] = {
Student(1001, 85.5, 90.0, 78.5),
Student(1002, 92.0, 87.5, 80.0),
Student(1003, 78.5, 88.0, 95.5)
};
// 调用函数进行功能操作
print(students, numStudents);
find(students, 1002, numStudents);
max(students, numStudents);
return 0;
}
这段代码定义了一个学生类Student,其中包含学号和三门课程成绩作为数据成员,并实现了相应的初始化函数和获取数据的函数。然后,根据题目要求,实现了print函数用于输出每个学生的信息,find函数用于查找指定学号的学生成绩并输出,以及max函数用于输出总成绩最高的学生信息。在主函数中,创建了一个Student对象数组,并为对象数组赋值,然后调用上述函数来完成功能。
public:
student() {}
student(long id, double score1, double score2, double score3) :
id_(id), score1_(score1), score2_(score2), score3_(score3) {}
void init(long id, double score1, double score2, double score3);
void get();
private:
long id_;
double score1_;
double score2_;
double score3_;
};
void student::init(long id, double score1, double score2, double score3)
{
id_ = id;
score1_ = score1;
score2_ = score2;
score3_ = score3;
}
void student::get()
{
cout << "学号:" << id_ << endl;
cout << "成绩1:" << score1_ << endl;
cout << "成绩2:" << score2_ << endl;
cout << "成绩3:" << score3_ << endl;
}
void print(student stu[], int n)
{
for (int i = 0; i < n; i++) {
stu[i].get();
}
}
void find(student stu[], long id)
{
for (int i = 0; i < n; i++) {
if (stu[i].id_ == id) {
cout << "学号为" << id << "的学生成绩为:" << stu[i].score1_ << " " << stu[i].score2_ << " " << stu[i].score3_ << endl;
return;
}
}
cout << "未找到学号为" << id << "的学生" << endl;
}
void max(student stu[], int n)
{
stude
for (int i = 0; i < n; i++) { // 遍历每个学生,找出总成绩最高的学生。
if (stu[i].score1_ + stu[i].score2_ + stu[i].score3_ > max_stu.score1_ + max_stu.score2_ + max_stu.score3_) { // 如果当前学生总成绩比max_stu的总成绩高,则更新max_stu的学号和三门课成绩。
max_stu = stu[i];
}
}
cout << "总成绩最高的学生信息为:" << endl; // 输出最高分数学生信息。
max_stu.get();
}
谢谢你很受启发