C++语言课程设计学号:*********姓名:楚归羽指导老师:张目录一、程序功能 (2)二、题目分析 (2)三、设计中遇到的问题及解决方法 (2)四、感想与心得 (2)五、程序说明 (2)六、函数调用关系及主要算法的实现 (8)七、数据测试 (8)一、程序功能程序采用单向链表类结构实现实现通讯录多个纪录的管理工作。
1、提示输入个字段,一次输入一个人的数据2、输入待删除记录的姓名,显示其所有信息,让用户确认是否删除3、按顺序显示所有记录,每显示10条记录暂停,按Enter继续4、输入姓名,显示其所有信息5、用户输入姓名,系统显示该姓名下所有信息,同时显示子菜单,用户按子菜单提示进行修改6、从实现建立的正文文件中批量导入数据。
程序运行时,用户需输入正文文件名7、将库表中数据写入一个正文文件中。
程序运行时,用户需输入正文文件名8、首先显示当前排序关键字,然后提示是否需要改变。
系统默认按姓名排序,可在按办公室电话排序之间切换9、退出系统二、题目分析程序采用单向链表类结构实现,每个结点代表一个通讯记录。
链表类实现通讯录多个纪录的管理工作。
三、设计中遇到的问题及解决方法初次接触课程设计,刚刚拿到题目,实在是茫然无头绪,于是反复研究了题目,仔细复习了链表部分的知识点,加上和同学的讨论,终于是有了一个大概的思路。
刚开始着手编写的时候问题不断,幸好经过练习册上的指示,搭好了基本的框架,可是链表部分仍然是出了很大的问题,经过自己的反复实验与不断努力终于编译成功。
后面的文本文件的读入与写入则是另一个难题,因为这一块练习的不是很多,只在课本上看到了一些皮毛,在编译与调试时也遇到了很大问题。
最后通过看书上的例题并在与同学交流探讨中解决了这些问题。
四、感想与心得C++作为一门必修课程,学习的过程实在是痛苦的,尤其是在编程的时候,因为总是会出现各种各样的错误。
只有通过反复的调试,通过无数次的排错,才能编写出正确的程序,更多的时候,一个很小的很无聊错误就能导致长时间的迷惑。
但是,这也让我学到了还能多,尤其是面对错误坚韧不拔的意志和不达目的绝不低头的毅力,这一点对科研工作想来也是相当重要的。
五、程序说明/*通讯录管理系统||Copyright:010910236 丁志伟*/#include <iostream.h>#include <string.h>#include <stdlib.h>#include <ctype.h>#include <iomanip.h>#include <fstream.h>/*------------------------分割线||节点类的定义--------------------------------------------*/class node{char Name[15];int OfficePhone;int HomePhone;char MobilePhone[15];char Email[20];node *next;public:node(char *n="a",int o=0,int h=0,char *m="b",char *e="c",node *ne=NULL)//初始化{if(n)strcpy(Name,n);if(m)strcpy(MobilePhone,m);if(e)strcpy(Email,e);next=ne;OfficePhone=o;HomePhone=h;}void show() //显示节点数据{cout<<setw(10)<<Name<<setw(11)<<OfficePhone<<"(O)"<<setw(11)<<HomePhone<<"(H)"<<setw(14)<<Mobile Phone<<"(M)"<<setw(11)<<Email<<endl;}void setName(char *s) //重置Name{strcpy(Name,s);}void setOfficePhone(int x) //重置officephone{OfficePhone=x;}void setHomePhone(int x) //重置homephone{HomePhone=x;}void setMobilePhone(char *s) //重置mobilephone{strcpy(MobilePhone,s);}void setEmail(char *s) //重置Emailail{strcpy(Email,s);}friend class addr;friend istream& operator>>(istream &,node *); //>>的重载};/*------------------------分割线||链表创建--------------------------------------------*/class addr{node *head;node *tail;int t;public:addr(node *h=NULL,node *ta=NULL,int tag=1){head=h;tail=ta;t=tag;}void addtail(node *p)//--------------------------------------------------------------添加到尾部{tail->next=p;tail=p;}void addsort(node *p)//--------------------------------------------------------------按序添加{node *p1,*p2;p1=head;p2=head;if(t==1)//姓名插入{if (head==NULL){head=p;tail=head;}else if (strcmp(p->Name,head->Name)==-1){p->next=head;head=p;}else if(strcmp(p->Name,tail->Name)>=0) {tail->next=p;tail=p;}else{while (strcmp(p2->Name,p->Name)==-1)//<{p1=p2;p2=p2->next;}p->next=p2;p1->next=p;}}if (t==2)//电话插入{if (head==NULL){head=p;tail=head;}else if (p->OfficePhone<=head->OfficePhone){p->next=head;head=p;}else if(p->OfficePhone>=tail->OfficePhone) {tail->next=p;tail=p;}else{while (p->OfficePhone>=p2->OfficePhone)//p2.OfficePhone<p.OfficePhone{p1=p2;p2=p2->next;}p->next=p2;p1->next=p;}}}/*------------------------分割线---------------------------------------------------------*/void sort() //-----------------------------------------------------------------排序{addr s2;s2.settag(t);node *p=head;while(p){node *p1=new node;node *p2;strcpy(p1->Name,p->Name);strcpy(p1->MobilePhone,p->MobilePhone);strcpy(p1->Email,p->Email);p1->OfficePhone=p->OfficePhone;p1->HomePhone=p->HomePhone;s2.addsort(p1);p2=p;p=p->next;delete p2;}s2.DisplayAll();head=s2.head;s2.head=NULL;tail=s2.tail;s2.tail=NULL;}node *lookup(char *na) //--------------------------------------------------------查找{node *p1;p1=head;while (p1){if (strcmp(p1->Name,na)==0) break;else p1=p1->next;}return(p1);}int Delete(char *na) //----------------------------------------------------------删除{node *p1,*p2;p1=lookup(na);if(p1=lookup(na)){p1->show();cout<<"Sure to Delete? [Y//N]"<<endl;char s;cin>>s;if(s=='Y'||s=='y'){if (strcmp(head->Name,na)==0){p1=head;head=head->next;delete p1;return 1;}else{p1=head;p2=head->next;while (strcmp(p2->Name,na)!=0 && p2->next!=NULL){p1=p2;p2=p2->next;}if (p2&&strcmp(p2->Name,na)==0){p1->next=p2->next;delete p2;return 1;}}}}else cout<<"No Record"<<endl;system("pause");return 0;}void DisplayAll() //-----------------------------------------------3 显示全部节点{node *p1=head;int k=0;system("cls");if(p1==NULL) cout<<"No Record"<<endl;while (p1){k++;p1->show();p1=p1->next;if(k%10==0){system("pause");system("cls");}}}void settag(int tag){t=tag;} //重置tagint gettag(){return(t);} //取tagnode *gethead(){return head;} //取headvoid creatlist(char *fileName) //----------------------------------------------文件读>>重载{ifstream in(fileName);if(!in){cout<<"wrong Name"<<endl;}else{ int n;in>>n;for(int i=1;i<=n;i++){node *s=new node;in>>s;addsort(s);}}in.close();}void writelist(char *fileName) //------------------------------------------------------文件写{ofstream out(fileName);if (head){node *p1=head;while (p1!=tail){out<<setw(10)<<p1->Name<<setw(11)<<p1->OfficePhone<<"(O)"<<setw(11)<<p1->HomePhone<<"(H)"<<setw( 14)<<p1->MobilePhone<<"(M)"<<setw(11)<<p1->Email<<endl;p1=p1->next;}out<<setw(10)<<tail->Name<<setw(11)<<tail->OfficePhone<<"(O)"<<setw(11)<<tail->HomePhone<<"(H)"<<setw(14 )<<tail->MobilePhone<<"(M)"<<setw(11)<<tail->Email<<endl;out.close();}}~addr(){node *p1=head;while(head){p1=head;head=head->next;delete p1;;}}};/*------------------------分割线---------------------------------------------------------*/istream& operator>>(istream &in,node *s) //>>的重载{in>>s->Name>>s->OfficePhone>>s->HomePhone>>s->MobilePhone>>s->Email;return in;}addr s1;void add(void)//--------------------------------------------------------------1 添加{node *s=new node;cin>>s;s1.addsort(s);}void del()//-------------------------------------------------------------------2 删除{char Name[15];s1.Delete(Name);cout<<"Success!"<<endl; system("pause");}void find()//------------------------------------------------------------------4 查找{char Name[15];node *p;cin>>Name;p=s1.lookup(Name);if(p)p->show();else cout<<"Sorry,no record!"<<endl;}void modify()//--------------------------------------------------------------------5 修改{char Name[15];node *p;char *ss[5]={"1.Modify Name","2.Modify OfficePhone","3.Modify HomePhone","4.Modify MobilePhone","5.Modify Email"};cin>>Name;p=s1.lookup(Name);if(p){p->show();cout<<"Input your choice:"<<endl;for (int j=0;j<5;j++)cout<<ss[j]<<endl;int i;cout<<"Choice_<1~5>:";cin>>i;switch (i){case 1:char sn[15];cout<<"Input the new Name:";cin>>sn;p->setName(sn);p->show();if (s1.gettag()==1)s1.sort();break;case 2:int so;cout<<"Input the new OfficePhone number:";cin>>so;p->setOfficePhone(so);p->show();if (s1.gettag()==2)s1.sort();break;case 3:int sh;cout<<"Input the new HomePhone number:";cin>>sh;p->setHomePhone(sh);p->show();break;case 4:char sm[15];cout<<"Input the new MobilePhone number:";p->setMobilePhone(sm);p->show();break;case 5:char se[20];cout<<"Input the new Email:";cin>>se;p->setEmail(se);p->show();break;}}else cout<<"Sorry,no record!"<<endl;}void addtxt()//--------------------------------------------------------------6 批量写入{char Name[15];cin>>Name;s1.creatlist(Name);cout<<"Success!"<<endl; system("pause");}void writetxt()//--------------------------------------------------------------7 批量写出{char Name[15];cin>>Name;s1.writelist(Name);cout<<"Success!"<<endl; system("pause");}void sort()//--------------------------------------------------------------8 排序{int k=s1.gettag();char ss;if(k==1){cout<<"Now it is sorted by Name."<<endl<<"Change to sort by OfficePhone numben? [Y//N]"<<endl;cin>>ss;if (ss=='Y'||ss=='y') {s1.settag(2);s1.sort();}}if(k==2){cout<<"Now it is sorted by OfficePhone nember."<<endl<<"Change to sort by Name? [Y//N]"<<endl;cin>>ss;if (ss=='Y'||ss=='y') {s1.settag(1);s1.sort();}}}void quit()//--------------------------------------------------------------9 退出{s1.writelist("data1.txt");s1.writelistbin();cout<<"Thanks For using Address list Manger System."<<endl;system("pause");exit(0);}/*------------------------分割线||菜单定义&&主程序---------------------------------------------------------*/int menu(){char *m[9]={"1.Add Record","2.Delete Record","3.Display All Record","4.Query by Name","5.Modify Record","6.Add from a Text File","7.Write to a Text File","8.Sort","9.Qiut"};int i;char c;do{system("cls"); //清屏cout<<"Welcome TO Address list Manger System"<<endl<<endl<<"====================================="<<endl;for (i=0;i<9;i++) cout<<m[i]<<endl;cout<<"Chioce_(1_8,9):";cin>>c;}while (c<'0'||c>'9');return(c-'0');}void main(){s1.creatlist("data.dat");for(;;){switch(menu()){case 1:cout<<"Please Input: \n<Name> <OfficePhone> <HomePhone> <MobilePhone> <Email>"<<endl;add(); cout<<"Success!"<<endl; system("pause");break;case 2:cout<<"Please Input the Name you'd delete:"<<endl;del();break;case 3:if(s1.gethead()) s1.DisplayAll();else cout<<"No record!"<<endl;system("pause");break;case 4:cout<<"Please Iput the Name you'd find:"<<endl;find();system("pause");break;case 5:cout<<"Please Input the Name you'd change:"<<endl;modify();system("pause");break;case 6:cout<<"Please input the text's Name:";addtxt();break;case 7:cout<<"Please input the text's Name:";writetxt();break;case 8:sort();system("pause");break;case 9:quit();}}}七、数据测试1、输入:1输出:Please Input:<Name> <OfficePhone> <HomePhone> <MobilePhone> <Email> 输入:a 3 3 3 a输出:Success!输入:1输出:Please Input:<Name> <OfficePhone> <HomePhone> <MobilePhone> <Email> 输入:b 1 1 1 b输出:Success!输入:1输出:Please Input:<Name> <OfficePhone> <HomePhone> <MobilePhone> <Email> 输入:c 2 2 2 c输出:Success!2、输入:2输出:Please Input the Name you'd delete:输入:c输出:c 2(O)2(H)2(M) cSure to Delete?[Y//N]输入:Y输出:Completed!输入:2输出:Please Input the Name you'd delete:输入:q输出:No Record!输出:Completed!3、输入:3输出:a 3(O) 3(H) 3(M) ab 1(O) 1(H) 1(M) bc 2(O) 2(H) 2(M) c4、输入:4输出:Please Input the Name you'd find:输入:a输出:a 3(O) 3(H) 3(M) a 输入:4输出:Please Input the Name you'd find:输入:q输出:Sorry,no record!5、输入:5输出:Please Input the Name you'd change:输入:b输出: b 1(O) 1(H) 1(M) b Input your chioce:1、Modify Name2、Modify OfficePhone3、Modify HomePhone4、Modify MobilePhone5、Modify EmailChioce_(1~5):输入:1输出:Input the new name:输入:a1输出: a 3(O) 3(H) 3(M) a a1 1(O) 1(H) 1(M) bc 2(O) 2(H) 2(M) c输入:5输出:Please Input the Name you'd change:输入:a1输出:a1 1(O) 1(H) 1(M) b Input your chioce:1、Modify Name2、Modify OfficePhone3、Modify HomePhone4、Modify MobilePhone5、Modify EmailChioce_(1~5):输入:2输出:Input the new OfficePhone number:输入:4输出:a1 4(O) 1(H) 1(M) b 输入:5输出:Please Input the Name you'd change:输入:a1输出:a1 4(O) 1(H) 1(M) b Input your chioce:6、Modify Name7、Modify OfficePhone8、Modify HomePhone9、Modify MobilePhone10、Modify EmailChioce_(1~5):输入:3输出:Input the new HomePhone number:输入:4输出:a1 4(O) 4(H) 1(M) b 输入:5输出:Please Input the Name you'd change:输入:a1输出:a1 4(O) 1(H) 1(M) b Input your chioce:1、Modify Name2、Modify OfficePhone3、Modify HomePhone4、Modify MobilePhone5、Modify EmailChioce_(1~5):输入:4输出:Input the new HomePhone number:输入:4输出:a1 4(O) 4(H) 4(M) b 输入:5输出:Please Input the Name you'd change:输入:a1输出:a1 4(O) 1(H) 1(M) b Input your chioce:1、Modify Name2、Modify OfficePhone3、Modify HomePhone4、Modify MobilePhone5、Modify EmailChioce_(1~5):输入:5输出:Input the new HomePhone number:输入:a1输出:a1 4(O) 4(H) 1(M) a1 6、输入:6输出:Please input the text's name:输入:data.txt输出:Success!7、输入:7输出:Please input the text's name:输入:data_1.txt输出:Success!8、输入:8输出:Now it is sorted by Name.Change to sort by OfficePhone numben? [Y//N]输入:y输出:c 2(O) 2(H) 2(M) ca 3(O) 3(H) 3(M) aa1 4(O) 4(H) 4(M) a1 输入:8输出:Now it is sorted by OfficePhone nember.Change to sort by Name? [Y//N]输入:y输出:a 3(O) 3(H) 3(M) a a1 4(O) 4(H) 4(M) a1c 2(O) 2(H) 2(M) c9、输入:9输出:Thanks For using Address list Manger System.C++语言课程设计Copyright:010910236丁志伟第15页。