一个很Low的通讯录管理系统(但是能用)C/C++单链表实现
通訊錄管理系統的設計
問題需求分析
在計算機還未普及之前通訊管理都是由聯系人采用名片,通訊錄往往采用的是筆錄手工記帳的方式來操作的?,F在一般的通訊錄管理都是采用計算機作為工具的實用的計算機通訊錄管理程序來幫助人們進行更有效的通訊錄信息管理。本通訊錄信息管理系統用計算機管理電子通訊錄的一種計算機應用技術的創新,通訊錄管理系統是典型的信息管理系統,其開發主要包括后臺文件存儲通信用戶記錄和維護,以及客戶端操作應用程序的開發兩個方面。要求應用程序功能完備,易使用等特點。
數據結構定義
我使用兩種類型的結構體的方式存儲數據, 通過鏈表的方式存儲通訊錄信息, 第一個結構是手機號碼包含如下內容: 編號, 姓名, 性別, 號碼, QQ, 微信號, 住址, 生日, 第二個結構體是節點, 每一個節點包含一個手機號和一個指針, 通過指針的相互連接形參鏈表, 也就是變成我們需要的通訊錄。
系統功能詳細設計
實現通訊錄管理系統的基本功能,可以實現電話用戶的查詢,電話號碼的查詢,電話號碼的添加及刪除等功能,以方便人們的記錄和通訊。
 通訊者姓名查詢:輸入用戶姓名,根據用戶姓名查詢對應用戶具體信息。
 通訊者號碼查詢:輸入用戶號碼,根據號碼查詢對應用戶具體信息。
 通訊者號碼查詢:輸入用戶性別,根據性別查詢對應用戶具體信息。
 通訊者生日查詢:輸入用戶,根據號碼查詢對應用戶具體信息
 通訊者信息添加:輸入用戶具體信息,將用戶具體信息存入指定文件中,便于以后的查詢。
 通信者信息修改:輸入用戶姓名或者號碼,根據輸入的用戶信息查詢指定用戶信息,然后對用戶的信息進行修改,修改保存后,將修改后的用戶信息存入文件中。
 通訊者信息刪除:輸入用戶信息,根據用戶信息查詢指定用戶,針對該用戶從文件中,刪除對應的信息。
 通訊錄信息顯示:顯示通訊錄中所有的用戶的具體信息,也可以指定輸出某一項具體信息。
 通訊者信息的保存:將所用用戶信息, 存入指定文件中。
 通訊者信息的加載:加載指定文件中所有用戶的信息, 存入鏈表中。
 通訊者信息的添加:添加用戶到鏈表中指定位置。
