【学习笔记】C++应用文本操作,多态实现职工管理系统

目录

此程序为根据黑马公开课展示实例进行编写,稍有改动

1.workersystem.cpp

2.wokerManager.h

3.wokerManager.cpp

4.多重指针数组


此程序为根据黑马公开课展示实例进行编写,稍有改动

1.workersystem.cpp

#include "wokerManager.h"
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

int main()
{

    wokerManager wm;

    while (1)
    {
        wm.showMenu();

        int choice = 0;

        cout << "请输入你的选择" << endl;

        cin >> choice;

        switch (choice)
        {
        case 0: //退出系统
        {
            wm.eixtSystem();
            break;
        }

        case 1: //添加职工
        {
            wm.addUser();

            break;
        }

        case 2: //显示员工信息
        {
            wm.showallinfo();
            break;
        }
        case 3: //查找员工信息
        {
            cout << "输入员工ID" << endl;

            cin >> choice;
            if (wm.searchuser(choice) != -1)
            {
                wm.worker_array[wm.searchuser(choice)]->showinfo();
                system("pause");
                system("cls");
            }
            else
            {
                cout << "查无此人" << endl;
                system("pause");
                system("cls");
            }
            break;
        }
        case 4: //修改员工信息
        {
            cout << "请输入需要修改的员工编号" << endl;
            cin >> choice;
            wm.modify_user(choice);
            break;
        }
        case 5: //删除职工信息
        {
            cout << "请输入需要删除的员工编号" << endl;
            cin >> choice;
            wm.deleteUser(choice);
            break;
        }
        case 6: //冒泡排序
        {
            break;
        }
        case 7: //删除所有信息
        {
            wm.deleteAll();
            break;
        }
        default:

            cout << "请重新输入" << endl;
            break;
        }
    }

    system("pause");

    return 0;
}

2.wokerManager.h

#pragma once
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

#define FILENAME "workersystem.txt"

class Worker;
class wokerManager
{
private:
    /* data */
public:
    wokerManager();
    ~wokerManager();

    void showMenu();//打印菜单

    void eixtSystem();//退出系统

    void addUser();//添加用户

    void save();//保存数据到文本

    int get_usernum();//统计当前用户数

    void init_system();//初始化当前内存

    void showallinfo();//打印所有用户

    int searchuser(int id);//根据ID查找用户

    void modify_user(int id);//修改用户信息

    void deleteUser(int id);//删除单一用户信息

    void deleteAll();//删除所有信息

    int current_num; //当前用户数

    Worker **worker_array; //用于维护用户的地址数组

    bool fileisEmpty;
};

class Worker
{
public:
    int num_id;

    string name;

    int num_depid;

    virtual void showinfo() = 0;
};

class Employee : public Worker
{
public:
    Employee(int id, string name, int depid);

    virtual void showinfo();
};

class Manager : public Worker
{
public:
    Manager(int id, string name, int depid);

    virtual void showinfo();
};

class CEO : public Worker
{
public:
    CEO(int id, string name, int depid);

    virtual void showinfo();
};

3.wokerManager.cpp

#include "wokerManager.h"

wokerManager::wokerManager()
{

    ifstream ifs;
    //判断文件是否存在
    ifs.open(FILENAME, ios::in);
    if (!ifs.is_open())
    {
        //     cout << "文件不存在" << endl;
        this->current_num = 0;
        this->worker_array = NULL;
        this->fileisEmpty = true;
        ifs.close();
        return;
    }
    //判断文件是否为空
    char ch;
    ifs >> ch;
    if (ifs.eof())
    {
        //     cout << "文件为空" << endl;
        this->current_num = 0;
        this->fileisEmpty = true;
        this->worker_array = NULL;
        ifs.close();
        return;
    }
    ifs.close();
    //文件不为空
    this->current_num = get_usernum();
    // cout << "职工人数为" << this->current_num << endl;
    this->init_system();
    this->fileisEmpty = false;
    return;
}

wokerManager::~wokerManager()
{
    if (this->worker_array != NULL)
    {
        for (int i = 0; i < this->current_num; i++)
        {
            if (this->worker_array[i] != NULL)
            {
                delete worker_array[i];
            }
        }

        delete[] this->worker_array;

        this->worker_array = NULL;

        cout << "销毁" << endl;

        system("pause");
    }
}

