• C++学习笔记(三十五)


    在完成对C语言的学习后,我最近开始了对C++和Java的学习,目前跟着视频学习了一些语法,也跟着敲了一些代码,有了一定的掌握程度。现在将跟着视频做的笔记进行整理。本篇博客是整理C++知识点的第三十五篇博客。

    本篇博客用C++实现了机房预约系统,本文是中部分。

    本系列博客所有C++代码都在Visual Studio 2022环境下编译运行。程序为64位。

    目录

    机房预约系统

    管理员子菜单搭建以及注销实现

    获取文件中学生和老师信息

    管理员添加账号实现

    管理员查看账号实现

    管理员查看机房信息实现

    管理员清空预约实现

    学生子菜单搭建以及注销实现

    学生申请预约功能实现


    机房预约系统

    接下来用C++实现一个机房预约系统,由于内容很长,分为三篇博客。本篇博客是第二部分。

    管理员子菜单搭建以及注销实现

    1. void do_administrator(identity* person)
    2. {
    3. cout << "Welcome, " << person->name << " !" << endl;
    4. administrator* adm = (administrator*)person;
    5. while (true) {
    6. adm->show_menu();
    7. int choice;
    8. cout << "Please enter your choice" << endl;
    9. cin >> choice;
    10. if (choice == 1) {
    11. adm->add_account();
    12. }
    13. else if (choice == 2) {
    14. adm->look_account();
    15. }
    16. else if (choice == 3) {
    17. adm->look_room();
    18. }
    19. else if (choice == 4) {
    20. adm->clear_record();
    21. }
    22. else if (choice == 0) {
    23. delete adm;
    24. cout << "Have logged out" << endl;
    25. system("pause");
    26. system("cls");
    27. break;
    28. }
    29. else {
    30. cout << "Enter error, please enter 1,2,3,4 or 0" << endl;
    31. system("pause");
    32. system("cls");
    33. }
    34. }
    35. }

    这是reservesystem.cpp中的内容。首先给出提示欢迎用户,然后将多态的指针向下转型,以调用子类的成员函数。随后进入循环,给出菜单并要求用户选择选项,每个选项进入不同的成员函数,输入0就销毁此指针并表示注销,随后退出此函数。输入其他选项就要求用户重新输入。

    1. void administrator::show_menu()
    2. {
    3. cout << "****************************************" << endl;
    4. cout << "********** 1. add account **************" << endl;
    5. cout << "********** 2. look account *************" << endl;
    6. cout << "********** 3. look room ****************" << endl;
    7. cout << "********** 4. clear reservation ********" << endl;
    8. cout << "********** 0. log out ******************" << endl;
    9. cout << "****************************************" << endl;
    10. }

    这是administrator.cpp的内容,输出菜单。

    获取文件中学生和老师信息

    1. administrator::administrator(string name, string password)
    2. {
    3. this->name = name;
    4. this->password = password;
    5. vstudent.clear();
    6. vteacher.clear();
    7. init_vector();
    8. vroom.clear();
    9. }

    这是administrator类的构造函数,将传入信息进行赋值,随后将vstudent vteacher和vroom清空并获取学生和教师信息。 

    1. void administrator::init_vector(void)
    2. {
    3. ifstream ifs;
    4. ifs.open(STUDENT_FILE, ios::in);
    5. int sid;
    6. string sname;
    7. string spassword;
    8. while (ifs >> sid && ifs >> sname && ifs >> spassword) {
    9. student s(sid, sname, spassword);
    10. this->vstudent.push_back(s);
    11. }
    12. ifs.close();
    13. ifs.open(TEACHER_FILE, ios::in);
    14. int tid;
    15. string tname;
    16. string tpassword;
    17. while (ifs >> tid && ifs >> tname && ifs >> tpassword) {
    18. teacher t(tid, tname, tpassword);
    19. this->vteacher.push_back(t);
    20. }
    21. ifs.close();
    22. }

    首先遍历存放学生信息的文件,将信息读取并构建一个student类对象,随后加入vstudent容器中,直至文件结束。然后遍历存放教师信息的文件,将信息读取并构建一个teacher类对象,随后加入vteacher容器中,直至文件结束。

    管理员添加账号实现

    1. void administrator::add_account(void)
    2. {
    3. while (true) {
    4. int choice;
    5. cout << "********** 1. add student **************" << endl;
    6. cout << "********** 2. add teacher **************" << endl;
    7. cout << "Please enter the choice" << endl;
    8. cin >> choice;
    9. if (choice == 1) {
    10. ofstream ofs;
    11. ofs.open(STUDENT_FILE, ios::in | ios::app);
    12. int id;
    13. while (true) {
    14. cout << "Please enter the student's number" << endl;
    15. cin >> id;
    16. bool flag = check_repeat(id, 1);
    17. if (flag == false) {
    18. break;
    19. }
    20. else {
    21. cout << "It is repeat,please enter again" << endl;
    22. }
    23. }
    24. string name;
    25. cout << "Please enter the name" << endl;
    26. cin >> name;
    27. string password;
    28. cout << "Please enter the password" << endl;
    29. cin >> password;
    30. ofs << id << " " << name << " " << password << endl;
    31. ofs.close();
    32. student s(id, name, password);
    33. vstudent.push_back(s);
    34. cout << "Done" << endl;
    35. system("pause");
    36. system("cls");
    37. break;
    38. }
    39. else if (choice == 2) {
    40. ofstream ofs;
    41. ofs.open(TEACHER_FILE, ios::in | ios::app);
    42. int id;
    43. while (true) {
    44. cout << "Please enter the teacher's number" << endl;
    45. cin >> id;
    46. bool flag = check_repeat(id, 2);
    47. if (flag == false) {
    48. break;
    49. }
    50. else {
    51. cout << "It is repeat,please enter again" << endl;
    52. }
    53. }
    54. string name;
    55. cout << "Please enter the name" << endl;
    56. cin >> name;
    57. string password;
    58. cout << "Please enter the password" << endl;
    59. cin >> password;
    60. ofs << id << " " << name << " " << password << endl;
    61. ofs.close();
    62. teacher t(id, name, password);
    63. vteacher.push_back(t);
    64. cout << "Done" << endl;
    65. system("pause");
    66. system("cls");
    67. break;
    68. }
    69. else {
    70. cout << "Enter error" << endl;
    71. system("pause");
    72. system("cls");
    73. }
    74. }
    75. }

    首先进入一个循环,给出输入1增加学生输入2增加教师,要求用户输入,输入1和2就在完成添加功能后退出循环,否则要求用户重新输入。

    如果输入1,首先要求输入学号,并用check_repeat函数检查重复(这个函数下面会放),重复就要求重新输入,不重复就可进行下面操作。然后输入学生姓名和密码,将这些信息写入存放学生信息的文件中,同时构建一个student对象并加入vstudent中。

    如果输入2,首先要求输入教师编号,并用check_repeat函数检查重复(这个函数下面会放),重复就要求重新输入,不重复就可进行下面操作。然后输入教师姓名和密码,将这些信息写入存放教师信息的文件中,同时构建一个teacher对象并加入vteacher中。

    1. bool administrator::check_repeat(int id, int type)
    2. {
    3. if (type == 1) {
    4. for (vector::iterator it = vstudent.begin(); it != vstudent.end(); ++it) {
    5. if (it->id == id) {
    6. return true;
    7. }
    8. }
    9. return false;
    10. }
    11. else if (type == 2) {
    12. for (vector::iterator it = vteacher.begin(); it != vteacher.end(); ++it) {
    13. if (it->id == id) {
    14. return true;
    15. }
    16. }
    17. return false;
    18. }
    19. }

    这是check_repeat函数,检查重复。如果是学生,就遍历vstudent,如果有学号重复就返回true,否则返回false。如果是教师,就遍历vteacher,如果有编号重复就返回true,否则返回false。

    管理员查看账号实现

    1. void administrator::look_account(void)
    2. {
    3. while (true) {
    4. int choice;
    5. cout << "********** 1. look student *************" << endl;
    6. cout << "********** 2. look teacher *************" << endl;
    7. cout << "Please enter the choice" << endl;
    8. cin >> choice;
    9. if (choice == 1) {
    10. cout << "Here are all student's information" << endl;
    11. for_each(vstudent.begin(), vstudent.end(), lookstudent);
    12. break;
    13. }
    14. else if (choice == 2) {
    15. cout << "Here are all teacher's information" << endl;
    16. for_each(vteacher.begin(), vteacher.end(), lookteacher);
    17. break;
    18. }
    19. else {
    20. cout << "Please enter 1 or 2" << endl;
    21. }
    22. }
    23. system("pause");
    24. system("cls");
    25. }

    首先进入一个循环,给出输入1显示学生输入2显示教师,要求用户输入,输入1和2就在完成显示功能后退出循环,否则要求用户重新输入。输入1就遍历vstudent输出学生信息,输入2就遍历vteacher输出教师信息。

    1. void lookstudent(student s) {
    2. cout << "The student's id is " << s.id << " and the name is " << s.name << " and the password is " << s.password << endl;
    3. }
    4. void lookteacher(teacher t) {
    5. cout << "The teacher's id is " << t.id << " and the name is " << t.name << " and the password is " << t.password << endl;
    6. }

    这是for_each算法用到的函数。

    管理员查看机房信息实现

    1. #pragma once
    2. #include
    3. using namespace std;
    4. class room
    5. {
    6. public:
    7. int num;
    8. int capacity;
    9. };

    这是room类的内容,实现了room类对象。成员变量num表示编号,capacity表示容量。

    1. void administrator::look_room(void)
    2. {
    3. ifstream ifs;
    4. ifs.open(ROOM_FILE, ios::in);
    5. int num;
    6. int capacity;
    7. while (ifs >> num && ifs >> capacity) {
    8. room r;
    9. r.num = num;
    10. r.capacity = capacity;
    11. vroom.push_back(r);
    12. }
    13. for (vector::iterator it = vroom.begin(); it != vroom.end(); ++it) {
    14. cout << "The num is " << it->num << " and the capacity is " << it->capacity << endl;
    15. }
    16. ifs.close();
    17. system("pause");
    18. system("cls");
    19. }

    这是look_room函数,先从文件中获取信息并构建对象加入vroom中,随后遍历vroom输出相关信息。

    管理员清空预约实现

    1. void administrator::clear_record(void)
    2. {
    3. ofstream ofs(REVERSE_FILE, ios::trunc);
    4. ofs.close();
    5. cout << "It has been cleared" << endl;
    6. system("pause");
    7. system("cls");
    8. }

    进入此函数后,清空文件并提示已清空。

    学生子菜单搭建以及注销实现

    1. void do_student(identity* person)
    2. {
    3. cout << "Welcome " << person->name << '!' << endl;
    4. student* stu = (student*)person;
    5. while (true) {
    6. stu->show_menu();
    7. int choice;
    8. cout << "Please enter your choice " << endl;
    9. cin >> choice;
    10. if (choice == 1) {
    11. stu->make_reverse();
    12. }
    13. else if (choice == 2) {
    14. stu->look_my_reverse();
    15. }
    16. else if (choice == 3) {
    17. stu->look_all_reverse();
    18. }
    19. else if (choice == 4) {
    20. stu->cancel_reverse();
    21. }
    22. else if (choice == 0) {
    23. delete stu;
    24. cout << "Have logged out" << endl;
    25. system("pause");
    26. system("cls");
    27. break;
    28. }
    29. else {
    30. cout << "Enter error, please enter 1,2,3,4 or 0" << endl;
    31. system("pause");
    32. system("cls");
    33. }
    34. }
    35. }

    这是reservesystem.cpp中的内容。首先给出提示欢迎用户,然后将多态的指针向下转型,以调用子类的成员函数。随后进入循环,给出菜单并要求用户选择选项,每个选项进入不同的成员函数,输入0就销毁此指针并表示注销,随后退出此函数。输入其他选项就要求用户重新输入。

    1. void student::show_menu(void)
    2. {
    3. cout << "****************************************" << endl;
    4. cout << "********** 1. apply reservation ********" << endl;
    5. cout << "********** 2. look my reservation ******" << endl;
    6. cout << "********** 3. look all reservation *****" << endl;
    7. cout << "********** 4. cancel reservation *******" << endl;
    8. cout << "********** 0. log out ******************" << endl;
    9. cout << "****************************************" << endl;
    10. }

    这是student类的show_menu成员函数,输出菜单。

    学生申请预约功能实现

    1. student::student(int id, string name, string password)
    2. {
    3. this -> id = id;
    4. this->name = name;
    5. this->password = password;
    6. ifstream ifs;
    7. ifs.open(ROOM_FILE, ios::in);
    8. int num;
    9. int capacity;
    10. while (ifs >> num && ifs >> capacity) {
    11. room r;
    12. r.num = num;
    13. r.capacity = capacity;
    14. vroom.push_back(r);
    15. }
    16. ifs.close();
    17. }

    这是student类的构造函数,将传入的信息赋值,随后遍历存储机房信息的文件,获取机房信息并加入vroom。

    1. void student::make_reverse(void){
    2. int daychoice;
    3. while (true) {
    4. cout << "*The open time is from Monday to Friday*" << endl;
    5. cout << "********** 1. Monday *******************" << endl;
    6. cout << "********** 2. Tuesday ******************" << endl;
    7. cout << "********** 3. Wednesday ****************" << endl;
    8. cout << "********** 4. Thursday *****************" << endl;
    9. cout << "********** 5. Friday *******************" << endl;
    10. cout << "Please enter the choice" << endl;
    11. cin >> daychoice;
    12. if (daychoice >= 1 && daychoice <= 5) {
    13. break;
    14. }
    15. else {
    16. cout << "Please enter 1,2,3,4 or 5" << endl;
    17. }
    18. }
    19. system("pause");
    20. system("cls");
    21. int timechoice;
    22. while (true) {
    23. cout << "***Please select morning or afternoon***" << endl;
    24. cout << "********** 1. morning ******************" << endl;
    25. cout << "********** 2. afternoon ****************" << endl;
    26. cout << "Please enter the choice" << endl;
    27. cin >> timechoice;
    28. if (timechoice == 1 || timechoice == 2) {
    29. break;
    30. }
    31. else {
    32. cout << "Please enter again" << endl;
    33. }
    34. }
    35. system("pause");
    36. system("cls");
    37. cout << "The room " << vroom[0].num << " has " << vroom[0].capacity << " seats" << endl;
    38. cout << "The room " << vroom[1].num << " has " << vroom[1].capacity << " seats" << endl;
    39. cout << "The room " << vroom[2].num << " has " << vroom[2].capacity << " seats" << endl;
    40. int roomchoice;
    41. while (true) {
    42. cout << "Please select the room" << endl;
    43. cin >> roomchoice;
    44. if (roomchoice >= 1 && roomchoice <= 3) {
    45. break;
    46. }
    47. else {
    48. cout << "Please enter again" << endl;
    49. }
    50. }
    51. cout << "Done,it is waiting to be audited" << endl;
    52. ofstream ofs(REVERSE_FILE, ios::app);
    53. ofs << "date:" << daychoice << " " << " time:" << timechoice << " " << " id:" << id << " ";
    54. ofs << " name:" << name << " " <<" room:" << roomchoice <<" "<< " status:" << "auditing" << endl;
    55. ofs.close();
    56. system("pause");
    57. system("cls");
    58. }

    首先提示用户1-5代表周一至周五,要求用户选择日期(输入错误要求重新输入)。然后提示1表示上午2表示下午,要求用户选择上午还是下午,同样输入错误要求重新输入。然后给出机房信息,要求用户选择机房,同样输入错误要求重新输入。随后完成申请的功能,提示状态是未批准,将信息写入文件中。

  • 相关阅读:
    程序设计竞赛-过了这个村没这个店
    Vue3上 使用腾讯地图 基础展示
    案例丨一座小镇,如何推动“零售娱乐化”的风潮?合适的数字化工具如何为品牌带来助力?
    你真的会维护接口测试用例吗?接口/接口自动化用例常见问题+解决...
    Gem5 Bug Record
    怎样用python爬虫实现自动监测百度是否收录域名
    测试Bard和ChatGPT关于双休的法规和推理
    WebRTC系列 -- iOS 音频采集之 ADM、APM和AudioState
    JDK JRE JVM区别
    3 种方法限制 K8s Pod 磁盘容量使用
  • 原文地址:https://blog.csdn.net/m0_71007572/article/details/126458710