您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 人事档案/员工关系 > 数据结构上机实验报告-通讯录
二O一O年5月26号数据结构上机实验报告通讯录陈冠豪20102105010101015通讯录内容有:姓名、通讯地址、电话号码。要求:(1)建立通信录(2)插入一个人员信息。(3)删除指定的人员信息。(4)按姓名项的字母顺序递增排列;(5)能查找通讯录中某人的信息;实验目的//////////ByLea//////////////////////2010210501//////////////用链式存储结构的通讯录//ByLea.(2010210501)#includestdio.h#includestring.h#includestdlib.h//#includesystem.h#definemaxsize100/*通讯信息的结构类型定义*/typedefstruct{charnum[10];charname[10];charphone[12];charaddres[12];}DataType;structpersons{charnum[10];charname[10];charphone[12];charaddres[12];}psn[maxsize];typedefstructnode{DataTypedata;structnode*next;}ListNode,*LinkList;voidInsertList(LinkListhead){LinkListp;p=(LinkList)malloc(sizeof(ListNode));p-next=NULL;printf(\nInputthenewnode\n);printf(number:\n);gets(p-data.num);printf(name:\n);gets(p-data.name);printf(phone:\n);gets(p-data.phone);printf(addres:\n);gets(p-data.addres);p-next=head-next;head-next=p;}/*查询函数:可以按编号或姓名在链表中查找出对应的记录。*/LinkListSearchList(LinkListhead){LinkListp;charch[10];printf(inputthename:\n);gets(ch);p=head;p=p-next;while(p!=NULL&&strcmp(ch,p-data.num)!=0&&strcmp(ch,p-data.name)!=0)p=p-next;returnp;}/*删除记录:可以按照编号或姓名在链表中顺序查找出对应的记录,然后将其删除。调用查询函数。*/voidDelNode(LinkListhead){charx[10];LinkListp,q;printf(inputthenameyouwanttodelete\n);gets(x);if(head==NULL){printf(thisisaemptylist.);return;}p=head;while(strcmp(x,p-data.num)!=0&&strcmp(x,p-data.name)!=0&&p-next!=NULL){q=p;p=p-next;}if(strcmp(x,p-data.num)==0||strcmp(x,p-data.name)==0){if(p==head)head=p-next;elseq-next=p-next;free(p);}elseprintf(Notfound.);return;}/*建立一个通讯录链表,采用手工输入的方法逐个输入每个同学的信息。*/LinkListCreatList(){charctn;LinkListL,s,r;//inti;/*intj;LinkListt;FILE*fp;*/L=(LinkList)malloc(sizeof(ListNode));L-next=NULL;r=L;while(ctn!='s'){s=(LinkList)malloc(sizeof(ListNode));printf(number:\n);gets(s-data.num);printf(name:\n);gets(s-data.name);printf(phone:\n);gets(s-data.phone);printf(addres:\n);gets(s-data.addres);printf(inputstoexit,anyotherbuttoncontinue!\n);ctn=getchar();r-next=s;r=s;}if(r!=NULL)r-next=NULL;/*fp=fopen(people.txt,w);for(t=L,j=0;t!=NULL;j++,t=t-next){strcpy(psn[j].num,t-data.num);strcpy(psn[j].name,t-data.name);strcpy(psn[j].phone,t-data.phone);strcpy(psn[j].addres,t-data.addres);fwrite(&psn[j],sizeof(structpersons),1,fp);}*//*SaveList(L);*/returnL;}voidorderList(LinkListhead)/*排序*/{LinkListp,q;q=p=head;for(;q-next;q=q-next)while(p-next){chartemp[10];intresult=strcmp(p-data.name,p-next-data.name);if(result){temp[10]=*p-data.name;*p-next-data.name=*p-data.name;*p-data.name=*p-next-data.name;}p=p-next;}printf(orderedthelist\n);}LinkListreadrecord()/*将文件的信息读入结构体数组在转存入链表中*/{intj,i;longk;FILE*fp;LinkListL,r,s;L=(LinkList)malloc(sizeof(ListNode));L-next=NULL;r=L;fp=fopen(people.txt,r+t);if(fp!=NULL){printf(\tOpenSuccess!\n);for(i=1;imaxsize;i++){j=fgetc(fp);if(j==EOF)returnL;k=i-1;fseek(fp,k*sizeof(structpersons),0);/*读取一个人的信息*/fread(&psn[i],sizeof(structpersons),1,fp);s=(LinkList)malloc(sizeof(ListNode));/*装存入链表中*/strcpy(s-data.num,psn[i].num);strcpy(s-data.name,psn[i].name);strcpy(s-data.phone,psn[i].phone);strcpy(s-data.addres,psn[i].addres);/*if(head==NULL)*//*用尾插法将其插入链表中*//*head=s;elser-next=s;r=s;*/r-next=s;r=s;}if(r!=NULL)r-next=NULL;returnL;}else{fp=fopen(people.txt,w);/*不能打开另开辟一个文件*/i=1;}}/*查看全部记录:从第一个结点到最后一个结点依次输出所有记录。*/voidPrintList(LinkListhead){LinkListp;p=head-next;/*使p指向第一个节点*/printf(numbernametelephonemobilephone\n);while(p!=NULL){printf(%s%s%s%s\n,p-data.num,p-data.name,p-data.phone,p-data.addres);p=p-next;}}/*显示菜单函数*/voidMenu(){printf(**************************PHONEBOOKS************************\n);printf(1--Creat2--Listall3--Search4--Insert5--Delete6--reorder0--Exit\n);printf(Pleaseselect(Input0--5)\n);}main(){intt;LinkListp,head;system(cls);head=readrecord();while(1){Menu();scanf(%d,&t);getchar();/*屏蔽回车*/switch(t){case1:printf(CreatthePhoneBooks.\n);head=CreatList();//SaveList(head);break;case2:printf(Listallrecord.\n);PrintList(head);break;case3:printf(Search.\n);p=SearchList(head);if(p){printf(No.NameTelephoneMobilephone\n);printf(%s%s%s%s\n,p-data.num,p-data.name,p-data.phone,p-data.addres);}elseprintf(Nobody.\n);break;case4:printf(Insertnewrecord.\n);InsertList(head);break;case5:printf(Deleterecord.\n);DelNode(head);break;case6:printf(reordingtherecord.\n);orderList(head);break;case0:printf(Exit.\n);return;}}}//////////ByLea//////////////////////2010210501//////////////用顺序存储结构的通讯录//ByLea.(2010210501)#includestdio.h#includestdlib.h#includestring.h#defineFORMAT%s\t\t%s\t\t%s\t%d\t%s\nintcount=0;structAddress{charName[21];intAge;charSex[6];charClassNum[11];charPhonNum[12];};intEnterface();intAdd(Addressbook[]);intModify(Addressbook[]);intDelent(Addressbook[]);intInsert(Addressbook[]);intFind(Addressbook[]);intShow(Addressbook[]);intFindName(Addressbook[]);intFindClassNum(Addressbook[]);intDelName(Addressbook[]);intDelClassNum(Addressbook[]);intFindPhonNum(Addressbook[]);intReadtxt(Addressbook[]);intWritetxt(Addressbook[]);intSearch(Addressbook[],charstr1[],intn);intFSearch(Addressbook[],charstr2[],intn);intTax
本文标题:数据结构上机实验报告-通讯录
链接地址:https://www.777doc.com/doc-4620767 .html