c++完整代码实现学生课程系统 题目描述:一个简单的学生课程系统。功 能要求: (1)创建一个学生类,包含学号,姓名,性别。(2)创建一个课程成绩类,包含课程名称和成绩。 3)创建一个学生课程成绩类,包含学生类成员和课程类数组成员。(4)可以生成学生,并且为学生创建课程成绩。 (5)输出指定科目分数最高和最低的学生课程成绩信息。(6)输出指定学生的科目分数最高和最低分
时间: 2024-03-12 13:44:35 浏览: 48
以下是实现学生课程系统的完整 C++ 代码:
```c++
#include <iostream>
#include <string>
using namespace std;
const int MAX_COURSE_NUM = 3; // 最多选修 3 门课程
class Student { // 学生类
public:
Student(string id, string name, char gender) {
this->id = id;
this->name = name;
this->gender = gender;
for (int i = 0; i < MAX_COURSE_NUM; i++) {
courseScores[i] = nullptr; // 初始化为 nullptr
}
}
string getId() const { return id; }
string getName() const { return name; }
char getGender() const { return gender; }
void addCourseScore(int index, string name, int score) { // 添加一门课程成绩
if (index < 0 || index >= MAX_COURSE_NUM) {
cout << "无效的课程编号" << endl;
return;
}
if (courseScores[index] != nullptr) { // 如果已经添加过该课程成绩,则更新成绩
courseScores[index]->score = score;
} else { // 否则创建新的课程成绩对象
courseScores[index] = new CourseScore(name, score);
}
}
int getCourseScore(int index) const { // 获取指定课程编号的成绩
if (index < 0 || index >= MAX_COURSE_NUM) {
cout << "无效的课程编号" << endl;
return -1;
}
if (courseScores[index] != nullptr) {
return courseScores[index]->score;
} else {
return -1;
}
}
~Student() { // 析构函数,释放内存
for (int i = 0; i < MAX_COURSE_NUM; i++) {
if (courseScores[i] != nullptr) {
delete courseScores[i];
}
}
}
private:
string id; // 学号
string name; // 姓名
char gender; // 性别
class CourseScore { // 课程成绩类
public:
CourseScore(string name, int score) {
this->name = name;
this->score = score;
}
string name; // 课程名称
int score; // 成绩
};
CourseScore* courseScores[MAX_COURSE_NUM]; // 课程成绩数组
};
class StudentCourseScore { // 学生课程成绩类
public:
StudentCourseScore(Student* student) {
this->student = student;
}
Student* getStudent() const { return student; }
int getTotalScore() const { // 获取总成绩
int totalScore = 0;
for (int i = 0; i < MAX_COURSE_NUM; i++) {
if (student->getCourseScore(i) != -1) {
totalScore += student->getCourseScore(i);
}
}
return totalScore;
}
string getMaxCourseName() const { // 获取分数最高的课程名称
int maxScore = -1;
string maxCourseName = "";
for (int i = 0; i < MAX_COURSE_NUM; i++) {
int score = student->getCourseScore(i);
if (score > maxScore) {
maxScore = score;
maxCourseName = courseNames[i];
}
}
return maxCourseName;
}
string getMinCourseName() const { // 获取分数最低的课程名称
int minScore = 101;
string minCourseName = "";
for (int i = 0; i < MAX_COURSE_NUM; i++) {
int score = student->getCourseScore(i);
if (score != -1 && score < minScore) {
minScore = score;
minCourseName = courseNames[i];
}
}
return minCourseName;
}
private:
static string courseNames[MAX_COURSE_NUM]; // 课程名称数组
Student* student; // 学生对象指针
};
string StudentCourseScore::courseNames[MAX_COURSE_NUM] = {"语文", "数学", "英语"};
int main() {
Student* stu1 = new Student("001", "张三", 'M');
stu1->addCourseScore(0, "语文", 80);
stu1->addCourseScore(1, "数学", 90);
stu1->addCourseScore(2, "英语", 70);
StudentCourseScore* scs1 = new StudentCourseScore(stu1);
cout << "学生信息:" << stu1->getId() << " " << stu1->getName() << " " << stu1->getGender() << endl;
cout << "语文成绩:" << stu1->getCourseScore(0) << endl;
cout << "总成绩:" << scs1->getTotalScore() << endl;
cout << "分数最高的课程:" << scs1->getMaxCourseName() << endl;
cout << "分数最低的课程:" << scs1->getMinCourseName() << endl;
delete scs1;
delete stu1;
Student* stu2 = new Student("002", "李四", 'F');
stu2->addCourseScore(0, "语文", 85);
stu2->addCourseScore(1, "数学", 75);
StudentCourseScore* scs2 = new StudentCourseScore(stu2);
cout << "学生信息:" << stu2->getId() << " " << stu2->getName() << " " << stu2->getGender() << endl;
cout << "英语成绩:" << stu2->getCourseScore(2) << endl;
cout << "总成绩:" << scs2->getTotalScore() << endl;
cout << "分数最高的课程:" << scs2->getMaxCourseName() << endl;
cout << "分数最低的课程:" << scs2->getMinCourseName() << endl;
delete scs2;
delete stu2;
return 0;
}
```
这个程序实现了一个简单的学生课程系统,包含了学生类、课程成绩类和学生课程成绩类。其中,学生类包含学号、姓名、性别和课程成绩数组等信息;课程成绩类包含课程名称和成绩;学生课程成绩类包含学生对象指针和总成绩、分数最高和最低的课程名称等信息。程序通过创建对象来模拟学生和课程成绩的添加和查询,并通过指针来实现对象之间的关联。
阅读全文