亲宝软件园·资讯

展开

C++职工信息管理系统

小生_无情 人气:0

1、项目需求

2、功能实现的具体思路为:

(1) 经行职工信息的读入,用while循环进行读入,对于职工信息类型的设定方便起见除年龄薪资int类型其他信息都用string类型,职工信息用ostream 流写入txt并将信息保存在txt文件中,打印职工信息由于c++读文件方式不方便所以用c语言方式读文件,文件读取以后将文件中读取的信息加载到vector容器中后打印在终端窗口,其他功能的实现也是将文件中的信息读取到vector容器中,后用vector容器中的重载函数进行功能的实现; 

3、头文件的编写的具体实现

#pragma once
#include<string>
#include<vector>
 
using namespace std;
 
class Worker {
public:
    Worker(string workNumber, string name, string gender,
        string education, string addr, string phoneNumber,
        int age, int salary);
    Worker();
    ~Worker();
    //职工信息描述
    string description();
 
    //录入职工信息
    static void addWorkerInformation(vector<Worker>&worker);
    void addWorkerInformations();
 
    //职工信息浏览
    void putWorkerInformation();
 
    //查询 按籍贯或学历查询职工信息
    void lookWorkerInformation();
 
    //按工资高低排序
    void sortWorkerInformation();
 
    //删除职工信息 按姓名删除
    void deleteWorkerInformation();
 
    //修改职工信息 把研究生薪资增加 500
    void addWorkerSalary();
 
    friend ostream& operator<<(ostream& os, const Worker& worker);   //友元函数直接输出对象的信息
    Worker& operator=(const Worker& worker);    //赋值构造函数
private:
    string workNumber;    //职工号
    string name;    //名字
    string gender;    //性别
    string education; //学历
    string addr;    //籍贯
    string phoneNumber; //电话号码
    int age;        //年龄
    int salary;  //薪资
 
    void loadWorkerInformation();
    void printWorkerInformation();
 
    vector<Worker>worker;
};

ostream& operator<<(ostream& os, const Worker& worker); //使用重载函数直接输出对象的信息进行输出

4、对头文件中函数的具体实现

#include<string>
#include<sstream>
#include<iostream>
#include<fstream>
#include<vector>
#include"worker.h"
 
#define NAME "workerInformation.txt"
#define ZERO "0"
#define MONEY 500
#define STUDENT "研究生"
 
Worker::Worker(string workNumber, string name, string gender,
    string education, string addr, string phoneNumber, 
    int age, int salary){
    this->addr = addr;
    this->age = age;
    this->gender = gender;
    this->name = name;
    this->phoneNumber = phoneNumber;
    this->education = education;
    this->salary = salary;
    this->workNumber = workNumber;
}
 
Worker::Worker(){
 
}
 
Worker::~Worker(){
 
}
 
//职工信息描述
string Worker::description(){
    stringstream ret;
    ret << "职工号:" << workNumber << "  性别:" << gender << "  名字:" << name
        << "  年龄:" << age << "  学历:" << education << "  籍贯:" << addr
        << "  薪资:" << salary << "  电话号码:" << phoneNumber;
    return ret.str();
}
 
void Worker::addWorkerInformation(vector<Worker>& worker){
    string workNumber;    //职工号
    string name;    //名字
    string gender;    //性别
    string education; //学历
    string addr;    //籍贯
    string phoneNumber; //电话号码
    int age;        //年龄
    int salary;  //年龄
 
    while (1) {
        cout << "输入职工号[输入 0 结束输入];";
        cin >> workNumber;
        if (workNumber == ZERO) {
            break;
        }
        cout << "输入名字:";
        cin >> name;
        cout << "输入性别:";
        cin >> gender;
        cout << "输入学历:";
        cin >> education;
        cout << "输入籍贯:";
        cin >> addr;
        cout << "输入电话号码:";
        cin >> phoneNumber;
        cout << "输入年龄:";
        cin >> age;
        cout << "输入薪资:";
        cin >> salary;
        worker.push_back(Worker(workNumber, name, gender,
            education, addr, phoneNumber,
            age, salary));
    }
    return;
}
 
void Worker::addWorkerInformations(){
    ofstream outFile;
    outFile.open(NAME,ios::app);
    Worker::addWorkerInformation(this->worker);
 
    for (int i = 0;i < worker.size();i++) {
        outFile << worker[i].description()<<endl;
    }
    worker.clear();
    outFile.close();
}
 
//职工信息浏览
void Worker::putWorkerInformation(){
    loadWorkerInformation();
    printWorkerInformation();
}
 
//查询 按籍贯或学历查询职工信息
void Worker::lookWorkerInformation(){
    string  addr;
    string education;
    cout << "输入要查询的职工籍贯:";
    cin >> addr;
    cout << "请输入要查询的职工学历:";
    cin >> education;
 
    loadWorkerInformation();
    for (int i = 0;i < worker.size();i++) {
        if ((worker[i].addr == addr)&&(worker[i].education == education)){
            cout << worker[i] << endl;
        }
    }
}
 
//按工资高低排序
void Worker::sortWorkerInformation(){
    loadWorkerInformation();
    Worker workers;
    for (int j = 0;j < worker.size() ;j++) {
        for (int i = j + 1;i < worker.size();i++) {
            if (worker[j].salary > worker[i].salary) {
                workers = worker[i];
                worker[i] = worker[j];
                worker[j] = workers;
            }
        }
    }
    printWorkerInformation();
}
 
//删除职工信息 按姓名删除
void Worker::deleteWorkerInformation(){
    ofstream outFile;
    string name;
    cout << "请输入你要删除人的姓名:";
    cin >> name;
 
    loadWorkerInformation();
    outFile.open(NAME);
    for (int i = 0;i < worker.size();) {
        if (worker[i].name == name) {
            worker.erase(worker.begin() + i);
        }
        else {
            outFile << worker[i] << endl;
            i++;
        }
    }
 
}
 
