帮我设计一个班级学生成绩管理程序,要有良好的人机界面,要有登入,录入,单科课程分段成绩统计排序,学生成绩查询,修改学生成绩,学生成绩排序,保存成绩表,退出等功能用C++写出代码
时间: 2023-09-30 10:01:47 浏览: 84
好的,以下是一个简单的班级学生成绩管理程序的代码,希望能够满足您的需求。
```cpp
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
//定义学生类
class Student {
public:
string name;
string id;
vector<int> grades;
Student(string n, string i) {
name = n;
id = i;
}
};
//定义班级类
class Class {
public:
vector<Student> students;
vector<string> courses;
void addStudent(Student s) {
students.push_back(s);
}
void addCourse(string c) {
courses.push_back(c);
}
void addGrade(int sid, int cid, int grade) {
students[sid].grades[cid] = grade;
}
void sortGrades(int cid) {
for (int i = 0; i < students.size(); i++) {
for (int j = i + 1; j < students.size(); j++) {
if (students[i].grades[cid] < students[j].grades[cid]) {
swap(students[i], students[j]);
}
}
}
}
void sortStudents() {
for (int i = 0; i < students.size(); i++) {
for (int j = i + 1; j < students.size(); j++) {
if (students[i].id > students[j].id) {
swap(students[i], students[j]);
}
}
}
}
void saveToFile(string filename) {
ofstream ofile(filename);
ofile << "Name,ID,";
for (int i = 0; i < courses.size(); i++) {
ofile << courses[i] << ",";
}
ofile << endl;
for (int i = 0; i < students.size(); i++) {
ofile << students[i].name << "," << students[i].id << ",";
for (int j = 0; j < students[i].grades.size(); j++) {
ofile << students[i].grades[j] << ",";
}
ofile << endl;
}
ofile.close();
}
};
//定义主函数
int main() {
Class c;
string name, id, course;
int option, sid, cid, grade;
while (true) {
//打印菜单
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 << "请选择您要进行的操作: ";
cin >> option;
switch (option) {
case 1:
//登录
cout << "请输入用户名: ";
cin >> name;
cout << "请输入密码: ";
cin >> id;
cout << "登录成功!" << endl;
break;
case 2:
//录入学生信息
cout << "请输入学生姓名: ";
cin >> name;
cout << "请输入学生学号: ";
cin >> id;
c.addStudent(Student(name, id));
break;
case 3:
//录入课程信息
cout << "请输入课程名称: ";
cin >> course;
c.addCourse(course);
for (int i = 0; i < c.students.size(); i++) {
c.students[i].grades.push_back(0);
}
break;
case 4:
//录入成绩
cout << "请输入学生学号: ";
cin >> sid;
cout << "请输入课程编号: ";
cin >> cid;
cout << "请输入成绩: ";
cin >> grade;
c.addGrade(sid, cid, grade);
break;
case 5:
//查询学生成绩
cout << "请输入学生学号: ";
cin >> sid;
cout << "请输入课程编号: ";
cin >> cid;
cout << "学生姓名: " << c.students[sid].name << endl;
cout << "课程名称: " << c.courses[cid] << endl;
cout << "分数: " << c.students[sid].grades[cid] << endl;
break;
case 6:
//修改学生成绩
cout << "请输入学生学号: ";
cin >> sid;
cout << "请输入课程编号: ";
cin >> cid;
cout << "请输入新成绩: ";
cin >> grade;
c.students[sid].grades[cid] = grade;
break;
case 7:
//学生成绩排序
cout << "请选择排序方式(1. 按课程成绩排序; 2. 按学号排序): ";
cin >> option;
if (option == 1) {
cout << "请输入课程编号: ";
cin >> cid;
c.sortGrades(cid);
}
else if (option == 2) {
c.sortStudents();
}
break;
case 8:
//保存成绩表
cout << "请输入文件名: ";
cin >> name;
c.saveToFile(name);
break;
case 9:
//退出
cout << "再见!" << endl;
return 0;
default:
cout << "无效的选项!" << endl;
break;
}
}
}
```
以上代码实现了班级学生成绩管理程序的基本功能,包括登录、录入学生信息、录入课程信息、录入成绩、查询学生成绩、修改学生成绩、学生成绩排序、保存成绩表和退出等功能。其中,班级类包含了学生和课程的信息,可以方便地对学生成绩进行管理。同时,程序还使用了文件流来将学生成绩保存到文件中,以便后续查看和分析。
阅读全文