您好,登錄后才能下訂單哦!
這篇“C++怎么實現職工信息管理系統”除了程序員外大部分人都不太理解,今天小編為了讓大家更加理解“C++怎么實現職工信息管理系統”,給大家總結了以下內容,具有一定借鑒價值,內容詳細步驟清晰,細節處理妥當,希望大家通過這篇文章有所收獲,下面讓我們一起來看看具體內容吧。
具體內容如下
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、這是本人在編寫程序的時候發現的坑如果有什么錯誤請批評指正。
感謝您的閱讀,希望您對“C++怎么實現職工信息管理系統”這一關鍵問題有了一定的理解,具體使用情況還需要大家自己動手實驗使用過才能領會,快去試試吧,如果想閱讀更多相關知識點的文章,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。