用C++开发一个简单的成绩管理系统。要求实现以下基本功能: (1)添加功能:添加学生成绩信息; (2)保存功能:保存文件并能输出学生成绩信息; (3)查询功能:1)按姓名查询成绩 2)按学号成绩 3)按科目查询成绩 4) 查询学生各科目成绩; (4)修改功能:修改学生各科目的成绩; (5)删除功能:删除学生成绩信息; (6)排序功能:按总成绩降序排列; (6)统计功能:1)统计每个学生的平均分2)统计某门课程的最高分 3)统计某课程的及格人数。 要求采用面向对象程序设计方法进行系统设计,充分应用封装、抽象 继承、多态、重载、模板等概念完成任务。 写出完整的代码
时间: 2024-03-11 17:50:13 浏览: 152
用C++做的学生成绩管理系统
好的,以下是一个简单的成绩管理系统的完整代码:
```
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int MAX_STUDENTS = 100;
struct Score {
float Chinese;
float Math;
float English;
float Total;
};
class Student {
public:
Student() {
name = "";
id = 0;
score.Chinese = 0;
score.Math = 0;
score.English = 0;
score.Total = 0;
}
Student(string name, int id, float chinese, float math, float english) {
this->name = name;
this->id = id;
score.Chinese = chinese;
score.Math = math;
score.English = english;
score.Total = chinese + math + english;
}
~Student() {}
void setName(string name) {
this->name = name;
}
void setID(int id) {
this->id = id;
}
void setScore(float chinese, float math, float english) {
score.Chinese = chinese;
score.Math = math;
score.English = english;
score.Total = chinese + math + english;
}
void deleteScore() {
score.Chinese = 0;
score.Math = 0;
score.English = 0;
score.Total = 0;
}
string getName() {
return name;
}
int getID() {
return id;
}
float getChinese() {
return score.Chinese;
}
float getMath() {
return score.Math;
}
float getEnglish() {
return score.English;
}
float getTotal() {
return score.Total;
}
bool operator==(const Student& other) {
return (name == other.name && id == other.id);
}
bool operator<(const Student& other) {
return (score.Total > other.score.Total);
}
private:
string name;
int id;
Score score;
};
class StudentManager {
public:
StudentManager() {
numStudents = 0;
}
~StudentManager() {}
void addStudent(Student student) {
if (numStudents == MAX_STUDENTS) {
cout << "Error: student list is full!" << endl;
return;
}
students[numStudents] = student;
numStudents++;
}
void saveToFile(string filename) {
ofstream outfile(filename);
if (!outfile) {
cout << "Error: cannot open file " << filename << "!" << endl;
return;
}
for (int i = 0; i < numStudents; i++) {
outfile << students[i].getName() << " " << students[i].getID() << " "
<< students[i].getChinese() << " " << students[i].getMath() << " "
<< students[i].getEnglish() << " " << students[i].getTotal() << endl;
}
outfile.close();
}
Student* searchByName(string name) {
for (int i = 0; i < numStudents; i++) {
if (students[i].getName() == name) {
return &students[i];
}
}
return NULL;
}
Student* searchByID(int id) {
for (int i = 0; i < numStudents; i++) {
if (students[i].getID() == id) {
return &students[i];
}
}
return NULL;
}
Student* searchBySubject(string subject) {
if (subject == "Chinese") {
float maxScore = 0;
Student* maxStudent = NULL;
for (int i = 0; i < numStudents; i++) {
if (students[i].getChinese() > maxScore) {
maxScore = students[i].getChinese();
maxStudent = &students[i];
}
}
return maxStudent;
}
else if (subject == "Math") {
float maxScore = 0;
Student* maxStudent = NULL;
for (int i = 0; i < numStudents; i++) {
if (students[i].getMath() > maxScore) {
maxScore = students[i].getMath();
maxStudent = &students[i];
}
}
return maxStudent;
}
else if (subject == "English") {
float maxScore = 0;
Student* maxStudent = NULL;
for (int i = 0; i < numStudents; i++) {
if (students[i].getEnglish() > maxScore) {
maxScore = students[i].getEnglish();
maxStudent = &students[i];
}
}
return maxStudent;
}
else {
return NULL;
}
}
Score* searchScores(Student student) {
return &(student.score);
}
void modifyScore(Student* student, float chinese, float math, float english) {
student->setScore(chinese, math, english);
}
void deleteStudent(Student* student) {
for (int i = 0; i < numStudents; i++) {
if (students[i] == *student) {
for (int j = i; j < numStudents - 1; j++) {
students[j] = students[j + 1];
}
numStudents--;
break;
}
}
}
float calculateAverageScore(Student student) {
return student.getTotal() / 3;
}
float calculateMaxScore(string subject) {
float maxScore = 0;
for (int i = 0; i < numStudents; i++) {
if (subject == "Chinese" && students[i].getChinese() > maxScore) {
maxScore = students[i].getChinese();
}
else if (subject == "Math" && students[i].getMath() > maxScore) {
maxScore = students[i].getMath();
}
else if (subject == "English" && students[i].getEnglish() > maxScore) {
maxScore = students[i].getEnglish();
}
}
return maxScore;
}
int calculatePassNum(string subject) {
int numPass = 0;
for (int i = 0; i < numStudents; i++) {
if (subject == "Chinese" && students[i].getChinese() >= 60) {
numPass++;
}
else if (subject == "Math" && students[i].getMath() >= 60) {
numPass++;
}
else if (subject == "English" && students[i].getEnglish() >= 60) {
numPass++;
}
}
return numPass;
}
void sortStudents() {
for (int i = 0; i < numStudents - 1; i++) {
for (int j = i + 1; j < numStudents; j++) {
if (students[i] < students[j]) {
Student temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
}
private:
Student students[MAX_STUDENTS];
int numStudents;
};
int main() {
StudentManager sm;
int choice = 0;
while (true) {
cout << "Please choose an operation:" << endl;
cout << "1. Add student" << endl;
cout << "2. Save to file" << endl;
cout << "3. Search by name" << endl;
cout << "4. Search by ID" << endl;
cout << "5. Search by subject" << endl;
cout << "6. Search scores" << endl;
cout << "7. Modify score" << endl;
cout << "8. Delete student" << endl;
cout << "9. Calculate average score" << endl;
cout << "10. Calculate max score" << endl;
cout << "11. Calculate pass num" << endl;
cout << "12. Sort students" << endl;
cout << "0. Exit" << endl;
cin >> choice;
if (choice == 0) {
break;
}
else if (choice == 1) {
string name;
int id;
float chinese, math, english;
cout << "Please enter student name: ";
cin >> name;
cout << "Please enter student ID: ";
cin >> id;
cout << "Please enter Chinese score: ";
cin >> chinese;
cout << "Please enter Math score: ";
cin >> math;
cout << "Please enter English score: ";
cin >> english;
Student student(name, id, chinese, math, english);
sm.addStudent(student);
cout << "Added student:" << endl;
cout << "Name: " << name << endl;
cout << "ID: " << id << endl;
cout << "Chinese score: " << chinese << endl;
cout << "Math score: " << math << endl;
cout << "English score: " << english << endl;
}
else if (choice == 2) {
string filename;
cout << "Please enter filename: ";
cin >> filename;
sm.saveToFile(filename);
cout << "Saved to file " << filename << "!" << endl;
}
else if (choice == 3) {
string name;
cout << "Please enter student name: ";
cin >> name;
Student* student = sm.searchByName(name);
if (student == NULL) {
cout << "Cannot find student " << name << "!" << endl;
}
else {
cout << "Name: " << student->getName() << endl;
cout << "ID: " << student->getID() << endl;
cout << "Chinese score: " << student->getChinese() << endl;
cout << "Math score: " << student->getMath() << endl;
cout << "English score: " << student->getEnglish() << endl;
cout << "Total score: " << student->getTotal() << endl;
}
}
else if (choice == 4) {
int id;
cout << "Please enter student ID: ";
cin >> id;
Student* student = sm.searchByID(id);
if (student == NULL) {
cout << "Cannot find student with ID " << id << "!" << endl;
}
else {
cout << "Name: " << student->getName() << endl;
cout << "ID: " << student->getID() << endl;
cout << "Chinese score: " << student->getChinese() << endl;
cout << "Math score: " << student->getMath() << endl;
cout << "English score: " << student->getEnglish() << endl;
cout << "Total score: " << student->getTotal() << endl;
}
}
else if (choice == 5) {
string subject;
cout << "Please enter subject (Chinese, Math, English): ";
cin >> subject;
Student* student = sm.searchBySubject(subject);
if (student == NULL) {
cout << "Cannot find max score of subject " << subject << "!" << endl;
}
else {
cout << "Name: " << student->getName() << endl;
cout << "ID: " << student->getID() << endl;
cout << subject << " score: " << student->getChinese() << endl;
}
}
else if (choice == 6) {
string name;
int id;
cout << "Please enter student name: ";
cin >> name;
cout << "Please enter student ID: ";
cin >> id;
Student* student = sm.searchByName(name);
if (student == NULL || student->getID() != id) {
cout << "Cannot find student " << name << " with ID " << id << "!" << endl;
}
else {
Score* score = sm.searchScores(*student);
cout << "Name: " << student->getName() << endl;
cout << "ID: " << student->getID() << endl;
cout << "Chinese score: " << score->Chinese << endl;
cout << "Math score: " << score->Math << endl;
cout << "English score: " << score->English << endl;
cout << "Total score: " << score->Total << endl;
}
}
else if (choice == 7) {
阅读全文