C++ 学生管理系统设计 - 实现学籍信息与成绩管理
C++ 学生管理系统设计
本项目使用 C++ 语言设计一个简单的学籍管理系统,实现学生基本信息和成绩的录入、查询、删除和排序功能,并提供文本菜单界面。
数据文件
- 学生基本信息文件 (A. TXT) 其内容格式如下:
学号 姓名 性别 宿舍号码 电话号码
01 张成成 男 501 87732111
02 李成华 女 101 87723112
03 王成凤 女 101 87723112
04 张明明 男 502 87734333
05 陈东 男 501 87732111
06 李果 男 502 87734333
07 张园园 女 102 87756122
... ... ... ...
- 学生成绩基本信息文件 (B. TXT) 其内容格式如下:
学号 课程编号 课程名称 学分 平时成绩 实验成绩 卷面成绩 综合成绩 实得学分
01 A01 大学物理 3 66 78 82
02 B03 高等数学 4 78 -1 90
01 B03 高等数学 4 45 -1 88
02 C01 VF 3 65 76 66
... ... ... ... ... ... ... ... ...
功能要求及说明
1. 数据录入功能
- 对 B. TXT 进行数据录入,只录入每个学生的学号、课程编号、课程名称、学分、平时成绩、实验成绩、卷面成绩共 7 个数据。
- 综合成绩、实得学分由程序根据条件自动运算。
综合成绩计算
- 如果本课程的实验成绩为 -1,则表示无实验,综合成绩 = 平时成绩 * 30% + 卷面成绩 * 70%;
- 如果实验成绩不为 -1,表示本课程有实验,综合成绩 = 平时成绩 * 15% + 实验成绩 * 0.15% + 卷面成绩 * 70%。
实得学分计算
- 采用等级学分制。
- 综合成绩在 90-100 之间,应得学分 = 学分 * 100%
- 综合成绩在 80-90 之间,应得学分 = 学分 * 80%
- 综合成绩在 70-80 之间,应得学分 = 学分 * 75%
- 综合成绩在 60-70 之间,应得学分 = 学分 * 60%
- 综合成绩在 60 以下,应得学分 = 学分 * 0%
2. 查询功能
A: 学生基本情况查询
- A1---输入一个学号或姓名(可实现选择),查出此生的基本信息并显示输出。
- A2---输入一个宿舍号码,可查询出本室所有的学生的基本信息并显示输出。
B: 成绩查询
- B1: 输入一个学号时,查询出此生的所有课程情况,格式如下:
学 号: xx 姓 名: xxxxx
课程编号: xxx 课程名称: xxxxx 综合成绩: xxxx 实得学分: xx
课程编号: xxx 课程名称: xxxx 综合成绩: xxxx 实得学分: xx
课程编号: xxx 课程名称: xxxxx 综合成绩: xxxx 实得学分: xx
... ... ... ...
共修: xx科,实得总学分为: xxx
3. 删除功能
- 当在 A. TXT 中删除一个学生时,自动地在 B. TXT 中删除此人所有信息。
4. 排序功能
- 能实现选择按综合成绩或实得学分升序或降序排序并显示数据。
其他要求
- 只能使用 C++ 语言,采用面向对象程序设计思想,源程序要有适当的注释,使程序容易阅读。
- 至少采用文本菜单界面(如果能采用图形菜单界面更好)。
- 学生可自动增加新功能模块(视情况可另外加分)。
- 写出课程设计报告,具体要求见相关说明文档内容。
示例代码
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
struct Student {
string id;
string name;
string gender;
string dormitory;
string phoneNumber;
};
struct Course {
string id;
string courseId;
string courseName;
int credit;
int dailyScore;
int experimentScore;
int finalScore;
int overallScore;
int obtainedCredit;
};
class StudentManagementSystem {
private:
vector<Student> students;
vector<Course> courses;
public:
void readStudentData(string filename) {
ifstream inputFile(filename);
if (!inputFile) {
cerr << "Error opening file: " << filename << endl;
return;
}
string line;
while (getline(inputFile, line)) {
Student student;
student.id = line.substr(0, 2);
student.name = line.substr(2, 6);
student.gender = line.substr(8, 4);
student.dormitory = line.substr(12, 4);
student.phoneNumber = line.substr(16);
students.push_back(student);
}
inputFile.close();
}
void readCourseData(string filename) {
ifstream inputFile(filename);
if (!inputFile) {
cerr << "Error opening file: " << filename << endl;
return;
}
string line;
while (getline(inputFile, line)) {
Course course;
course.id = line.substr(0, 2);
course.courseId = line.substr(2, 4);
course.courseName = line.substr(6, 10);
course.credit = stoi(line.substr(16, 2));
course.dailyScore = stoi(line.substr(18, 2));
course.experimentScore = stoi(line.substr(20, 2));
course.finalScore = stoi(line.substr(22, 2));
if (course.experimentScore == -1) {
course.overallScore = course.dailyScore * 0.3 + course.finalScore * 0.7;
} else {
course.overallScore = course.dailyScore * 0.15 + course.experimentScore * 0.15 + course.finalScore * 0.7;
}
if (course.overallScore >= 90) {
course.obtainedCredit = course.credit;
} else if (course.overallScore >= 80) {
course.obtainedCredit = course.credit * 0.8;
} else if (course.overallScore >= 70) {
course.obtainedCredit = course.credit * 0.75;
} else if (course.overallScore >= 60) {
course.obtainedCredit = course.credit * 0.6;
} else {
course.obtainedCredit = 0;
}
courses.push_back(course);
}
inputFile.close();
}
void printStudentInfo(string id) {
for (const auto& student : students) {
if (student.id == id || student.name == id) {
cout << "学号:" << student.id << "\t姓名:" << student.name << "\t性别:" << student.gender << "\t宿舍号码:" << student.dormitory << "\t电话号码:" << student.phoneNumber << endl;
return;
}
}
cout << "未找到学生信息" << endl;
}
void printStudentInfoByDormitory(string dormitory) {
for (const auto& student : students) {
if (student.dormitory == dormitory) {
cout << "学号:" << student.id << "\t姓名:" << student.name << "\t性别:" << student.gender << "\t宿舍号码:" << student.dormitory << "\t电话号码:" << student.phoneNumber << endl;
}
}
}
void printCourseInfo(string id) {
int totalCredit = 0;
int obtainedTotalCredit = 0;
for (const auto& course : courses) {
if (course.id == id) {
cout << "学号:" << id << "\t姓名:" << getStudentNameById(id) << endl;
cout << "课程编号:" << course.courseId << "\t课程名称:" << course.courseName << "\t综合成绩:" << course.overallScore << "\t实得学分:" << course.obtainedCredit << endl;
totalCredit += course.credit;
obtainedTotalCredit += course.obtainedCredit;
}
}
cout << "共修:" << courses.size() << "科,实得总学分为:" << obtainedTotalCredit << "\/" << totalCredit << endl;
}
void deleteStudent(string id) {
students.erase(remove_if(students.begin(), students.end(), [&](const Student& student) { return student.id == id; }), students.end());
courses.erase(remove_if(courses.begin(), courses.end(), [&](const Course& course) { return course.id == id; }), courses.end());
}
void sortStudentsByOverallScore(bool ascending) {
if (ascending) {
sort(students.begin(), students.end(), [&](const Student& a, const Student& b) {
return getOverallScoreById(a.id) < getOverallScoreById(b.id);
});
} else {
sort(students.begin(), students.end(), [&](const Student& a, const Student& b) {
return getOverallScoreById(a.id) > getOverallScoreById(b.id);
});
}
}
void sortStudentsByObtainedCredit(bool ascending) {
if (ascending) {
sort(students.begin(), students.end(), [&](const Student& a, const Student& b) {
return getObtainedCreditById(a.id) < getObtainedCreditById(b.id);
});
} else {
sort(students.begin(), students.end(), [&](const Student& a, const Student& b) {
return getObtainedCreditById(a.id) > getObtainedCreditById(b.id);
});
}
}
private:
string getStudentNameById(string id) {
for (const auto& student : students) {
if (student.id == id) {
return student.name;
}
}
return "";
}
int getOverallScoreById(string id) {
int overallScore = 0;
for (const auto& course : courses) {
if (course.id == id) {
overallScore += course.overallScore;
}
}
return overallScore;
}
int getObtainedCreditById(string id) {
int obtainedCredit = 0;
for (const auto& course : courses) {
if (course.id == id) {
obtainedCredit += course.obtainedCredit;
}
}
return obtainedCredit;
}
};
int main() {
StudentManagementSystem system;
system.readStudentData("A.txt");
system.readCourseData("B.txt");
int choice;
string idOrName;
string dormitory;
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 << "0. 退出" << endl;
cout << "请输入选择:" << endl;
cin >> choice;
switch (choice) {
case 1:
cout << "请输入学号或姓名:" << endl;
cin >> idOrName;
system.printStudentInfo(idOrName);
break;
case 2:
cout << "请输入学号:" << endl;
cin >> idOrName;
system.printCourseInfo(idOrName);
break;
case 3:
cout << "请输入学号:" << endl;
cin >> idOrName;
system.deleteStudent(idOrName);
break;
case 4:
system.sortStudentsByOverallScore(true);
break;
case 5:
system.sortStudentsByOverallScore(false);
break;
case 6:
system.sortStudentsByObtainedCredit(true);
break;
case 7:
system.sortStudentsByObtainedCredit(false);
break;
case 0:
return 0;
default:
cout << "无效选择" << endl;
break;
}
}
return 0;
}
总结
该项目提供了一个简单的学生管理系统示例,涵盖了基本的学生信息和成绩管理功能。在实际应用中,您可能需要添加更多功能和错误处理机制以满足更复杂的需求。
原文地址: https://www.cveoy.top/t/topic/o1Ab 著作权归作者所有。请勿转载和采集!