用vector容器写职工管理系统

职工信息管理系统:

  1. 存在一个数据文件,用来存储职工各种信息:职工号,姓名,年龄,性别,
    邮编,部门,工资

  2. 可以注册新职工;

  3. 允许修改职工信息

  4. 允许删除职工信息;

4,按照按照姓名和部门查询职工信息;

  1. 可以按照工资多少进行排名,

  2. 可以浏览所有职工信息;

.有一个主界面,供选择和调用上述选项。

.用C++中,文件和链表实现

1、头文件control.h

#ifndef CONTROL_H
#define CONTROL_H

#include <iostream>
#include <vector>
#include <string>
#include <functional>
#include <algorithm>
#include <fstream>
using namespace std;

class Stuff
{
public:
    string ID;
    string name;
    string age;
    string sex;
    string postcodes;
    string department;
    string salary;
};
class Control
{
public:
    void system_init(vector<Stuff> &p);
    void jiemian();
    void Creat_list_tail(vector<Stuff> &p);
    void Del(vector<Stuff> &p); 
    void Change(vector<Stuff> &p);
    void Search(vector<Stuff> &p);
    void Display(vector<Stuff> &p);
    void Sort(vector<Stuff> &p);
};



#endif

2、主函数Staff management system.cpp

#include "control.h"

void Control::jiemian()
{
    cout<<endl;
    cout<<endl;
    cout<<"*******************************************"<<endl;
    cout<<"*       请选择您需要的操作!              *"<<endl;
    cout<<"*       (1)增加职工                       *"<<endl;
    cout<<"*       (2)删除职工                       *"<<endl;
    cout<<"*       (3)修改职工信息                   *"<<endl;    
    cout<<"*       (4)查询职工信息                   *"<<endl;
    cout<<"*       (5)输出所有职工的数据             *"<<endl;
    cout<<"*       (6)按工资排序                     *"<<endl;
    cout<<"*       (0)退出                           *"<<endl;    
    cout<<"*       选择相对的括号里的阿拉伯数字!     *"<<endl;
    cout<<"*******************************************";
    cout<<endl;
    cout<<endl;
}

void Control::Creat_list_tail(vector<Stuff> &vec)
{
    Stuff m;
    cout<<"请输入职工号:";
    cin>>m.ID;
    cout<<endl<<"请输入姓名:";
    cin>>m.name;
    cout<<endl<<"请输入年龄:";
    cin>>m.age;
    cout<<endl<<"请输入性别:";
    cin>>m.sex;
    cout<<endl<<"请输入邮编:";
    cin>>m.postcodes;
    cout<<endl<<"请输入部门:";
    cin>>m.department;
    cout<<endl<<"请输入工资:";
    cin>>m.salary;
    vec.push_back(m);
}
void Control::Del(vector<Stuff> &vec)
{
    string department;
    string name;
    int temp = 0;
    cout<<"请输入部门:";
    cin>>department;
    cout<<endl<<"请输入姓名";
    cin>>name;
    vector<Stuff>::iterator it;
    for(it=vec.begin();  it!=vec.end(); ++it)
    {
        if( ((*it).department == department) && ((*it).name == name))
        {
            vec.erase(it);  
            temp = 1;
            break;
        }
    }
    if(temp)
    {
        cout<<"删除职工"<<name<<"信息成功"<<endl;
    }
    else
    {
        cout<<"未找到匹配信息"<<endl;
    }
}
void Control::Change(vector<Stuff> &vec)
{
    string department;
    string name;
    int temp = 0;
    cout<<"请输入部门:"<<endl;
    cin>>department;
    cout<<"请输入姓名"<<endl;
    cin>>name;
    vector<Stuff>::iterator it;
    for(it=vec.begin();  it!=vec.end(); ++it)
    {
        if( ((*it).department == department) && ((*it).name==name))
        {   
            cout<<"旧的数据"<<endl;
            cout<<"职工号:"<<(*it).ID<<" 姓名:"<<(*it).name<<" 年龄:"<<(*it).age<<" 性别:"<<(*it).sex<<" 邮编:"<<(*it).postcodes<<" 部门:"<<(*it).department<<" 工资:"<<(*it).salary<<endl;
            cout<<"请输入新数据..."<<endl;
            cout<<"请输入职工号:";
            cin>>(*it).ID;
            cout<<endl<<"请输入姓名:";
            cin>>(*it).name;
            cout<<endl<<"请输入年龄:";
            cin>>(*it).age;
            cout<<endl<<"请输入性别:";
            cin>>(*it).sex;
            cout<<endl<<"请输入邮编:";
            cin>>(*it).postcodes;
            cout<<endl<<"请输入部门:";
            cin>>(*it).department;  
            temp = 1;
            break;
        }
    }
    if(temp)
    {
        cout<<"修改职工"<<name<<"信息成功"<<endl;
    }
    else
    {
        cout<<"未找到匹配信息"<<endl;
    }
}
void Control::Search(vector<Stuff> &vec)
{
    string department;
    string name;
    int temp = 0;
    cout<<"请输入部门:"<<endl;
    cin>>department;
    cout<<"请输入姓名"<<endl;
    cin>>name;
    vector<Stuff>::iterator it;
    for(it=vec.begin();  it!=vec.end(); ++it)
    {
        if( ((*it).department == department)&&((*it).name == name) )
        {
            cout<<"职工号:"<<(*it).ID<<" 姓名:"<<(*it).name<<" 年龄:"<<(*it).age<<" 性别:"<<(*it).sex<<" 邮编:"<<(*it).postcodes<<" 部门:"<<(*it).department<<" 工资:"<<(*it).salary<<endl;        
            temp = 1;
        }
    }
    if(!temp)
    {
        cout<<"未找到匹配信息"<<endl;
    }
}
void Control::Display(vector<Stuff> &vec)
{
    vector<Stuff>::iterator  it;
    for(it=vec.begin(); it!=vec.end(); it++)
    {
        cout<<"职工号:"<<(*it).ID<<" 姓名:"<<(*it).name<<" 年龄:"<<(*it).age<<" 性别:"<<(*it).sex<<" 邮编:"<<(*it).postcodes<<" 部门:"<<(*it).department<<" 工资:"<<(*it).salary<<endl;
    }
}

