基于C++泛型編程職工管理系統(tǒng)
前言:
前面介紹到了C++的泛型編程,并實現(xiàn)了萬能容器,不過那使用的是數(shù)組,今天呢咱帶大家實踐一下使用泛型技術(shù),結(jié)合單鏈表實現(xiàn)一個職工管理系統(tǒng)。保證大家看完之后有所感悟。
一、泛型編程思想
所謂泛型就是類型不固定,只需修改少量代碼就可以擴展為其他類型的應用,由于C++是一門靜態(tài)編譯型的語言,變量的類型都是事先編譯好的,如果不用泛型進行編程,一段代碼始終就是那么點作用。使用泛型編程后,可以很簡單的對其他類型進行擴展。泛型編程核心思想就是將數(shù)據(jù)類型設置為模板,第一次編譯是對模板進行編譯,第二次編譯會帶入人為傳的類型參數(shù)。前面文章有講函數(shù)模板與類模板,忘記的小伙伴可以去看看。
二、單鏈表是什么?
單鏈表中的單代表一條,鏈表意思就是一個個節(jié)點鏈接起來的表結(jié)構(gòu)。
其最典型的特征就是節(jié)點只有一個指針域。并且該指針域指向下一節(jié)點的地址
1.圖示
- 鏈表中的第一個節(jié)點被稱為頭結(jié)點,一般不存儲數(shù)據(jù),指向頭結(jié)點的指針稱為頭指針
- 第一個存儲數(shù)據(jù)的節(jié)點稱為首節(jié)點,末尾節(jié)點稱為尾節(jié)點,指針域賦空,防止變?yōu)橐爸羔槨?/li>
2.鏈表的節(jié)點結(jié)構(gòu)【節(jié)點類】
①常規(guī)鏈表節(jié)點
- 包含數(shù)據(jù)域,指針域。指針域指針類型與節(jié)點類型保持一致。
class node { private: ?? ?//數(shù)據(jù)域 ?? ?string data1; ?? ?string data2; ?? ?//指針域 ?? ?node* next; public: ?? ?node() { ?? ?} }
②泛型鏈表節(jié)點
其中T代表的是一種不確定的數(shù)據(jù)類型,data是一個T類型的對象,其作用類似于結(jié)構(gòu)體存儲數(shù)據(jù)域的信息,但是在c++中他必須用類實現(xiàn),因為該數(shù)據(jù)類型要有屬于自己的屬性與方法。node* next
代表一個T類型的node指針,其本質(zhì)還是node指針,只不過T的類型決定著node *指向的節(jié)點中的data的類型。
代碼如下:
template<typename T> class node { private: ?? ?//數(shù)據(jù)域 ?? ?T data; ?? ?//指針域 ?? ?node<T>* next; public: ?? ?node() { ?? ?} }
3.鏈表類
常規(guī)鏈表類中需要包含一個頭指針,指向鏈表的頭結(jié)點,然后創(chuàng)建一個鏈表對其增刪改查泛型編程中的鏈表類,也要是一個鏈表類。實現(xiàn)類型的參數(shù)化,具體如下:
template<typename T> class link { private: ?? ?//傳入類型的時候,先傳給link然后link會傳給node; ?? ?node<T>* head; public: ?? ?link() { ?? ?} ?? ?bool add(){ ?? ?} ?? ?bool del(){ ?? ?} ?? ?
三、泛型編程核心
1.實現(xiàn)數(shù)據(jù)類
泛型就是要將你寫的類型,像對待int string類型那樣對待。首先要進行的就是運算符重載重載了運算符你可以使用cin,cout直接對相應的對象進行輸入,輸出。可以直接使用=進行賦值。
具體實現(xiàn)如下:
class officer { ?? ?//重載了標準輸入輸出流函數(shù)、文件輸入輸出流函數(shù)(可以直接存文件) ?? ?friend ostream& operator<<(ostream& out, officer& obj); ?? ?friend istream& operator>>(istream& in, officer& obj); ?? ?friend ofstream& operator<<(ofstream& outfile, officer& obj);//--------輸出到文件 ?? ?friend ifstream& operator>>(ifstream& infile, officer& obj);//---------讀取文件 private: ?? ?string id_card; ?? ?string name; ?? ?string sex; ?? ?int age; ?? ?string post; ?? ?int money; public://---------------------------------私有屬性管理方法 ?? ?officer() { ?? ??? ?id_card = ""; ?? ??? ?name = ""; ?? ??? ?sex = ""; ?? ??? ?age = 0; ?? ??? ?post = ""; ?? ??? ?money = 0; ?? ?} ?? ?//拷貝構(gòu)造函數(shù)結(jié)合賦值函數(shù),直接進行賦值。 ?? ?officer(officer& obj) { ?? ??? ?this->id_card = obj.getid(); ?? ??? ?this->name = obj.getname(); ?? ??? ?this->sex = obj.getsex(); ?? ??? ?this->age = obj.getage(); ?? ??? ?this->money = obj.getmoney(); ?? ??? ?this->post = obj.getpost(); ?? ?} ?? ?officer& operator=(officer& obj) { ?? ??? ?this->id_card = obj.getid(); ?? ??? ?this->name = obj.getname(); ?? ??? ?this->sex = obj.getsex(); ?? ??? ?this->age = obj.getage(); ?? ??? ?this->money = obj.getmoney(); ?? ??? ?this->post = obj.getpost(); ?? ??? ?return *this; ?? ?} ?? ?//查重時使用 ?? ?bool operator==(officer& obj) { ?? ??? ?if (this->getid() == obj.getid() && this->getname() == obj.getname() && this->getsex() == obj.getsex()\ ?? ??? ??? ?&& this->getage() == obj.getage() && this->getpost() == obj.getpost() && this->getmoney() == obj.getmoney()) { ?? ??? ??? ?return true; ?? ??? ?} ?? ??? ?return false; ?? ?} ?? ?bool operator!=(officer& obj) { ?? ??? ?if (this->getid() == obj.getid()) { ?? ??? ??? ?return false; ?? ??? ?} ?? ??? ?return true; ?? ?} ?? ?//排序時使用,根據(jù)工資的高低。 ?? ?bool operator>(officer& obj) { ?? ??? ?if (this->getmoney() > obj.getmoney()) { ?? ??? ??? ?return true; ?? ??? ?} ?? ??? ?return false; ?? ?} ?? ?bool operator<(officer& obj) { ?? ??? ?if (this->getmoney() < obj.getmoney()) { ?? ??? ??? ?return true; ?? ??? ?} ?? ??? ?return false; ?? ?} ?? ?void setpost(string post) { ?? ??? ?this->post = post; ?? ?} ?? ?void setmoney(int money) { ?? ??? ?this->money = money; ?? ?} ?? ?void setid(string id) { ?? ??? ?id_card = id; ?? ?} ?? ?void setname(string name) { ?? ??? ?this->name = name; ?? ?} ?? ?void setsex(string sex) { ?? ??? ?this->sex = sex; ?? ?} ?? ?void setage(int age) { ?? ??? ?this->age = age; ?? ?} ?? ?string getid() { ?? ??? ?return id_card; ?? ?} ?? ?string getname() { ?? ??? ?return name; ?? ?} ?? ?string getsex() { ?? ??? ?return sex; ?? ?} ?? ?int getage() { ?? ??? ?return age; ?? ?} ?? ?string getpost() { ?? ??? ?return post; ?? ?} ?? ?int getmoney() { ?? ??? ?return money; ?? ?} };
2.實現(xiàn)鏈表類
泛型的鏈表類、節(jié)點類一般就是寫死的,做到換一個數(shù)據(jù)類還可以用的效果所以在泛型鏈表類中的提示性語句要有一定的泛化程度,不可以針對某種類型提示。
template<typename T> class link { private: ?? ?node<T>* head; public: ?? ?link() { ?? ??? ?string classname; ?? ??? ?ifstream infile; ?? ??? ?node<T>* p, * q; ?? ??? ?p = new node<T>; ?? ??? ?p->setnext(NULL); ?? ??? ?head = new node<T>; ?? ??? ?head->setnext(NULL); ?? ??? ?q = head; ?? ??? ?classname = head->GetClass(); ?? ??? ?classname.erase(remove(classname.begin(), classname.end(), '<'), classname.end()); ?? ??? ?classname.erase(remove(classname.begin(), classname.end(), '>'), classname.end()); ?? ??? ?//cout << classname << endl; ?? ??? ?infile.open(classname); ?? ??? ?while (infile >> p->opedata()) { ?? ??? ??? ?q->setnext(p); ?? ??? ??? ?q = q->getnext(); ?? ??? ??? ?p = new node<T>; ?? ??? ??? ?p->setnext(NULL); ?? ??? ?} ?? ??? ?delete p; ?? ??? ?infile.close(); ?? ?} ?? ?void addnode() {//-------------------------增 ?? ??? ?node<T>* p, * q; ?? ??? ?p = new node<T>; ?? ??? ?p->setnext(NULL); ?? ??? ?cout << "請輸入您要存儲的數(shù)據(jù):" << endl; ?? ??? ?cin >> p->opedata(); ?? ??? ?q = head; ?? ??? ?for (; q->getnext() != NULL; q = q->getnext()) { ?? ??? ??? ?if (q->getnext()->opedata() == p->opedata()) { ?? ??? ??? ??? ?cout << "您輸入的數(shù)據(jù),已存在,不需要重復錄入" << endl; ?? ??? ??? ??? ?system("pause"); ?? ??? ??? ??? ?delete p; ?? ??? ??? ??? ?return; ?? ??? ??? ?} ?? ??? ?} ?? ??? ?q->setnext(p); ?? ??? ?savelink(); ?? ??? ?cout << "存儲成功!" << endl; ?? ??? ?system("pause"); ?? ?} ?? ?void delnode() {//---------------------------刪 ?? ??? ?bool k = false; ?? ??? ?node<T>* p, * q, * r; ?? ??? ?p = new node<T>; ?? ??? ?p->setnext(NULL); ?? ??? ?cout << "請輸入您要刪除的數(shù)據(jù)" << endl; ?? ??? ?cin >> p->opedata(); ?? ??? ?q = head; ?? ??? ?for (; q->getnext() != NULL; q = q->getnext()) { ?? ??? ??? ?if (q->getnext()->opedata() == p->opedata() && q->getnext()->getnext() != NULL) { ?? ??? ??? ??? ?r = q->getnext(); ?? ??? ??? ??? ?q->getnext() = q->getnext()->getnext(); ?? ??? ??? ??? ?delete r; ?? ??? ??? ??? ?k = true; ?? ??? ??? ??? ?break; ?? ??? ??? ?} ?? ??? ??? ?else if (q->getnext()->opedata() == p->opedata() && q->getnext()->getnext() == NULL) { ?? ??? ??? ??? ?r = q->getnext(); ?? ??? ??? ??? ?delete r; ?? ??? ??? ??? ?q->setnext(NULL); ?? ??? ??? ??? ?k = true; ?? ??? ??? ??? ?break; ?? ??? ??? ?} ?? ??? ?} ?? ??? ?if (k == false) { ?? ??? ??? ?cout << "沒有找到您要刪除的數(shù)據(jù),請核實后再來!" << endl; ?? ??? ?} ?? ??? ?else if (k == true) { ?? ??? ??? ?savelink(); ?? ??? ??? ?cout << "刪除成功!" << endl; ?? ??? ?} ?? ??? ?delete p; ?? ??? ?system("pause"); ?? ??? ?return; ?? ?} ?? ?void altenode() {//-------------------------------改 ?? ??? ?int judgecin = 0; ?? ??? ?bool k = false; ?? ??? ?node<T>* p, * q, * r; ?? ??? ?p = new node<T>; ?? ??? ?p->setnext(NULL); ?? ??? ?cout << "請輸入您要改動的數(shù)據(jù)" << endl; ?? ??? ?judgecin = 1; ?? ??? ?cin >> p->opedata(); ?? ??? ?judgecin = 0; ?? ??? ?q = head; ?? ??? ?for (; q->getnext() != NULL; q = q->getnext()) { ?? ??? ??? ?if (q->getnext()->opedata() == p->opedata()) { ?? ??? ??? ??? ?cout << "請輸入新的數(shù)據(jù):" << endl; ?? ??? ??? ?rewrite: ?? ??? ??? ??? ?cin >> p->opedata(); ?? ??? ??? ??? ?for (r = head; r->getnext() != NULL; r = r->getnext()) { ?? ??? ??? ??? ??? ?if (r->getnext()->opedata() == p->opedata() && p->opedata() != q->getnext()->opedata()) { ?? ??? ??? ??? ??? ??? ?system("cls"); ?? ??? ??? ??? ??? ??? ?judgecin++; ?? ??? ??? ??? ??? ??? ?if (judgecin == 3) { ?? ??? ??? ??? ??? ??? ??? ?cout << "您多次輸入信息錯誤,請核實后再來,將要返回主菜單" << endl; ?? ??? ??? ??? ??? ??? ??? ?delete p; ?? ??? ??? ??? ??? ??? ??? ?system("pause"); ?? ??? ??? ??? ??? ??? ??? ?return; ?? ??? ??? ??? ??? ??? ?} ?? ??? ??? ??? ??? ??? ?cout << "請輸入您自己的身份信息!或輸入新的身份證號" << endl; ?? ??? ??? ??? ??? ??? ?goto rewrite; ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} ?? ??? ??? ??? ?q->getnext()->opedata() = p->opedata(); ?? ??? ??? ??? ?k = true; ?? ??? ??? ?} ?? ??? ?} ?? ??? ?if (k == true) { ?? ??? ??? ?savelink(); ?? ??? ??? ?cout << "修改成功!" << endl; ?? ??? ?} ?? ??? ?else { ?? ??? ??? ?cout << "修改失敗!沒有找到該數(shù)據(jù)!" << endl; ?? ??? ?} ?? ??? ?delete p; ?? ??? ?system("pause"); ?? ??? ?return; ?? ?} ?? ?void selnode() {//----------------------------------查 ?? ??? ?cout << "請輸入您要查找的數(shù)據(jù)" << endl; ?? ??? ?bool k = false; ?? ??? ?node<T>* p, * q; ?? ??? ?p = new node<T>; ?? ??? ?p->setnext(NULL); ?? ??? ?cin >> p->opedata(); ?? ??? ?for (q = head; q->getnext() != NULL; q = q->getnext()) { ?? ??? ??? ?if (q->getnext()->opedata() == p->opedata()) { ?? ??? ??? ??? ?k = true; ?? ??? ??? ??? ?cout << "您要查找的數(shù)據(jù)如下!" << endl; ?? ??? ??? ??? ?cout << q->getnext()->opedata() << endl; ?? ??? ??? ??? ?//break; ?? ??? ??? ?} ?? ??? ?} ?? ??? ?if (k == false) { ?? ??? ??? ?cout << "沒有找到您要查找的數(shù)據(jù)!抱歉" << endl; ?? ??? ?} ?? ??? ?system("pause"); ?? ??? ?return; ?? ?} ?? ?void printlink() {//------------------------------打印鏈表 ?? ??? ?node<T>* p; ?? ??? ?sortlink(); ?? ??? ?for (p = head; p->getnext() != NULL; p = p->getnext()) { ?? ??? ??? ?cout << p->getnext()->opedata() << endl; ?? ??? ?} ?? ??? ?system("pause"); ?? ?} ?? ?void sortlink() {//-------------------------------排序 ?? ??? ?node<T>* p, * q; ?? ??? ?if (head->getnext() == NULL) { ?? ??? ??? ?cout << "沒有數(shù)據(jù),無需排序!" << endl; ?? ??? ??? ?return; ?? ??? ?} ?? ??? ?if (head->getnext()->getnext() == NULL) { ?? ??? ??? ?cout << "一組數(shù)據(jù),無需排序!" << endl; ?? ??? ??? ?return; ?? ??? ?} ?? ??? ?for (p = head->getnext(); p->getnext() != NULL; p = p->getnext()) { ?? ??? ??? ?for (q = p->getnext(); q != NULL; q = q->getnext()) { ?? ??? ??? ??? ?if (q->opedata() > p->opedata()) { ?? ??? ??? ??? ??? ?node<T> temp; ?? ??? ??? ??? ??? ?temp = *q; ?? ??? ??? ??? ??? ?*q = *p; ?? ??? ??? ??? ??? ?*p = temp; ?? ??? ??? ??? ??? ?temp.getnext() = q->getnext(); ?? ??? ??? ??? ??? ?q->getnext() = p->getnext(); ?? ??? ??? ??? ??? ?p->getnext() = temp.getnext(); ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ?} ?? ?} ?? ?void savelink() {//--------------------------------------存鏈表 ?? ??? ?ofstream outfile; ?? ??? ?string classname; ?? ??? ?node<T>* p; ?? ??? ?p = head; ?? ??? ?classname = head->GetClass(); ?? ??? ?classname.erase(remove(classname.begin(), classname.end(), '<'), classname.end()); ?? ??? ?classname.erase(remove(classname.begin(), classname.end(), '>'), classname.end()); ?? ??? ?outfile.open(classname); ?? ??? ?while (p->getnext() != NULL) { ?? ??? ??? ?p = p->getnext(); ?? ??? ??? ?outfile << p->opedata() << endl; ?? ??? ?} ?? ??? ?outfile.close(); ?? ?} ?? ?~link() {//--------------------------------------------銷毀鏈表 ?? ??? ?node<T>* p; ?? ??? ?p = head->getnext(); ?? ??? ?while (p != NULL) { ?? ??? ??? ?delete head; ?? ??? ??? ?head = p; ?? ??? ??? ?p = p->getnext(); ?? ??? ?} ?? ?}// };
四、運行截圖
1.主菜單
2.查看信息
3.更換數(shù)據(jù)類型
- 將officer類型注釋掉,使用int類型
4.再次顯示所有信息【拋轉(zhuǎn)】
- 可以看到就算將
officer
類型換為int類型,程序依舊可以穩(wěn)定的運行。 - 這里打印職工信息系統(tǒng)的提示性語句出現(xiàn)到了這里,如果大家真的理解了泛型的思想
- 肯定可以輕而易舉的改掉這處不足,有什么想法的小伙伴評論區(qū)留言博主吧。
五、源碼
char off_menu(); class officer; int mynum(string str) {//-----------------判斷字符串是否全為數(shù)字 ?? ?for (unsigned int i = 0; i < str.length(); i++) { ?? ??? ?if (!isdigit(str[i])) { ?? ??? ??? ?return 1; ?? ??? ?} ?? ?} ?? ?return 0; } template<typename T> class node { private: ?? ?T data; ?? ?node<T>* next; public: ?? ?node() { ?? ?} ?? ?void setnext(node<T>* p) { ?? ??? ?if (p != NULL) { ?? ??? ??? ?next = new node<T>; ?? ??? ??? ?next->data = p->opedata(); ?? ??? ??? ?next->next = p->getnext(); ?? ??? ?} ?? ??? ?else ?? ??? ??? ?next = p; ?? ?} ?? ?T& opedata() { ?? ??? ?return data; ?? ?} ?? ?node<T>*& getnext() { ?? ??? ?return next; ?? ?} ?? ?const char* GetClass() ?? ?{ ?? ??? ?return typeid(*this).name(); ?? ?} }; template<typename T> class link { private: ?? ?node<T>* head; public: ?? ?link() { ?? ??? ?string classname; ?? ??? ?ifstream infile; ?? ??? ?node<T>* p, * q; ?? ??? ?p = new node<T>; ?? ??? ?p->setnext(NULL); ?? ??? ?head = new node<T>; ?? ??? ?head->setnext(NULL); ?? ??? ?q = head; ?? ??? ?classname = head->GetClass(); ?? ??? ?classname.erase(remove(classname.begin(), classname.end(), '<'), classname.end()); ?? ??? ?classname.erase(remove(classname.begin(), classname.end(), '>'), classname.end()); ?? ??? ?//cout << classname << endl; ?? ??? ?infile.open(classname); ?? ??? ?while (infile >> p->opedata()) { ?? ??? ??? ?q->setnext(p); ?? ??? ??? ?q = q->getnext(); ?? ??? ??? ?p = new node<T>; ?? ??? ??? ?p->setnext(NULL); ?? ??? ?} ?? ??? ?delete p; ?? ??? ?infile.close(); ?? ?} ?? ?void addnode() {//-------------------------增 ?? ??? ?node<T>* p, * q; ?? ??? ?p = new node<T>; ?? ??? ?p->setnext(NULL); ?? ??? ?cout << "請輸入您要存儲的數(shù)據(jù):" << endl; ?? ??? ?cin >> p->opedata(); ?? ??? ?q = head; ?? ??? ?for (; q->getnext() != NULL; q = q->getnext()) { ?? ??? ??? ?if (q->getnext()->opedata() == p->opedata()) { ?? ??? ??? ??? ?cout << "您輸入的數(shù)據(jù),已存在,不需要重復錄入" << endl; ?? ??? ??? ??? ?system("pause"); ?? ??? ??? ??? ?delete p; ?? ??? ??? ??? ?return; ?? ??? ??? ?} ?? ??? ?} ?? ??? ?q->setnext(p); ?? ??? ?savelink(); ?? ??? ?cout << "存儲成功!" << endl; ?? ??? ?system("pause"); ?? ?} ?? ?void delnode() {//---------------------------刪 ?? ??? ?bool k = false; ?? ??? ?node<T>* p, * q, * r; ?? ??? ?p = new node<T>; ?? ??? ?p->setnext(NULL); ?? ??? ?cout << "請輸入您要刪除的數(shù)據(jù)" << endl; ?? ??? ?cin >> p->opedata(); ?? ??? ?q = head; ?? ??? ?for (; q->getnext() != NULL; q = q->getnext()) { ?? ??? ??? ?if (q->getnext()->opedata() == p->opedata() && q->getnext()->getnext() != NULL) { ?? ??? ??? ??? ?r = q->getnext(); ?? ??? ??? ??? ?q->getnext() = q->getnext()->getnext(); ?? ??? ??? ??? ?delete r; ?? ??? ??? ??? ?k = true; ?? ??? ??? ??? ?break; ?? ??? ??? ?} ?? ??? ??? ?else if (q->getnext()->opedata() == p->opedata() && q->getnext()->getnext() == NULL) { ?? ??? ??? ??? ?r = q->getnext(); ?? ??? ??? ??? ?delete r; ?? ??? ??? ??? ?q->setnext(NULL); ?? ??? ??? ??? ?k = true; ?? ??? ??? ??? ?break; ?? ??? ??? ?} ?? ??? ?} ?? ??? ?if (k == false) { ?? ??? ??? ?cout << "沒有找到您要刪除的數(shù)據(jù),請核實后再來!" << endl; ?? ??? ?} ?? ??? ?else if (k == true) { ?? ??? ??? ?savelink(); ?? ??? ??? ?cout << "刪除成功!" << endl; ?? ??? ?} ?? ??? ?delete p; ?? ??? ?system("pause"); ?? ??? ?return; ?? ?} ?? ?void altenode() {//-------------------------------改 ?? ??? ?int judgecin = 0; ?? ??? ?bool k = false; ?? ??? ?node<T>* p, * q, * r; ?? ??? ?p = new node<T>; ?? ??? ?p->setnext(NULL); ?? ??? ?cout << "請輸入您要改動的數(shù)據(jù)" << endl; ?? ??? ?judgecin = 1; ?? ??? ?cin >> p->opedata(); ?? ??? ?judgecin = 0; ?? ??? ?q = head; ?? ??? ?for (; q->getnext() != NULL; q = q->getnext()) { ?? ??? ??? ?if (q->getnext()->opedata() == p->opedata()) { ?? ??? ??? ??? ?cout << "請輸入新的數(shù)據(jù):" << endl; ?? ??? ??? ?rewrite: ?? ??? ??? ??? ?cin >> p->opedata(); ?? ??? ??? ??? ?for (r = head; r->getnext() != NULL; r = r->getnext()) { ?? ??? ??? ??? ??? ?if (r->getnext()->opedata() == p->opedata() && p->opedata() != q->getnext()->opedata()) { ?? ??? ??? ??? ??? ??? ?system("cls"); ?? ??? ??? ??? ??? ??? ?judgecin++; ?? ??? ??? ??? ??? ??? ?if (judgecin == 3) { ?? ??? ??? ??? ??? ??? ??? ?cout << "您多次輸入信息錯誤,請核實后再來,將要返回主菜單" << endl; ?? ??? ??? ??? ??? ??? ??? ?delete p; ?? ??? ??? ??? ??? ??? ??? ?system("pause"); ?? ??? ??? ??? ??? ??? ??? ?return; ?? ??? ??? ??? ??? ??? ?} ?? ??? ??? ??? ??? ??? ?cout << "請輸入您自己的身份信息!或輸入新的身份證號" << endl; ?? ??? ??? ??? ??? ??? ?goto rewrite; ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} ?? ??? ??? ??? ?q->getnext()->opedata() = p->opedata(); ?? ??? ??? ??? ?k = true; ?? ??? ??? ?} ?? ??? ?} ?? ??? ?if (k == true) { ?? ??? ??? ?savelink(); ?? ??? ??? ?cout << "修改成功!" << endl; ?? ??? ?} ?? ??? ?else { ?? ??? ??? ?cout << "修改失敗!沒有找到該數(shù)據(jù)!" << endl; ?? ??? ?} ?? ??? ?delete p; ?? ??? ?system("pause"); ?? ??? ?return; ?? ?} ?? ?void selnode() {//----------------------------------查 ?? ??? ?cout << "請輸入您要查找的數(shù)據(jù)" << endl; ?? ??? ?bool k = false; ?? ??? ?node<T>* p, * q; ?? ??? ?p = new node<T>; ?? ??? ?p->setnext(NULL); ?? ??? ?cin >> p->opedata(); ?? ??? ?for (q = head; q->getnext() != NULL; q = q->getnext()) { ?? ??? ??? ?if (q->getnext()->opedata() == p->opedata()) { ?? ??? ??? ??? ?k = true; ?? ??? ??? ??? ?cout << "您要查找的數(shù)據(jù)如下!" << endl; ?? ??? ??? ??? ?cout << q->getnext()->opedata() << endl; ?? ??? ??? ??? ?//break; ?? ??? ??? ?} ?? ??? ?} ?? ??? ?if (k == false) { ?? ??? ??? ?cout << "沒有找到您要查找的數(shù)據(jù)!抱歉" << endl; ?? ??? ?} ?? ??? ?system("pause"); ?? ??? ?return; ?? ?} ?? ?void printlink() {//------------------------------打印鏈表 ?? ??? ?node<T>* p; ?? ??? ?sortlink(); ?? ??? ?for (p = head; p->getnext() != NULL; p = p->getnext()) { ?? ??? ??? ?cout << p->getnext()->opedata() << endl; ?? ??? ?} ?? ??? ?system("pause"); ?? ?} ?? ?void sortlink() {//-------------------------------排序 ?? ??? ?node<T>* p, * q; ?? ??? ?if (head->getnext() == NULL) { ?? ??? ??? ?cout << "沒有數(shù)據(jù),無需排序!" << endl; ?? ??? ??? ?return; ?? ??? ?} ?? ??? ?if (head->getnext()->getnext() == NULL) { ?? ??? ??? ?cout << "一組數(shù)據(jù),無需排序!" << endl; ?? ??? ??? ?return; ?? ??? ?} ?? ??? ?for (p = head->getnext(); p->getnext() != NULL; p = p->getnext()) { ?? ??? ??? ?for (q = p->getnext(); q != NULL; q = q->getnext()) { ?? ??? ??? ??? ?if (q->opedata() > p->opedata()) { ?? ??? ??? ??? ??? ?node<T> temp; ?? ??? ??? ??? ??? ?temp = *q; ?? ??? ??? ??? ??? ?*q = *p; ?? ??? ??? ??? ??? ?*p = temp; ?? ??? ??? ??? ??? ?temp.getnext() = q->getnext(); ?? ??? ??? ??? ??? ?q->getnext() = p->getnext(); ?? ??? ??? ??? ??? ?p->getnext() = temp.getnext(); ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ?} ?? ?} ?? ?void savelink() {//--------------------------------------存鏈表 ?? ??? ?ofstream outfile; ?? ??? ?string classname; ?? ??? ?node<T>* p; ?? ??? ?p = head; ?? ??? ?classname = head->GetClass(); ?? ??? ?classname.erase(remove(classname.begin(), classname.end(), '<'), classname.end()); ?? ??? ?classname.erase(remove(classname.begin(), classname.end(), '>'), classname.end()); ?? ??? ?outfile.open(classname); ?? ??? ?while (p->getnext() != NULL) { ?? ??? ??? ?p = p->getnext(); ?? ??? ??? ?outfile << p->opedata() << endl; ?? ??? ?} ?? ??? ?outfile.close(); ?? ?} ?? ?~link() {//--------------------------------------------銷毀鏈表 ?? ??? ?node<T>* p; ?? ??? ?p = head->getnext(); ?? ??? ?while (p != NULL) { ?? ??? ??? ?delete head; ?? ??? ??? ?head = p; ?? ??? ??? ?p = p->getnext(); ?? ??? ?} ?? ?}// }; class officer { ?? ?friend ostream& operator<<(ostream& out, officer& obj); ?? ?friend istream& operator>>(istream& in, officer& obj); ?? ?friend ofstream& operator<<(ofstream& outfile, officer& obj);//--------輸出到文件 ?? ?friend ifstream& operator>>(ifstream& infile, officer& obj);//---------讀取文件 private: ?? ?string id_card; ?? ?string name; ?? ?string sex; ?? ?int age; ?? ?string post; ?? ?int money; public://---------------------------------私有屬性管理方法 ?? ?officer() { ?? ??? ?id_card = ""; ?? ??? ?name = ""; ?? ??? ?sex = ""; ?? ??? ?age = 0; ?? ??? ?post = ""; ?? ??? ?money = 0; ?? ?} ?? ?officer(officer& obj) { ?? ??? ?this->id_card = obj.getid(); ?? ??? ?this->name = obj.getname(); ?? ??? ?this->sex = obj.getsex(); ?? ??? ?this->age = obj.getage(); ?? ??? ?this->money = obj.getmoney(); ?? ??? ?this->post = obj.getpost(); ?? ?} ?? ?officer& operator=(officer& obj) { ?? ??? ?this->id_card = obj.getid(); ?? ??? ?this->name = obj.getname(); ?? ??? ?this->sex = obj.getsex(); ?? ??? ?this->age = obj.getage(); ?? ??? ?this->money = obj.getmoney(); ?? ??? ?this->post = obj.getpost(); ?? ??? ?return *this; ?? ?} ?? ?bool operator==(officer& obj) { ?? ??? ?if (this->getid() == obj.getid() && this->getname() == obj.getname() && this->getsex() == obj.getsex()\ ?? ??? ??? ?&& this->getage() == obj.getage() && this->getpost() == obj.getpost() && this->getmoney() == obj.getmoney()) { ?? ??? ??? ?return true; ?? ??? ?} ?? ??? ?return false; ?? ?} ?? ?bool operator!=(officer& obj) { ?? ??? ?if (this->getid() == obj.getid()) { ?? ??? ??? ?return false; ?? ??? ?} ?? ??? ?return true; ?? ?} ?? ?bool operator>(officer& obj) { ?? ??? ?if (this->getmoney() > obj.getmoney()) { ?? ??? ??? ?return true; ?? ??? ?} ?? ??? ?return false; ?? ?} ?? ?bool operator<(officer& obj) { ?? ??? ?if (this->getmoney() < obj.getmoney()) { ?? ??? ??? ?return true; ?? ??? ?} ?? ??? ?return false; ?? ?} ?? ?void setpost(string post) { ?? ??? ?this->post = post; ?? ?} ?? ?void setmoney(int money) { ?? ??? ?this->money = money; ?? ?} ?? ?void setid(string id) { ?? ??? ?id_card = id; ?? ?} ?? ?void setname(string name) { ?? ??? ?this->name = name; ?? ?} ?? ?void setsex(string sex) { ?? ??? ?this->sex = sex; ?? ?} ?? ?void setage(int age) { ?? ??? ?this->age = age; ?? ?} ?? ?string getid() { ?? ??? ?return id_card; ?? ?} ?? ?string getname() { ?? ??? ?return name; ?? ?} ?? ?string getsex() { ?? ??? ?return sex; ?? ?} ?? ?int getage() { ?? ??? ?return age; ?? ?} ?? ?string getpost() { ?? ??? ?return post; ?? ?} ?? ?int getmoney() { ?? ??? ?return money; ?? ?} }; ostream& operator<<(ostream& out, officer& obj) { ?? ?out << obj.getid() << "\t"; ?? ?out << obj.getname() << "\t"; ?? ?out << obj.getsex() << "\t"; ?? ?out << obj.getage() << "\t"; ?? ?out << obj.getpost() << "\t"; ?? ?out << obj.getmoney() << "\t"; ?? ?//?? ?cout << endl; ?? ?return out; } istream& operator>>(istream& in, officer& obj) { ?? ?cout << "身份證(18位):"; id_here: ?? ?in >> obj.id_card; ?? ?if (obj.getid() == "00") { ?? ??? ?return in; ?? ?} ?? ?else if (obj.getid().length() != 18) { ?? ??? ?cout << "輸入格式不規(guī)范請重新輸入:"; ?? ??? ?goto id_here; ?? ?} ?? ?//if (judgecin == 1) { ?? ?//?? ?return in; ?? ?//} ?? ?cout << "姓名:"; ?? ?in >> obj.name; ?? ?cout << "性別:"; sex_here: ?? ?in >> obj.sex; ?? ?if (obj.getid() == "00") { ?? ??? ?return in; ?? ?} ?? ?else if (obj.getsex() != "男" && obj.getsex() != "女") { ?? ??? ?cout << "請輸入準確的性別:"; ?? ??? ?goto sex_here; ?? ?} ?? ?cout << "年齡:"; ?? ?string age; age_here: ?? ?in >> age; ?? ?if (obj.getid() == "00") { ?? ??? ?return in; ?? ?} ?? ?else if (mynum(age) || age.length() > 3) { ?? ??? ?cout << "輸入不規(guī)范請重新輸入:"; ?? ??? ?goto age_here; ?? ?} ?? ?obj.age = atoi(age.c_str()); ?? ?cout << "職位:"; post_here: ?? ?in >> obj.post; ?? ?if (obj.getid() == "00") { ?? ??? ?return in; ?? ?} ?? ?else if (obj.getpost() != "監(jiān)獄長" && obj.getpost() != "探長" && \ ?? ??? ?obj.getpost() != "參謀長" && obj.getpost() != "大警司" && obj.getpost() != "小警司") { ?? ??? ?cout << "請輸入職位(監(jiān)獄長,探長,參謀長,大警司,小警司):"; ?? ??? ?goto post_here; ?? ?} ?? ?if (obj.getpost() == "監(jiān)獄長") { ?? ??? ?obj.money = 30000; ?? ?} ?? ?else if (obj.getpost() == "探長") { ?? ??? ?obj.money = 24000; ?? ?} ?? ?else if (obj.getpost() == "參謀長") { ?? ??? ?obj.money = 24500; ?? ?} ?? ?else if (obj.getpost() == "大警司") { ?? ??? ?obj.money = 20000; ?? ?} ?? ?else if (obj.getpost() == "小警司") { ?? ??? ?obj.money = 18000; ?? ?} ?? ?return in; } ofstream& operator<<(ofstream& outfile, officer& obj) { ?? ?outfile << obj.getid() << " " << obj.getname() << " " << obj.getsex() << " " << obj.getage()\ ?? ??? ?<< " " << obj.getpost() << " " << obj.getmoney();// << endl; ?? ?return outfile; } ifstream& operator>>(ifstream& infile, officer& obj) { ?? ?string post1; ?? ?int money1; ?? ?string id1; ?? ?string name1; ?? ?string sex1; ?? ?int age1; ?? ?infile >> id1 >> name1 >> sex1 >> age1 >> post1 >> money1; ?? ?obj.setid(id1); ?? ?obj.setname(name1); ?? ?obj.setsex(sex1); ?? ?obj.setage(age1); ?? ?obj.setpost(post1); ?? ?obj.setmoney(money1); ?? ?return infile; } int main() { ?? ?//link<officer> myarray; ?? ?link<int> myarray; ?? ?char menun; ?? ?while (1) { ?? ??? ?menun = off_menu(); ?? ??? ?if (menun == '1') { ?? ??? ??? ?system("cls"); ?? ??? ??? ?int len; ?? ??? ??? ?cout << "請輸入您要增加節(jié)點的個數(shù):"; ?? ??? ??? ?cin >> len; ?? ??? ??? ?for (int i = 0; i < len; i++) { ?? ??? ??? ??? ?myarray.addnode(); ?? ??? ??? ??? ?system("cls"); ?? ??? ??? ?} ?? ??? ?} ?? ??? ?else if (menun == '2') { ?? ??? ??? ?myarray.delnode(); ?? ??? ?} ?? ??? ?else if (menun == '3') { ?? ??? ??? ?myarray.altenode(); ?? ??? ?} ?? ??? ?else if (menun == '4') { ?? ??? ??? ?myarray.selnode(); ?? ??? ?} ?? ??? ?else if (menun == '5') { ?? ??? ??? ?cout << "身份證\t\t\t姓名\t性別\t年齡\t職位\t工資" << endl; ?? ??? ??? ?myarray.printlink(); ?? ??? ?} ?? ??? ?else if (menun == '6') { ?? ??? ??? ?break; ?? ??? ?} ?? ??? ?system("cls"); ?? ?} ?? ?return 0; } char off_menu() { ?? ?char n; ?? ?n = '0'; ?? ?system("cls"); ?? ?cout << "\n\n\n"; ?? ?cout << "\t\t\t\t\t-------歡迎使用本信息管理系統(tǒng)-------" << endl; cout << "\t\t\t\t\t*\t ? ? \t\t\t ? *" << endl; ?? ?cout << "\t\t\t\t\t*\t ? 1.錄入員工信息\t ? *" << endl; cout << "\t\t\t\t\t*\t ? ? \t\t\t ? *" << endl; ?? ?cout << "\t\t\t\t\t*\t ? 2.刪除員工信息\t ? *" << endl; cout << "\t\t\t\t\t*\t ? ? \t\t\t ? *" << endl; ?? ?cout << "\t\t\t\t\t*\t ? 3.修改員工信息\t ? *" << endl; cout << "\t\t\t\t\t*\t ? ? \t\t\t ? *" << endl; ?? ?cout << "\t\t\t\t\t*\t ? 4.查詢員工信息\t ? *" << endl; cout << "\t\t\t\t\t*\t ? ? \t\t\t ? *" << endl; ?? ?cout << "\t\t\t\t\t*\t ? 5.查看員工信息\t ? *" << endl; cout << "\t\t\t\t\t*\t ? ? \t\t\t ? *" << endl; ?? ?cout << "\t\t\t\t\t*\t ? 6.退出該系統(tǒng)\t\t ? *" << endl; cout << "\t\t\t\t\t*\t ? ? \t\t\t ? *" << endl; ?? ?cout << "\t\t\t\t\t------------------------------------" << endl; ?? ?cout << "———————————————————————————————————————————————————————----------" << endl; ?? ?cout << "———————————————————————————————————————————————————————----------" << endl; ?? ?cout << "please your choose:"; ?? ?while (1) { ?? ??? ?n = _getch(); ?? ??? ?if (n >= '1' && n <= '6') { ?? ??? ??? ?break; ?? ??? ?} ?? ?} ?? ?cout << endl; ?? ?return n; }
總結(jié):
理解了泛型會對python等解釋性語言有更好的理解。希望大家能夠好好理解這個項目,在博主的基礎之上更上一層樓
到此這篇關于基于C++泛型編程職工管理系統(tǒng)的文章就介紹到這了,更多相關C++泛型編程職工管理系統(tǒng)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Linux?C/C++實現(xiàn)顯示NIC流量統(tǒng)計信息
NIC流量統(tǒng)計信息是由操作系統(tǒng)維護的,當數(shù)據(jù)包通過NIC傳輸時,操作系統(tǒng)會更新相關的計數(shù)器,通過讀取這些計數(shù)器,我們可以獲得關于網(wǎng)絡流量的信息,下面我們就來學習一下如何通過C/C++實現(xiàn)顯示NIC流量統(tǒng)計信息吧2024-01-01