C++学生学籍管理系统设计与实现
C++学生学籍管理系统设计与实现
本项目使用C++语言实现了一个简单的学籍管理系统,包含学生基本信息和成绩信息的录入、查询、删除和排序功能。
1. 系统设计
1.1 数据结构
- 学生基本信息:
struct Student { string id; string name; string gender; string dormNumber; string phoneNumber; }; - 学生成绩:
struct Score { string id; string courseCode; string courseName; int credit; int dailyScore; int labScore; int examScore; int totalScore; int earnedCredit; };
1.2 系统功能
- 数据录入: 从文本文件中读取学生基本信息和成绩信息。
- 查询功能:
- 按学号或姓名查询学生基本信息。
- 按宿舍号码查询学生基本信息。
- 按学号查询学生成绩。
- 删除功能: 删除指定学生的信息。
- 排序功能: 按综合成绩或实得学分升序或降序排序学生成绩。
2. 代码实现
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
// 定义学生基本信息的结构体
struct Student {
string id;
string name;
string gender;
string dormNumber;
string phoneNumber;
};
// 定义学生成绩的结构体
struct Score {
string id;
string courseCode;
string courseName;
int credit;
int dailyScore;
int labScore;
int examScore;
int totalScore;
int earnedCredit;
};
// 学生管理系统类
class StudentManagementSystem {
private:
vector<Student> students; // 存储学生基本信息的容器
vector<Score> scores; // 存储学生成绩的容器
public:
// 从A.TXT文件中读取学生基本信息
void readStudentData() {
ifstream file("A.TXT");
string line;
while (getline(file, line)) {
Student student;
student.id = line.substr(0, 2);
student.name = line.substr(3, 6);
student.gender = line.substr(10, 2);
student.dormNumber = line.substr(13, 3);
student.phoneNumber = line.substr(18, 8);
students.push_back(student);
}
file.close();
}
// 从B.TXT文件中读取学生成绩
void readScoreData() {
ifstream file("B.TXT");
string line;
while (getline(file, line)) {
Score score;
score.id = line.substr(0, 2);
score.courseCode = line.substr(3, 3);
score.courseName = line.substr(7, 9);
score.credit = stoi(line.substr(17, 1));
score.dailyScore = stoi(line.substr(19, 2));
score.labScore = stoi(line.substr(22, 2));
score.examScore = stoi(line.substr(25, 2));
score.totalScore = calculateTotalScore(score.dailyScore, score.labScore, score.examScore);
score.earnedCredit = calculateEarnedCredit(score.totalScore, score.credit);
scores.push_back(score);
}
file.close();
}
// 计算综合成绩
int calculateTotalScore(int dailyScore, int labScore, int examScore) {
if (labScore == -1) {
return dailyScore * 0.3 + examScore * 0.7;
} else {
return dailyScore * 0.15 + labScore * 0.15 + examScore * 0.7;
}
}
// 计算实得学分
int calculateEarnedCredit(int totalScore, int credit) {
if (totalScore >= 90) {
return credit;
} else if (totalScore >= 80) {
return credit * 0.8;
} else if (totalScore >= 70) {
return credit * 0.75;
} else if (totalScore >= 60) {
return credit * 0.6;
} else {
return 0;
}
}
// 根据学号或姓名查询学生基本信息
void searchStudentInfo(string keyword) {
for (const auto& student : students) {
if (student.id == keyword || student.name == keyword) {
cout << "学号: " << student.id << "\t姓名: " << student.name << "\t性别: " << student.gender << "\t宿舍号码: " << student.dormNumber << "\t电话号码: " << student.phoneNumber << endl;
}
}
}
// 根据宿舍号码查询学生基本信息
void searchStudentInfoByDormNumber(string dormNumber) {
for (const auto& student : students) {
if (student.dormNumber == dormNumber) {
cout << "学号: " << student.id << "\t姓名: " << student.name << "\t性别: " << student.gender << "\t宿舍号码: " << student.dormNumber << "\t电话号码: " << student.phoneNumber << endl;
}
}
}
// 根据学号查询学生成绩
void searchScoreInfo(string id) {
int totalEarnedCredit = 0;
for (const auto& score : scores) {
if (score.id == id) {
cout << "学号: " << score.id << "\t课程编号: " << score.courseCode << "\t课程名称: " << score.courseName << "\t综合成绩: " << score.totalScore << "\t实得学分: " << score.earnedCredit << endl;
totalEarnedCredit += score.earnedCredit;
}
}
cout << "共修: " << scores.size() << "科,实得总学分为: " << totalEarnedCredit << endl;
}
// 删除学生信息
void deleteStudentInfo(string id) {
students.erase(remove_if(students.begin(), students.end(), [&](const Student& student) {
return student.id == id;
}), students.end());
scores.erase(remove_if(scores.begin(), scores.end(), [&](const Score& score) {
return score.id == id;
}), scores.end());
}
// 按综合成绩升序排序学生成绩
void sortScoresByTotalScoreAscending() {
sort(scores.begin(), scores.end(), [](const Score& a, const Score& b) {
return a.totalScore < b.totalScore;
});
}
// 按实得学分升序排序学生成绩
void sortScoresByEarnedCreditAscending() {
sort(scores.begin(), scores.end(), [](const Score& a, const Score& b) {
return a.earnedCredit < b.earnedCredit;
});
}
// 按综合成绩降序排序学生成绩
void sortScoresByTotalScoreDescending() {
sort(scores.begin(), scores.end(), [](const Score& a, const Score& b) {
return a.totalScore > b.totalScore;
});
}
// 按实得学分降序排序学生成绩
void sortScoresByEarnedCreditDescending() {
sort(scores.begin(), scores.end(), [](const Score& a, const Score& b) {
return a.earnedCredit > b.earnedCredit;
});
}
};
int main() {
StudentManagementSystem system;
system.readStudentData();
system.readScoreData();
string keyword;
cout << "请输入学号或姓名进行查询:";
cin >> keyword;
system.searchStudentInfo(keyword);
string dormNumber;
cout << "请输入宿舍号码进行查询:";
cin >> dormNumber;
system.searchStudentInfoByDormNumber(dormNumber);
string id;
cout << "请输入学号进行查询:";
cin >> id;
system.searchScoreInfo(id);
string deleteId;
cout << "请输入要删除的学生学号:";
cin >> deleteId;
system.deleteStudentInfo(deleteId);
system.sortScoresByTotalScoreAscending();
system.sortScoresByEarnedCreditAscending();
system.sortScoresByTotalScoreDescending();
system.sortScoresByEarnedCreditDescending();
return 0;
}
3. 总结
本项目是一个简单的学生学籍管理系统,展示了C++语言在数据结构和算法方面的应用,并实现了基本的系统功能。可以根据实际需求进行扩展和完善。
4. 未来改进方向
- 图形界面: 使用Qt等框架实现图形界面,提升用户体验。
- 数据库连接: 将数据存储到数据库中,提高数据管理效率。
- 增加功能模块: 添加例如课程管理、教师管理等功能模块。
- 数据验证: 添加数据验证功能,确保数据输入的正确性和完整性。
原文地址: https://www.cveoy.top/t/topic/o1EL 著作权归作者所有。请勿转载和采集!