首页 > 其他分享 >workmanager.cpp

workmanager.cpp

时间:2024-06-24 10:45:35浏览次数:13  
标签:arr cout int worker num workmanager cpp id

#include <workmanager.h>
#include <worker.h>
#include <fstream>

workmanager::workmanager()
{
ifstream ifs;
ifs.open(FILENAME , ios::in);

if ( !ifs.is_open())//如果文件不存在初始化
{
    this->m_num = 0;//初始化人数为零
    this->m_arr = NULL;//初始化职工信息为零
    this->m_isempty = true;//文件为空初始化为真
    ifs.close();
    return;
}

char ch;//文件存在 但是为空
ifs >> ch;
if (ifs.eof())
{
    this->m_isempty = true;//文件为空初始化为真
    this->m_arr = NULL;//初始化职工信息为零
    this->m_num = 0;//初始化人数为零
    ifs.close();
    return;
}

this->m_num = this->m_getnum();//文件不为空 统计人数
this->m_arr = new worker*[this->m_num];//开辟空间
this->m_in();//将文件中的数据读出到数组中   

}

void workmanager::m_show()
{
if (this->m_isempty)
{
cout << "当前系统人数为零" << endl;
system("pause");
system("cls");
return;
}

for (int i = 0; i < this->m_num; i++)
{
    this->m_arr[i]->m_show();
    cout << endl;
}  
system("pause");
system("cls");

}

void workmanager::m_delete()
{
int chose;
cout << "请输入您要删除的职工编号: ";
cin >> chose;

for (int i = 0; i < this->m_num; i++)
{
    if (this->m_arr[i]->m_id == chose)
    {
        for (int j = i; j < this->m_num; j++)
        {
            this->m_arr[j] = this->m_arr[j+1];
        }
        this->m_num--;
        cout << "成功删除该员工!" << endl;
        break;
    }
    if (i == this->m_num - 1)
    {
        cout << "未找到所需删除员工!" << endl;
    }          
}

this->save();
system("pause");
system("cls");

}

void workmanager::m_modify()
{
int chose;
int id;
string name;
int job;
cout << "请输入所需修改员工的职工编号:" << endl;
cin >> chose;

for (int i = 0; i < this->m_num; i++)
{
    if (this->m_arr[i]->m_id == chose)
    {
        cout << "请输入修改后的职工编号: " << endl;
        cin >> id;
        cout << "请输入修改后的职工姓名: " << endl;
        cin >> name;
        cout << "请输入修改后的职工岗位: " << endl;
        cin >> job;

        worker *m_worker = NULL;
        if (job == 1)
        {
            m_worker = new employee(id, name, job);
        }
        else if (job == 2)
        {
            m_worker = new manager(id, name, job);
        }
        else if (job == 3)
        {
            m_worker = new boss(id, name, job);
        }
        this->m_arr[i] = m_worker;
        cout << "修改成功!" << endl;
    }
    if (i == this->m_num - 1)
    {
        cout << "未找到所需修改员工!" << endl;
    } 
}
this->save();
system("pause");
system("cls");  

}

void workmanager::m_find()
{
int chose;
cout << "请输入您要查找的职工编号: ";
cin >> chose;

for (int i = 0; i < this->m_num; i++)
{
    if (this->m_arr[i]->m_id == chose)
    {
        this->m_arr[i]->m_show();
        cout << endl;
        break;
    }    
    if (i == this->m_num - 1)
    {
        cout << "未找到该员工!" << endl;
    }   
} 
system("pause");
system("cls");    

}

void workmanager::m_sort()
{
int chose;
cout << "请选择您所需的排序方式: " << endl;
cout << "按1键升序排序" << endl;
cout << "按2键降序排序" << endl;
cin >> chose;

if (chose == 1)
{
    for (int i = 0; i < this->m_num -1; i++)
    {
        for (int j = 0; j < this->m_num - i - 1; j++)
        {
            if (this->m_arr[j]->m_id > this->m_arr[j+1]->m_id)
            {
                worker *m_worker = this->m_arr[j];
                this->m_arr[j] = this->m_arr[j+1];
                this->m_arr[j+1] = m_worker; 
            }  
        }  
    } 
    cout << "升序排序完成!" << endl;  
}
else if (chose == 2)
{
    for (int i = 0; i < this->m_num -1; i++)
    {
        for (int j = this->m_num -1; j > i; j--)
        {
            if (this->m_arr[j]->m_id > this->m_arr[j-1]->m_id)
            {
                worker *m_worker = this->m_arr[j];
                this->m_arr[j] = this->m_arr[j-1];
                this->m_arr[j-1] = m_worker; 
            }  
        }  
    }
    cout << "降序排序完成!" << endl; 
}
else
{
    cout << "输入错误!" << endl;
}
 
this->save();
system("pause");
system("cls");  

}

