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


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

    本篇博客用C++实现了机房预约系统,本文是下部分。最后有总结,结束C++学习笔记系列。

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

    目录

    机房预约系统

    学生显示自身预约实现

    学生显示所有预约实现

    学生取消预约实现

    教师子菜单搭建以及注销实现

    教师显示所有预约实现

    教师审核预约实现

    总结


    机房预约系统

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

    学生显示自身预约实现

    1. void student::look_my_reverse(void){
    2. ifstream ifs;
    3. ifs.open(REVERSE_FILE, ios::in);
    4. map<int, map> mm;
    5. string date;
    6. string time;
    7. string id;
    8. string name;
    9. string room;
    10. string status;
    11. while (ifs >> date && ifs >> time && ifs >> id && ifs >> name && ifs >> room && ifs >> status) {
    12. map m;
    13. string date1, date2;
    14. int pos;
    15. pos = date.find(":");
    16. date1 = date.substr(0, pos);
    17. date2 = date.substr(pos + 1, date.size() - pos - 1);
    18. if (date2 == "1") {
    19. m.insert(make_pair(date1, "Monday"));
    20. }
    21. else if (date2 == "2") {
    22. m.insert(make_pair(date1, "Tuesday"));
    23. }
    24. else if (date2 == "3") {
    25. m.insert(make_pair(date1, "Wednesday"));
    26. }
    27. else if (date2 == "4") {
    28. m.insert(make_pair(date1, "Thursday"));
    29. }
    30. else if (date2 == "5") {
    31. m.insert(make_pair(date1, "Friday"));
    32. }
    33. string time1, time2;
    34. pos = time.find(":");
    35. time1 = time.substr(0, pos);
    36. time2 = time.substr(pos + 1, time.size() - pos - 1);
    37. if (time2 == "1") {
    38. m.insert(make_pair(time1, "morning"));
    39. }
    40. else if (time2 == "2") {
    41. m.insert(make_pair(time1, "afternoon"));
    42. }
    43. string id1, id2;
    44. pos = id.find(":");
    45. id1 = id.substr(0, pos);
    46. id2 = id.substr(pos + 1, id.size() - pos - 1);
    47. if (atoi(id2.c_str()) != this->id) {
    48. m.clear();
    49. continue;
    50. }
    51. m.insert(make_pair(id1, id2));
    52. string name1, name2;
    53. pos = name.find(":");
    54. name1 = name.substr(0, pos);
    55. name2 = name.substr(pos + 1, name.size() - pos - 1);
    56. m.insert(make_pair(name1, name2));
    57. string room1, room2;
    58. pos = room.find(":");
    59. room1 = room.substr(0, pos);
    60. room2 = room.substr(pos + 1, room.size() - pos - 1);
    61. m.insert(make_pair(room1, room2));
    62. string status1, status2;
    63. pos = status.find(":");
    64. status1 = status.substr(0, pos);
    65. status2 = status.substr(pos + 1, status.size() - pos - 1);
    66. m.insert(make_pair(status1, status2));
    67. mm.insert(make_pair(mm.size() + 1, m));
    68. }
    69. if (mm.size() == 0) {
    70. cout << "No record" << endl;
    71. return;
    72. }
    73. for (map<int, map>::iterator ait = mm.begin(); ait != mm.end(); ++ait) {
    74. cout << "The number is " << ait->first << endl;
    75. for (map::iterator bit = ait->second.begin(); bit != ait->second.end(); ++bit) {
    76. cout << bit->first << ":" << bit->second << " ";
    77. }
    78. cout << endl;
    79. }
    80. ifs.close();
    81. system("pause");
    82. system("cls");
    83. }

    这是student类的成员函数,创建了一个map容器mm,键为int类型,值为键和值都是string的map容器。随后遍历文件,对获取的信息进行处理,每次遍历都创建一个键和值是string的map容器m。将处理的信息加入m,每次循环结束后将当前的条数和m加入mm。如果id不是学生当前id,就结束本次循环,清空m。文件遍历完成后输出信息。

    学生显示所有预约实现

    1. void student::look_all_reverse(void){
    2. ifstream ifs;
    3. ifs.open(REVERSE_FILE, ios::in);
    4. map<int, map> mm;
    5. string date;
    6. string time;
    7. string id;
    8. string name;
    9. string room;
    10. string status;
    11. while (ifs >> date && ifs >> time && ifs >> id && ifs >> name && ifs >> room && ifs >> status) {
    12. map m;
    13. string date1, date2;
    14. int pos;
    15. pos = date.find(":");
    16. date1 = date.substr(0, pos);
    17. date2 = date.substr(pos + 1, date.size() - pos - 1);
    18. if (date2 == "1") {
    19. m.insert(make_pair(date1, "Monday"));
    20. }
    21. else if (date2 == "2") {
    22. m.insert(make_pair(date1, "Tuesday"));
    23. }
    24. else if (date2 == "3") {
    25. m.insert(make_pair(date1, "Wednesday"));
    26. }
    27. else if (date2 == "4") {
    28. m.insert(make_pair(date1, "Thursday"));
    29. }
    30. else if (date2 == "5") {
    31. m.insert(make_pair(date1, "Friday"));
    32. }
    33. string time1, time2;
    34. pos = time.find(":");
    35. time1 = time.substr(0, pos);
    36. time2 = time.substr(pos + 1, time.size() - pos - 1);
    37. if (time2 == "1") {
    38. m.insert(make_pair(time1, "morning"));
    39. }
    40. else if (time2 == "2") {
    41. m.insert(make_pair(time1, "afternoon"));
    42. }
    43. string id1, id2;
    44. pos = id.find(":");
    45. id1 = id.substr(0, pos);
    46. id2 = id.substr(pos + 1, id.size() - pos - 1);
    47. m.insert(make_pair(id1, id2));
    48. string name1, name2;
    49. pos = name.find(":");
    50. name1 = name.substr(0, pos);
    51. name2 = name.substr(pos + 1, name.size() - pos - 1);
    52. m.insert(make_pair(name1, name2));
    53. string room1, room2;
    54. pos = room.find(":");
    55. room1 = room.substr(0, pos);
    56. room2 = room.substr(pos + 1, room.size() - pos - 1);
    57. m.insert(make_pair(room1, room2));
    58. string status1, status2;
    59. pos = status.find(":");
    60. status1 = status.substr(0, pos);
    61. status2 = status.substr(pos + 1, status.size() - pos - 1);
    62. m.insert(make_pair(status1, status2));
    63. mm.insert(make_pair(mm.size() + 1, m));
    64. }
    65. ifs.close();
    66. for (map<int, map>::iterator ait = mm.begin(); ait != mm.end(); ++ait) {
    67. cout << "The number is " << ait->first << endl;
    68. for (map::iterator bit = ait->second.begin(); bit != ait->second.end(); ++bit) {
    69. cout << bit->first << ":" << bit->second << " ";
    70. }
    71. cout << endl;
    72. }
    73. ifs.close();
    74. system("pause");
    75. system("cls");
    76. }

    这一部分和上一部分基本完全一样,就是不再筛选学号是否为当前学号。

    学生取消预约实现

    1. void student::cancel_reverse(){
    2. int i = 0;
    3. map<int, int>kv;
    4. ifstream ifs;
    5. ifs.open(REVERSE_FILE, ios::in);
    6. map<int, map> mm;
    7. string date;
    8. string time;
    9. string id;
    10. string name;
    11. string room;
    12. string status;
    13. while (ifs >> date && ifs >> time && ifs >> id && ifs >> name && ifs >> room && ifs >> status) {
    14. map m;
    15. string date1, date2;
    16. int pos;
    17. pos = date.find(":");
    18. date1 = date.substr(0, pos);
    19. date2 = date.substr(pos + 1, date.size() - pos - 1);
    20. if (date2 == "1") {
    21. m.insert(make_pair(date1, "Monday"));
    22. }
    23. else if (date2 == "2") {
    24. m.insert(make_pair(date1, "Tuesday"));
    25. }
    26. else if (date2 == "3") {
    27. m.insert(make_pair(date1, "Wednesday"));
    28. }
    29. else if (date2 == "4") {
    30. m.insert(make_pair(date1, "Thursday"));
    31. }
    32. else if (date2 == "5") {
    33. m.insert(make_pair(date1, "Friday"));
    34. }
    35. string time1, time2;
    36. pos = time.find(":");
    37. time1 = time.substr(0, pos);
    38. time2 = time.substr(pos + 1, time.size() - pos - 1);
    39. if (time2 == "1") {
    40. m.insert(make_pair(time1, "morning"));
    41. }
    42. else if (time2 == "2") {
    43. m.insert(make_pair(time1, "afternoon"));
    44. }
    45. string id1, id2;
    46. pos = id.find(":");
    47. id1 = id.substr(0, pos);
    48. id2 = id.substr(pos + 1, id.size() - pos - 1);
    49. m.insert(make_pair(id1, id2));
    50. string name1, name2;
    51. pos = name.find(":");
    52. name1 = name.substr(0, pos);
    53. name2 = name.substr(pos + 1, name.size() - pos - 1);
    54. m.insert(make_pair(name1, name2));
    55. string room1, room2;
    56. pos = room.find(":");
    57. room1 = room.substr(0, pos);
    58. room2 = room.substr(pos + 1, room.size() - pos - 1);
    59. m.insert(make_pair(room1, room2));
    60. string status1, status2;
    61. pos = status.find(":");
    62. status1 = status.substr(0, pos);
    63. status2 = status.substr(pos + 1, status.size() - pos - 1);
    64. m.insert(make_pair(status1, status2));
    65. mm.insert(make_pair(mm.size() + 1, m));
    66. if (atoi(id2.c_str()) == this->id && (status2 == "adopted" || status2 == "auditing")) {
    67. i += 1;
    68. kv.insert(make_pair(i, mm.size()));
    69. }
    70. }
    71. ifs.close();
    72. for (map<int, int>::iterator iiit = kv.begin(); iiit != kv.end(); ++iiit) {
    73. cout << "The number is " << iiit->first << endl;
    74. for (map<int, map>::iterator ait = mm.begin(); ait != mm.end(); ++ait) {
    75. if (iiit->second == ait->first) {
    76. for (map::iterator bit = ait->second.begin(); bit != ait->second.end(); ++bit) {
    77. cout << bit->first << ":" << bit->second << " ";
    78. }
    79. }
    80. }
    81. cout << endl;
    82. }
    83. while (true) {
    84. cout << "Please enter the number to cancel,0 to exit" << endl;
    85. int choice;
    86. cin >> choice;
    87. if (choice == 0) {
    88. break;
    89. }else if(choice > 0 && choice <= kv.size()) {
    90. for (map<int, int>::iterator iiit = kv.begin(); iiit != kv.end(); ++iiit) {
    91. if (iiit->first == choice) {
    92. for (map<int, map>::iterator ait = mm.begin(); ait != mm.end(); ++ait) {
    93. if (iiit->second == ait->first) {
    94. int i = 0;
    95. for (map::iterator bit = ait->second.begin(); bit != ait->second.end(); ++bit) {
    96. if (i == 4) {
    97. bit->second = "cancelled";
    98. }
    99. i += 1;
    100. }
    101. }
    102. }
    103. }
    104. }
    105. for (map<int, map>::iterator ait = mm.begin(); ait != mm.end(); ++ait) {
    106. cout << "The number is " << ait->first << endl;
    107. for (map::iterator bit = ait->second.begin(); bit != ait->second.end(); ++bit) {
    108. cout << bit->first << ":" << bit->second << " ";
    109. }
    110. cout << endl;
    111. }
    112. cout << "Done" << endl;
    113. ofstream ofs;
    114. ofs.open(REVERSE_FILE, ios::trunc);
    115. ofs.close();
    116. ofs.open(REVERSE_FILE, ios::in);
    117. for (map<int, map>::iterator ait = mm.begin(); ait != mm.end(); ++ait) {
    118. int i = 0;
    119. int daychoice;
    120. string tempid;
    121. string tempname;
    122. string roomchoice;
    123. string status;
    124. int timechoice;
    125. for (map::iterator bit = ait->second.begin(); bit != ait->second.end(); ++bit) {
    126. if (i == 0) {
    127. if (bit->second == "Monday") {
    128. daychoice = 1;
    129. }
    130. else if (bit->second == "Tuesday") {
    131. daychoice = 2;
    132. }
    133. else if (bit->second == "Wednesday") {
    134. daychoice = 3;
    135. }
    136. else if (bit->second == "Thursday") {
    137. daychoice = 4;
    138. }
    139. else if (bit->second == "Friday") {
    140. daychoice = 5;
    141. }
    142. }
    143. else if (i == 1) {
    144. tempid = bit->second;
    145. }
    146. else if (i == 2) {
    147. tempname = bit->second;
    148. }
    149. else if (i == 3) {
    150. roomchoice = bit->second;
    151. }
    152. else if (i == 4) {
    153. status = bit->second;
    154. }
    155. else if (i == 5) {
    156. if (bit->second == "morning") {
    157. timechoice = 1;
    158. }
    159. else if(bit->second == "afternoon") {
    160. timechoice = 2;
    161. }
    162. }
    163. i += 1;
    164. }
    165. ofs << "date:" << daychoice << " " << " time:" << timechoice << " " << " id:" << tempid << " ";
    166. ofs << " name:" << tempname << " " << " room:" << roomchoice << " " << " status:" << status << endl;
    167. }
    168. break;
    169. }
    170. else {
    171. cout << "Please enter the right choice" << endl;
    172. }
    173. }
    174. system("pause");
    175. system("cls");
    176. }

    这是student类的成员函数。创建了一个map容器mm,键为int类型,值为键和值都是string的map容器。还有一个map容器kv,键和值都是int类型。随后遍历文件,对获取的信息进行处理,每次遍历都创建一个键和值是string的map容器m。将处理的信息加入m,每次循环结束后将当前的条数和m加入mm。如果学号是学生当前学号,并且状态是待审核或者审核通过,就将当前mm的大小作为值,满足当前条件的信息数作为键加入kv。

    随后大循环遍历kv,小循环遍历mm,只有mm当前的键是kv当前的值时,才会输出信息。就是做到信息对应。

    然后用户要选择取消的条目,条目的总数就是kv的元素个数,不能超过这个个数。输入0就代表不取消任何记录,输入错误要求重新输入。输入正确后,选项就代表kv的键,找到这个键在kv中对应的值,然后找到此值在mm中对应的键,然后将状态改为已取消。

    然后显示修改后的内容,清空文件原来的内容并重新写入。

    个人感觉这一部分当初写得不好,太麻烦,但是后来也没改。后面还有一处极为类似的代码。

    教师子菜单搭建以及注销实现

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

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

    1. void teacher::show_menu()
    2. {
    3. cout << "****************************************" << endl;
    4. cout << "********** 1. look reservation *********" << endl;
    5. cout << "********** 2. audit reservation ********" << endl;
    6. cout << "********** 0. log out ******************" << endl;
    7. cout << "****************************************" << endl;
    8. }

    这是teacher类的成员函数,显示菜单。

    教师显示所有预约实现

    1. void teacher::look_reverse(void)
    2. {
    3. ifstream ifs;
    4. ifs.open(REVERSE_FILE, ios::in);
    5. map<int, map> mm;
    6. string date;
    7. string time;
    8. string id;
    9. string name;
    10. string room;
    11. string status;
    12. while (ifs >> date && ifs >> time && ifs >> id && ifs >> name && ifs >> room && ifs >> status) {
    13. map m;
    14. string date1, date2;
    15. int pos;
    16. pos = date.find(":");
    17. date1 = date.substr(0, pos);
    18. date2 = date.substr(pos + 1, date.size() - pos - 1);
    19. if (date2 == "1") {
    20. m.insert(make_pair(date1, "Monday"));
    21. }
    22. else if (date2 == "2") {
    23. m.insert(make_pair(date1, "Tuesday"));
    24. }
    25. else if (date2 == "3") {
    26. m.insert(make_pair(date1, "Wednesday"));
    27. }
    28. else if (date2 == "4") {
    29. m.insert(make_pair(date1, "Thursday"));
    30. }
    31. else if (date2 == "5") {
    32. m.insert(make_pair(date1, "Friday"));
    33. }
    34. string time1, time2;
    35. pos = time.find(":");
    36. time1 = time.substr(0, pos);
    37. time2 = time.substr(pos + 1, time.size() - pos - 1);
    38. if (time2 == "1") {
    39. m.insert(make_pair(time1, "morning"));
    40. }
    41. else if (time2 == "2") {
    42. m.insert(make_pair(time1, "afternoon"));
    43. }
    44. string id1, id2;
    45. pos = id.find(":");
    46. id1 = id.substr(0, pos);
    47. id2 = id.substr(pos + 1, id.size() - pos - 1);
    48. m.insert(make_pair(id1, id2));
    49. string name1, name2;
    50. pos = name.find(":");
    51. name1 = name.substr(0, pos);
    52. name2 = name.substr(pos + 1, name.size() - pos - 1);
    53. m.insert(make_pair(name1, name2));
    54. string room1, room2;
    55. pos = room.find(":");
    56. room1 = room.substr(0, pos);
    57. room2 = room.substr(pos + 1, room.size() - pos - 1);
    58. m.insert(make_pair(room1, room2));
    59. string status1, status2;
    60. pos = status.find(":");
    61. status1 = status.substr(0, pos);
    62. status2 = status.substr(pos + 1, status.size() - pos - 1);
    63. m.insert(make_pair(status1, status2));
    64. mm.insert(make_pair(mm.size() + 1, m));
    65. }
    66. ifs.close();
    67. for (map<int, map>::iterator ait = mm.begin(); ait != mm.end(); ++ait) {
    68. cout << "The number is " << ait->first << endl;
    69. for (map::iterator bit = ait->second.begin(); bit != ait->second.end(); ++bit) {
    70. cout << bit->first << ":" << bit->second << " ";
    71. }
    72. cout << endl;
    73. }
    74. ifs.close();
    75. system("pause");
    76. system("cls");
    77. }

    这个功能上面实现过了,这里就不再详细解释了。

    教师审核预约实现

    1. void teacher::audit_reverse(void)
    2. {
    3. int i = 0;
    4. map<int, int>kv;
    5. ifstream ifs;
    6. ifs.open(REVERSE_FILE, ios::in);
    7. map<int, map> mm;
    8. string date;
    9. string time;
    10. string id;
    11. string name;
    12. string room;
    13. string status;
    14. while (ifs >> date && ifs >> time && ifs >> id && ifs >> name && ifs >> room && ifs >> status) {
    15. map m;
    16. string date1, date2;
    17. int pos;
    18. pos = date.find(":");
    19. date1 = date.substr(0, pos);
    20. date2 = date.substr(pos + 1, date.size() - pos - 1);
    21. if (date2 == "1") {
    22. m.insert(make_pair(date1, "Monday"));
    23. }
    24. else if (date2 == "2") {
    25. m.insert(make_pair(date1, "Tuesday"));
    26. }
    27. else if (date2 == "3") {
    28. m.insert(make_pair(date1, "Wednesday"));
    29. }
    30. else if (date2 == "4") {
    31. m.insert(make_pair(date1, "Thursday"));
    32. }
    33. else if (date2 == "5") {
    34. m.insert(make_pair(date1, "Friday"));
    35. }
    36. string time1, time2;
    37. pos = time.find(":");
    38. time1 = time.substr(0, pos);
    39. time2 = time.substr(pos + 1, time.size() - pos - 1);
    40. if (time2 == "1") {
    41. m.insert(make_pair(time1, "morning"));
    42. }
    43. else if (time2 == "2") {
    44. m.insert(make_pair(time1, "afternoon"));
    45. }
    46. string id1, id2;
    47. pos = id.find(":");
    48. id1 = id.substr(0, pos);
    49. id2 = id.substr(pos + 1, id.size() - pos - 1);
    50. m.insert(make_pair(id1, id2));
    51. string name1, name2;
    52. pos = name.find(":");
    53. name1 = name.substr(0, pos);
    54. name2 = name.substr(pos + 1, name.size() - pos - 1);
    55. m.insert(make_pair(name1, name2));
    56. string room1, room2;
    57. pos = room.find(":");
    58. room1 = room.substr(0, pos);
    59. room2 = room.substr(pos + 1, room.size() - pos - 1);
    60. m.insert(make_pair(room1, room2));
    61. string status1, status2;
    62. pos = status.find(":");
    63. status1 = status.substr(0, pos);
    64. status2 = status.substr(pos + 1, status.size() - pos - 1);
    65. m.insert(make_pair(status1, status2));
    66. mm.insert(make_pair(mm.size() + 1, m));
    67. if (status2 == "auditing") {
    68. i += 1;
    69. kv.insert(make_pair(i, mm.size()));
    70. }
    71. }
    72. ifs.close();
    73. if (kv.size() == 0) {
    74. cout << "No record to be audited" << endl;
    75. return;
    76. }
    77. while (true) {
    78. for (map<int, int>::iterator iiit = kv.begin(); iiit != kv.end(); ++iiit) {
    79. cout << "The number is " << iiit->first << endl;
    80. for (map<int, map>::iterator ait = mm.begin(); ait != mm.end(); ++ait) {
    81. if (iiit->second == ait->first) {
    82. for (map::iterator bit = ait->second.begin(); bit != ait->second.end(); ++bit) {
    83. cout << bit->first << ":" << bit->second << " ";
    84. }
    85. }
    86. }
    87. cout << endl;
    88. }
    89. cout << "Please enter the number to audit,0 to exit" << endl;
    90. int choice;
    91. cin >> choice;
    92. if (choice == 0) {
    93. break;
    94. }
    95. else if (choice > 0 && choice <= kv.size()) {
    96. int audit;
    97. while (true) {
    98. cout << "Please enter the audit" << endl;
    99. cout << "1.yes 2.no" << endl;
    100. cin >> audit;
    101. if (audit == 1 || audit == 2) {
    102. break;
    103. }
    104. else {
    105. cout << "Please enter 1 or 2" << endl;
    106. }
    107. }
    108. for (map<int, int>::iterator iiit = kv.begin(); iiit != kv.end(); ++iiit) {
    109. if (iiit->first == choice) {
    110. for (map<int, map>::iterator ait = mm.begin(); ait != mm.end(); ++ait) {
    111. if (iiit->second == ait->first) {
    112. int i = 0;
    113. for (map::iterator bit = ait->second.begin(); bit != ait->second.end(); ++bit) {
    114. if (i == 4&&audit == 1) {
    115. bit->second = "adopted";
    116. }
    117. else if(i ==4 && audit == 2) {
    118. bit->second = "refused";
    119. }
    120. i += 1;
    121. }
    122. }
    123. }
    124. }
    125. }
    126. for (map<int, map>::iterator ait = mm.begin(); ait != mm.end(); ++ait) {
    127. cout << "The number is " << ait->first << endl;
    128. for (map::iterator bit = ait->second.begin(); bit != ait->second.end(); ++bit) {
    129. cout << bit->first << ":" << bit->second << " ";
    130. }
    131. cout << endl;
    132. }
    133. cout << "Done" << endl;
    134. ofstream ofs;
    135. ofs.open(REVERSE_FILE, ios::trunc);
    136. ofs.close();
    137. ofs.open(REVERSE_FILE, ios::in);
    138. for (map<int, map>::iterator ait = mm.begin(); ait != mm.end(); ++ait) {
    139. int i = 0;
    140. int daychoice;
    141. string tempid;
    142. string tempname;
    143. string roomchoice;
    144. string status;
    145. int timechoice;
    146. for (map::iterator bit = ait->second.begin(); bit != ait->second.end(); ++bit) {
    147. if (i == 0) {
    148. if (bit->second == "Monday") {
    149. daychoice = 1;
    150. }
    151. else if (bit->second == "Tuesday") {
    152. daychoice = 2;
    153. }
    154. else if (bit->second == "Wednesday") {
    155. daychoice = 3;
    156. }
    157. else if (bit->second == "Thursday") {
    158. daychoice = 4;
    159. }
    160. else if (bit->second == "Friday") {
    161. daychoice = 5;
    162. }
    163. }
    164. else if (i == 1) {
    165. tempid = bit->second;
    166. }
    167. else if (i == 2) {
    168. tempname = bit->second;
    169. }
    170. else if (i == 3) {
    171. roomchoice = bit->second;
    172. }
    173. else if (i == 4) {
    174. status = bit->second;
    175. }
    176. else if (i == 5) {
    177. if (bit->second == "morning") {
    178. timechoice = 1;
    179. }
    180. else if (bit->second == "afternoon") {
    181. timechoice = 2;
    182. }
    183. }
    184. i += 1;
    185. }
    186. ofs << "date:" << daychoice << " " << " time:" << timechoice << " " << " id:" << tempid << " ";
    187. ofs << " name:" << tempname << " " << " room:" << roomchoice << " " << " status:" << status << endl;
    188. }
    189. ofs.close();
    190. }
    191. else {
    192. cout << "Please enter the right choice" << endl;
    193. }
    194. kv.clear();
    195. mm.clear();
    196. int i = 0;
    197. ifs.open(REVERSE_FILE, ios::in);
    198. string date;
    199. string time;
    200. string id;
    201. string name;
    202. string room;
    203. string status;
    204. while (ifs >> date && ifs >> time && ifs >> id && ifs >> name && ifs >> room && ifs >> status) {
    205. map m;
    206. string date1, date2;
    207. int pos;
    208. pos = date.find(":");
    209. date1 = date.substr(0, pos);
    210. date2 = date.substr(pos + 1, date.size() - pos - 1);
    211. if (date2 == "1") {
    212. m.insert(make_pair(date1, "Monday"));
    213. }
    214. else if (date2 == "2") {
    215. m.insert(make_pair(date1, "Tuesday"));
    216. }
    217. else if (date2 == "3") {
    218. m.insert(make_pair(date1, "Wednesday"));
    219. }
    220. else if (date2 == "4") {
    221. m.insert(make_pair(date1, "Thursday"));
    222. }
    223. else if (date2 == "5") {
    224. m.insert(make_pair(date1, "Friday"));
    225. }
    226. string time1, time2;
    227. pos = time.find(":");
    228. time1 = time.substr(0, pos);
    229. time2 = time.substr(pos + 1, time.size() - pos - 1);
    230. if (time2 == "1") {
    231. m.insert(make_pair(time1, "morning"));
    232. }
    233. else if (time2 == "2") {
    234. m.insert(make_pair(time1, "afternoon"));
    235. }
    236. string id1, id2;
    237. pos = id.find(":");
    238. id1 = id.substr(0, pos);
    239. id2 = id.substr(pos + 1, id.size() - pos - 1);
    240. m.insert(make_pair(id1, id2));
    241. string name1, name2;
    242. pos = name.find(":");
    243. name1 = name.substr(0, pos);
    244. name2 = name.substr(pos + 1, name.size() - pos - 1);
    245. m.insert(make_pair(name1, name2));
    246. string room1, room2;
    247. pos = room.find(":");
    248. room1 = room.substr(0, pos);
    249. room2 = room.substr(pos + 1, room.size() - pos - 1);
    250. m.insert(make_pair(room1, room2));
    251. string status1, status2;
    252. pos = status.find(":");
    253. status1 = status.substr(0, pos);
    254. status2 = status.substr(pos + 1, status.size() - pos - 1);
    255. m.insert(make_pair(status1, status2));
    256. mm.insert(make_pair(mm.size() + 1, m));
    257. if (status2 == "auditing") {
    258. i += 1;
    259. kv.insert(make_pair(i, mm.size()));
    260. }
    261. }
    262. ifs.close();
    263. if (kv.size() == 0) {
    264. cout << "No record to be audited" << endl;
    265. system("pause");
    266. system("cls");
    267. return;
    268. }
    269. }
    270. system("pause");
    271. system("cls");
    272. }

    这一部分代码实现和上面的学生取消预约是类似的,这里只说一下不同的地方。同样这一处设计也不行,当初赶工后的结果。

    首先,加入kv的计数原则改为状态在审核中的条目。然后这里设计了循环,每次循环的开头显示审核中的信息,并要求用户输入要决定的选项。输入0退出,然后条目的编号同样不能大于kv的元素个数。然后是输入1表示同意输入2表示拒绝,随后修改相关的条目。然后是输出修改后信息,写入文件。最后,由于在每次循环开头输出信息,因此在完成这一系列操作后,要从文件中将信息读取,覆盖原来容器的内容。

    那么现在机房预约系统也结束了,虽然像上面提到的一样,两个地方的代码太多,但是整体还可以,这个有一定的难度。

    总结

    那么C++的所有三十六篇笔记就结束了,从8月4日到现在,用了大约二十天,将前面看视频的笔记整理了一下。在文章划分上,尽量将同一块知识点放在一块。现在对C++有了一定的掌握程度,接下来就该看书进阶了。就目前知识来讲C++确实有一定难度,后面的进阶只会更难。现在我已经线上大二开学,在家上网课,估计会上不到一个月网课。总之,接下来该继续努力了。

  • 相关阅读:
    基于typeorm的nestjs项目使用@zdhsoft/tmg将数据库生成数据模型
    java编程基础总结——31.多线程编程相关知识
    Oracle常见的等待事件
    15:00面试,15:06就出来了,问的问题有点变态。。。
    新学期,新FLAG | 要以码为梦而非夜郎自大
    Python 算法高级篇:归并排序的优化与外部排序
    Shell在日常工作中的应用实践
    四、矩阵的分类
    [树形dp]Hanging Hearts Codeforces1740E
    第11章 通信网络
  • 原文地址:https://blog.csdn.net/m0_71007572/article/details/126471981