通訊錄——資料結構課設
阿新 • • 發佈:2019-01-21
幫同學寫了n多課設,覺得蠻簡單,沒怎麼在意,這又花20min幫人寫了一個,決定發在部落格上,以後有誰要類似的就直接給個連結, ;-) 機智的窩
任務要求:
題目描述:通訊錄的基本屬性包括編號、姓名、性別、住址、聯絡電話等。要求實現最基本的功能模組如下:
(1)通訊錄的建立;該模組主要完成將資料儲存工作。記錄可以從文字檔案中讀入,也可以從鍵盤逐條輸入記 錄。
(2)通訊錄查詢;使用者可以按照聯絡人的姓名或電話號碼查詢。若查到,則顯示該記錄的資訊;否則,顯示查詢 失敗的提示資訊。
(3)通訊錄的維護;實現對記錄的修改、刪除、插入和排序等操作。
(4)通訊錄的輸出;實現螢幕顯示和將記錄資訊寫入文字檔案中
功能要求及說明:
(1)使用選單選擇操作,具有友好的人機互動提示和顯示,方便使用者輸入及檢視程式執行過程、結果;
(2)程式可以根據使用者的選擇多次執行,直到使用者選擇退出;
(3)對於執行解決問題的步驟(例如從鍵盤輸入的資料、輸出到顯示器的結果),除了能夠在顯示器上顯示以 外,能夠將處理後的結果用檔案的方式儲存到outfile.txt檔案中。
其實就是對連結串列的一些操作了,下面直接貼程式碼吧:
#include <iostream> #include <cstdio> #include <string> #include <fstream> using namespace std; typedef struct Node{ //聯絡人節點資訊 string name; string sex; string adress; string phoneNum; struct Node * next; Node(Node * p = NULL ){ next= p;} Node(const string &na,const string &se,const string &ad,const string &ph, Node * p = NULL){ name = na; sex= se; adress = ad; phoneNum =ph; next= p; } }Person; class phonePage{ public : phonePage(){ //建構函式, Head = new Person(); } //為新插入的聯絡人建立節點 Person *creatP(string na, string se, string ad,string ph){ Person *tmp= new Person(na,se,ad,ph); return tmp; } //為新插入的聯絡人建立節點 Person *creatP(){ Person *tmp= new Person(); cout<<"輸入聯絡人的姓名:"; cin>> tmp->name; cout<<"輸入聯絡人的性別:"; cin>> tmp->sex; cout<<"輸入聯絡人的地址:"; cin>> tmp->adress; cout<<"輸入聯絡人的號碼:"; cin>> tmp->phoneNum; tmp->next= NULL; return tmp; } //檔案讀取聯絡人 void addPfromFile(){ ifstream ins; string na,se,ad,ph; ins.open("input.txt"); Person * newP; Person *inse; while(ins>>na>> se>> ad >> ph ){ newP= creatP(na,se,ad,ph); inse = findLast(); inse->next = newP; } cout<< "匯入聯絡人完畢"<<endl; ins.close(); } //查詢插入聯絡人的位置 Person *findLast(){ Person *p = Head; while( p->next !=NULL ) p=p->next; return p; } //鍵盤錄入聯絡人資訊 void addPformKb(){ Person * newP =creatP(); newP->next = Head->next; Head->next = newP; } //將所有聯絡人資訊寫到output.txt void WtoFile(){ ofstream outs; outs.open("output.txt"); Person *p = Head->next; while(p != NULL ){ outs<<p->name<<" "<< p->sex << " "<< p->adress << " "<< p->phoneNum<< endl; p=p->next; } outs.close(); } //修改聯絡人資訊 void resetPerson(Person *someP){ if(someP == NULL ){ cout<<"查無此人"<<endl; }else{ cout<<"要修改的聯絡人為."<<endl; printP(someP); cout<< "重新輸入該聯絡人的資訊."<<endl; cout<<"輸入聯絡人的姓名:"; cin>> someP->name; cout<<"輸入聯絡人的性別:"; cin>> someP->sex; cout<<"輸入聯絡人的地址:"; cin>> someP->adress; cout<<"輸入聯絡人的號碼:"; cin>> someP->phoneNum; cout<<"修改成功."<<endl; } } //查詢需要刪除的聯絡人,並返回其前一節點的指標 Person * findFrontDelP(){ Person * q= Head; Person * p =Head->next; int cho; string str; cout<<"輸入要刪除的聯絡的資訊"<<endl; cout<<"1:按姓名查詢."<<endl; cout<<"2:按號碼查詢."<<endl; cout<<"輸入選擇:"; cin>> cho; if(cho == 1 ) cout<< "輸入聯絡人姓名:"; else if(cho == 2 ) cout<< "輸入聯絡人號碼:"; else{ cout<< "選擇輸入有誤."<<endl; return NULL; } cin>> str; while( p!=NULL ){ if(cho == 1 ){ if(p->name == str ) break; }else{ if(p->phoneNum == str ) break; } p=p->next; q=q->next; } if(p!=NULL ) return q; return NULL; } //刪除某人 void delP(){ Person * someP= findFrontDelP(); if( someP == NULL ){ cout<< "聯絡人不存在."<<endl; }else{ cout<<"要刪除的聯絡人為:"<<endl; printP(someP->next); char suredel; cout<< "是否確定刪除:"<<endl; cout<<"Y:確定刪除."<<endl; cout<<"N:放棄."<<endl; cout<<"輸入選項:"; cin>> suredel; if(suredel == 'Y' || suredel == 'y'){ Person * del = someP->next; someP->next = someP->next->next; delete del; WtoFile(); } } } //查詢聯絡人,並返回該節點指標 Person *findP(){ Person *p =Head->next; int cho; string str; cout<<"1:按姓名查詢."<<endl; cout<<"2:按號碼查詢."<<endl; cout<<"輸入選擇:"; cin>> cho; if(cho == 1 ) cout<< "輸入聯絡人姓名:"; else if(cho == 2 ) cout<< "輸入聯絡人號碼:"; else{ cout<< "選擇輸入有誤."<<endl; return NULL; } cin>> str; while(p!=NULL){ if(cho == 1 ){ if(p->name == str ) break; }else{ if(p->phoneNum == str ) break; } p=p->next; } if(p == NULL ) return NULL; return p; } //列印所有聯絡人 void printAll(){ Person *someP= Head->next; int num=0; if(someP == NULL ) cout<< "通訊錄為空"<<endl; while(someP != NULL ){ cout<< ++num<< " :"<<endl; printP(someP); someP= someP->next; } } //輸出查詢到的聯絡人 void printP(Person *somebody){ if(somebody !=NULL ){ cout<<"姓名:" <<somebody->name <<endl; cout<<"性別:" <<somebody->sex <<endl; cout<<"地址:" << somebody->adress <<endl; cout<<"電話:" <<somebody->phoneNum <<endl; }else{ cout<< "查無此人"<<endl; } } private: Person * Head; }; int mnue(){ int cho; cout<<"------------------------"<<endl; cout << " 田茂茂通訊錄 " << endl; cout<<"功能選單:"<<endl; cout<<"1:通訊錄的建立"<<endl; cout<<"2:通訊錄查詢"<<endl; cout<<"3:通訊錄的維護"<<endl; cout<<"4:通訊錄的輸出"<<endl; cout<<"5:退出"<<endl; cout<<"輸入選擇:"; cin>> cho; while(cho<1 || cho > 5){ cout<<"輸入有誤,從新輸入."<<endl; cout<<"輸入選擇:"; cin>>cho; } return cho; } int main() { phonePage *tml = new phonePage(); int cho; while( cho =mnue()){ if(cho == 1){ int tmp; cout<<"1:檔案中匯入通訊錄"<<endl; cout<<"2:鍵盤錄入"<<endl; cout<<"輸入選項:"; cin>>tmp; if(tmp == 1){ tml->addPfromFile(); }else if(tmp == 2){ tml->addPformKb(); }else{ cout<<"輸入有誤."<<endl; } }else if(cho == 2){ tml->printP( tml->findP() ); }else if(cho == 3){ int tmp; cout<<"1:修改"<<endl; cout<<"2:刪除"<<endl; cout<<"3:查詢"<<endl; cout<<"輸入選項:"; cin>> tmp; if(tmp == 1){ tml->resetPerson( tml->findP() ); }else if(tmp == 2){ tml->delP(); }else if(tmp == 3){ tml->printP( tml->findP() ) ; }else{ cout<<"輸入有誤."<<endl; } }else if(cho == 4){ cout<<"你的通訊錄如下:"<<endl; tml->printAll(); tml->WtoFile(); }else{ break; } } return 0; }