/
//添加用户///
/
void wokerManager::addUser()
{
    cout << "请输入添加人数" << endl;

    int addnum = 0;

    cin >> addnum; //保存 用户输入数量

    if (addnum > 0)
    {

        int newsize = addnum + this->current_num;

        Worker **newspace = new Worker *[newsize]; //注意此时worker**指向的是一个worker*类型的数组,与指针指向数组类似,此时newspace[i]所编译出的值就等同于 数组名[i]
                                                   //因此newspace[i]实际上直接指向数组成员(非数组第i个成员的地址)即*worker类型的指针,*newspace[i]直接指向对应成员

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

        for (int i = 0; i < addnum; i++)
        {
            string name;
            int select;
            int id;

            cout << "请输入第" << i + 1 << "位用户数据" << endl;

            cout << "姓名:" << endl;

            cin >> name;

            cout << "编号:" << endl;

            cin >> id;

            while (1)
            {
                cout << "请选择职位:\n1.职工\n2.经理\n3.CEO" << endl;

                cin >> select;

                if (select == 1 || select == 2 || select == 3)
                {
                    break;
                }
                else
                {
                    cout << "职位选择错误,请重新输入" << endl;
                    system("pause");
                    system("cls");
                }
            }
            Worker *worker = NULL;

            switch (select)
            {
            case 1:
                worker = new Employee(id, name, select);
                break;

            case 2:
                worker = new Manager(id, name, select);
                break;

            case 3:
                worker = new CEO(id, name, select);
                break;
            }
            newspace[i + this->current_num] = worker; //注意此时worker**指向的是一个worker*类型的数组,与指针指向数组类似,此时newspace[i]所编译出的值就等同于 数组名[i]
                                                      //因此newspace[i]实际上直接指向数组成员(非数组第i个成员的地址)即*worker类型的指针,*newspace[i]直接指向对应成员
        }

        //释放原有的空间
        if (this->worker_array != NULL)
        {
            delete[] this->worker_array;
        }

        this->worker_array = newspace;

        this->current_num = newsize;

        this->fileisEmpty = false;

        wokerManager::save();

        system("pause");

        system("cls");

        cout << "保存成功!" << endl;

        system("pause");

        system("cls");
    }
    else
    {
        cout << "输入有误" << endl;

        system("pause");

        system("cls");
    }
}

void wokerManager::showMenu()
{

    cout << "*****\t**************\t******" << endl;
    cout << "*****\t欢迎使用职工系统\t****" << endl;
    cout << "*****\t1.添加职工信息\t*****" << endl;
    cout << "*****\t2.显示职工信息\t*****" << endl;
    cout << "*****\t3.查找职工信息\t*****" << endl;
    cout << "*****\t4.修改职工信息\t*****" << endl;
    cout << "*****\t5.删除职工信息\t*****" << endl;
    cout << "*****\t6.按照编号排序\t*****" << endl;
    cout << "*****\t7.清空所有文档\t*****" << endl;
    cout << "*****\t0.退出职工系统\t*****" << endl;
    cout << "*****\t**************\t*****" << endl
         << endl;
}

void wokerManager::save()
{

    ofstream ofs;

    ofs.open(FILENAME, ios::out);

    for (int i = 0; i < this->current_num; i++)
    {
        ofs << this->worker_array[i]->num_id << " " << this->worker_array[i]->name << " " << this->worker_array[i]->num_depid << endl;
    }
    ofs.close();
}

void wokerManager::eixtSystem()
{
    system("cls");

    cout << "欢迎下次使用" << endl;

    system("pause");

    exit(0);
}

int wokerManager::get_usernum()
{
    ifstream ifs;

    ifs.open(FILENAME, ios::in);

    char buf[256] = {0};

    int usernum = 0;

    while (ifs.getline((char *)buf, 100))
    {
        usernum++;
    }
    ifs.close();

    return usernum;
}

void wokerManager::init_system()
{

    this->worker_array = new Worker *[this->current_num];

    ifstream ifs;

    ifs.open(FILENAME, ios::in);

    int id;

    int depid;

    string name;

    int i = 0; //数组元素位置

    while (ifs >> id && ifs >> name && ifs >> depid)
    {
        Worker *worker; //我们现在要存的是父类的指针

        if (depid == 1)
        {

            worker = new Employee(id, name, depid);
        }
        if (depid == 2)
        {
            worker = new Manager(id, name, depid);
        }
        if (depid == 3)
        {
            worker = new CEO(id, name, depid);
        }
        this->worker_array[i] = worker;

        i++;
    }
    ifs.close();
}

/
//显示用户///
/

void wokerManager::showallinfo()
{
    if (this->fileisEmpty)
    {
        cout << "当前用户数为空!" << endl;
    }
    else
    {
        for (int i = 0; i < this->current_num; i++)
        {
            this->worker_array[i]->showinfo();
        }
    }
    system("pause");
    system("cls");
}

