帮我用c++写一个学生成绩管理系统的课程设计,并实现以下要求: 定义学生(student)类,其中包括姓名、性别、学号、班级和语文数学英语物理学科的成绩。 设计菜单实现功能选择; 输入功能:输入学生信息,并以txt格式保存到文件中; 计算每个学生的总分与平均分的功能,而且能够分别按照学生的每一科的功课成绩和总分的成绩排序。 能根据学号修改学生信息; 能根据学号删除学生信息; 查询功能: 能够根据学号查询学生信息; 能够根据姓名、班级查询学生信息; 统计学生成绩,按照班级和科目计算平均分。 所有的增加、修改、删除能同步到txt文件;也从txt文件读取数据到程序。
时间: 2023-12-30 12:06:14 浏览: 62
学习成绩管理系统2.0.zip
好的,我可以帮您完成这个课程设计。
首先,我们需要定义一个学生类,包括姓名、性别、学号、班级和语文数学英语物理学科的成绩。可以写成如下代码:
```c++
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
class Student {
public:
string name; // 姓名
string gender; // 性别
string id; // 学号
string cls; // 班级
double chinese; // 语文成绩
double math; // 数学成绩
double english; // 英语成绩
double physics; // 物理成绩
double sum; // 总分
double avg; // 平均分
// 构造函数
Student(string n, string g, string i, string c, double ch, double m, double e, double p)
: name(n), gender(g), id(i), cls(c), chinese(ch), math(m), english(e), physics(p) {
sum = chinese + math + english + physics;
avg = sum / 4;
}
};
```
接下来,我们需要实现菜单,以实现不同的功能。可以写成如下代码:
```c++
void printMenu() {
cout << "1. 添加学生信息" << endl;
cout << "2. 修改学生信息" << endl;
cout << "3. 删除学生信息" << endl;
cout << "4. 查询学生信息" << endl;
cout << "5. 统计学生成绩" << endl;
cout << "6. 退出程序" << endl;
cout << "请选择操作:";
}
int main() {
vector<Student> students; // 存储所有学生信息
// 从文件中读取学生信息
ifstream ifs("students.txt");
if (ifs.is_open()) {
string line;
while (getline(ifs, line)) {
string n, g, i, c;
double ch, m, e, p;
stringstream ss(line);
ss >> n >> g >> i >> c >> ch >> m >> e >> p;
Student s(n, g, i, c, ch, m, e, p);
students.push_back(s);
}
ifs.close();
}
while (true) {
printMenu();
int choice;
cin >> choice;
switch (choice) {
case 1: // 添加学生信息
// TODO
break;
case 2: // 修改学生信息
// TODO
break;
case 3: // 删除学生信息
// TODO
break;
case 4: // 查询学生信息
// TODO
break;
case 5: // 统计学生成绩
// TODO
break;
case 6: // 退出程序
// 将学生信息保存到文件中
ofstream ofs("students.txt");
if (ofs.is_open()) {
for (auto& s : students) {
ofs << s.name << " " << s.gender << " " << s.id << " " << s.cls << " "
<< s.chinese << " " << s.math << " " << s.english << " " << s.physics << endl;
}
ofs.close();
}
cout << "程序已退出。" << endl;
return 0;
default:
cout << "输入无效,请重新输入。" << endl;
break;
}
}
return 0;
}
```
然后,我们需要实现菜单中各个功能对应的代码。
1. 添加学生信息
```c++
void addStudent(vector<Student>& students) {
string n, g, i, c;
double ch, m, e, p;
cout << "请输入学生姓名:";
cin >> n;
cout << "请输入学生性别:";
cin >> g;
cout << "请输入学生学号:";
cin >> i;
cout << "请输入学生班级:";
cin >> c;
cout << "请输入学生语文成绩:";
cin >> ch;
cout << "请输入学生数学成绩:";
cin >> m;
cout << "请输入学生英语成绩:";
cin >> e;
cout << "请输入学生物理成绩:";
cin >> p;
Student s(n, g, i, c, ch, m, e, p);
students.push_back(s);
cout << "添加成功。" << endl;
}
```
2. 修改学生信息
```c++
void modifyStudent(vector<Student>& students) {
string i;
cout << "请输入要修改的学生学号:";
cin >> i;
for (auto& s : students) {
if (s.id == i) {
cout << "请输入新的学生姓名(原姓名为 " << s.name << "):";
cin >> s.name;
cout << "请输入新的学生性别(原性别为 " << s.gender << "):";
cin >> s.gender;
cout << "请输入新的学生班级(原班级为 " << s.cls << "):";
cin >> s.cls;
cout << "请输入新的学生语文成绩(原成绩为 " << s.chinese << "):";
cin >> s.chinese;
cout << "请输入新的学生数学成绩(原成绩为 " << s.math << "):";
cin >> s.math;
cout << "请输入新的学生英语成绩(原成绩为 " << s.english << "):";
cin >> s.english;
cout << "请输入新的学生物理成绩(原成绩为 " << s.physics << "):";
cin >> s.physics;
s.sum = s.chinese + s.math + s.english + s.physics;
s.avg = s.sum / 4;
cout << "修改成功。" << endl;
return;
}
}
cout << "查无此人。" << endl;
}
```
3. 删除学生信息
```c++
void deleteStudent(vector<Student>& students) {
string i;
cout << "请输入要删除的学生学号:";
cin >> i;
for (auto it = students.begin(); it != students.end(); ++it) {
if (it->id == i) {
students.erase(it);
cout << "删除成功。" << endl;
return;
}
}
cout << "查无此人。" << endl;
}
```
4. 查询学生信息
```c++
void queryStudent(vector<Student>& students) {
int choice;
cout << "1. 根据学号查询" << endl;
cout << "2. 根据姓名查询" << endl;
cout << "3. 根据班级查询" << endl;
cout << "请选择查询方式:";
cin >> choice;
switch (choice) {
case 1: { // 根据学号查询
string i;
cout << "请输入要查询的学生学号:";
cin >> i;
for (auto& s : students) {
if (s.id == i) {
cout << "姓名:" << s.name << endl;
cout << "性别:" << s.gender << endl;
cout << "学号:" << s.id << endl;
cout << "班级:" << s.cls << endl;
cout << "语文成绩:" << s.chinese << endl;
cout << "数学成绩:" << s.math << endl;
cout << "英语成绩:" << s.english << endl;
cout << "物理成绩:" << s.physics << endl;
cout << "总分:" << s.sum << endl;
cout << "平均分:" << s.avg << endl;
return;
}
}
cout << "查无此人。" << endl;
break;
}
case 2: { // 根据姓名查询
string n;
cout << "请输入要查询的学生姓名:";
cin >> n;
vector<Student> result;
for (auto& s : students) {
if (s.name == n) {
result.push_back(s);
}
}
if (result.empty()) {
cout << "查无此人。" << endl;
break;
}
cout << "查询结果:" << endl;
for (auto& s : result) {
cout << "姓名:" << s.name << endl;
cout << "性别:" << s.gender << endl;
cout << "学号:" << s.id << endl;
cout << "班级:" << s.cls << endl;
cout << "语文成绩:" << s.chinese << endl;
cout << "数学成绩:" << s.math << endl;
cout << "英语成绩:" << s.english << endl;
cout << "物理成绩:" << s.physics << endl;
cout << "总分:" << s.sum << endl;
cout << "平均分:" << s.avg << endl;
}
break;
}
case 3: { // 根据班级查询
string c;
cout << "请输入要查询的学生班级:";
cin >> c;
vector<Student> result;
for (auto& s : students) {
if (s.cls == c) {
result.push_back(s);
}
}
if (result.empty()) {
cout << "查无此人。" << endl;
break;
}
cout << "查询结果:" << endl;
for (auto& s : result) {
cout << "姓名:" << s.name << endl;
cout << "性别:" << s.gender << endl;
cout << "学号:" << s.id << endl;
cout << "班级:" << s.cls << endl;
cout << "语文成绩:" << s.chinese << endl;
cout << "数学成绩:" << s.math << endl;
cout << "英语成绩:" << s.english << endl;
cout << "物理成绩:" << s.physics << endl;
cout << "总分:" << s.sum << endl;
cout << "平均分:" << s.avg << endl;
}
break;
}
default:
cout << "输入无效,请重新输入。" << endl;
break;
}
}
```
5. 统计学生成绩
```c++
void statisticStudent(vector<Student>& students) {
int choice;
cout << "1. 按班级统计" << endl;
cout << "2. 按科目统计" << endl;
cout << "请选择统计方式:";
cin >> choice;
switch (choice) {
case 1: { // 按班级统计
vector<string> clsList;
for (auto& s : students) {
if (find(clsList.begin(), clsList.end(), s.cls) == clsList.end()) {
clsList.push_back(s.cls);
}
}
for (auto& c : clsList) {
double sum = 0;
int count = 0;
for (auto& s : students) {
if (s.cls == c) {
sum += s.sum;
++count;
}
}
double avg = sum / count;
cout << "班级:" << c << ",平均分:" << avg << endl;
}
break;
}
case 2: { // 按科目统计
double chineseSum = 0, mathSum = 0, englishSum = 0, physicsSum = 0;
for (auto& s : students) {
chineseSum += s.chinese;
mathSum += s.math;
englishSum += s.english;
physicsSum += s.physics;
}
double chineseAvg = chineseSum / students.size();
double mathAvg = mathSum / students.size();
double englishAvg = englishSum / students.size();
double physicsAvg = physicsSum / students.size();
cout << "语文平均分:" << chineseAvg << endl;
cout << "数学平均分:" << mathAvg << endl;
cout << "英语平均分:" << englishAvg << endl;
cout << "物理平均分:" << physicsAvg << endl;
break;
}
default:
cout << "输入无效,请重新输入。" << endl;
break;
}
}
```
最后,将各个功能对应的代码填入菜单中即可。
阅读全文