void workmanager::m_clean()
{
int chose;
cout << "请确认是否清空文件!" << endl;
cout << "按1键继续清空" << endl;
cout << "按其他键返回" << endl;
cin >> chose;
if (chose == 1)
{
worker *m_worker[] = {};
this->m_arr = m_worker;
this->m_num = 0;
this->m_isempty = true;
}
else
{
system("cls");
return;
}

this->save();
cout << "清空成功!" << endl;
system("pause");
system("cls"); 

}

int workmanager::m_getnum()
{
ifstream ifs;

ifs.open(FILENAME , ios::in);
int id;
string name;
int job;
int num = 0;

while (ifs >> id && ifs >> name && ifs >> job)
{
    num++;
}
return num;

}

void workmanager::m_in()
{
ifstream ifs;
ifs.open(FILENAME , ios::in);
int id;
string name;
int job;

int num = 0;
while (ifs >> id && ifs >> name && ifs >> job)
{
    worker *m_worker = NULL;
    if (job == 1)
    {
        m_worker = new employee(id, name, job);
    }
    else if (job == 2)
    {
        m_worker = new manager(id, name, job);
    }
    else if (job == 3)
    {
        m_worker = new boss(id, name, job);
    }
    this->m_arr[num] = m_worker;
    num++;        
}   
ifs.close();

}

workmanager::~workmanager()
{
if (this->m_arr != NULL)
{
delete[] this->m_arr;
this->m_arr = NULL;
}
}

void workmanager::save()
{
ofstream ofs;
ofs.open(FILENAME,ios::out);
for (int i = 0; i < this->m_num; i++)
{
ofs << this->m_arr[i]->m_id << " "
<< this->m_arr[i]->m_name << " "
<< this->m_arr[i]->m_job << endl;
}
ofs.close();
}

void workmanager::menu()
{
cout << "" << endl;
cout << "
欢迎使用职工管理系统
" << endl;
cout << "
0.退出管理系统
" << endl;
cout << "1.增加职工信息" << endl;
cout << "2.显示职工信息" << endl;
cout << "3.删除离职职工" << endl;
cout << "4.修改职工信息" << endl;
cout << "5.查找职工信息" << endl;
cout << "6.按照编号排序" << endl;
cout << "7.清空所有文档" << endl;
cout << "**********************************" << endl;
}

void workmanager::m_add()
{
cout << "请输入员工个数: " << endl;
int addnum;//添加人数
cin >> addnum;

if(addnum > 0)
{
    int sumnum = this->m_num + addnum;//总人数
    worker **newspace = new worker*[sumnum];//二级指针指向堆区数组指针

    if(this->m_num != 0)//将原空间数据存入新空间
    {
        for (int  i = 0; i < this->m_num; i++)
        {
            newspace[i] = this->m_arr[i];
        } 
    }

    for (int i = 0; i < addnum; i++)//将添加数据至新空间
    {
        int id;
        string name;
        int chose;

        cout << "请输入第" << i+1 << "个职工的职工编号: " << endl;
        cin >> id;
        cout << "请输入第" << i+1 << "个职工的职工姓名: " << endl;
        cin >> name;
        cout << "请选择第" << i+1 << "个职工的职工岗位: " << endl;
        cout << "1.员工" << endl;
        cout << "2.经理" << endl;
        cout << "3.老板" << endl;
        cin >> chose;
        worker * m_worker = NULL;
        if(chose == 1)
        {
            m_worker = new employee(id,name,chose);
        }
        else if (chose == 2)
        {
            m_worker = new manager(id,name,chose);
        }
        else if (chose == 3)
        {
            m_worker = new boss(id,name,chose);
        }
        newspace[this->m_num+i] = m_worker; 
    }      
    delete[] this->m_arr;//清空原空间
    this->m_arr = newspace;//更改新空间指向 即原空间
    this->m_num = sumnum;//重写原空间人数
    this->m_isempty = false;//更新文件不为空
    cout << "添加成功!" << endl;
    this->save();
}
else
{
    cout << "输入有误,请从新输入!" << endl;
}
system("pause");
system("cls");

}

标签:arr,cout,int,worker,num,workmanager,cpp,id
From: https://www.cnblogs.com/ningmeng8180/p/18264526

相关文章