C++ 简单的信息管理系统

    技术2026-08-06  20

    一次比较综合性的大作业 主要使用的就是通过Vector容器来处理数据 关于vector容器的增,删,改,查等等用法 也复习了很多关于C++的一些知识 比如 类,多态,指针之类的

    #include<iostream> #include<string> #include<vector> //容器 #include<fstream> using namespace std; /*summary Student类: 这个类代表的是每一个学生的信息,包括基本信息(姓名,年龄,性别,报名情况等) 用一个vector容器来放学生得报名情况 主要方法功能有学生课程得增删改查 */ class Student { public: int age; string name; string sex; vector<string> lectures; string lectures_list; public: Student() { } ~Student() { } void input() { cout << "请输入姓名: "; string name; cin >> name; this->name = name; cout << endl; cout << "请输入性别: "; string sex; cin >> sex; this->sex = sex; cout << endl; cout << "请输入年龄: "; int age; cin >> age; this->age = age; } void show() { cout << "名字是: " << this->name << endl; cout << "性别是: " << this->sex << endl; cout << "年龄是: " << this->age << endl; } void add_lecture(string lecture_name) { this->lectures.push_back(lecture_name); } void show_lecture() { for (int i = 0; i < this->lectures.size(); i++) { cout << this->lectures[i] << endl; } } void addLectureName() { for (int i = 0; i < this->lectures.size(); i++) { this->lectures_list += this->lectures[i] + " "; } } void delete_lecture(string value) { vector<string>::iterator it; for (it = this->lectures.begin(); it != this->lectures.end();) { if (*it == value) { it = this->lectures.erase(it); } else { ++it; } } } }; vector<Student> students; //学生总名单 //Summary //Lecture类: // Lecture类是课程的总类,可以派生出艺术类和体育类 class Lecture { public: string lecture_name; //虚函数 virtual string get_lecturename(int judge) { return NULL; }; }; //summary //Art类: // 继承自Lecture类 // 通过一个get_lecturename方法来获取学生所报名的课程 class Art : public Lecture { //文艺类 public: string get_lecturename(int judge) { if (judge == 0) { return "音乐"; } else if (judge == 1) { return "设计"; } else { return "编程"; } } }; //summary //Sport类: // 继承自Lecture类 // 通过一个get_lecturename方法来获取学生所报名的课程 class Sport : public Lecture { //体育类 public: string get_lecturename(int judge) { if (judge == 0) { return "拳击"; } else { return "跆拳道"; } } }; //summary //add_student()方法: // 主要功能是添加学生 // 所使用的方法是先创建一个学生,然后通过输入信息完善学生的报名情况。 // 最后再加入Student的vector容器中 void add_student() { //添加学生 Student stu = Student(); Lecture* lecture_Type; Art art = Art(); Sport sport = Sport(); stu.input(); while (1) { cout << "请选择学生想要学习的类型" << endl; cout << "0、体育类" << endl << "1、文艺类" << endl; int chose; cin >> chose; if (chose == 0) { lecture_Type = &sport; cout << "0、拳击" << endl << "1、跆拳道" << endl; } else { lecture_Type = &art; cout << "0、音乐" << endl << "1、设计" << endl << "2、编程" << endl; } int chose_lec; cin >> chose_lec; string lecture_name = lecture_Type->get_lecturename(chose_lec); stu.add_lecture(lecture_name); cout << "是否添加课程? y/n"; char chose_continue; cin >> chose_continue; if (chose_continue == 'n') { students.push_back(stu); break; } } } //summary //delete_student()方法: // 主要功能是删除某个学生 // 通过先输入需要删除学生的名字,然后判断Student容器中是否存在该学生 // 存在就删除,不存在就提示输入有误 void delete_student() { //删除某个学生 cout << "请输入需要删除的学生姓名:" << endl; string name; cin >> name; vector<Student>::iterator it; bool is_find = false; for (it = students.begin(); it != students.end();) { if (it->name == name) { it = students.erase(it); is_find = true; } else { ++it; } } if (is_find == true) { cout << "已经成功删除 " << name << " 同学的信息。" << endl; } else { cout << "没有找到,请重新确认输入是否有误" << endl; } } //summary //recompose_student()方法: // 主要功能修改学生信息 // 也是先通过输入姓名查找学生,再创建一个Student指针指向找到的学生 // 再通过指针来修改学生的信息 void recompose_student() { //修改学生信息 cout << "请输入想要修改的学生姓名" << endl; bool is_find = false; Student* FindStu = NULL; string name; cin >> name; for (int i = 0; i < students.size(); i++) { if (students[i].name == name) { is_find = true; FindStu = &students[i]; } } if (FindStu == NULL) { cout << "没有找到该学生,请确认输入是否有误。" << endl; } else { cout << "查找的学生是" << FindStu->name << "同学." << endl; cout << "该学生的补课内容是:" << endl; FindStu->show_lecture(); cout << "请输入想要删除的课程名字。" << endl; string name_lecture; cin >> name_lecture; vector<string>::iterator it; it = find(FindStu->lectures.begin(), FindStu->lectures.end(), name_lecture); bool is_Consist = false; if (it != FindStu->lectures.end()) { is_Consist = true; } else { is_Consist = false; } if (is_Consist == true) { FindStu->delete_lecture(name_lecture); cout << name_lecture << "课程已经删除成功!" << endl; } else { cout << "没有找到该课程,请重新操作!" << endl; } } } //summary //see_student()方法: // 主要实现功能是查询学生的报名信息 // 通过输入名字来查找 void see_student() { //查询特定学生 cout << "您想要查询哪个学生? 请输入姓名:" << endl;; string name; cin >> name; bool isFind = false; for (int i = 0; i < students.size(); i++) { if (students[i].name == name) { cout << name << "的基本信息如下:" << endl; students[i].show(); students[i].show_lecture(); isFind = true; break; } } if (isFind == true) {} else { cout << name << "没有找到!请确认输入是否有误!" << endl; } } //summary //show_nameList()方法: // 主要功能是展示已经报名的学生基本信息 // 通过遍历Student容器得到 void show_nameList() { //展示已报名的学生列表 int count = 0; for (int i = 0; i < students.size(); i++) { cout << ++count << "-" << students[i].name << endl; } } //summary //save()方法(): // 主要功能是将当前的信息保存到本地文件"data.txt"中去 // 通过fstream中的 open()方法和write来向文件中写入信息 void save() { //保存信息到本地文件 int count = 0; fstream write; ofstream outfile("data.txt"); write.open("data.txt"); for (int i = 0; i < students.size(); i++) { students[i].addLectureName(); write << ++count << " " << students[i].name << " " << students[i].sex << " " << students[i].age << " | " << students[i].lectures_list << endl; students[i].lectures_list = ""; } write.close(); } int main() { //主菜单 cout << "欢迎使用学生选课 信息管理系统" << endl; while (1) { cout << "=====================================" << endl; cout << "1、添加学生" << endl; cout << "2、删除学生" << endl; cout << "3、修改学生" << endl; cout << "4、查询学生" << endl; cout << "5、列出现有的学生名单" << endl; cout << "6、将信息输出到一个txt文件中" << endl; cout << "9、退出程序" << endl<<"======================================"<<endl; int input; cin >> input; if (input == 9) { break; } else if (input == 1) { add_student(); } else if (input == 2) { delete_student(); } else if (input == 3) { recompose_student(); } else if (input == 4) { see_student(); } else if (input == 5) { show_nameList(); } else if (input == 6) { save(); } else { break; } } }
    Processed: 0.008, SQL: 9