编写C++程序:
设计一个学生类student,
具有的私有数据成员是:注册号、姓名、数学成绩、英语成绩、计算机成绩;
具有的公有成员函数是:求三门课总成绩的函数sum();求三门课平均成绩的函数average();显示学生数据信息的函数print();
获取学生注册号的函数get reg num();设置学生数据信息的函数set stu_inf()。
编制主函数,说明一个student类对象的数组并进行全班学生信息的输入与设置,而后求出每一学生的总成绩、平均成绩、全班学生总成绩最高分、全班学生总平均分,并在输入一个注册号后,输出该学生有关的全部数据信息。
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
int regNum;//注册号
string name;//姓名
int mathScore;//数学成绩
int englishScore;//英语成绩
int computerScore;//计算机成绩
public:
//设置学生数据信息
void setStuInf(int regNum, string name, int mathScore, int englishScore, int computerScore) {
this->regNum = regNum;
this->name = name;
this->mathScore = mathScore;
this->englishScore = englishScore;
this->computerScore = computerScore;
}
//获取学生注册号
int getRegNum() {
return regNum;
}
//求三门课总成绩
int sum() {
return mathScore + englishScore + computerScore;
}
//求三门课平均成绩
double average() {
return static_cast<double>(sum()) / 3;
}
//显示学生数据信息
void print() {
cout << "注册号: " << regNum << endl;
cout << "姓名: " << name << endl;
cout << "数学成绩: " << mathScore << endl;
cout << "英语成绩: " << englishScore << endl;
cout << "计算机成绩: " << computerScore << endl;
}
};
int main() {
const int numStudents = 3; // 假设有3个学生
Student students[numStudents];
// 设置学生信息
for (int i = 0; i < numStudents; i++) {
int regNum, mathScore, englishScore, computerScore;
string name;
cout << "请输入第 " << i + 1 << " 个学生的注册号: ";
cin >> regNum;
cout << "请输入第 " << i + 1 << " 个学生的姓名: ";
cin >> name;
cout << "请输入第 " << i + 1 << " 个学生的数学成绩: ";
cin >> mathScore;
cout << "请输入第 " << i + 1 << " 个学生的英语成绩: ";
cin >> englishScore;
cout << "请输入第 " << i + 1 << " 个学生的计算机成绩: ";
cin >> computerScore;
students[i].setStuInf(regNum, name, mathScore, englishScore, computerScore);
}
// 计算全班学生总成绩和平均成绩
int totalScore = 0;//总成绩
double averageScore = 0.0;//平均成绩
int highestScore = 0;//最高分
for (int i = 0; i < numStudents; i++) {
totalScore += students[i].sum();
averageScore += students[i].average();
if (students[i].sum() > highestScore) {
highestScore = students[i].sum();
}
}
averageScore /= numStudents;
// 输出全班学生总成绩和平均成绩
cout << "全班学生总成绩最高分: " << highestScore << endl;
cout << "全班学生平均成绩: " << averageScore << endl;
// 输入一个注册号,输出该学生的全部数据信息
int searchRegNum;
cout << "请输入要查询的学生的注册号: ";
cin >> searchRegNum;
for (int i = 0; i < numStudents; i++) {
if (students[i].getRegNum() == searchRegNum) {
students[i].print();
break;
}
}
return 0;
}