用C++写定义一个动态数组类用于存放学生信息(学号、姓名、高数成绩、英语成绩),实现学生成绩的管理。实现如下功能:程序运行后,提示使用者输入学生的个数,根据学生的个数动态的创建数组。然后逐个输入每个学生的学号、高数成绩、英语成绩。录入时,已经录入的学号不允许再次录入。成绩范围在0-100分,如超出,则为无效。完成录入后,将成绩以列表的形式输出,统计并打印出每门课程的最高成绩、最低成绩、平均成绩。之后提示可进行成绩查询,输入学号,如果学号正确,输出该学生的成绩。如果学号不正确,提示重新输入。成绩列表示例学号 姓名 高数成绩 英语成绩 ---------------------------------------------------- 20140823 张三 98 87 20140824 李四 78 99 … ----------------------------------------------------
时间: 2023-08-15 13:13:46 浏览: 148
好的,我来帮您解答这个问题。以下是动态数组类的实现代码:
```cpp
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Student {
public:
string id;
string name;
int mathScore;
int engScore;
};
class StudentManager {
public:
void createStudents() {
int num;
cout << "请输入学生人数:";
cin >> num;
students.resize(num);
for (int i = 0; i < num; i++) {
string id;
cout << "请输入学生学号:";
cin >> id;
for (int j = 0; j < i; j++) {
if (students[j].id == id) {
cout << "学号已存在,请重新输入:" << endl;
cin >> id;
j = -1;
}
}
students[i].id = id;
cout << "请输入学生姓名:";
cin >> students[i].name;
cout << "请输入学生高数成绩:";
cin >> students[i].mathScore;
if (students[i].mathScore < 0 || students[i].mathScore > 100) {
students[i].mathScore = -1;
}
cout << "请输入学生英语成绩:";
cin >> students[i].engScore;
if (students[i].engScore < 0 || students[i].engScore > 100) {
students[i].engScore = -1;
}
}
}
void showStudents() {
cout << "学号\t姓名\t高数成绩\t英语成绩" << endl;
cout << "---------------------------------------" << endl;
for (int i = 0; i < students.size(); i++) {
cout << students[i].id << "\t" << students[i].name << "\t";
if (students[i].mathScore == -1) {
cout << "无效\t";
} else {
cout << students[i].mathScore << "\t";
}
if (students[i].engScore == -1) {
cout << "无效\t";
} else {
cout << students[i].engScore << "\t";
}
cout << endl;
}
cout << "---------------------------------------" << endl;
}
void showStatistics() {
int mathSum = 0, engSum = 0, mathMax = 0, engMax = 0, mathMin = 100, engMin = 100;
double mathAvg = 0, engAvg = 0;
int num = students.size();
for (int i = 0; i < num; i++) {
if (students[i].mathScore != -1) {
mathSum += students[i].mathScore;
if (students[i].mathScore > mathMax) {
mathMax = students[i].mathScore;
}
if (students[i].mathScore < mathMin) {
mathMin = students[i].mathScore;
}
}
if (students[i].engScore != -1) {
engSum += students[i].engScore;
if (students[i].engScore > engMax) {
engMax = students[i].engScore;
}
if (students[i].engScore < engMin) {
engMin = students[i].engScore;
}
}
}
mathAvg = mathSum * 1.0 / (num * 1.0);
engAvg = engSum * 1.0 / (num * 1.0);
cout << "高数成绩:最高分:" << mathMax << ",最低分:" << mathMin << ",平均分:" << mathAvg << endl;
cout << "英语成绩:最高分:" << engMax << ",最低分:" << engMin << ",平均分:" << engAvg << endl;
}
void searchStudent() {
string id;
cout << "请输入要查询的学生学号:";
cin >> id;
for (int i = 0; i < students.size(); i++) {
if (students[i].id == id) {
cout << "学号\t姓名\t高数成绩\t英语成绩" << endl;
cout << "---------------------------------------" << endl;
cout << students[i].id << "\t" << students[i].name << "\t";
if (students[i].mathScore == -1) {
cout << "无效\t";
} else {
cout << students[i].mathScore << "\t";
}
if (students[i].engScore == -1) {
cout << "无效\t";
} else {
cout << students[i].engScore << "\t";
}
cout << endl;
cout << "---------------------------------------" << endl;
return;
}
}
cout << "学号不存在,请重新输入:" << endl;
searchStudent();
}
private:
vector<Student> students;
};
int main() {
StudentManager sm;
sm.createStudents();
sm.showStudents();
sm.showStatistics();
sm.searchStudent();
return 0;
}
```
上述代码实现了动态数组的创建,学生信息的录入,成绩列表的输出,成绩统计信息的输出,以及学生信息查询功能。在录入学生信息时,如果输入的学号已经存在,程序会提示重新输入。在录入学生成绩时,如果成绩不在0-100分之间,程序会将该成绩视为无效。在输出成绩列表时,无效成绩会显示为“无效”。在统计成绩信息时,无效成绩不会计入平均分和最高、最低分的计算。在查询学生信息时,如果输入的学号不存在,程序会提示重新输入。
阅读全文