日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

【C++课程设计】基于单向链表的通讯录管理程序

發布時間:2023/12/31 c/c++ 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【C++课程设计】基于单向链表的通讯录管理程序 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文目錄

  • 通訊錄效果圖
  • 問題描述
  • 任務要求
  • 設計思想
  • 功能模塊
    • 1,數據存儲
    • 2,交互界面
    • 3,主類功能匹配與整合
  • 程序流程圖
  • 源碼實現
    • 【1】Link.h
    • 【2】MYGUI.h
    • 【3】通訊錄.cpp
  • 不足
  • 課程設計報告與源碼獲取


通訊錄效果圖

問題描述

該設計采用菜單作為應用程序的主要界面,用控制語句來改變程序執行的順序,控制語句是實現結構化程序設計的基礎。該設計的任務是利用一個簡單實用的菜單,通過菜單單項進行選擇,實現和完成通訊錄管理中常用的幾個不同的功能。通訊者所包含信息請自行設定

任務要求

菜單內容:
(0)通訊錄鏈表的建立
(1)通訊者結點的插入
(2)通訊者結點的查詢
(3)通訊者結點的刪除
(4)通訊錄鏈表的輸出
(5)退出管理系統
設計要求:
使用0-5來選擇菜單項,可擴展功能。
功能函數設計
5個不同功能的算法實現編程題,目的是練習利用鏈表結構來解決實際應用問題的能力,進一步理解和熟悉線形表的鏈式存儲結構


設計思想

將數據存儲的鏈表,用戶交互界面,核心測試類分成三大模塊。鏈表,界面作為頭文件,引入到測試類中,實現功能的分塊整合。不同模塊中有具體的實現,通過接口類,暴露實現方法名稱,隱藏實現細節。

功能模塊

1,數據存儲

以簡單單向列表的形式來存儲數據,鏈表實現類繼承接口類,并實現其中的方法。

2,交互界面

通過字符畫的形式構建交互界面,并將繪制界面的各個方法,封裝入界面繪制工具類中,定義在“MYGUI.h”的頭文件中。

3,主類功能匹配與整合

將數據存儲模塊“Link.h”和交互界面“MYGUI.h”載入主類中,通過子類的工具類,根據用戶的不同操作匹配調用各個頭文件中的方法,并整合流程與方法。


通訊錄程序模塊圖

程序流程圖

程序流程圖

源碼實現

【1】Link.h