函數關系調用圖
設計體會
此處設計中出現的問題就是對C++文件操作的不熟悉,導致做文件操作的時候出現很多問題, 這個程序中還有很多不完善的地方, 就比如查找, 添加, 修改, 當數據非常多的時候, 如果還是采用順序操作, 那么將會引起很大性能問題。
完整代碼
#include<iostream> #include<stdlib.h> #include<fstream> #include<string> #include<conio.h> using namespace std; typedef struct{int id; string name, sex, number, qq, weChat, addr, birth; }Telephone; struct Node{Telephone tele;struct Node *next; }; void Input(Telephone &t){//數據域的輸入cout<<"請輸入聯系人的姓名:"<<endl;cin>>t.name;cout<<"請輸入聯系人的電話:"<<endl;while(true){cin>>t.number;int k = 1; if(t.number.length() != 11){k = 0;cout<<"電話號碼輸入有誤(長度不夠),請重新輸入:"<<endl;}else{for(int i=0; i<11; i++){//判斷號碼是否為數字 if(t.number[i] < '0' || t.number[i] > '9'){k = 0;cout<<"電話號碼輸入有誤(非數字),請重新輸入:"<<endl;}}}if(k) break;cout<<"K="<<k<<endl; }cout<<"請輸入聯系人的性別(male或female):"<<endl;while(cin>>t.sex){if(t.sex == "男" || t.sex == "女") break;else cout<<t.sex<<"輸入有誤,請重新輸入:"<<endl;}cout<<"請輸入聯系人的地址:"<<endl;cin>>t.addr;cout<<"請輸入聯系人QQ號:"<<endl;cin>>t.qq;cout<<"請輸入聯系人WeChat號:"<<endl;cin>>t.weChat;cout<<"請輸入聯系人生日:"<<endl;cin>>t.birth; } void Output(Telephone t){cout<<"id="<<t.id<<" name="<<t.name<<" sex="<<t.sex<<" addr="<<t.addr<<" qq="<<t.qq<<" wechat="<<t.weChat<<" birth="<<t.birth<<endl; } int LengthLinkList(Node* L){//求表長 int len=0;Node *p=L;while(p->next){len++;p = p->next;}return len; } void save(Node* L){ofstream out;out.open("TelephoneList.txt", ios::out | ios::app);if (!out.is_open())return;out<<LengthLinkList(L)<<" ";for(Node *p = L->next; p != NULL; p = p->next){out<<p->tele.id<<" "<<p->tele.name<<" "<<p->tele.sex<<" "<<p->tele.number<<" "<<p->tele.qq<<" "<<p->tele.weChat<<" "<<p->tele.addr<<" "<<p->tele.birth<<" ";}out.close(); } Node* load(Node* L){Node *p = L, *q;ifstream in("TelephoneList.txt");string str[8];char buffer[256];if(!in.is_open()){cout<<"加載文件錯誤"<<endl; return NULL;} cout << "載入通訊錄文件" << endl;in.getline(buffer, 100, ' ');string data = string(buffer);int sum = 0;for(int j = 0; j < data.length(); j++){sum = sum*10 + data[j] - 48;}for(int i = 0; i < sum; i++){for (int j = 0; j < 8; j++){in.getline(buffer, 100, ' ');str[j] = string(buffer);}q = new Node();q->tele.id = 0; string s = str[0];cout<<"s="<<s<<" s.length="<<s.length()<<" s[0]="<<s[0]<<endl;for(int j = 0; j < s.length(); j++){q->tele.id = q->tele.id*10 + s[j]-48;} cout<<"tele.id="<<q->tele.id<<endl;q->tele.name = str[1];q->tele.sex = str[2];q->tele.number = str[3];q->tele.qq = str[4];q->tele.weChat = str[5];q->tele.addr = str[6];q->tele.birth = str[7];q->next = p->next;p->next = q;p = q;}in.close();return p; } int ID = 0; void CreateLinkList(Node* L){//尾插法創建鏈表,頭結點不存內容 Node *node, *p = load(L);Telephone t;int f = 1;ID = LengthLinkList(L);if(p != NULL){L = p;}while(f != 0){Input(t);t.id = ID++;node = new Node();node->tele = t;node->next = NULL;L->next = node;L = node; cout<<"輸入1繼續,輸入0結束:"<<endl;cin>>f;} } void DisplayLinkList(Node* L){//瀏覽鏈表Node* p = L->next;cout<<"通訊錄"<<endl; while(p != NULL){Output(p->tele);p = p->next;} } void findName(Node* L,string str){//按姓名Node *p=L->next;while(p != NULL){if(p->tele.name == str) Output(p->tele); p=p->next;} } void findSex(Node* L,string str){//按性別Node *p=L->next;while(p != NULL){if(p->tele.sex == str) Output(p->tele); p=p->next;} } void birthSearch(Node* L,string str){//按生日Node *p=L->next;while(p != NULL){if(p->tele.birth == str) Output(p->tele); p=p->next;} } void add(Node* L, int i, Telephone t){//插入元素(從0號開始計算) Node *p = L, *s;t.id = ID++;if(i >= LengthLinkList(L)){cout<<"插入位置錯誤"<<endl;return; }else{s = new Node();s->tele = t;for(int j = 0; j < i; j++){p = p->next;} s->next = p->next;p->next = s;} } void remove(Node* L, string name){//刪除元素Node *p, *q; if(L == NULL){cout<<"輸入錯誤"<<endl; return; }p = L;q = L->next;while(true){if(q->tele.name == name){p->next = q->next;delete q;break;}p = p->next;q = q->next;} } void update(Node* L, string name){//修改聯系人信息Node *p = L->next;while(p != NULL){if(p->tele.name == name){cout<<"開始修改"<<endl; Input(p->tele);return; }p = p->next;}cout<<"沒找到對應聯系人"<<endl; } void freeLinkList(Node* L){//釋放整個鏈表的內存Node *p = L, *q = L;while(p != NULL){p = p->next;free(q);q = p;} } void menu(){//主菜單cout<<"單鏈表通訊錄\n\n";cout<<"1.通訊錄鏈表的建立;\n";cout<<"2.通訊者信息的插入;\n";cout<<"3.通訊者信息的查詢;\n";cout<<"4.通訊者信息的修改;\n";cout<<"5.通訊者信息的刪除;\n";cout<<"6.通訊者信息的保存;\n";cout<<"7.通訊者信息的輸出;\n";cout<<"8.通訊者信息的載入;\n";cout<<"9.退出管理系統;\n"; } int main(){int n;char c, k, m;string data;Telephone x;//初始化頭結點 Node* L = new Node();L->next = NULL; while(1){menu();cout<<"請選擇:"<<endl;cin>>k;system("cls");switch(k){case '1':CreateLinkList(L);cout<<"創建成功"<<endl;system("cls");break;case '2':Input(x);cout<<"輸入要插入的位置"<<endl;cin>>n; add(L, n, x);DisplayLinkList(L);cout<<"輸入任意鍵繼續";cin>>m;system("cls");break;case '3':while(1){cout<<"1.按姓名查詢\n2.按性別查詢\n3.按生日查詢\n4.返回主菜單\n請輸入相應的序號進行選擇:"<<endl; cin>>c; system("cls");switch(c){case '1':cout<<"請輸入所要找查聯系人的姓名:"<<endl; cin>>data; findName(L, data);cout<<"輸入任意鍵繼續";cin>>m;system("cls");break;case '2':cout<<"請輸入所要找查聯系人的性別:"<<endl; cin>>data; findName(L, data);cout<<"輸入任意鍵繼續";cin>>m;system("cls");break;case '3':cout<<"請輸入所要找查聯系人的生日:"<<endl; cin>>data; findName(L, data);cout<<"輸入任意鍵繼續";cin>>m;system("cls");break;case '4':goto loopOut;break;default:printf("輸入錯誤!\n");system("cls");break;}}loopOut:system("cls");break;case '4':cout<<"請輸入所要修改聯系人的姓名:"<<endl;cin>>data; update(L, data);system("cls");break;case '5':cout<<"請輸入所要刪除聯系人的姓名:"<<endl;cin>>data; remove(L, data);DisplayLinkList(L);cout<<"輸入任意鍵繼續";cin>>m;system("cls");break;case '6':save(L);system("cls");case '7':DisplayLinkList( L);cout<<"輸入任意鍵繼續";cin>>m;system("cls");break;case '8':load(L);ID = LengthLinkList(L);cout<<"輸入任意鍵繼續";cin>>m;system("cls");break;case '9':freeLinkList(L);exit(-1);default:printf("輸入錯誤!");system("cls");}}return 0; }上面有錯, 還請指出, 如果認為我寫的還不錯, 還請點個贊, 多多支持一下, O(∩_∩)O~~
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的一个很Low的通讯录管理系统(但是能用)C/C++单链表实现的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 做人力资源需要掌握python_9种人力
- 下一篇: mft按钮设计_哈汽机组660MW超临界
