基于C++泛型編程職工管理系統(tǒng)
前言:
前面介紹到了C++的泛型編程,并實(shí)現(xiàn)了萬能容器,不過那使用的是數(shù)組,今天呢咱帶大家實(shí)踐一下使用泛型技術(shù),結(jié)合單鏈表實(shí)現(xiàn)一個(gè)職工管理系統(tǒng)。保證大家看完之后有所感悟。
一、泛型編程思想
所謂泛型就是類型不固定,只需修改少量代碼就可以擴(kuò)展為其他類型的應(yīng)用,由于C++是一門靜態(tài)編譯型的語言,變量的類型都是事先編譯好的,如果不用泛型進(jìn)行編程,一段代碼始終就是那么點(diǎn)作用。使用泛型編程后,可以很簡單的對其他類型進(jìn)行擴(kuò)展。泛型編程核心思想就是將數(shù)據(jù)類型設(shè)置為模板,第一次編譯是對模板進(jìn)行編譯,第二次編譯會(huì)帶入人為傳的類型參數(shù)。前面文章有講函數(shù)模板與類模板,忘記的小伙伴可以去看看。
二、單鏈表是什么?
單鏈表中的單代表一條,鏈表意思就是一個(gè)個(gè)節(jié)點(diǎn)鏈接起來的表結(jié)構(gòu)。
其最典型的特征就是節(jié)點(diǎn)只有一個(gè)指針域。并且該指針域指向下一節(jié)點(diǎn)的地址
1.圖示
- 鏈表中的第一個(gè)節(jié)點(diǎn)被稱為頭結(jié)點(diǎn),一般不存儲(chǔ)數(shù)據(jù),指向頭結(jié)點(diǎn)的指針稱為頭指針
- 第一個(gè)存儲(chǔ)數(shù)據(jù)的節(jié)點(diǎn)稱為首節(jié)點(diǎn),末尾節(jié)點(diǎn)稱為尾節(jié)點(diǎn),指針域賦空,防止變?yōu)橐爸羔槨?/li>
2.鏈表的節(jié)點(diǎn)結(jié)構(gòu)【節(jié)點(diǎn)類】
①常規(guī)鏈表節(jié)點(diǎn)
- 包含數(shù)據(jù)域,指針域。指針域指針類型與節(jié)點(diǎn)類型保持一致。
class node { private: ?? ?//數(shù)據(jù)域 ?? ?string data1; ?? ?string data2; ?? ?//指針域 ?? ?node* next; public: ?? ?node() { ?? ?} }
②泛型鏈表節(jié)點(diǎn)
其中T代表的是一種不確定的數(shù)據(jù)類型,data是一個(gè)T類型的對象,其作用類似于結(jié)構(gòu)體存儲(chǔ)數(shù)據(jù)域的信息,但是在c++中他必須用類實(shí)現(xiàn),因?yàn)樵摂?shù)據(jù)類型要有屬于自己的屬性與方法。node* next
代表一個(gè)T類型的node指針,其本質(zhì)還是node指針,只不過T的類型決定著node *指向的節(jié)點(diǎn)中的data的類型。
代碼如下:
template<typename T> class node { private: ?? ?//數(shù)據(jù)域 ?? ?T data; ?? ?//指針域 ?? ?node<T>* next; public: ?? ?node() { ?? ?} }
3.鏈表類
常規(guī)鏈表類中需要包含一個(gè)頭指針,指向鏈表的頭結(jié)點(diǎn),然后創(chuàng)建一個(gè)鏈表對其增刪改查泛型編程中的鏈表類,也要是一個(gè)鏈表類。實(shí)現(xiàn)類型的參數(shù)化,具體如下:
template<typename T> class link { private: ?? ?//傳入類型的時(shí)候,先傳給link然后link會(huì)傳給node; ?? ?node<T>* head; public: ?? ?link() { ?? ?} ?? ?bool add(){ ?? ?} ?? ?bool del(){ ?? ?} ?? ?
三、泛型編程核心
1.實(shí)現(xiàn)數(shù)據(jù)類
泛型就是要將你寫的類型,像對待int string類型那樣對待。首先要進(jìn)行的就是運(yùn)算符重載重載了運(yùn)算符你可以使用cin,cout直接對相應(yīng)的對象進(jìn)行輸入,輸出??梢灾苯邮褂?進(jìn)行賦值。
具體實(shí)現(xiàn)如下:
class officer { ?? ?//重載了標(biāo)準(zhǔn)輸入輸出流函數(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ù),直接進(jìn)行賦值。 ?? ?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; ?? ?} ?? ?//查重時(shí)使用 ?? ?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; ?? ?} ?? ?//排序時(shí)使用,根據(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.實(shí)現(xiàn)鏈表類
泛型的鏈表類、節(jié)點(diǎn)類一般就是寫死的,做到換一個(gè)數(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 << "請輸入您要存儲(chǔ)的數(shù)據(jù):" << endl; ?? ??? ?cin >> p->opedata(); ?? ??? ?q = head; ?? ??? ?for (; q->getnext() != NULL; q = q->getnext()) { ?? ??? ??? ?if (q->getnext()->opedata() == p->opedata()) { ?? ??? ??? ??? ?cout << "您輸入的數(shù)據(jù),已存在,不需要重復(fù)錄入" << endl; ?? ??? ??? ??? ?system("pause"); ?? ??? ??? ??? ?delete p; ?? ??? ??? ??? ?return; ?? ??? ??? ?} ?? ??? ?} ?? ??? ?q->setnext(p); ?? ??? ?savelink(); ?? ??? ?cout << "存儲(chǔ)成功!" << endl; ?? ??? ?system("pause"); ?? ?} ?? ?void delnode() {//---------------------------刪 ?? ??? ?bool k = false; ?? ??? ?node<T>* p, * q, * r; ?? ??? ?p = new node<T>; ?? ??? ?p->setnext(NULL); ?? ??? ?cout << "請輸入您要?jiǎng)h除的數(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 << "沒有找到您要?jiǎng)h除的數(shù)據(jù),請核實(shí)后再來!" << 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 << "請輸入您要改動(dòng)的數(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 << "您多次輸入信息錯(cuò)誤,請核實(shí)后再來,將要返回主菜單" << endl; ?? ??? ??? ??? ??? ??? ??? ?delete p; ?? ??? ??? ??? ??? ??? ??? ?system("pause"); ?? ??? ??? ??? ??? ??? ??? ?return; ?? ??? ??? ??? ??? ??? ?} ?? ??? ??? ??? ??? ??? ?cout << "請輸入您自己的身份信息!或輸入新的身份證號(hào)" << 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(); ?? ??? ?} ?? ?}// };
四、運(yùn)行截圖
1.主菜單
2.查看信息
3.更換數(shù)據(jù)類型
- 將officer類型注釋掉,使用int類型
4.再次顯示所有信息【拋轉(zhuǎn)】
- 可以看到就算將
officer
類型換為int類型,程序依舊可以穩(wěn)定的運(yù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 << "請輸入您要存儲(chǔ)的數(shù)據(jù):" << endl; ?? ??? ?cin >> p->opedata(); ?? ??? ?q = head; ?? ??? ?for (; q->getnext() != NULL; q = q->getnext()) { ?? ??? ??? ?if (q->getnext()->opedata() == p->opedata()) { ?? ??? ??? ??? ?cout << "您輸入的數(shù)據(jù),已存在,不需要重復(fù)錄入" << endl; ?? ??? ??? ??? ?system("pause"); ?? ??? ??? ??? ?delete p; ?? ??? ??? ??? ?return; ?? ??? ??? ?} ?? ??? ?} ?? ??? ?q->setnext(p); ?? ??? ?savelink(); ?? ??? ?cout << "存儲(chǔ)成功!" << endl; ?? ??? ?system("pause"); ?? ?} ?? ?void delnode() {//---------------------------刪 ?? ??? ?bool k = false; ?? ??? ?node<T>* p, * q, * r; ?? ??? ?p = new node<T>; ?? ??? ?p->setnext(NULL); ?? ??? ?cout << "請輸入您要?jiǎng)h除的數(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 << "沒有找到您要?jiǎng)h除的數(shù)據(jù),請核實(shí)后再來!" << 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 << "請輸入您要改動(dòng)的數(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 << "您多次輸入信息錯(cuò)誤,請核實(shí)后再來,將要返回主菜單" << endl; ?? ??? ??? ??? ??? ??? ??? ?delete p; ?? ??? ??? ??? ??? ??? ??? ?system("pause"); ?? ??? ??? ??? ??? ??? ??? ?return; ?? ??? ??? ??? ??? ??? ?} ?? ??? ??? ??? ??? ??? ?cout << "請輸入您自己的身份信息!或輸入新的身份證號(hào)" << 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 << "請輸入準(zhǔn)確的性別:"; ?? ??? ?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é)點(diǎn)的個(gè)數(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é):
理解了泛型會(huì)對python等解釋性語言有更好的理解。希望大家能夠好好理解這個(gè)項(xiàng)目,在博主的基礎(chǔ)之上更上一層樓
到此這篇關(guān)于基于C++泛型編程職工管理系統(tǒng)的文章就介紹到這了,更多相關(guān)C++泛型編程職工管理系統(tǒng)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Linux?C/C++實(shí)現(xiàn)顯示NIC流量統(tǒng)計(jì)信息
NIC流量統(tǒng)計(jì)信息是由操作系統(tǒng)維護(hù)的,當(dāng)數(shù)據(jù)包通過NIC傳輸時(shí),操作系統(tǒng)會(huì)更新相關(guān)的計(jì)數(shù)器,通過讀取這些計(jì)數(shù)器,我們可以獲得關(guān)于網(wǎng)絡(luò)流量的信息,下面我們就來學(xué)習(xí)一下如何通過C/C++實(shí)現(xiàn)顯示NIC流量統(tǒng)計(jì)信息吧2024-01-01C語言實(shí)現(xiàn)的學(xué)生選課系統(tǒng)代碼分享
這篇文章主要介紹了C語言實(shí)現(xiàn)的學(xué)生選課系統(tǒng)代碼分享,具有一定參考價(jià)值,需要的朋友可以了解下。2017-10-10C語言實(shí)現(xiàn)學(xué)生信息管理系統(tǒng)(鏈表)
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)學(xué)生信息管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06C語言中字符的輸入輸出以及計(jì)算字符個(gè)數(shù)的方法詳解
這篇文章主要介紹了C語言中字符的輸入輸出以及計(jì)算字符個(gè)數(shù)的方法,是C語言入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-11-11C++中實(shí)現(xiàn)子進(jìn)程執(zhí)行和管道通信詳解
在這篇博客中,我們將深入探索如何在 C++ 程序中實(shí)現(xiàn)子進(jìn)程的創(chuàng)建與執(zhí)行,以及父子進(jìn)程間的管道通信,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-01-01實(shí)例講解C++編程中l(wèi)ambda表達(dá)式的使用
這篇文章主要介紹了C++編程中l(wèi)ambda表達(dá)式的使用實(shí)例,lambda表達(dá)式特性的引入在C++11中可謂千呼萬喚始出來,非常重要,需要的朋友可以參考下2016-01-01深入理解c++實(shí)現(xiàn)Qt信號(hào)和槽機(jī)制
信號(hào)和槽機(jī)制是 Qt 的核心機(jī)制,可以讓編程人員將互不相關(guān)的對象綁定在一起,實(shí)現(xiàn)對象之間的通信,本文就來深入理解c++實(shí)現(xiàn)Qt信號(hào)和槽機(jī)制,感興趣的可以了解一下2023-08-08