#include<iostream> #include<string> #include<fstream> using namespace std; static int counts=0;//鏈數 static int foot;//索引 class ILink{//接口類,暴露方法,隱藏細節 protected:virtual void add(string data)=0;virtual int count()=0;virtual bool isEmpty()=0;virtual string get(int index)=0;virtual void set(int index)=0;virtual void toArray()=0;virtual bool contains(string data)=0;virtual void remove(int index)=0;virtual void clean()=0;virtual void fileOut(char* path)=0;virtual void fileIn(char* path)=0; }; class Node{//結點類 public:string data;//結點數據 Node *next=NULL;//保留下一節點 Node(string data){this->data=data;}//Node類對外支持的方法,主要使用了遞歸 void addNode(Node* newNode){if(this->next==NULL){this->next=newNode;}else{this->next->addNode(newNode);}}void toArrayNode(){cout<<"["<<foot++<<"]"<<this->data<<endl;if(this->next!=NULL){this->next->toArrayNode();}}string getNode(int index){if(foot++==index){return this->data;}else{return this->next->getNode(index);}}void setNode(int index,string data){if(foot++==index){this->data=data;}else{this->next->setNode(index,data);}}bool containsNode(string data){if(this->data==data){return 1;}else if(this->next==NULL){return 0;}else{return this->next->containsNode(data);}}void removeNode(Node* previous,int index){//一定要接受指針,進行地址傳遞,不能進行值傳遞 if(foot++==index){previous->next=this->next;}else if(this->next!=NULL){this->next->removeNode(this,index);}}void fileOutNode(char* path)throw (int){ofstream file;file.open(path,ios::app);if(!file){throw 0;return ;}file<<this->data<<endl;if(this->next!=NULL){this->next->fileOutNode(path);}else{file.close();//關閉文件流 }}}; class Link:public ILink{private:Node *root=NULL;//初始化根節點 public:~Link(){delete root;}//Link類對外支持的方法 void add(string data){//add的兩種形式:向根節點添加,向子節點添加 Node* newNode=new Node(data);if(this->root==NULL){ this->root=newNode;}else{this->root->addNode(newNode);}counts++;//鏈數加一 } int count(){return counts;}bool isEmpty(){return counts==0;}void toArray(){if(this->isEmpty()){cout<<"鏈表中沒有數據"<<endl;}foot=1;root->toArrayNode();}string get(int index) throw (int){if(index>counts|index<=0){//兩個判斷都要進行,所以不用短路或“||” throw 0;//索引無效 return "ERROR";}foot=1;//初始化索引 return this->root->getNode(index);}void set(int index)throw (int){if(index>counts|index<=0){//兩個判斷都要進行,所以不用短路或“||” throw 0;//索引無效 return;}string str[4];cout<<"通訊者 [姓名]:";cin>>str[0];cout<<"通訊者 [電話]:";cin>>str[1];cout<<"通訊者 [QQ]:";cin>>str[2];str[3]="[姓名]:"+str[0]+" [電話]:"+str[1]+" [QQ]:"+str[2];foot=1;//初始化索引 this->root->setNode(index,str[4]);}bool contains(string data){return this->root->containsNode(data);}void remove(int index) throw (int){//remove的兩種形式:輸出根節點,刪除子節點 if(index>counts|index<=0){//兩個判斷都要進行,所以不用短路或“||” throw 0;//索引無效 return;}if(index==1){this->root=this->root->next;counts--;//鏈數減一 }else{foot=1;this->root->removeNode(this->root,index); counts--;}}void fileOut(char* path){//鏈表導出 if(this->isEmpty()){cout<<"鏈表中沒有數據"<<endl;}root->fileOutNode(path);} void fileIn(char* path)throw(int){//文件導入 ifstream file(path);string data;if(!file){throw 0;return;}getline(file,data);//整行導入 while(!file.eof()&&data!=""){//判斷文件結尾 this->add(data);getline(file,data);//整行導入 }file.close(); }void clean(){//清空根節點 this->root=NULL;counts=0;} };

【2】MYGUI.h

#include<iostream> #include<string> #include<cstdlib> using namespace std; class MYGUI{//繪制界面工具類 public:static void start(){//設置窗口大小 system("mode con cols=55 lines=36");}static void menu(){//畫菜單 cout<<" +-----------------------------------------------------+\n";cout<<" | |\n";cout<<" | 0 000000 0 00000000 |\n";cout<<" | 0 0 0 0 00000 0 |\n";cout<<" | 00 0 0 0000000 |\n";cout<<" | 0000000 0 0 0 |\n";cout<<" | 00 0 0 0 000 0 0 00000000000 |\n";cout<<" | 0 0000000 0 00000 0 |\n";cout<<" | 0 0 0 0 0 0 0 0 0 0 |\n";cout<<" | 0 0000000 0 0 0 0 000 0 |\n";cout<<" | 0 0 0 0 0 0 0 0 0 0 |\n";cout<<" | 0 0 0 00 00 0 0 0 0 0 0 |\n";cout<<" | 0 0 0 00 00 0 00 |\n";cout<<" | 0 000000000 0 0 00 |\n";cout<<" | |\n";cout<<" |-----------------------------------------------------|\n";cout<<" | |\n";cout<<" | [SoftMenu] |\n";cout<<" | |\n";cout<<" | (0) 通訊錄鏈表的建立 |\n";cout<<" | |\n";cout<<" | (1) 通訊者鏈表的導入 |\n";cout<<" | |\n";cout<<" | (2) 通訊者結點的插入 |\n";cout<<" | |\n";cout<<" | (3) 通訊者結點的查詢 |\n";cout<<" | |\n";cout<<" | (4) 通訊者結點的修改 |\n";cout<<" | |\n";cout<<" | (5) 通訊者結點的刪除 |\n";cout<<" | |\n";cout<<" | (6) 通訊錄鏈表的清空 |\n";cout<<" | |\n";cout<<" | (7) 通訊錄鏈表的打印 |\n";cout<<" | |\n";cout<<" | (8) 通訊錄鏈表的導出 |\n";cout<<" | |\n";cout<<" | (9) 退出管理系統 |\n";cout<<" | |\n";cout<<" | |\n";cout<<" +-----------------------------------------------------+\n"; cout<<"【請輸入功能選項】:"<<flush;//flush用于刷新此處的緩沖區,防止輸入數據延滯 }static void tip(string tip){//畫提示信息cout<<endl;cout<<" +-----------------"<<tip<<"----------------+\n";cout<<" | |\n";} static void tipMenu(){cout<<" | |\n";cout<<" +-----------------------------------------------------+\n";cout<<" |(0)創建鏈表 (1)導入鏈表 (2)插入結點 (3)查詢結點|\n";cout<<" |(4)修改結點 (5)刪除結點 (6)清空鏈表 (7)打印鏈表|\n";cout<<" |(8)導出鏈表 (9)退出系統 |\n";cout<<" +-----------------------------------------------------+\n";cout<<"【請輸入功能選項】:"; } };

