diff --git a/源.cpp b/源.cpp new file mode 100644 index 0000000..85a9f0f --- /dev/null +++ b/源.cpp @@ -0,0 +1,96 @@ +#include +#include +#include +#include + +struct Student { + int id; + std::string name; + std::string gender; + int age; + std::string major; +}; + +class StudentManagementSystem { + +public: + // ѧϢ + std::vector students; + void addStudent(const Student& s) { + students.push_back(s); + } + + // ѯѧϢ + void searchStudent(int id) { + bool found = false; + for (const Student& s : students) { + if (s.id == id) { + std::cout << "ѧţ" << s.id << ", " << s.name << ", Ա" << s.gender << ", 䣺" << s.age << ", רҵ" << s.major << std::endl; + found = true; + break; + } + } + if (!found) { + std::cout << "δҵѧŵѧ" << std::endl; + } + } + + // ɾѧϢ + void deleteStudent(int id) { + auto it = students.begin(); + while (it != students.end()) { + if (it->id == id) { + it = students.erase(it); + } + else { + ++it; + } + } + } + + // ѧϢ + void insertStudent(const Student& s) { + students.push_back(s); + } + + // ѧ + void sortByID() { + std::sort(students.begin(), students.end(), [](const Student& a, const Student& b) { + return a.id < b.id; + }); + } +}; + +int main() { + StudentManagementSystem sms; + + // һЩѧϢ + Student s1 = { 1, "", "", 20, "ѧ" }; + Student s2 = { 2, "", "Ů", 21, "ӹ" }; + Student s3 = { 3, "", "", 19, "ѧ" }; + + sms.addStudent(s1); + sms.addStudent(s2); + sms.addStudent(s3); + + // ѯѧϢ + sms.searchStudent(2); + + // ɾѧϢ + sms.deleteStudent(2); + + // ѧϢ + Student s4 = { 4, "", "Ů", 22, "ѧ" }; + sms.insertStudent(s4); + + // + sms.sortByID(); + + // ѧϢ + std::cout << "ѧϢ" << std::endl; + for (const Student& s : sms.students) { + std::cout << "ѧţ" << s.id << ", " << s.name << ", Ա" << s.gender << ", 䣺" << s.age << ", רҵ" << s.major << std::endl; + } + + return 0; +} \ No newline at end of file