通訊錄管理
——數據結構課程設計報告
學生姓名 |
幸廣輝 |
學生班級 |
信管1134班 |
學生學號 |
201311671422 |
完成時間:2015年1月8日
目錄
1、需求分析
1.1課程設計目的
1.2課程設計要求
2、分析
2.1功能模塊
2.2概要設計
4、測試
5、收穫與體會
附錄:源程序
1.1課程設計目的
通過課程設計,學會運用數據結構知識,針對具體應用,自己設計合理數據結構,確定存儲結構,並能設計具體操作算法,選擇使用具體語言進行實現。掌握C++較複雜程序的組織和設計過程,調試技巧。學習解決實際問題的能力。
1.2課程設計要求
用數據結構的雙鏈表製作一款通訊管理軟件,初步要求:
每個好友信息包含姓名、性別、住址、郵編、年齡、電話、QQ、微信帳號、生日等。
‚作爲一個完整的系統,應具有友好的界面和較強的容錯能力。
2.1主要功能
1進入主功能菜單界面,功能選擇。
2創建聯繫人列表並保存修改後的通訊錄
3刪除創建的聯繫人並保存修改後的通訊錄
4打印出保存後的通訊錄當未創建通訊錄時提示用戶創建新的通訊錄
5根據用戶輸入的聯繫人的首字母查找符合條件的聯繫人
6退出通訊錄管理系統
3概要設計
主要流程圖如下:
‚根據系統的功能模塊
1.主界面模塊:完成系統菜單的顯示
2.功能選擇模塊:根據用戶的選擇,完成其相應的功能
(1)通訊錄聯繫人的信息錄入:完成用戶從界面輸入聯繫人的信息,保存入系統的功能。
(2)通訊錄信息刪除:刪除所存儲的聯繫人信息。
(3)通訊錄信息查詢:完成用戶從界面輸入查找的條件
(4)通訊錄信息輸出:在屏幕上輸出所有聯繫人的信息。
4測試
1菜單界面
2功能測試
①錄入模塊
②刪除模塊
③查詢模塊
④打印模塊
5心得與體會
這次的課程設計感觸頗深,首先深刻感受到程序的實際應用性,這學期的課程設計的題目都是貼近實際生活的問題,我們就能夠很清楚的明白自己寫的程序要解決什麼樣的實際問題,應該解決什麼樣的實際問題,覺得自己的程序更有實用價值。
這次設計中不斷的出現問題,我則自己努力,不斷的改正錯誤,改進自己的程序,有些自己實在是無法解決的問題,則會與其他同學討論,或上網查詢、搜尋資料……在不斷的改進過程中,深刻的認識到自己程序的漏洞和不健全性,認識到自己對雙鏈表略懂皮毛。以致有很多很簡單的錯誤調試了很長時間。通過這次設計,不但讓我進一步加深了對知識的鞏固,而且很好的鍛鍊了我的獨立思考能力,以及分析問題解決問題的能力。今後凡事都不要着急,要冷靜的分析思考,越是急越是無法解決,只有沉着冷靜深入思考才能真正的解決問題,只要自己努力凡事都能解決。還有就是深刻體會到了平時多練習的重要性,平時如果不多練習,在編寫自己的程序時會手忙腳亂無所適從。
代碼
源程序
#include <iostream>
using namespace std;
#include <stdlib.h>
#include <string.h>
typedef struct message
{
char name[30];//定義變量的字符型
char sex[4];
char sage[10];
char number[20];
char email[30];
char adress[100];
char qq[120];
char weixin[120];
char birthday[10];
struct message *proir; //結構體
struct message *next;
} mess;
mess *head=NULL;
mess *p[100];
int man=0;
//////////////////////////////////////////////////////////////////////////////
//////////////////////////函數
char menu(); //菜單
void add(); //增加
void del(); //刪除
char del_menu();
void del_name();
void del_adress();
void del_number();
void del_email();
void del_qq();
void del_sex();
void del_sage();
void del_weixin();
void del_birthday();
void find(); //查找
char find_menu();
void find_name();
void find_adress();
void find_number();
void find_email();
void find_qq();
void find_sex();
void find_sage();
void find_weixin();
void find_birthday();
void print(); //打印
char print_menu();
void print_time();
void print_word();
void file_save();
//////////////////////////////////////////////////////////////////////////// //////////////////////////////////主函數
int main()
{
FILE *fp=fopen("1.txt","r+");
if(fp==NULL)
cout<<"通訊錄爲空!!\n";
else
{
fseek(fp,0,2);
if(ftell(fp)==0)
cout<<"通訊錄爲空!\n";
else
{
mess *p2,*tmp;
rewind(fp);
head=(mess *)malloc(sizeof(mess));
head->next=NULL;
p[0]=head;
while(feof(fp)==0)
{
p2=(mess *)malloc(sizeof(mess));
cout<<"%s\t%s\t%s\t%s\t%s\n",p2->name,p2->adress,p2->number,p2->email,p2->qq,p2->sex,p2->sage,p2->weixin,p2->birthday;
tmp=head;
while(tmp->next!=NULL)
tmp=tmp->next;
tmp->next=p2;
p2->next=NULL;
p2->proir=tmp;
man++;
p[man]=p2;
p[man+1]=NULL;
}
man--;
p2->proir->next=NULL;
cout<<"數據讀取完畢!\n";
int flag=fclose(fp);
if(flag!=0)
{cout<<"關閉文件失敗!";
return 0;}
}
}
char i;
i=menu();
switch(i)
{
case '1':add();break;
case '2':del();break;
case '3':find();break;
case '4':print();break;
case '5':file_save();breake;
case '6':return 0;
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////主菜單
char menu()
{
char i;
cout<<"---------------功能菜單---------------\n"<<endl;
cout<<" | 1.錄入信息\t 2.刪除信息 |\n"<<endl;
cout<<" | 3.查找信息\t 4.顯示信息 |\n"<<endl;
cout<<" | 5.保存並退出 6.退出 \t |\n"<<endl;
cout<<"--------------------------------------\n"<<endl;
while(1)
{
cout<<"請輸入功能編號:";
cin>>i;
if('0'<i&&i<'7')
return i;
else
cout<<"無效指令!";
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////錄入函數
void add()
{
mess *p1;
p1=(mess *)malloc(sizeof(mess));
cout<<"請輸入姓名:\n";
cin>>p1->name;
cout<<"請輸入性別:\n";
cin>>p1->sex;
cout<<"請輸入年齡:\n";
cin>>p1->sage;
cout<<"請輸入地址:\n";
cin>>p1->adress;
cout<<"請輸入手機號碼:\n";
cin>>p1->number;
cout<<"請輸入郵箱:\n";
cin>>p1->email;
cout<<"請輸入qq號碼:\n";
cin>>p1->qq;
cout<<"請輸入微信號碼:\n";
cin>>p1->weixin;
cout<<"請輸入生日:\n";
cin>>p1->birthday;
if(head==NULL)
{
head=(mess *)malloc(sizeof(mess));
head->next=p1;
p1->next=NULL;
p1->proir=head;
p[0]=head;
man++;
p[man]=p1;
p[man+1]=NULL;
}
else
{
mess *p2=head->next;
while(p2->next!=NULL)
p2=p2->next;
p2->next=p1;
p1->next=NULL;
p1->proir=p2;
man++;
p[man]=p1;
p[man+1]=NULL;
}
cout<<"添加成功!通訊錄內現有%d個聯繫人\n是否繼續錄入信息<y/n>........",man;
char ch;
cin>>ch;
if(ch=='n');
else if(ch=='y') add();
else {cout<<"無效指令!請重新輸入:";}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////刪除函數
void del()
{
char i=del_menu();
switch(i)
{
case '1':del_name();break;
case '2':del_sex();break;
case '3':del_sage();break;
case '4':del_adress();break;
case '5':del_number();break;
case '6':del_email();break;
case '7':del_qq();break;
case '8':del_weixin();break;
case '9':del_birthday();break;
case '10':break;
}
}
////////////////////
char del_menu()
{
char i;
cout<<"---------------刪除菜單---------------\n"<<endl;
cout<<" | 1.按姓名刪除\n | 2.按微信號刪除 \n"<<endl;
cout<<" | 3.按手機號碼刪除\n | 4.按郵箱刪除 \n"<<endl;
cout<<" | 5.按qq號刪除 \n | 6.返回主菜單\n"<<endl;
cout<<"--------------------------------------\n"<<endl;
while(1)
{
cout<<"請輸入功能編號:";
cin>>i;
if('0'<i&&i<'7')
return i;
else
cout<<"無效指令!";
}
}
///////////////////
void del_sex(){};
void del_sage(){};
void del_adress(){};
void del_birthday(){};
void del_name()
{
if(man==0)
cout<<"通訊錄爲空!\n\n";
else
{
char ch[30];
int i=1;
mess *p1=head->next;
cout<<"請輸入被刪用戶的姓名:\n";
cin>>ch;
while(p1!=NULL)
{
if(strcmp(p1->name,ch)==0)
{
while(p[i]!=p1)
i++;
while(p[i]!=NULL)
{p[i]=p[i+1];i++;}
p1->proir->next=p1->next;
if(p1->next!=NULL)
p1->next->proir=p1->proir;
cout<<"此用戶信息已刪除!";
cout<<"是否繼續刪除<y/n>........";
man--;
char tmp;
cin>>tmp;
if(tmp=='n')break;
else if(tmp=='y') {del();break;}
else {cout<<"無效指令!請重新輸入:";}
}
p1=p1->next;
}
if(p1==NULL)
{
cout<<"查無此人!\n";
cout<<"是否繼續刪除<y/n>........";
char tmp;
cin>>tmp;
if(tmp=='n');
else if(tmp=='y') del();
else {cout<<"無效指令!請重新輸入:";
}
}
}}
/////////////////////
void del_weixin()
{
if(man==0)
cout<<"通訊錄爲空!\n\n";
else
{
char ch[30];
int i=1;
mess *p1=head->next;
cout<<"請輸入被刪用戶的微信號:\n";
cin>>ch;
while(p1!=NULL)
{
if(strcmp(p1->weixin,ch)==0)
{
while(p[i]!=p1)
i++;
while(p[i]!=NULL)
{p[i]=p[i+1];i++;}
p1->proir->next=p1->next;
if(p1->next!=NULL)
p1->next->proir=p1->proir;
cout<<"此用戶信息已刪除!";
cout<<"是否繼續刪除<y/n>........";
man--;
char tmp;
cin>>tmp;
if(tmp=='n')break;
else if(tmp=='y') {del();break;}
else {cout<<"無效指令!請重新輸入:";}
}
p1=p1->next;
}
if(p1==NULL)
{
cout<<"查無此人!\n";
cout<<"是否繼續刪除<y/n>........";
char tmp;
cin>>tmp;
if(tmp=='n');
else if(tmp=='y') del();
else {cout<<"無效指令!請重新輸入:";
}
}
}}
/////////////////////////////
void del_number()
{
if(man==0)
cout<<"通訊錄爲空!\n\n";
else
{
char ch[30];
int i=1;
mess *p1=head->next;
cout<<"請輸入被刪用戶的電話號碼:\n";
cin>>ch;
while(p1!=NULL)
{
if(strcmp(p1->number,ch)==0)
{
while(p[i]!=p1)
i++;
while(p[i]!=NULL)
{p[i]=p[i+1];i++;}
p1->proir->next=p1->next;
if(p1->next!=NULL)
p1->next->proir=p1->proir;
cout<<"此用戶信息已刪除!";
cout<<"是否繼續刪除<y/n>........";
man--;
char tmp;
cin>>tmp;
if(tmp=='n')break;
else if(tmp=='y') {del();break;}
else {cout<<"無效指令!請重新輸入:";}
}
p1=p1->next;
}
if(p1==NULL)
{
cout<<"查無此人!\n";
cout<<"是否繼續刪除<y/n>........";
char tmp;
cin>>tmp;
if(tmp=='n');
else if(tmp=='y') del();
else {cout<<"無效指令!請重新輸入:";
}
}
}}
/////////////////////
void del_email()
{
if(man==0)
cout<<"通訊錄爲空!\n\n";
else
{
char ch[30];
int i=1;
mess *p1=head->next;
cout<<"請輸入被刪用戶的郵箱:\n";
cin>>ch;
while(p1!=NULL)
{
if(strcmp(p1->email,ch)==0)
{
while(p[i]!=p1)
i++;
while(p[i]!=NULL)
{p[i]=p[i+1];i++;}
p1->proir->next=p1->next;
if(p1->next!=NULL)
p1->next->proir=p1->proir;
cout<<"此用戶信息已刪除!";
cout<<"是否繼續刪除<y/n>........";
man--;
char tmp;
cin>>tmp;
if(tmp=='n')break;
else if(tmp=='y') {del();break;}
else {cout<<"無效指令!請重新輸入:";}
}
p1=p1->next;
}
if(p1==NULL)
{
cout<<"查無此人!\n";
cout<<"是否繼續刪除<y/n>........";
char tmp;
cin>>&tmp;
if(tmp=='n');
else if(tmp=='y') del();
else {cout<<"無效指令!請重新輸入:";
}
}
}}
/////////////////////
void del_qq()
{
if(man==0)
cout<<"通訊錄爲空!\n\n";
else
{
char ch[30];
int i=1;
mess *p1=head->next;
cout<<"請輸入被刪用戶的qq:\n";
cin>>ch;
while(p1!=NULL)
{
if(strcmp(p1->qq,ch)==0)
{
while(p[i]!=p1)
i++;
while(p[i]!=NULL)
{p[i]=p[i+1];i++;}
p1->proir->next=p1->next;
if(p1->next!=NULL)
p1->next->proir=p1->proir;
cout<<"此用戶信息已刪除!";
cout<<"是否繼續刪除<y/n>........";
man--;
char tmp;
cin>>tmp;
if(tmp=='n')break;
else if(tmp=='y') {del();break;}
else {cout<<"無效指令!請重新輸入:";}
}
p1=p1->next;
}
if(p1==NULL)
{
cout<<"查無此人!\n";
cout<<"是否繼續刪除<y/n>........";
char tmp;
cin>>tmp;
if(tmp=='n');
else if(tmp=='y') del();
else {cout<<"無效指令!請重新輸入:";
}
}
}}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////查找函數
void find()
{
char i=find_menu();
switch(i)
{
case '1':find_name();break;
case '2':find_sex();break;
case '3':find_sage();break;
case '4':find_adress();break;
case '5':find_number();break;
case '6':find_email();break;
case '7':find_qq();break;
case '8':find_weixin();break;
case '9':find_birthday();break;
case '10':break;
}
}
///////////////////////////
char find_menu()
{
char i;
cout<<"---------------查找菜單---------------\n"<<endl;
cout<<" | 1.按姓名查找\n | 2.按微信號查找 \n"<<endl;
cout<<" | 3.按手機號碼查找\n | 4.按郵箱查找 \n"<<endl;
cout<<" | 5.按qq號查找 \n | 6.返回主菜單\n"<<endl;
cout<<"--------------------------------------\n"<<endl;
while(1)
{
cout<<"請輸入功能編號:";
cin>>&i;
if('0'<i&&i<'7')
return i;
else
cout<<"無效指令!";
}
}
///////////////////////////
void find_sex(){};
void find_sage(){};
void find_adress(){};
void find_birthday(){};
void find_name()
{
if(man==0)
cout<<"通訊錄爲空!\n\n";
else
{
char ch[30];
mess *p1=head->next;
cout<<"請輸入查找用戶的姓名:\n";
cin>>ch;
while(p1!=NULL)
{
if(strcmp(p1->name,ch)==0)
{
cout<<"姓名\t性別\t年齡\t地址\t手機\t郵箱\tqq\t微信\t生日\n";
cout<<"%s\t%s\t%s\t%s\t%s\n",p1->name,p1->sex,p1->sage,p1->adress,p1->number,p1->email,p1->qq,p1->weixin,p1->birthday;
cout<<"是否繼續查找<y/n>........";
char tmp;
cin>>tmp;
if(tmp=='n')break;
else if(tmp=='y') {find();break;}
else {cout<<"無效指令!請重新輸入:";}
}
p1=p1->next;
}
if(p1==NULL)
{
cout<<"查無此人!\n";
cout<<"是否繼續查找<y/n>........";
char tmp;
cin>>tmp;
if(tmp=='n');
else if(tmp=='y') del();
else {cout<<"無效指令!請重新輸入:";
}
}
}}
void find_weixin()
{
if(man==0)
cout<<"通訊錄爲空!\n\n";
else
{
char ch[30];
mess *p1=head->next;
cout<<"請輸入查找用戶的地址:\n";
cin>>ch;
while(p1!=NULL)
{
if(strcmp(p1->weixin,ch)==0)
{
cout<<"姓名\t性別\t年齡\t地址\t手機\t郵箱\tqq\t微信\t生日\n";
cout<<"%s\t%s\t%s\t%s\t%s\n",p1->name,p1->sex,p1->sage,p1->adress,p1->number,p1->email,p1->qq,p1->weixin,p1->birthday;
cout<<"是否繼續查找<y/n>........";
char tmp;
cin>>tmp;
if(tmp=='n')break;
else if(tmp=='y') {find();break;}
else {cout<<"無效指令!請重新輸入:";}
}
p1=p1->next;
}
if(p1==NULL)
{
cout<<"查無此人!\n";
cout<<"是否繼續查找<y/n>........";
char tmp;
cin>>tmp;
if(tmp=='n');
else if(tmp=='y') del();
else {cout<<"無效指令!請重新輸入:";
}
}
}}
void find_number()
{
if(man==0)
cout<<"通訊錄爲空!\n\n";
else
{
char ch[30];
mess *p1=head->next;
cout<<"請輸入查找用戶的手機號碼:\n";
cin>>ch;
while(p1!=NULL)
{
if(strcmp(p1->number,ch)==0)
{
cout<<"姓名\t性別\t年齡\t地址\t手機\t郵箱\tqq\t微信\t生日\n";
cout<<"%s\t%s\t%s\t%s\t%s\n",p1->name,p1->sex,p1->sage,p1->adress,p1->number,p1->email,p1->qq,p1->weixin,p1->birthday;
cout<<"是否繼續查找<y/n>........";
char tmp;
cin>>tmp;
if(tmp=='n')break;
else if(tmp=='y') {find();break;}
else {cout<<"無效指令!請重新輸入:";}
}
p1=p1->next;
}
if(p1==NULL)
{
cout<<"查無此人!\n";
cout<<"是否繼續查找<y/n>........";
char tmp;
cin>>tmp;
if(tmp=='n');
else if(tmp=='y') del();
else {cout<<"無效指令!請重新輸入:";
}
}
}}
void find_email()
{
if(man==0)
cout<<"通訊錄爲空!\n\n";
else
{
char ch[30];
mess *p1=head->next;
cout<<"請輸入查找用戶的郵箱:\n";
cin>>ch;
while(p1!=NULL)
{
if(strcmp(p1->email,ch)==0)
{
cout<<"姓名\t性別\t年齡\t地址\t手機\t郵箱\tqq\t微信\t生日\n";
cout<<"%s\t%s\t%s\t%s\t%s\n",p1->name,p1->sex,p1->sage,p1->adress,p1->number,p1->email,p1->qq,p1->weixin,p1->birthday;
cout<<"是否繼續查找<y/n>........";
char tmp;
cin>>tmp;
if(tmp=='n')break;
else if(tmp=='y') {find();break;}
else {cout<<"無效指令!請重新輸入:";}
}
p1=p1->next;
}
if(p1==NULL)
{
cout<<"查無此人!\n";
cout<<"是否繼續查找<y/n>........";
char tmp;
cin>>tmp;
if(tmp=='n');
else if(tmp=='y') del();
else {cout<<"無效指令!請重新輸入:";
}
}
}}
void find_qq()
{
if(man==0)
cout<<"通訊錄爲空!\n\n";
else
{
char ch[30];
mess *p1=head->next;
cout<<"請輸入查找用戶的qq:\n";
cin>>ch;
while(p1!=NULL)
{
if(strcmp(p1->qq,ch)==0)
{
cout<<"姓名\t性別\t年齡\t地址\t手機\t郵箱\tqq\t微信\t生日\n";
cout<<"%s\t%s\t%s\t%s\t%s\n",p1->name,p1->sex,p1->sage,p1->adress,p1->number,p1->email,p1->qq,p1->weixin,p1->birthday;
cout<<"是否繼續查找<y/n>........";
char tmp;
cin>>tmp;
if(tmp=='n')break;
else if(tmp=='y') {find();break;}
else {cout<<"無效指令!請重新輸入:";}
}
p1=p1->next;
}
if(p1==NULL)
{
cout<<"查無此人!\n";
cout<<"是否繼續查找<y/n>........";
char tmp;
cin>>tmp;
if(tmp=='n');
else if(tmp=='y') del();
else {cout<<"無效指令!請重新輸入:";
}
}
}}
///////////////////////////////////////////////////////////////////////////////////////////////////////////顯示函數
void print()
{
char c;
c=print_menu();
switch(c)
{
case '1':print_time();break;
case '2':print_word();break;
}
}
//////////////
char print_menu()
{
char c;
cout<<"---------------顯示菜單---------------\n"<<endl;
cout<<" | 1.按時間先後排序\n | 2.按首字母排序 \n"<<endl;
cout<<"--------------------------------------\n"<<endl;
while(1)
{
cout<<"請輸入功能編號:";
cin>>c;
if('0'<c&&c<'3')
return c;
else
cout<<"無效指令!";
}
}
///////////////
void print_time()
{
if(man==0)
cout<<"通訊錄爲空!\n";
else
{
cout<<"姓名\t性別\t年齡\t地址\t手機\t郵箱\tqq\t微信\t生日\n";
for(int i=1;i<=man;i++)
cout<<"%s\t%s\t%s\t%s\t%s\n",p[i]->name,p[i]->sex,p[i]->sage,p[i]->adress,p[i]->number,p[i]->email,p[i]->qq,p[i]->weixin,p[i]->birthday;
}
}
//////////////
void print_word()
{
if(man==0)
cout<<"通訊錄爲空!\n";
else if(man==1)
{
mess *p=head->next;
cout<<"姓名\t性別\t年齡\t地址\t手機\t郵箱\tqq\t微信\t生日\n"; cout<<"%s\t%s\t%s\t%s\t%s\n",p->name,p->sex,p->sage,p->adress,p->number,p->email,p->qq,p->weixin,p->birthday;
}
else
{
mess *p1=head->next,*p2=p1->next;
for(int i=0;i<man;i++)
{
p1=head->next;p2=p1->next;
while(p2->next!=NULL)
{
if(strcmp(p1->name,p2->name)>0)
{
p1->next=p2->next;
p2->next->proir=p1;
p2->proir=p1->proir;
p1->proir->next=p2;
p1->proir=p2;
p2->next=p1;
p2=p2->next->next;
}
else
{
p1=p1->next;
p2=p2->next;
}
}
if(strcmp(p1->name,p2->name)>0)
{
p2->proir=p1->proir;
p1->proir->next=p2;
p1->next=NULL;
p2->next=p1;
p1->proir=p2;
}
}
cout<<"姓名\t性別\t年齡\t地址\t手機\t郵箱\tqq\t微信\t生日\n";
p1=head->next;
for(int j=0;j<man;j++)
{
cout<<"%s\t%s\t%s\t%s\t%s\n",p1->name,p1->sex,p1->sage,p1->adress,p1->number,p1->email,p1->qq,p1->weixin,p1->birthday; p1=p1->next;
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////文件保存
void file_save()
{
FILE *fp=fopen("1.txt","w+");
if(fp==NULL)
cout<<"保存失敗!\n";
else
{
//fputs("姓名\t地址\t手機\t郵箱\tqq\n",fp);
for(int i=1;i<=man;i++)
cout<<fp,"%s\t%s\t%s\t%s\t%s\n",p[i]->name,p[i]->sex,p[i]->sage,p[i]->adress,p[i]->number,p[i]->email,p[i]->qq,p[i]->weixin,p[i]->birthday;
//char ch[5]="結束";
//fcout<<fp,"%s",ch);
int j=fclose(fp);
if(j==0)
cout<<"保存成功!\n";
else
cout<<"保存失敗!\n";
}
}