• C++学习笔记(十九)


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

    本篇博客用C++实现职工管理系统,本文是下半部分。

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

    目录

    职工管理系统

    显示成员

    删除职工

    修改职工

    查找职工

    排序

    清空文件实现

    程序主体


    职工管理系统

    这一部分接上一篇笔记。

    显示成员

    1. void workmanager::showworker(void)
    2. {
    3. if (number == 0) {
    4. cout << "It is blank" << endl;
    5. return;
    6. }
    7. int i;
    8. for (i = 0; i < number; i += 1) {
    9. workertable[i]->show();
    10. }
    11. system("pause");
    12. system("cls");
    13. }

    workmanager类的showworker函数遍历workertable,并依次调用取出对象的show函数进行输出。

    删除职工

    1. void workmanager::deleteworker(void)
    2. {
    3. if (number == 0) {
    4. cout << "It is blank" << endl;
    5. return;
    6. }
    7. string id;
    8. cout << "Please enter the number of the worker" << endl;
    9. cin >> id;
    10. int index = searchworker(id);
    11. if (index == -1) {
    12. cout << "Can't found it,fail to delete" << endl;
    13. }
    14. else {
    15. int i;
    16. for (i = index; i < number - 1; i += 1) {
    17. workertable[i] = workertable[i + 1];
    18. }
    19. number -= 1;
    20. save();
    21. cout << "Succeed to delete" << endl;
    22. }
    23. system("pause");
    24. system("cls");
    25. }

    workmanager类的deleteworker函数实现删除用户。首先要求输入待删除职工的编号,然后在searchworker函数中进行查找(找到返回下标,否则返回-1),如果结果为-1,就提示删除失败。找到后,就从此位置开始,将后面的元素依次前移一位,总数减一,随后调用save函数进行保存,并提示删除成功。

    1. int workmanager::searchworker(string id)
    2. {
    3. int i;
    4. for (i = 0; i < number; i += 1) {
    5. if (workertable[i]->number == id) {
    6. return i;
    7. }
    8. }
    9. return -1;
    10. }

    这是workmanager的searchworker函数,接受一个string参数,并查找是否有这个参数为编号的职工。遍历workertable进行查找,找到后就返回其下标,没有找到就返回-1。

    修改职工

    1. void workmanager::reviseworker(void)
    2. {
    3. if (number == 0) {
    4. cout << "It is blank" << endl;
    5. return;
    6. }
    7. string id;
    8. cout << "Please enter the number of the worker" << endl;
    9. cin >> id;
    10. int index = searchworker(id);
    11. if (index == -1) {
    12. cout << "Can't found it,fail to revise" << endl;
    13. }
    14. else {
    15. worker* work = NULL;
    16. delete workertable[index];
    17. cout << "Please enter the number" << endl;
    18. string tempnumber;
    19. cin >> tempnumber;
    20. cout << "Please enter the name" << endl;
    21. string tempname;
    22. cin >> tempname;
    23. cout << "Please enter the depart" << endl;
    24. cout << "1.employee 2.manager 3.boss" << endl;
    25. while (true) {
    26. int tempdepart;
    27. cin >> tempdepart;
    28. if (tempdepart == 1) {
    29. work = new employee(tempnumber, tempname, "employee");
    30. break;
    31. }
    32. else if (tempdepart == 2) {
    33. work = new manager(tempnumber, tempname, "manager");
    34. break;
    35. }
    36. else if (tempdepart == 3) {
    37. work = new boss(tempnumber, tempname, "boss");
    38. break;
    39. }
    40. else {
    41. cout << "Please enter 1,2 or 3" << endl;
    42. }
    43. }
    44. workertable[index] = work;
    45. save();
    46. cout << "Succeed to revise" << endl;
    47. }
    48. system("pause");
    49. system("cls");
    50. }

    workmanager类的reviseworker函数进行修改操作,首先要求输入待修改职工编号,然后利用searchworker函数进行查找,找不到就提示修改失败。找到后就要求输入信息,并根据类型创建对应类型的指针。创建完后就将对应下标修改为指向新指针,然后用save函数进行保存。最后提示修改成功。

    查找职工

    1. void workmanager::findworker(void)
    2. {
    3. if (number == 0) {
    4. cout << "It is blank" << endl;
    5. return;
    6. }
    7. int choice;
    8. cout << "1.search by number 2.search by name" << endl;
    9. while (true) {
    10. cout << "Please enter your choice:";
    11. cin >> choice;
    12. if (choice == 1) {
    13. string tempnumber;
    14. cout << "Please enter the number: ";
    15. cin >> tempnumber;
    16. int i;
    17. int index = -1;
    18. for (i = 0; i < number; i += 1) {
    19. if (tempnumber == workertable[i]->number) {
    20. index = i;
    21. break;
    22. }
    23. }
    24. if (index == -1) {
    25. cout << "Can not found" << endl;
    26. }
    27. else {
    28. cout << "Succeed to found" << endl;
    29. workertable[index]->show();
    30. }
    31. break;
    32. }
    33. else if (choice == 2) {
    34. string tempname;
    35. cout << "Please enter the name: ";
    36. cin >> tempname;
    37. int i;
    38. int index = -1;
    39. for (i = 0; i < number; i += 1) {
    40. if (tempname == workertable[i]->name) {
    41. index = i;
    42. break;
    43. }
    44. }
    45. if (index == -1) {
    46. cout << "Can not found" << endl;
    47. }
    48. else {
    49. cout << "Succeed to found" << endl;
    50. workertable[index]->show();
    51. }
    52. break;
    53. }
    54. else {
    55. cout << "Please enter 1 or 2" << endl;
    56. }
    57. }
    58. system("pause");
    59. system("cls");
    60. }

    workmanager类的sortworker函数提供查找函数功能。首先提示输入1按编号查找,输入2按名字查找,然后让用户输入。程序设计了死循环,输入1和2进行对应查找随后跳出循环,否则提示输入1或2。如果输入1,就让用户输入职工编号,随后遍历查找,找到就输出信息,否则提示找不到。输入2就让用户输入名字,随后遍历查找,找到就输出信息,否则提示找不到。

    排序

    1. void workmanager::sortworker(void)
    2. {
    3. if (number == 0) {
    4. cout << "It is blank" << endl;
    5. return;
    6. }
    7. int choice;
    8. cout << "1. sort by number 2.sort by name" << endl;
    9. while (true) {
    10. cout << "Please enter the choice: ";
    11. cin >> choice;
    12. if (choice == 1) {
    13. char choice;
    14. cout << "A sort by asc B sort by desc" << endl;
    15. while (true) {
    16. cout << "Please enter A or B: " << endl;
    17. cin >> choice;
    18. if (choice == 'A') {
    19. int i, j;
    20. for (i = 0; i < number - 1; i += 1) {
    21. for (j = 0; j < number - i - 1; j += 1) {
    22. if (workertable[j]->number > workertable[j + 1]->number) {
    23. worker* temp = workertable[j];
    24. workertable[j] = workertable[j + 1];
    25. workertable[j + 1] = temp;
    26. }
    27. }
    28. }
    29. break;
    30. }
    31. else if (choice == 'B') {
    32. int i, j;
    33. for (i = 0; i < number - 1; i += 1) {
    34. for (j = 0; j < number - i - 1; j += 1) {
    35. if (workertable[j]->number < workertable[j + 1]->number) {
    36. worker* temp = workertable[j];
    37. workertable[j] = workertable[j + 1];
    38. workertable[j + 1] = temp;
    39. }
    40. }
    41. }
    42. break;
    43. }
    44. else {
    45. }
    46. }
    47. save();
    48. break;
    49. }
    50. else if (choice == 2) {
    51. char choice;
    52. cout << "A sort by asc B sort by desc" << endl;
    53. while (true) {
    54. cout << "Please enter A or B: " << endl;
    55. cin >> choice;
    56. if (choice == 'A') {
    57. int i, j;
    58. for (i = 0; i < number - 1; i += 1) {
    59. for (j = 0; j < number - i - 1; j += 1) {
    60. if (workertable[j]->name > workertable[j + 1]->name) {
    61. worker* temp = workertable[j];
    62. workertable[j] = workertable[j + 1];
    63. workertable[j + 1] = temp;
    64. }
    65. }
    66. }
    67. break;
    68. }
    69. else if (choice == 'B') {
    70. int i, j;
    71. for (i = 0; i < number - 1; i += 1) {
    72. for (j = 0; j < number - i - 1; j += 1) {
    73. if (workertable[j]->name < workertable[j + 1]->name) {
    74. worker* temp = workertable[j];
    75. workertable[j] = workertable[j + 1];
    76. workertable[j + 1] = temp;
    77. }
    78. }
    79. }
    80. break;
    81. }
    82. else {
    83. }
    84. }
    85. save();
    86. break;
    87. }
    88. else {
    89. cout << "Please enter 1 or 2" << endl;
    90. }
    91. }
    92. }

    workmanager类的sortworker函数进行排序。首先提示输入1按编号排序,输入2按姓名排序。然后进入循环,要求用户输入选择,输入1和2,则在完成排序后结束循环,否则提示用户输入1和2。

    输入1后,提示输入A表示升序,输入B表示降序,要求用户输入,此处同样用了个类似的循环,只有成功进行操作后才退出,否则要求继续输入。在完成排序后会进行保存。

    输入2后,提示输入A表示升序,输入B表示降序,要求用户输入,此处同样用了个类似的循环,只有成功进行操作后才退出,否则要求继续输入。在完成排序后会进行保存。

    清空文件实现

    1. void workmanager::clear(void)
    2. {
    3. cout << "Do you want to clear the information? input y to confirm" << endl;
    4. char ch;
    5. cin >> ch;
    6. if (ch == 'y') {
    7. ofstream ofs;
    8. ofs.open("information.txt", ios::trunc);
    9. int i;
    10. for (i = 0; i < number; i += 1) {
    11. delete workertable[i];
    12. }
    13. delete[] workertable;
    14. workertable = NULL;
    15. number = 0;
    16. cout << "It has been cleared!" << endl;
    17. system("pause");
    18. system("cls");
    19. }
    20. else {
    21. system("pause");
    22. system("cls");
    23. return;
    24. }
    25. }

    workmanager类的clear函数进行清空操作。首先会提示用户按y进行清空,否则不清空,要求用户输入。在输入y后,清空文件,同时遍历workertable,将内容逐一删除,随后删除workertable本身。将数量置为0,随后提示已经清空。

    程序主体

    实现了功能后,还需要搭建主体,将功能连起来,形成完整的程序。下面是system.cpp中的内容。

    1. #include
    2. #include "workmanager.h"
    3. #include"worker.h"
    4. #include"manager.h"
    5. #include"employee.h"
    6. #include"boss.h"
    7. using namespace std;
    8. int main(void)
    9. {
    10. int choice;
    11. workmanager wm;
    12. while (true) {
    13. wm.showmenu();
    14. cout << "Please enter the choice: ";
    15. cin >> choice;
    16. switch (choice) {
    17. case 0:
    18. wm.exitsystem();
    19. break;
    20. case 1:
    21. wm.addworker();
    22. break;
    23. case 2:
    24. wm.showworker();
    25. break;
    26. case 3:
    27. wm.deleteworker();
    28. break;
    29. case 4:
    30. wm.reviseworker();
    31. break;
    32. case 5:
    33. wm.findworker();
    34. break;
    35. case 6:
    36. wm.sortworker();
    37. break;
    38. case 7:
    39. wm.clear();
    40. break;
    41. default:
    42. system("cls");
    43. break;
    44. }
    45. if (choice == 0) {
    46. break;
    47. }
    48. }
    49. return 0;
    50. }

    程序创建了一个workmanager类对象wm,随后输入选项对应执行wm的某个成员函数。

    职工管理系统结束,这个程序还是有一定难度的,一定程度上检验了前面对面向对象这一部分的掌握程度。由于测试输出的功能过多,本文不放运行结果。

  • 相关阅读:
    软件测试|MySQL WHERE条件查询详解:筛选出需要的数据
    Navigation 组件(三) ViewModel,LiveData,DataBinding 组合使用
    PostgreSQL 如果想知道表中某个条件查询条件在索引中效率 ?
    Java面试题-Java核心基础-第三天(基本数据类型)
    3分钟学会批量混合查询多家快递的物流信息
    MAUI Blazor 权限经验分享 (定位,使用相机)
    Docker 镜像仓库是什么?有哪些镜像仓库命令?
    《Python+Kivy(App开发)从入门到实践》自学笔记:高级UX部件——Popup弹窗
    在Docker中安装redis
    Web 自动化神器 TestCafe(二)—元素定位篇
  • 原文地址:https://blog.csdn.net/m0_71007572/article/details/126372350