【3】通訊錄.cpp

#include<iostream> #include<string> #include"MYGUI.h"//載入界面繪制類 #include"Link.h"//載入鏈表,提供數據存儲支持 using namespace std; int option;//輸入選項 Link *plink=NULL;//鏈表指針 char input[2];//用戶輸入值,用于驗證 class Util{//操作工具類 public://輸入驗證,確保數據為0-5整數,驗證通過返回解碼后的整數,否則返回-1 static int inputExam(char* input){if(input[0]>='0'&&input[0]<='9'&&input[1]==NULL){return input[0]-48;}return -1;} //輸入選項編號,通過switch來匹配各個選項的功能 static void option(int option){switch(option){case 0:{ //創建鏈表 Util::creatLink();break;} case 1:{//導入文件 Util::importLink();break;}case 2:{//插入結點 Util::insertNode();break;} case 3:{//查詢結點Util::findNode();break;}case 4:{//修改節點 Util::alterNode();break;} case 5:{//刪除結點Util::deleteNode();break;}case 6:{//清空鏈表 Util::cleanLink();break;} case 7:{//輸出鏈表 Util::showLink();break;}case 8:{//鏈表導出 Util::exportLink();break;} }}static void creatLink(){MYGUI::tip("(0) 通訊錄鏈表的建立");if(plink!=NULL){char str;cout<<"【已存在通訊錄鏈表,是否重新創建(y/n)】:"<<flush;cin>>str;if(str=='y'){plink->clean(); //并沒有重新創建新的鏈表,只不過是將原有鏈表清空了,這樣節約內存開支 cout<<"【創建通訊錄鏈表成功】\n";MYGUI::tipMenu();}else if(str=='n'){MYGUI::tipMenu();}else{cout<<"【輸入錯誤,請輸入(y/n)】\n";Util::option(0);}} else{plink=new Link();cout<<"【創建通訊錄鏈表成功】\n";MYGUI::tipMenu();} } static void importLink(){MYGUI::tip("(1) 通訊錄鏈表的導入"); if(plink!=NULL){char path[100]; //支持char[100]范圍內的路徑 cout<<"【請輸入導入文件路徑名稱(.txt)】:";cin>>path; try{plink->fileIn(path); cout<<"【導入文件成功】\n";MYGUI::tipMenu();}catch(int){cout<<"【文件打開失敗,檢查輸入路徑】\n";MYGUI::tipMenu();}}else{cout<<"【尚未創建通訊者鏈表,請創建】\n";MYGUI::tipMenu();} }static void insertNode(){MYGUI::tip("(2) 通訊者結點的插入");if(plink!=NULL){string str[4];//存儲輸入的數據 cout<<"【插入第"<<plink->count()+1<<"個通訊者信息】\n";cout<<"通訊者 [姓名]:";cin>>str[0];cout<<"通訊者 [電話]:";cin>>str[1];cout<<"通訊者 [QQ]:";cin>>str[2];str[3]="[姓名]:"+str[0]+" [電話]:"+str[1]+" [QQ]:"+str[2];plink->add(str[3]);cout<<"【第"<<plink->count()<<"個通訊者信息錄入完成】\n";MYGUI::tipMenu(); }else{cout<<"【尚未創建通訊者鏈表,請創建】\n";MYGUI::tipMenu();} }static void findNode(){MYGUI::tip("(3) 通訊者結點的查詢"); if(plink!=NULL){if(plink->isEmpty()){cout<<"【尚未錄入通訊者信息,請錄入】\n";MYGUI::tipMenu();}else{int index;//輸入的查詢索引 string data;//存儲查詢得到的信息 cout<<"【請輸入要查詢通訊者的編號】:";cin>>index;try{//異常處理 data=plink->get(index);cout<<"【查詢結果】\n";cout<<data<<endl;MYGUI::tipMenu();}catch(int){cout<<"【查詢失敗,檢查編號】\n";MYGUI::tipMenu();} }}else{cout<<"【尚未創建通訊者鏈表,請創建】\n";MYGUI::tipMenu();} }static void alterNode(){MYGUI::tip("(4) 通訊者結點的修改"); if(plink!=NULL){if(plink->isEmpty()){cout<<"【尚未錄入通訊者信息,請錄入】\n";MYGUI::tipMenu();}else{int index;cout<<"【請輸入需要修改通訊者的編號】:";cin>>index;try{plink->set(index); cout<<"【修改成功】\n";MYGUI::tipMenu();}catch(int){cout<<"【修改失敗,檢查編號】\n";MYGUI::tipMenu();}}}else{cout<<"【尚未創建通訊者鏈表,請創建】\n";MYGUI::tipMenu();}} static void deleteNode(){MYGUI::tip("(5) 通訊者結點的刪除"); if(plink!=NULL){if(plink->isEmpty()){cout<<"【尚未錄入通訊者信息,請錄入】\n";MYGUI::tipMenu();}else{int index;cout<<"【請輸入需要刪除通訊者的編號】:";cin>>index; try{//異常處理 plink->remove(index);cout<<"【刪除成功】\n";MYGUI::tipMenu();}catch(int){cout<<"【刪除失敗,檢查編號】\n";MYGUI::tipMenu();}}}else{cout<<"【尚未創建通訊者鏈表,請創建】\n";MYGUI::tipMenu();} }static void cleanLink(){MYGUI::tip("(6) 通訊者鏈表的清空"); if(plink!=NULL){if(plink->isEmpty()){cout<<"【空鏈表,無需清空】\n";MYGUI::tipMenu();}else{plink->clean();cout<<"【清空成功】\n";MYGUI::tipMenu();} } else{cout<<"【未創建通訊者鏈表,請創建】\n";MYGUI::tipMenu();}}static void showLink(){MYGUI::tip("(7) 通訊錄鏈表的輸出"); if(plink!=NULL){if(plink->isEmpty()){cout<<"【未錄入通訊者信息,請錄入】\n";MYGUI::tipMenu();}else{cout<<"【輸出結果】\n" ;plink->toArray(); MYGUI::tipMenu();}}else{cout<<"【尚未創建通訊者鏈表,請創建】\n";MYGUI::tipMenu();} }static void exportLink(){MYGUI::tip("(8) 通訊錄鏈表的導出"); if(plink!=NULL){if(plink->isEmpty()){cout<<"【尚未錄入通訊者信息,請錄入】\n";MYGUI::tipMenu();}else{char path[100]; //支持char[100]范圍內的路徑 cout<<"【請輸入導出文件路徑名稱(.txt)】:";cin>>path; try{plink->fileOut(path); cout<<"【導出文件成功】\n"; MYGUI::tipMenu();}catch(int){cout<<"【文件打開失敗,檢查輸入路徑】\n";MYGUI::tipMenu(); }}}else{cout<<"【尚未創建通訊者鏈表,請創建】\n";MYGUI::tipMenu();} } };int main(){MYGUI::menu();//畫界面 cin>>input;option=Util::inputExam(input);while(option==-1){cout<<"【輸入錯誤,請輸入 0~9 范圍類的整數】:";input[1]=NULL;//初始化input[1] cin>>input;option=Util::inputExam(input);}while(option!=9){//循環輸入 Util::option(option);cin>>input;option=Util::inputExam(input);//輸入驗證 while(option==-1){cout<<"【輸入錯誤,請輸入 0~9 范圍類的整數】:";input[1]=NULL;//初始化input[1] cin>>input;option=Util::inputExam(input);//輸入驗證 }}cout<<"\n【bye~】";//結束 return 0; }

不足

沒有統一的異常處理類,Link需要使用模板來增加鏈表存儲數據類型,擴展鏈表用途。日后有時間再填。

課程設計報告與源碼獲取

【猛擊此處】

總結

以上是生活随笔為你收集整理的【C++课程设计】基于单向链表的通讯录管理程序的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。