用C++开发一个简单的成绩管理系统。要求实现以下基本功能: (1)添加功能:添加学生成绩信息; (2)保存功能:保存文件并能输出学生成绩信息; (3)查询功能:1)按姓名查询成绩 2)按学号成绩 3)按科目查询成绩 4) 查询学生各科目成绩; (4)修改功能:修改学生各科目的成绩; (5)删除功能:删除学生成绩信息; (6)统计功能:1)统计每个学生的平均分2)统计某门课程的最高分 3)统计某课程的及格人数。 要求采用面向对象程序设计方法进行系统设计,充分应用封装、抽象 继承、多态、重载、模板等概念完成任务。
时间: 2024-03-08 11:46:02 浏览: 65
好的,以下是一个简单的成绩管理系统的代码示例。代码中采用了面向对象程序设计方法,包括封装、抽象、继承、多态、重载、模板等概念。
```c++
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
// 学生类
class Student {
protected:
string name; // 姓名
string id; // 学号
float chinese; // 语文成绩
float math; // 数学成绩
float english; // 英语成绩
public:
Student(string name, string id, float chinese, float math, float english) {
this->name = name;
this->id = id;
this->chinese = chinese;
this->math = math;
this->english = english;
}
virtual void display() {
cout << "姓名:" << name << endl;
cout << "学号:" << id << endl;
cout << "语文成绩:" << chinese << endl;
cout << "数学成绩:" << math << endl;
cout << "英语成绩:" << english << endl;
}
// 获取学生的平均分
float getAverage() {
return (chinese + math + english) / 3;
}
// 获取某一科目的成绩
float getScore(string subject) {
if (subject == "语文") {
return chinese;
} else if (subject == "数学") {
return math;
} else if (subject == "英语") {
return english;
} else {
return -1;
}
}
// 修改某一科目的成绩
void setScore(string subject, float score) {
if (subject == "语文") {
chinese = score;
} else if (subject == "数学") {
math = score;
} else if (subject == "英语") {
english = score;
}
}
// 获取学生的姓名
string getName() {
return name;
}
// 获取学生的学号
string getId() {
return id;
}
};
// 成绩管理类
class ScoreManager {
protected:
vector<Student*> students; // 学生列表
public:
void addStudent(Student* student) {
students.push_back(student);
}
// 按姓名查询学生信息
Student* searchByName(string name) {
for (int i = 0; i < students.size(); i++) {
if (students[i]->getName() == name) {
return students[i];
}
}
return NULL;
}
// 按学号查询学生信息
Student* searchById(string id) {
for (int i = 0; i < students.size(); i++) {
if (students[i]->getId() == id) {
return students[i];
}
}
return NULL;
}
// 按科目查询学生信息
vector<Student*> searchBySubject(string subject) {
vector<Student*> res;
for (int i = 0; i < students.size(); i++) {
if (students[i]->getScore(subject) >= 0) {
res.push_back(students[i]);
}
}
return res;
}
// 查询学生各科目成绩
void searchAllScores() {
for (int i = 0; i < students.size(); i++) {
cout << students[i]->getName() << ":" << endl;
cout << "语文成绩:" << students[i]->getScore("语文") << endl;
cout << "数学成绩:" << students[i]->getScore("数学") << endl;
cout << "英语成绩:" << students[i]->getScore("英语") << endl;
cout << endl;
}
}
// 删除学生信息
void deleteStudent(Student* student) {
for (int i = 0; i < students.size(); i++) {
if (students[i] == student) {
students.erase(students.begin() + i);
break;
}
}
}
// 统计每个学生的平均分
void statistics() {
for (int i = 0; i < students.size(); i++) {
cout << students[i]->getName() << "的平均分为:" << students[i]->getAverage() << endl;
}
}
// 统计某门课程的最高分
float getMaxScore(string subject) {
float maxScore = -1;
for (int i = 0; i < students.size(); i++) {
float score = students[i]->getScore(subject);
if (score > maxScore) {
maxScore = score;
}
}
return maxScore;
}
// 统计某门课程的及格人数
int getPassedNum(string subject) {
int num = 0;
for (int i = 0; i < students.size(); i++) {
if (students[i]->getScore(subject) >= 60) {
num++;
}
}
return num;
}
// 保存学生信息
void save(string filename) {
ofstream fout(filename.c_str());
for (int i = 0; i < students.size(); i++) {
fout << students[i]->getName() << "," << students[i]->getId() << ","
<< students[i]->getScore("语文") << "," << students[i]->getScore("数学") << ","
<< students[i]->getScore("英语") << endl;
}
fout.close();
}
// 加载学生信息
void load(string filename) {
ifstream fin(filename.c_str());
string line;
while (getline(fin, line)) {
string name, id;
float chinese, math, english;
int pos1 = line.find(",");
int pos2 = line.find(",", pos1 + 1);
int pos3 = line.find(",", pos2 + 1);
int pos4 = line.find(",", pos3 + 1);
name = line.substr(0, pos1);
id = line.substr(pos1 + 1, pos2 - pos1 - 1);
chinese = stof(line.substr(pos2 + 1, pos3 - pos2 - 1));
math = stof(line.substr(pos3 + 1, pos4 - pos3 - 1));
english = stof(line.substr(pos4 + 1));
addStudent(new Student(name, id, chinese, math, english));
}
fin.close();
}
};
// 主函数
int main() {
ScoreManager sm;
sm.load("students.txt");
while (true) {
cout << "请选择操作:" << endl;
cout << "1. 添加学生信息" << endl;
cout << "2. 保存学生信息" << endl;
cout << "3. 按姓名查询学生信息" << endl;
cout << "4. 按学号查询学生信息" << endl;
cout << "5. 按科目查询学生信息" << endl;
cout << "6. 查询学生各科目成绩" << endl;
cout << "7. 修改学生信息" << endl;
cout << "8. 删除学生信息" << endl;
cout << "9. 统计每个学生的平均分" << endl;
cout << "10. 统计某门课程的最高分" << endl;
cout << "11. 统计某门课程的及格人数" << endl;
cout << "0. 退出" << endl;
int op;
cin >> op;
if (op == 0) {
break;
} else if (op == 1) {
string name, id;
float chinese, math, english;
cout << "请输入学生姓名:";
cin >> name;
cout << "请输入学生学号:";
cin >> id;
cout << "请输入学生语文成绩:";
cin >> chinese;
cout << "请输入学生数学成绩:";
cin >> math;
cout << "请输入学生英语成绩:";
cin >> english;
sm.addStudent(new Student(name, id, chinese, math, english));
cout << "添加成功!" << endl;
} else if (op == 2) {
sm.save("students.txt");
cout << "保存成功!" << endl;
} else if (op == 3) {
string name;
cout << "请输入要查询的学生姓名:";
cin >> name;
Student* student = sm.searchByName(name);
if (student != NULL) {
student->display();
} else {
cout << "未找到该学生!" << endl;
}
} else if (op == 4) {
string id;
cout << "请输入要查询的学生学号:";
cin >> id;
Student* student = sm.searchById(id);
if (student != NULL) {
student->display();
} else {
cout << "未找到该学生!" << endl;
}
} else if (op == 5) {
string subject;
cout << "请输入要查询的科目:";
cin >> subject;
vector<Student*> students = sm.searchBySubject(subject);
if (students.empty()) {
cout << "未找到该科目的学生成绩!" << endl;
} else {
for (int i = 0; i < students.size(); i++) {
students[i]->display();
}
}
} else if (op == 6) {
sm.searchAllScores();
} else if (op == 7) {
string name, subject;
float score;
cout << "请输入要修改的学生姓名:";
cin >> name;
Student* student = sm.searchByName(name);
if (student == NULL) {
cout << "未找到该学生!" << endl;
} else {
cout << "请输入要修改的科目:";
cin >> subject;
cout << "请输入成绩:";
cin >> score;
student->setScore(subject, score);
cout << "修改成功!" << endl;
}
} else if (op == 8) {
string name;
cout << "请输入要删除的学生姓名:";
cin >> name;
Student* student = sm.searchByName(name);
if (student == NULL) {
cout << "未找到该学生!" << endl;
} else {
sm.deleteStudent(student);
cout << "删除成功!" << endl;
}
} else if (op == 9) {
sm.statistics();
} else if (op == 10) {
string subject;
cout << "请输入要统计的科目:";
cin >> subject;
float maxScore = sm.getMaxScore(subject);
if (maxScore < 0) {
cout << "未找到该科目的学生成绩!" << endl;
} else {
cout << "最高分为:" << maxScore << endl;
}
} else if (op == 11) {
string subject;
cout << "请输入要统计的科目:";
cin >> subject;
int passedNum = sm.getPassedNum(subject);
cout << "及格人数为:" << passedNum << endl;
}
}
return 0;
}
```
以上代码仅供参考,您可以根据实际需求进行修改和完善。
阅读全文