//修改职工信息 把研究生薪资增加 500
void Worker::addWorkerSalary(){
    loadWorkerInformation();
    int salary;
 
    ofstream outFile;
    outFile.open(NAME);
 
    for (int i = 0;i < worker.size();) {
        if (worker[i].education == STUDENT) {
            salary = worker[i].salary + MONEY;
            outFile<<Worker(worker[i].workNumber,worker[i].name,worker[i].gender,
                worker[i].education, worker[i].addr, worker[i].phoneNumber,
                worker[i].age, salary)<<endl;
 
            worker.erase(worker.begin()+i);
        }
        else {
            outFile << worker[i] << endl;
            i++;
        }
    }
    worker.clear();
}
 
Worker& Worker::operator=(const Worker& worker) {
    addr = worker.addr;
    age = worker.age;
    gender = worker.gender;
    education = worker.education;
    name = worker.name;
    phoneNumber = worker.phoneNumber;
    salary = worker.salary;
    workNumber = worker.workNumber;
 
    return *this;
}
 
void Worker::loadWorkerInformation(){
    char workNumber[32];    //职工号
    char name[32];    //名字
    char gender[32];    //性别
    char education[32]; //学历
    char addr[32];    //籍贯
    char phoneNumber[32]; //电话号码
    int age;        //年龄
    int salary;  //年龄
 
    string line;
    ifstream inFile;
    inFile.open(NAME);
 
    if (!inFile.is_open()) {
        cout << "----职工数据库打开失败----";
        inFile.close();
        exit(1);
    }
    while (1) {
        getline(inFile, line);
        if (inFile.eof()) {
            break;
        }
        if (line == "")continue;
 
        sscanf_s(line.c_str(), "职工号:%s 性别:%s 名字:%s 年龄:%d 学历:%s 籍贯:%s 薪资:%d 电话号码:%s",
            workNumber, sizeof(workNumber), gender, sizeof(gender), name, sizeof(name),
            &age, education, sizeof(education), addr, sizeof(addr), &salary, 
            phoneNumber, sizeof(phoneNumber));
 
        worker.push_back(Worker(string(workNumber), string(name), string(gender),
            string(education), string(addr), string(phoneNumber),
            age, salary));
    }
}
 
void Worker::printWorkerInformation(){
    for (int i = 0;i < worker.size();i++) {
        cout << worker[i] << endl<<endl;
    }
    worker.clear();
}
 
ostream& operator<<(ostream& os, const Worker& worker){
    os<<  "职工号:" <<worker.workNumber << "  性别:" << worker.gender << "  名字:" << worker.name
        << "  年龄:" << worker.age << "  学历:" << worker.education << "  籍贯:" << worker.addr
        << "  薪资:" << worker.salary << "  电话号码:" << worker.phoneNumber;
    return os;
}

5、main函数的具体实现

#include<stdlib.h>
#include<iostream>
#include<conio.h>
#include"worker.h"
 
#define ONE '1'
#define TWO '2'
#define THREE '3'
#define FOUR '4'
#define FIVE '5'
#define SIX '6'
#define SEVEN '7'
 
int main(void) {
    Worker work;
    cout << "输入【1】根据籍贯和学历对职工信息进行查询" << endl;
    cout << "输入【2】按薪水对职工信息进行排序" << endl;
    cout << "输入【3】打印职工信息" << endl;
    cout << "输入【4】增加职工信息" << endl;
    cout << "输入【5】增加研究生薪水" << endl;
    cout << "输入【6】删除信息" << endl;
    cout << "输入【7】退出功能" << endl << endl;
 
    bool quit = false;
    do {
        
        if (_kbhit()) {
            char ch = _getch();
            switch(ch){
            case  ONE:
                work.lookWorkerInformation();//根据籍贯和学历对职工信息进行查询
                cout << "---已完成该功能---" << endl << endl;
                break;
            case  TWO:
                work.sortWorkerInformation();//按薪水对职工信息进行排序
                cout << "---已完成该功能---" << endl << endl;
                break;
            case THREE: 
                work.putWorkerInformation();//打印职工信息;
                cout << "---已完成该功能---" << endl << endl;
                break;
            case  FOUR:
                work.addWorkerInformations();//增加职工信息
                cout << "---已完成该功能---" << endl << endl;
                break;
            case  FIVE:
                work.addWorkerSalary();//增加研究生薪水
                cout << "---已完成该功能---" << endl << endl;
                break;
            case  SIX:
                work.deleteWorkerInformation();//删除信息
                cout << "---已完成该功能---" << endl << endl;
                break;
            case  SEVEN:
                quit = true;//退出循环
                break;
            default:
                cout << "----无其他功能选项----";
                break;
            }
        }
    } while (quit == false);
    
    system("pause");
    return 0;
}

6、功能3,4,5,6,的实现都是将文件中的信息加载到vector中,并且使用的都是保存了worker这个类对象的同一个容器(假如先实现工能3,将信息加载到vector中后功能3结束以后,功能3使用到的职工信息依然在vector这个容器中,所以使用完一个函数后要将vector中的信息清理完全,不然会影响下一个函数的使用)。

7、在实现功能5的时候会使用到vector容器的重载函数( worker.erase(worker.begin()+i);)使用完以后返回的是下一个迭代器指向下一个对象,所以在使用使用for循环遍历容器的时候因注意for (int i = 0;i < worker.size();i++)  中i++具体放在哪个位置比较合适。

8、这是本人在编写程序的时候发现的坑如果有什么错误请批评指正。

加载全部内容

相关教程
猜你喜欢
用户评论