实现功能
- 添加、删除、修改学生
- 为学生添加、删除、修改成绩
- 将数据保存在students.txt和grades.txt里
效果图(部分功能)
添加学生
添加成绩
源代码
这里就不分多个文件了 编译时在连接器命令行加入以下命令 -std=c++11
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
#include <limits>
using namespace std;
struct Student {
string id;
string name;
string gender;
int age;
string class_name;
};
struct Grade {
string student_id;
string course_name;
int grade;
};
vector<Student> students;
vector<Grade> grades;
void loadStudents() {
ifstream infile("students.txt");
if (infile.is_open()) {
Student s;
while (infile >> s.id >> s.name >> s.gender >> s.age >> s.class_name) {
students.push_back(s);
}
infile.close();
}
}
void saveStudents() {
ofstream outfile("students.txt");
for (const auto& s : students) {
outfile << s.id << " " << s.name << " " << s.gender << " " << s.age << " " << s.class_name << endl;
}
}
void loadGrades() {
ifstream infile("grades.txt");
if (infile.is_open()) {
Grade g;
while (infile >> g.student_id >> g.course_name >> g.grade) {
grades.push_back(g);
}
infile.close();
}
}
void saveGrades() {
ofstream outfile("grades.txt");
for (const auto& g : grades) {
outfile << g.student_id << " " << g.course_name << " " << g.grade << endl;
}
}
void addStudent() {
Student s;
cout << "输入学生学号 Enter student ID: ";
cin >> s.id;
cout << "输入学生姓名 Enter student name: ";
cin >> s.name;
cout << "输入学生性别 Enter student gender: ";
cin >> s.gender;
cout << "输入学生年龄 Enter student age: ";
while (!(cin >> s.age)) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "无效输入,请输入有效的年龄 Invalid input, please enter a valid age: ";
}
cout << "输入学生班级 Enter student class: ";
cin >> s.class_name;
students.push_back(s);
saveStudents();
cout << "学生添加成功 Student added successfully." << endl;
}
void modifyStudent() {
string id;
cout << "输入要修改的学生ID Enter student ID to modify: ";
cin >> id;
auto it = find_if(students.begin(), students.end(), [&id](const Student& s) {
return s.id == id;
});
if (it != students.end()) {
cout << "输入学生新的姓名 Enter new student name: ";
cin >> it->name;
cout << "输入学生新的性别 Enter new student gender: ";
cin >> it->gender;
cout << "输入学生新的年龄 Enter new student age: ";
while (!(cin >> it->age)) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "无效输入,请输入有效的年龄 Invalid input, please enter a valid age: ";
}
cout << "输入学生新的班级 Enter new student class: ";
cin >> it->class_name;
saveStudents();
cout << "学生信息修改成功 Student information modified successfully." << endl;
} else {
cout << "找不到学生ID Student ID not found." << endl;
}
}
void deleteStudent() {
string id;
cout << "输入要删除的学生ID Enter student ID to delete: ";
cin >> id;
auto it = remove_if(students.begin(), students.end(), [&id](const Student& s) {
return s.id == id;
});
if (it != students.end()) {
students.erase(it, students.end());
saveStudents();
cout << "学生已成功删除 Student deleted successfully." << endl;
} else {
cout << "找不到学生ID Student ID not found." << endl;
}
}
void queryStudent() {
string id;
cout << "输入要查询的学生ID Enter student ID to query: ";
cin >> id;
auto it = find_if(students.begin(), students.end(), [&id](const Student& s) {
return s.id == id;
});
if (it != students.end()) {
cout << "学生学号 Student ID: " << it->id << endl;
cout << "学生姓名 Student name: " << it->name << endl;
cout << "学生性别 Student gender: " << it->gender << endl;
cout << "学生年龄 Student age: " << it->age << endl;
cout << "学生班级 Student class: " << it->class_name << endl;
} else {
cout << "找不到学生ID Student ID not found." << endl;
}
}
void addGrade() {
Grade g;
cout << "输入学生学号 Enter student ID: ";
cin >> g.student_id;
cout << "输入课程名称 Enter course name: ";
cin >> g.course_name;
cout << "输入等级 Enter grade: ";
while (!(cin >> g.grade)) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "无效输入,请输入有效的等级 Invalid input, please enter a valid grade: ";
}
grades.push_back(g);
saveGrades();
cout << "成绩添加成功 Grade added successfully." << endl;
}
void modifyGrade() {
string id, course;
cout << "输入学生学号 Enter student ID: ";
cin >> id;
cout << "输入课程名称 Enter course name: ";
cin >> course;
auto it = find_if(grades.begin(), grades.end(), [&id, &course](const Grade& g) {
return g.student_id == id && g.course_name == course;
});
if (it != grades.end()) {
cout << "输入新的等级 Enter new grade: ";
while (!(cin >> it->grade)) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "无效输入,请输入有效的等级 Invalid input, please enter a valid grade: ";
}
saveGrades();
cout << "成绩修改成功 Grade modified successfully." << endl;
} else {
cout << "未找到成绩记录 Grade record not found." << endl;
}
}
void deleteGrade() {
string id, course;
cout << "输入学生学号 Enter student ID: ";
cin >> id;
cout << "输入课程名称 Enter course name: ";
cin >> course;
auto it = remove_if(grades.begin(), grades.end(), [&id, &course](const Grade& g) {
return g.student_id == id && g.course_name == course;
});
if (it != grades.end()) {
grades.erase(it, grades.end());
saveGrades();
cout << "成绩删除成功 Grade deleted successfully." << endl;
} else {
cout << "未找到成绩记录 Grade record not found." << endl;
}
}
void queryGrade() {
string id, course;
cout << "输入学生学号 Enter student ID: ";
cin >> id;
cout << "输入课程名称 Enter course name: ";
cin >> course;
auto it = find_if(grades.begin(), grades.end(), [&id, &course](const Grade& g) {
return g.student_id == id && g.course_name == course;
});
if (it != grades.end()) {
cout << "学生学号 Student ID: " << it->student_id << endl;
cout << "课程名称 Course name: " << it->course_name << endl;
cout << "成绩 Grade: " << it->grade << endl;
} else {
cout << "未找到成绩记录 Grade record not found." << endl;
}
}
void displayMenu() {
cout << "学生管理系统 Student Management System" << endl;
cout << "1.添加学生 Add Student" << endl;
cout << "2.修改学生 Modify Student" << endl;
cout << "3.删除学生 Delete Student" << endl;
cout << "4.查找学生 Query Student" << endl;
cout << "5.添加成绩 Add Grade" << endl;
cout << "6.修改成绩 Modify Grade" << endl;
cout << "7.删除成绩 Delete Grade" << endl;
cout << "8.查找成绩 Query Grade" << endl;
cout << "9.退出 Exit" << endl;
}
enum MenuOption {
ADD_STUDENT = 1,
MODIFY_STUDENT,
DELETE_STUDENT,
QUERY_STUDENT,
ADD_GRADE,
MODIFY_GRADE,
DELETE_GRADE,
QUERY_GRADE,
EXIT
};
int main() {
loadStudents();
loadGrades();
int choice;
do {
displayMenu();
cout << "输入您的选择 Enter your choice: ";
while (!(cin >> choice)) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "无效输入,请输入有效的选项 Invalid input, please enter a valid option: ";
}
MenuOption option = static_cast<MenuOption>(choice);
switch (option) {
case ADD_STUDENT:
addStudent();
break;
case MODIFY_STUDENT:
modifyStudent();
break;
case DELETE_STUDENT:
deleteStudent();
break;
case QUERY_STUDENT:
queryStudent();
break;
case ADD_GRADE:
addGrade();
break;
case MODIFY_GRADE:
modifyGrade();
break;
case DELETE_GRADE:
deleteGrade();
break;
case QUERY_GRADE:
queryGrade();
break;
case EXIT:
cout << "退出 Exiting..." << endl;
break;
default:
cout << "选择无效,请重试 Invalid choice. Please try again." << endl;
}
} while (choice != EXIT);
return 0;
}