bool cmp( Stuff &a, Stuff &b ) 
{
    return a.salary > b.salary;
}  

void Control::Sort(vector<Stuff> &vec)
{
    sort(vec.begin(), vec.end(), cmp);     
    Display(vec);
}

void Control::system_init(vector<Stuff> &vec)
{
    fstream infile("stud.txt",ios::in);
    if(!infile)
    {
        cerr<<"open error!"<<endl;
        abort( );
    }

    Stuff temp;
    Control t;
    while(1)
    {   
        memset(&temp, 0, sizeof(temp));
        if(infile.read((char *)&temp,sizeof(Stuff)))
        {       
            cout<<"职工号:"<<(temp).ID<<" 姓名:"<<(temp).name<<" 年龄:"<<(temp).age<<" 性别:"<<(temp).sex<<" 邮编:"<<(temp).postcodes<<" 部门:"<<(temp).department<<" 工资:"<<(temp).salary<<endl;     
            vec.push_back(temp);        
        }
        else
        {
            infile.close( );
            cout<<"载入本地信息成功"<<endl;
            break;
        }
    }
}

int main()
{
    char flag;
    vector<Stuff> vec;
    Control con;

    con.system_init(vec);

    do
    {   
        con.jiemian(); //显示界面
        cin>>flag;
        cout<<endl;
        switch(flag)
        {
        case '1':
            con.Creat_list_tail(vec);
            break;
        case '2':
            con.Del(vec);
            break;
        case '3':
            con.Change(vec);
            break;
        case '4':
            con.Search(vec);
            break;
        case '5':
            con.Display(vec);
            break;  
        case '6':
            con.Sort(vec);
            break;
        }
    }while(flag!='0');


    vector<Stuff>::iterator it;
    Stuff temp;
    fstream outfile("stud.txt",ios::out);
    if(!outfile)
    {
        cerr<<"open error!"<<endl;
        abort( );
    }
    for(it = vec.begin(); it  != vec.end(); it++)
    {
        temp.age = it->age;
        temp.department = it->department;
        temp.ID = it->ID;
        temp.name = it->name;
        temp.sex = it->sex;
        temp.postcodes = it->postcodes;
        temp.salary = it->salary;

        outfile.write((char *)&temp,sizeof Stuff);
        cout<<"职工号:"<<(*it).ID<<" 姓名:"<<(*it).name<<" 年龄:"<<(*it).age<<" 性别:"<<(*it).sex<<" 邮编:"<<(*it).postcodes<<" 部门:"<<(*it).department<<" 工资:"<<(*it).salary<<endl;        
    }    
    cout<<"保存信息成功!"<<endl;
    outfile.close( );

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值