在完成对C语言的学习后,我最近开始了对C++和Java的学习,目前跟着视频学习了一些语法,也跟着敲了一些代码,有了一定的掌握程度。现在将跟着视频做的笔记进行整理。本篇博客是整理C++知识点的第十九篇博客。
本篇博客用C++实现职工管理系统,本文是下半部分。
本系列博客所有C++代码都在Visual Studio 2022环境下编译运行。程序为64位。
目录
职工管理系统
这一部分接上一篇笔记。
显示成员
void workmanager::showworker(void)
{
if (number == 0) {
cout << "It is blank" << endl;
return;
}
int i;
for (i = 0; i < number; i += 1) {
workertable[i]->show();
}
system("pause");
system("cls");
}
workmanager类的showworker函数遍历workertable,并依次调用取出对象的show函数进行输出。
删除职工
void workmanager::deleteworker(void)
{
if (number == 0) {
cout << "It is blank" << endl;
return;
}
string id;
cout << "Please enter the number of the worker" << endl;
cin >> id;
int index = searchworker(id);
if (index == -1) {
cout << "Can't found it,fail to delete" << endl;
}
else {
int i;
for (i = index; i < number - 1; i += 1) {
workertable[i] = workertable[i + 1];
}
number -= 1;
save();
cout << "Succeed to delete" << endl;
}
system("pause");
system("cls");
}
workmanager类的deleteworker函数实现删除用户。首先要求输入待删除职工的编号,然后在searchworker函数中进行查找(找到返回下标,否则返回-1),如果结果为-1,就提示删除失败。找到后,就从此位置开始,将后面的元素依次前移一位,总数减一,随后调用save函数进行保存,并提示删除成功。
int workmanager::searchworker(string id)
{
int i;
for (i = 0; i < number; i += 1) {
if (workertable[i]->number == id) {
return i;
}
}
return -1;
}
这是workmanager的searchworker函数,接受一个string参数,并查找是否有这个参数为编号的职工。遍历workertable进行查找,找到后就返回其下标,没有找到就返回-1。
修改职工
void workmanager::reviseworker(void)
{
if (number == 0) {
cout << "It is blank" << endl;
return;
}
string id;
cout << "Please enter the number of the worker" << endl;
cin >> id;
int index = searchworker(id);
if (index == -1) {
cout << "Can't found it,fail to revise" << endl;
}
else {
worker* work = NULL;
delete workertable[index];
cout << "Please enter the number" << endl;
string tempnumber;
cin >> tempnumber;
cout << "Please enter the name" << endl;
string tempname;
cin >> tempname;
cout << "Please enter the depart" << endl;
cout << "1.employee 2.manager 3.boss" << endl;
while (true) {
int tempdepart;
cin >> tempdepart;
if (tempdepart == 1) {
work = new employee(tempnumber, tempname, "employee");
break;
}
else if (tempdepart == 2) {
work = new manager(tempnumber, tempname, "manager");
break;
}
else if (tempdepart == 3) {
work = new boss(tempnumber, tempname, "boss");
break;
}
else {
cout << "Please enter 1,2 or 3" << endl;
}
}
workertable[index] = work;
save();
cout << "Succeed to revise" << endl;
}
system("pause");
system("cls");
}
workmanager类的reviseworker函数进行修改操作,首先要求输入待修改职工编号,然后利用searchworker函数进行查找,找不到就提示修改失败。找到后就要求输入信息,并根据类型创建对应类型的指针。创建完后就将对应下标修改为指向新指针,然后用save函数进行保存。最后提示修改成功。
查找职工
void workmanager::findworker(void)
{
if (number == 0) {
cout << "It is blank" << endl;
return;
}
int choice;
cout << "1.search by number 2.search by name" << endl;
while (true) {
cout << "Please enter your choice:";
cin >> choice;
if (choice == 1) {
string tempnumber;
cout << "Please enter the number: ";
cin >> tempnumber;
int i;
int index = -1;
for (i = 0; i < number; i += 1) {
if (tempnumber == workertable[i]->number) {
index = i;
break;
}
}
if (index == -1) {
cout << "Can not found" << endl;
}
else {
cout << "Succeed to found" << endl;
workertable[index]->show();
}
break;
}
else if (choice == 2) {
string tempname;
cout << "Please enter the name: ";
cin >> tempname;
int i;
int index = -1;
for (i = 0; i < number; i += 1) {
if (tempname == workertable[i]->name) {
index = i;
break;
}
}
if (index == -1) {
cout << "Can not found" << endl;
}
else {
cout << "Succeed to found" << endl;
workertable[index]->show();
}
break;
}
else {
cout << "Please enter 1 or 2" << endl;
}
}
system("pause");
system("cls");
}
workmanager类的sortworker函数提供查找函数功能。首先提示输入1按编号查找,输入2按名字查找,然后让用户输入。程序设计了死循环,输入1和2进行对应查找随后跳出循环,否则提示输入1或2。如果输入1,就让用户输入职工编号,随后遍历查找,找到就输出信息,否则提示找不到。输入2就让用户输入名字,随后遍历查找,找到就输出信息,否则提示找不到。
排序
void workmanager::sortworker(void)
{
if (number == 0) {
cout << "It is blank" << endl;
return;
}
int choice;
cout << "1. sort by number 2.sort by name" << endl;
while (true) {
cout << "Please enter the choice: ";
cin >> choice;
if (choice == 1) {
char choice;
cout << "A sort by asc B sort by desc" << endl;
while (true) {
cout << "Please enter A or B: " << endl;
cin >> choice;
if (choice == 'A') {
int i, j;
for (i = 0; i < number - 1; i += 1) {
for (j = 0; j < number - i - 1; j += 1) {
if (workertable[j]->number > workertable[j + 1]->number) {
worker* temp = workertable[j];
workertable[j] = workertable[j + 1];
workertable[j + 1] = temp;
}
}
}
break;
}
else if (choice == 'B') {
int i, j;
for (i = 0; i < number - 1; i += 1) {
for (j = 0; j < number - i - 1; j += 1) {
if (workertable[j]->number < workertable[j + 1]->number) {
worker* temp = workertable[j];
workertable[j] = workertable[j + 1];
workertable[j + 1] = temp;
}
}
}
break;
}
else {
}
}
save();
break;
}
else if (choice == 2) {
char choice;
cout << "A sort by asc B sort by desc" << endl;
while (true) {
cout << "Please enter A or B: " << endl;
cin >> choice;
if (choice == 'A') {
int i, j;
for (i = 0; i < number - 1; i += 1) {
for (j = 0; j < number - i - 1; j += 1) {
if (workertable[j]->name > workertable[j + 1]->name) {
worker* temp = workertable[j];
workertable[j] = workertable[j + 1];
workertable[j + 1] = temp;
}
}
}
break;
}
else if (choice == 'B') {
int i, j;
for (i = 0; i < number - 1; i += 1) {
for (j = 0; j < number - i - 1; j += 1) {
if (workertable[j]->name < workertable[j + 1]->name) {
worker* temp = workertable[j];
workertable[j] = workertable[j + 1];
workertable[j + 1] = temp;
}
}
}
break;
}
else {
}
}
save();
break;
}
else {
cout << "Please enter 1 or 2" << endl;
}
}
}
workmanager类的sortworker函数进行排序。首先提示输入1按编号排序,输入2按姓名排序。然后进入循环,要求用户输入选择,输入1和2,则在完成排序后结束循环,否则提示用户输入1和2。
输入1后,提示输入A表示升序,输入B表示降序,要求用户输入,此处同样用了个类似的循环,只有成功进行操作后才退出,否则要求继续输入。在完成排序后会进行保存。
输入2后,提示输入A表示升序,输入B表示降序,要求用户输入,此处同样用了个类似的循环,只有成功进行操作后才退出,否则要求继续输入。在完成排序后会进行保存。
清空文件实现
void workmanager::clear(void)
{
cout << "Do you want to clear the information? input y to confirm" << endl;
char ch;
cin >> ch;
if (ch == 'y') {
ofstream ofs;
ofs.open("information.txt", ios::trunc);
int i;
for (i = 0; i < number; i += 1) {
delete workertable[i];
}
delete[] workertable;
workertable = NULL;
number = 0;
cout << "It has been cleared!" << endl;
system("pause");
system("cls");
}
else {
system("pause");
system("cls");
return;
}
}
workmanager类的clear函数进行清空操作。首先会提示用户按y进行清空,否则不清空,要求用户输入。在输入y后,清空文件,同时遍历workertable,将内容逐一删除,随后删除workertable本身。将数量置为0,随后提示已经清空。
程序主体
实现了功能后,还需要搭建主体,将功能连起来,形成完整的程序。下面是system.cpp中的内容。
#include<iostream>
#include "workmanager.h"
#include"worker.h"
#include"manager.h"
#include"employee.h"
#include"boss.h"
using namespace std;
int main(void)
{
int choice;
workmanager wm;
while (true) {
wm.showmenu();
cout << "Please enter the choice: ";
cin >> choice;
switch (choice) {
case 0:
wm.exitsystem();
break;
case 1:
wm.addworker();
break;
case 2:
wm.showworker();
break;
case 3:
wm.deleteworker();
break;
case 4:
wm.reviseworker();
break;
case 5:
wm.findworker();
break;
case 6:
wm.sortworker();
break;
case 7:
wm.clear();
break;
default:
system("cls");
break;
}
if (choice == 0) {
break;
}
}
return 0;
}
程序创建了一个workmanager类对象wm,随后输入选项对应执行wm的某个成员函数。
职工管理系统结束,这个程序还是有一定难度的,一定程度上检验了前面对面向对象这一部分的掌握程度。由于测试输出的功能过多,本文不放运行结果。