/
//查找用户///
/
int wokerManager::searchuser(int id)
{

    for (int i = 0; i < this->current_num; i++)
    {
        if (this->worker_array[i]->num_id == id)
        {
            return i;
        }
    }
    return -1;
}
/
//修改用户///
/
void wokerManager::modify_user(int id)
{
    int num = this->searchuser(id);

    if (this->searchuser(id) == -1)
    {
        cout << "查无此人" << endl;

        system("pause");

        system("cls");
    }
    else
    {
        cout << "请输入新的姓名:" << endl;
        cin >> this->worker_array[num]->name;

        cout << "请输入新的职位:\n1.职工\n2.经理\n3.CEO" << endl;
        cin >> this->worker_array[num]->num_depid;
        this->save();
        system("pause");

        system("cls");
    }
}
/
//删除单一用户///
/
void wokerManager::deleteUser(int id)
{
    int num = this->searchuser(id);

    if (num == -1)
    {
        cout << "查无此人" << endl;

        system("pause");

        system("cls");

        return;
    }
    else
    {
        delete this->worker_array[num];

        for (int i = num; i < this->current_num; i++)
        {
            this->worker_array[i] = this->worker_array[i + 1];
            cout << worker_array[i] << endl;
        }
    }

    this->current_num--;

    if (this->current_num == 0)
    {

        delete[] this->worker_array;

        this->worker_array = NULL;

        this->fileisEmpty = true;
    }
    //同步数据到文件中
    this->save();

    cout << "删除成功" << endl;

    system("pause");

    system("cls");
}

void wokerManager::deleteAll()
{
    int choice;
    cout << "是否删除?\n1.确认\n2.取消" << endl;
    cin >> choice;
    if (choice == 1)
    {
        if (this->worker_array != NULL)
        {
            for (int i = 0; i < this->current_num; i++)
            {
                if (this->worker_array[i] != NULL)
                {
                    delete worker_array[i];
                }
            }

            delete[] this->worker_array;
        }
        this->worker_array = NULL;
        this->current_num = 0;
        this->fileisEmpty = true;
        this->save();
        cout << "删除成功" << endl;
        system("pause");
        system("cls");
    }
    return;
}


//职工//

Employee::Employee(int id, string name, int depid)
{

    num_id = id;
    this->name = name;
    num_depid = depid;
}

void Employee::showinfo()
{
    cout << "职工编号: " << num_id
         << "\t职工姓名: " << name
         << "\t职工岗位: 员工"
         << "\t职责: 打工人" << endl;
}


//经理//

Manager::Manager(int id, string name, int depid)
{

    num_id = id;
    this->name = name;
    num_depid = depid;
}
void Manager::showinfo()
{
    cout << "职工编号: " << num_id
         << "\t职工姓名: " << name
         << "\t职工岗位: 经理"
         << "\t职责: 高级打工人" << endl;
}

//CEO///

CEO::CEO(int id, string name, int depid)
{

    num_id = id;
    this->name = name;
    num_depid = depid;
}
void CEO::showinfo()
{
    cout << "职工编号: " << num_id
         << "\t职工姓名: " << name
         << "\t职工岗位: CEO"
         << "\t职责: 究极打工人" << endl;
}

4.多重指针数组

#include <iostream>

using namespace std;

int main()
{

	int a[5] = {0, 1, 2, 3, 4};

	int *arr[2] = {&a[0], &a[1]};

	int **p_arr;

	p_arr = arr;

	cout << sizeof(p_arr) << endl;

	cout << sizeof(arr) << endl; //arr为int型数组
cout<<"arr数组的首地址"<<endl;
	cout << p_arr << endl; //arr数组的首地址
	cout << &arr[0] << endl;
	cout << &arr << endl
		 << endl;

cout<<"数组a的首地址"<<endl;
	cout << *p_arr << endl;//数组a的首地址
	cout << arr[0] << endl;
	cout << a << endl
		 << endl;

cout<<"数组arr[1]的地址"<<endl;
	cout << &p_arr[1] << endl;
	cout << &arr[1] << endl
		 << endl;

cout<<"数组a[1]的地址"<<endl;
	cout<<p_arr[1]<<endl;
	cout<<arr[1]<<endl;
	cout<<&a[1]<<endl<<endl;
		
cout<<"数组a[0]的值"<<endl;
	cout << *p_arr[0] << endl;
	cout << *arr[0] << endl;
	cout << a[0] << endl;

	system("pause");

	return 0;
}

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值