C++ 学籍管理系统设计:实现基本功能
#include
using namespace std;
// 学生基本信息结构体 struct StudentInfo { string id; // 学号 string name; // 姓名 string gender; // 性别 string dorm; // 宿舍号码 string phone; // 电话号码 };
// 学生成绩结构体 struct StudentScore { string id; // 学号 string course_id; // 课程编号 string course_name; // 课程名称 int credit; // 学分 double usual_score; // 平时成绩 double lab_score; // 实验成绩 double exam_score; // 卷面成绩 double total_score; // 综合成绩 int earned_credit; // 实得学分 };
// 学籍管理系统类 class StudentManagementSystem { public: // 构造函数 StudentManagementSystem() { // 读取学生基本信息 ifstream fin("A.TXT"); string line; while (getline(fin, line)) { StudentInfo info; info.id = line.substr(0, 2); info.name = line.substr(3, 6); info.gender = line.substr(10, 1); info.dorm = line.substr(12, 3); info.phone = line.substr(16, 8); m_studentInfos.push_back(info); } fin.close();
// 读取学生成绩信息
fin.open("B.TXT");
while (getline(fin, line)) {
StudentScore score;
score.id = line.substr(0, 2);
score.course_id = line.substr(3, 3);
score.course_name = line.substr(7, 6);
score.credit = stoi(line.substr(14, 1));
score.usual_score = stod(line.substr(16, 2));
score.lab_score = stod(line.substr(19, 2));
score.exam_score = stod(line.substr(22, 2));
if (score.lab_score == -1) { // 无实验
score.total_score = score.usual_score * 0.3 + score.exam_score * 0.7;
} else { // 有实验
score.total_score = score.usual_score * 0.15 + score.lab_score * 0.15 + score.exam_score * 0.7;
}
// 计算实得学分
if (score.total_score >= 90) {
score.earned_credit = score.credit;
} else if (score.total_score >= 80) {
score.earned_credit = score.credit * 0.8;
} else if (score.total_score >= 70) {
score.earned_credit = score.credit * 0.75;
} else if (score.total_score >= 60) {
score.earned_credit = score.credit * 0.6;
} else {
score.earned_credit = score.credit * 0;
}
m_studentScores.push_back(score);
}
fin.close();
}
// 数据录入功能
void inputScore() {
StudentScore score;
cout << "请输入学号:";
cin >> score.id;
cout << "请输入课程编号:";
cin >> score.course_id;
cout << "请输入课程名称:";
cin >> score.course_name;
cout << "请输入学分:";
cin >> score.credit;
cout << "请输入平时成绩:";
cin >> score.usual_score;
cout << "请输入实验成绩(无实验请输-1):";
cin >> score.lab_score;
cout << "请输入卷面成绩:";
cin >> score.exam_score;
if (score.lab_score == -1) { // 无实验
score.total_score = score.usual_score * 0.3 + score.exam_score * 0.7;
} else { // 有实验
score.total_score = score.usual_score * 0.15 + score.lab_score * 0.15 + score.exam_score * 0.7;
}
// 计算实得学分
if (score.total_score >= 90) {
score.earned_credit = score.credit;
} else if (score.total_score >= 80) {
score.earned_credit = score.credit * 0.8;
} else if (score.total_score >= 70) {
score.earned_credit = score.credit * 0.75;
} else if (score.total_score >= 60) {
score.earned_credit = score.credit * 0.6;
} else {
score.earned_credit = score.credit * 0;
}
m_studentScores.push_back(score);
// 写入文件
ofstream fout("B.TXT", ios::app);
fout << score.id << " " << score.course_id << " " << score.course_name << " " << score.credit << " "
<< score.usual_score << " " << score.lab_score << " " << score.exam_score << endl;
fout.close();
}
// 学生基本情况查询
void searchStudentInfo() {
int choice;
cout << "请选择查询方式(1-学号,2-姓名):";
cin >> choice;
if (choice == 1) {
string id;
cout << "请输入学号:";
cin >> id;
for (auto info : m_studentInfos) {
if (info.id == id) {
cout << "学号:" << info.id << " 姓名:" << info.name << " 性别:" << info.gender
<< " 宿舍号码:" << info.dorm << " 电话号码:" << info.phone << endl;
return;
}
}
cout << "未找到该学生" << endl;
} else if (choice == 2) {
string name;
cout << "请输入姓名:";
cin >> name;
for (auto info : m_studentInfos) {
if (info.name == name) {
cout << "学号:" << info.id << " 姓名:" << info.name << " 性别:" << info.gender
<< " 宿舍号码:" << info.dorm << " 电话号码:" << info.phone << endl;
}
}
} else {
cout << "输入有误" << endl;
}
}
// 成绩查询
void searchScore() {
string id;
cout << "请输入学号:";
cin >> id;
cout << "学号:" << id << endl;
double total_score_sum = 0; // 综合成绩之和
int earned_credit_sum = 0; // 实得学分之和
for (auto score : m_studentScores) {
if (score.id == id) {
cout << "课程编号:" << score.course_id << " 课程名称:" << score.course_name
<< " 综合成绩:" << setprecision(2) << fixed << score.total_score
<< " 实得学分:" << score.earned_credit << endl;
total_score_sum += score.total_score;
earned_credit_sum += score.earned_credit;
}
}
cout << "共修:" << m_studentScores.size() << " 科,实得总学分为:" << earned_credit_sum << endl;
cout << "平均综合成绩为:" << setprecision(2) << fixed << total_score_sum / m_studentScores.size() << endl;
}
// 删除功能
void deleteStudent(string id) {
// 删除学生基本信息
for (auto it = m_studentInfos.begin(); it != m_studentInfos.end(); ++it) {
if (it->id == id) {
m_studentInfos.erase(it);
break;
}
}
// 删除学生成绩信息
vector<StudentScore> newScores;
for (auto score : m_studentScores) {
if (score.id != id) {
newScores.push_back(score);
}
}
m_studentScores = newScores;
// 更新文件
ofstream fout("A.TXT");
for (auto info : m_studentInfos) {
fout << info.id << " " << info.name << " " << info.gender << " " << info.dorm << " " << info.phone << endl;
}
fout.close();
fout.open("B.TXT");
for (auto score : m_studentScores) {
fout << score.id << " " << score.course_id << " " << score.course_name << " " << score.credit << " "
<< score.usual_score << " " << score.lab_score << " " << score.exam_score << endl;
}
fout.close();
}
// 排序功能
void sortScores(bool by_total_score, bool ascending) {
if (by_total_score) { // 按综合成绩排序
if (ascending) { // 升序
sort(m_studentScores.begin(), m_studentScores.end(), [](const StudentScore& a, const StudentScore& b) {
return a.total_score < b.total_score;
});
} else { // 降序
sort(m_studentScores.begin(), m_studentScores.end(), [](const StudentScore& a, const StudentScore& b) {
return a.total_score > b.total_score;
});
}
} else { // 按实得学分排序
if (ascending) { // 升序
sort(m_studentScores.begin(), m_studentScores.end(), [](const StudentScore& a, const StudentScore& b) {
return a.earned_credit < b.earned_credit;
});
} else { // 降序
sort(m_studentScores.begin(), m_studentScores.end(), [](const StudentScore& a, const StudentScore& b) {
return a.earned_credit > b.earned_credit;
});
}
}
// 输出排序结果
cout << "学号\t课程编号\t课程名称\t学分\t平时成绩\t实验成绩\t卷面成绩\t综合成绩\t实得学分" << endl;
for (auto score : m_studentScores) {
cout << score.id << "\t" << score.course_id << "\t\t" << score.course_name << "\t\t" << score.credit << "\t"
<< score.usual_score << "\t\t" << score.lab_score << "\t\t" << score.exam_score << "\t\t"
<< setprecision(2) << fixed << score.total_score << "\t\t" << score.earned_credit << endl;
}
}
private:
vector
int main() { StudentManagementSystem sms; int choice; while (true) { cout << "请选择功能(1-数据录入,2-学生基本情况查询,3-成绩查询,4-删除,5-排序,0-退出):"; cin >> choice; switch (choice) { case 1: // 数据录入功能 sms.inputScore(); break; case 2: // 学生基本情况查询 sms.searchStudentInfo(); break; case 3: // 成绩查询 sms.searchScore(); break; case 4: { // 删除功能 string id; cout << "请输入要删除的学号:"; cin >> id; sms.deleteStudent(id); break; } case 5: { // 排序功能 int sortChoice, orderChoice; cout << "请选择排序方式(1-按综合成绩,2-按实得学分):"; cin >> sortChoice; cout << "请选择排序顺序(1-升序,2-降序):"; cin >> orderChoice; sms.sortScores(sortChoice == 1, orderChoice == 1); break; } case 0: // 退出程序 return 0; default: cout << "输入有误,请重新输入" << endl; } } return 0;
原文地址: https://www.cveoy.top/t/topic/oWVF 著作权归作者所有。请勿转载和采集!