学生成绩管理系统c++
时间: 2023-09-11 11:08:09 浏览: 41
好的,学生成绩管理系统可以分为以下几个部分:
1. 学生信息管理:包括学生基本信息的录入、修改、删除和查询等操作。
2. 成绩信息管理:包括成绩录入、修改、删除和查询等操作。
3. 成绩统计分析:包括班级总分、平均分、最高分、最低分等统计分析功能。
4. 数据存储:成绩信息和学生信息需要存储到文件中,以便程序下次运行时可以读取。
下面是一个简单的 C++ 实现,供您参考:
```c++
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
// 学生信息结构体
struct Student {
char id[10]; // 学号
char name[20]; // 姓名
int age; // 年龄
};
// 成绩信息结构体
struct Score {
char id[10]; // 学号
float chinese; // 语文成绩
float math; // 数学成绩
float english; // 英语成绩
};
// 学生信息管理类
class StudentManager {
public:
// 构造函数
StudentManager() {
fstream fs("students.dat", ios::in | ios::binary);
if (!fs.is_open()) {
fs.close();
fs.open("students.dat", ios::out | ios::binary);
fs.close();
}
}
// 添加学生信息
void addStudent() {
Student stu;
cout << "请输入学号:";
cin >> stu.id;
cout << "请输入姓名:";
cin >> stu.name;
cout << "请输入年龄:";
cin >> stu.age;
ofstream ofs("students.dat", ios::app | ios::binary);
ofs.write((char*)&stu, sizeof(stu));
ofs.close();
cout << "添加成功!" << endl;
}
// 修改学生信息
void modifyStudent() {
char id[10];
cout << "请输入要修改的学生学号:";
cin >> id;
fstream fs("students.dat", ios::in | ios::out | ios::binary);
Student stu;
bool flag = false;
while (fs.read((char*)&stu, sizeof(stu))) {
if (strcmp(stu.id, id) == 0) {
flag = true;
cout << "请输入新的学号:";
cin >> stu.id;
cout << "请输入新的姓名:";
cin >> stu.name;
cout << "请输入新的年龄:";
cin >> stu.age;
fs.seekp(-sizeof(stu), ios::cur);
fs.write((char*)&stu, sizeof(stu));
cout << "修改成功!" << endl;
break;
}
}
if (!flag) {
cout << "未找到该学生!" << endl;
}
fs.close();
}
// 删除学生信息
void deleteStudent() {
char id[10];
cout << "请输入要删除的学生学号:";
cin >> id;
fstream fs("students.dat", ios::in | ios::binary);
ofstream ofs("temp.dat", ios::out | ios::binary);
Student stu;
bool flag = false;
while (fs.read((char*)&stu, sizeof(stu))) {
if (strcmp(stu.id, id) != 0) {
ofs.write((char*)&stu, sizeof(stu));
} else {
flag = true;
}
}
fs.close();
ofs.close();
remove("students.dat");
rename("temp.dat", "students.dat");
if (flag) {
cout << "删除成功!" << endl;
} else {
cout << "未找到该学生!" << endl;
}
}
// 查询学生信息
void queryStudent() {
char id[10];
cout << "请输入要查询的学生学号:";
cin >> id;
fstream fs("students.dat", ios::in | ios::binary);
Student stu;
bool flag = false;
while (fs.read((char*)&stu, sizeof(stu))) {
if (strcmp(stu.id, id) == 0) {
flag = true;
cout << "学号:" << stu.id << "\t姓名:" << stu.name << "\t年龄:" << stu.age << endl;
break;
}
}
if (!flag) {
cout << "未找到该学生!" << endl;
}
fs.close();
}
};
// 成绩信息管理类
class ScoreManager {
public:
// 构造函数
ScoreManager() {
fstream fs("scores.dat", ios::in | ios::binary);
if (!fs.is_open()) {
fs.close();
fs.open("scores.dat", ios::out | ios::binary);
fs.close();
}
}
// 添加成绩信息
void addScore() {
Score score;
cout << "请输入学号:";
cin >> score.id;
cout << "请输入语文成绩:";
cin >> score.chinese;
cout << "请输入数学成绩:";
cin >> score.math;
cout << "请输入英语成绩:";
cin >> score.english;
ofstream ofs("scores.dat", ios::app | ios::binary);
ofs.write((char*)&score, sizeof(score));
ofs.close();
cout << "添加成功!" << endl;
}
// 修改成绩信息
void modifyScore() {
char id[10];
cout << "请输入要修改的学生学号:";
cin >> id;
fstream fs("scores.dat", ios::in | ios::out | ios::binary);
Score score;
bool flag = false;
while (fs.read((char*)&score, sizeof(score))) {
if (strcmp(score.id, id) == 0) {
flag = true;
cout << "请输入新的语文成绩:";
cin >> score.chinese;
cout << "请输入新的数学成绩:";
cin >> score.math;
cout << "请输入新的英语成绩:";
cin >> score.english;
fs.seekp(-sizeof(score), ios::cur);
fs.write((char*)&score, sizeof(score));
cout << "修改成功!" << endl;
break;
}
}
if (!flag) {
cout << "未找到该学生!" << endl;
}
fs.close();
}
// 删除成绩信息
void deleteScore() {
char id[10];
cout << "请输入要删除的学生学号:";
cin >> id;
fstream fs("scores.dat", ios::in | ios::binary);
ofstream ofs("temp.dat", ios::out | ios::binary);
Score score;
bool flag = false;
while (fs.read((char*)&score, sizeof(score))) {
if (strcmp(score.id, id) != 0) {
ofs.write((char*)&score, sizeof(score));
} else {
flag = true;
}
}
fs.close();
ofs.close();
remove("scores.dat");
rename("temp.dat", "scores.dat");
if (flag) {
cout << "删除成功!" << endl;
} else {
cout << "未找到该学生!" << endl;
}
}
// 查询成绩信息
void queryScore() {
char id[10];
cout << "请输入要查询的学生学号:";
cin >> id;
fstream fs("scores.dat", ios::in | ios::binary);
Score score;
bool flag = false;
while (fs.read((char*)&score, sizeof(score))) {
if (strcmp(score.id, id) == 0) {
flag = true;
cout << "学号:" << score.id << "\t语文成绩:" << score.chinese << "\t数学成绩:" << score.math << "\t英语成绩:" << score.english << endl;
break;
}
}
if (!flag) {
cout << "未找到该学生!" << endl;
}
fs.close();
}
// 统计分析成绩信息
void analysisScore() {
fstream fs("scores.dat", ios::in | ios::binary);
Score score;
int count = 0;
float chineseTotal = 0, mathTotal = 0, englishTotal = 0;
float chineseMax = 0, mathMax = 0, englishMax = 0;
float chineseMin = 100, mathMin = 100, englishMin = 100;
while (fs.read((char*)&score, sizeof(score))) {
count++;
chineseTotal += score.chinese;
mathTotal += score.math;
englishTotal += score.english;
if (score.chinese > chineseMax) { chineseMax = score.chinese; }
if (score.math > mathMax) { mathMax = score.math; }
if (score.english > englishMax) { englishMax = score.english; }
if (score.chinese < chineseMin) { chineseMin = score.chinese; }
if (score.math < mathMin) { mathMin = score.math; }
if (score.english < englishMin) { englishMin = score.english; }
}
fs.close();
cout << "班级总人数:" << count << endl;
cout << "语文总分:" << chineseTotal << "\t平均分:" << chineseTotal / count << "\t最高分:" << chineseMax << "\t最低分:" << chineseMin << endl;
cout << "数学总分:" << mathTotal << "\t平均分:" << mathTotal / count << "\t最高分:" << mathMax << "\t最低分:" << mathMin << endl;
cout << "英语总分:" << englishTotal << "\t平均分:" << englishTotal / count << "\t最高分:" << englishMax << "\t最低分:" << englishMin << endl;
}
};
int main() {
int choice;
StudentManager studentManager;
ScoreManager scoreManager;
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 << "0. 退出程序" << endl;
cin >> choice;
switch (choice) {
case 1:
studentManager.addStudent();
break;
case 2:
studentManager.modifyStudent();
break;
case 3:
studentManager.deleteStudent();
break;
case 4:
studentManager.queryStudent();
break;
case 5:
scoreManager.addScore();
break;
case 6:
scoreManager.modifyScore();
break;
case 7:
scoreManager.deleteScore();
break;
case 8:
scoreManager.queryScore();
break;
case 9:
scoreManager.analysisScore();
break;
case 0:
return 0;
default:
cout << "输入错误,请重新输入!" << endl;
}
}
return 0;
}
```
在这个实现中,学生信息和成绩信息都是以二进制文件的形式存储的,存储文件为 students.dat 和 scores.dat。在程序运行时,如果这两个文件不存在,则会自动创建。
你可以根据自己的需求对代码进行修改和扩展。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![dta](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rpm](https://img-home.csdnimg.cn/images/20210720083646.png)