C++实现职工信息管理系统
本文实例为大家分享了c++实现职工信息管理系统的具体代码,供大家参考,具体内容如下
1、项目需求
【C++实现职工信息管理系统】
文章图片
2、功能实现的具体思路为:
(1) 经行职工信息的读入,用while循环进行读入,对于职工信息类型的设定方便起见除年龄和薪资用int类型其他信息都用string类型,职工信息用ostream 流写入txt并将信息保存在txt文件中,打印职工信息由于c++读文件方式不方便所以用c语言方式读文件,文件读取以后将文件中读取的信息加载到vector容器中后打印在终端窗口,其他功能的实现也是将文件中的信息读取到vector容器中,后用vector容器中的重载函数进行功能的实现;
3、头文件的编写的具体实现
#pragma once#include#includeusing 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); void addWorkerInformations(); //职工信息浏览void putWorkerInformation(); //查询 按籍贯或学历查询职工信息void lookWorkerInformation(); //按工资高低排序void sortWorkerInformation(); //删除职工信息 按姓名删除void deleteWorkerInformation(); //修改职工信息 把研究生薪资增加 500void 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; };
ostream& operator<<(ostream& os, const Worker& worker); //使用重载函数直接输出对象的信息进行输出
4、对头文件中函数的具体实现
#include#include#include#include #include #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){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()< > 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++; }} } //修改职工信息 把研究生薪资增加 500void 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<
5、main函数的具体实现
#include#include#include #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){caseONE:work.lookWorkerInformation(); //根据籍贯和学历对职工信息进行查询cout << "---已完成该功能---" << endl << endl; break; caseTWO:work.sortWorkerInformation(); //按薪水对职工信息进行排序cout << "---已完成该功能---" << endl << endl; break; case THREE: work.putWorkerInformation(); //打印职工信息; cout << "---已完成该功能---" << endl << endl; break; caseFOUR:work.addWorkerInformations(); //增加职工信息cout << "---已完成该功能---" << endl << endl; break; caseFIVE:work.addWorkerSalary(); //增加研究生薪水cout << "---已完成该功能---" << endl << endl; break; caseSIX:work.deleteWorkerInformation(); //删除信息cout << "---已完成该功能---" << endl << endl; break; caseSEVEN: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、这是本人在编写程序的时候发现的坑如果有什么错误请批评指正。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
推荐阅读
- python实现有效的括号判断实例代码
- C#循环与循环控制的表达式树实现
- Jackson2的JsonSchema实现java实体类生成json方式
- 初识C++的const关键字|初识C++的const关键字,常量与常变量
- Java编写实现坦克大战小游戏
- 详解Nacos 配置中心客户端配置缓存动态更新的源码实现
- 云原生技术赋能ISV实现应用现代化
- C++实现校园导游系统
- 如何利用Python实现n*n螺旋矩阵
- C语言实现二叉树层次遍历介绍