C++ advanced articles C++ realizes the staff management system

1. Task description

      Use C++ to implement an employee management system, make the system have the basic functions of adding, deleting, modifying, and checking, and realize it with the idea of ​​object-oriented programming.

2. Code

      The main function is as follows:

#include<iostream>
#include<string>
#include "manager.h"

using namespace std;


int main()
{
    Manager manager;

    while(true)
    {
        manager.show_menu();
        int input_num;
        cout << "请输入选项:" << endl;
        cin >> input_num;
        if(input_num==0)
        {
            cout << "欢迎下次使用学生管理系统!" << endl;
            break;
        }
        else if(input_num==1)
        {
            manager.add();
        }
        else if(input_num==2)
        {
            if(manager.m_workernum == 0)
            {
                cout << "员工管理系统中没有学生" << endl;
            }
            manager.show();
        }
        else if(input_num == 3)
        {
            int ID;
            cout << "请输入要删除员工的ID:" << endl;
            cin >> ID;
            bool is_exists = manager.is_exists(ID);
            if(!is_exists)
            {
                cout << "该员工不在员工管理系统里面" << endl;
            }
            else
            {
                manager.move_worker(ID);
            }
        }

        else if(input_num == 4)
        {
            int ID;
            cout << "请输入要修改员工的ID:" << endl;
            cin >> ID;
            bool is_exists = manager.is_exists(ID);
            if(!is_exists)
            {
                cout << "该员工不在员工管理系统里面" << endl;
            }
            else
            {
                manager.modify_worker(ID);
            }
        }


        else if(input_num == 5)
        {
            int ID;
            cout << "请输入要查找员工的ID:" << endl;
            cin >> ID;
            bool is_exists = manager.is_exists(ID);
            if(!is_exists)
            {
                cout << "该员工不在员工管理系统里面" << endl;
            }
            else
            {
                manager.find_worker(ID);
            }
        }


        else if(input_num == 6)
        {
            cout << "清空员工管理系统!" << endl;
            manager.clear_system();
        }


        else
        {
            cout << "输入错误,请重新输入!" << endl;
        }
    }
    return 0;
}

        The header file is as follows:

#include<iostream>
#include<string>

using namespace std;


class Worker
{
public:
    Worker(int ID, string name, int score);

    void show();

public:
    int m_ID;
    string m_name;
    int m_score;
};



class Manager
{
public:
    Manager();

    void show_menu();

    void add();

    void show();

    bool is_exists(int ID);

    void move_worker(int ID);

    void modify_worker(int ID);

    void find_worker(int ID);

    void clear_system();

    int m_workernum;
    Worker ** m_workerarray;

};

        Implementation of member functions in the header file:

#include<iostream>
#include<string>
#include "manager.h"

using namespace std;


Worker::Worker(int ID, string name, int score)
{
    m_ID = ID;
    m_name = name;
    m_score = score;
}


void Worker::show()
{
    string rank;
    if(this->m_score<60)
    {
        rank = "不及格";
    }
    else if(this->m_score>=90)
    {
        rank = "高分成绩";
    }
    else
    {
        rank = "成绩合格";
    }

    cout << "ID:" << this->m_ID
         << "\t姓名:" << this->m_name
         << "\t分数:" << this->m_score
         << "\t成绩类型:" << rank
         << endl;
}


Manager::Manager()
{
    this->m_workernum = 0;
    this->m_workerarray = NULL;
}


void Manager::show_menu()
{
    cout << "******欢迎使用员工管理系统******" << endl;
    cout << "******     0:退出系统     ******" << endl;
    cout << "******     1:增加员工     ******" << endl;
    cout << "******     2:显示员工     ******" << endl;
    cout << "******     3:删除员工     ******" << endl;
    cout << "******     4:修改员工     ******" << endl;
    cout << "******     5:查找员工     ******" << endl;
    cout << "******     6:清空系统     ******" << endl;
    cout << "******欢迎使用员工管理系统******" << endl;
}


void Manager::add()
{
    cout << "请输入要增加的人数" << endl;
    int add_num;
    cin >> add_num;
    int m_workernum_new = this->m_workernum + add_num;
    Worker ** newspace = new Worker * [m_workernum_new];

    if(this->m_workerarray != NULL)
    {
        for(int i=0; i < this->m_workernum; i++)
        {
            newspace[i] = this->m_workerarray[i];
        }
    }

    for(int i=0; i < add_num; i++)
    {
        cout << "请输入员工ID:" << endl;
        int ID;
        cin >> ID;
        cout << "请输入员工姓名:" << endl;
        string name;
        cin >> name;
        cout << "请输入员工分数:" << endl;
        int score;
        cin >> score;
        Worker * worker = new Worker(ID, name, score);
        newspace[this->m_workernum+i] = worker;
    }
    cout << "成功添加了" << add_num << "个员工" << endl;

    delete [] this->m_workerarray;
    this->m_workernum = m_workernum_new;
    this->m_workerarray = newspace;
}


void Manager::modify_worker(int ID)
{
    int index;
    //先找到删除员工的索引
    for(int i=0; i<this->m_workernum; i++)
    {
        if(this->m_workerarray[i]->m_ID == ID)
        {
            index = i;
            break;
        }
    }

    cout << "请重新输入员工的姓名:" << endl;
    string name;
    cin >> name;

    cout << "请重新输入员工的分数:" << endl;
    int score;
    cin >> score;

    Worker * worker = new Worker(ID, name, score);
    this->m_workerarray[index] = worker;
}


void Manager::clear_system()
{
    delete [] this->m_workerarray;
    this->m_workerarray = NULL;
    this->m_workernum = 0;
}

bool Manager::is_exists(int ID)
{
    if(this->m_workerarray == NULL || this->m_workernum ==0)
    {
        return false;
    }
    for(int i=0; i<this->m_workernum; i++)
    {
        if(this->m_workerarray[i]->m_ID == ID)
        {
            return true;
        }
    }
    return false;
}


void Manager::move_worker(int ID)
{
    int index;
    //先找到删除员工的索引
    for(int i=0; i<this->m_workernum; i++)
    {
        if(this->m_workerarray[i]->m_ID == ID)
        {
            index = i;
            break;
        }
    }

    for(int i=index; i < this->m_workernum-1; i++)
    {
        this->m_workerarray[i] = this->m_workerarray[i+1];
    }

    this->m_workernum--;
}


void Manager::find_worker(int ID)
{
    for(int i=0; i<this->m_workernum; i++)
    {
        if(this->m_workerarray[i]->m_ID == ID)
        {
            this->m_workerarray[i]->show();
        }
    }
}

void Manager::show()
{
    for(int i=0; i < this->m_workernum; i++)
    {
        this->m_workerarray[i]->show();
    }
}

 

Record: For the beginners of C++ introduction.

Guess you like

Origin blog.csdn.net/Guo_Python/article/details/112007437