我发现,失恋的好处就是,作业终于能自己写完了。。。好吧,写得很烂,很不精简,将就着看吧。
test3.cpp
- #include<cstdio>
- #include<cstring>
- #include<cstdlib>
- typedef int status;
- #define TURE 1
- #define FALSE 0
- #define OK 1
- #define ERROR 0
- #define OVERFLOW -2
- #define INFEASIBLE -1
- #define MAXSIZE 100
- typedef struct{
- char name[20],tel[20],email[20];
- }elemtype;
- typedef struct{
- elemtype *seq;
- int lenght;
- }Seqlist;
- struct node{
- elemtype date;
- struct node *next = NULL;
- };
- #include”test3_link.h”
- #include”test3_Seq.h”
- int main()
- {
- int option;
- status sta;
- Seqlist L;
- L.lenght = 0;
- struct node link;
- struct node* s;
- InitAddressBook(L);//初始化通讯录
- InitRecover(link);//初始化回收站
- while(1)
- {
- printf(“\n==============通讯录基本操作菜单==============\n0.退出程序\t\t1.显示联系人\n2.插入联系人\t\t3.删除联系人\n4.恢复联系人\t\t5.修改联系人\n6.查找通讯录\t\t7.显示回收站\n8.查找回收站\t\t9.彻底删除联系人\n10.清空回收站\n\n你选择的功能是:”);
- scanf(“%d”,&option);
- if(option == 0) break;
- switch (option)
- {
- case 1:
- BrowseAddressBook(L);
- break;
- case 2:
- sta = InsertAddressBook(L);
- if(sta > 0) printf(“插入成功!\n”);
- else printf(“插入失败!\n”);
- break;
- case 3:
- sta = DeleteAddressBook(L,link);
- if(sta > 0) printf(“删除成功,你可以在回收站中恢复该联系人\n”);
- else printf(“删除失败!\n”);
- break;
- case 4:
- sta = RecoveryRecover(L,link);
- if(sta > 0) printf(“恢复成功!\n”);
- else printf(“无法恢复!\n”);
- break;
- case 5:
- sta = ChangeAddressBook(L);
- if(sta > 0) printf(“修改成功!\n”);
- else printf(“修改失败!\n”);
- break;
- case 6:
- SearchAddressBook(L);
- break;
- case 7:
- BrowseRecover(link);
- break;
- case 8:
- sta = SearchRecover(link,s);
- if(sta > 0) printf(“%s\t\t%s\t\t%s\n查找完成!\n”,s->date.name,s->date.tel,s->date.email);
- else printf(“查找无法完成!\n”);
- break;
- case 9:
- //struct node* s;
- sta = SearchRecover(link,s);
- if(sta > 0)
- {
- sta = DeleteRecover(link,s);
- //printf(“1”);
- if(sta > 0) printf(“小李飞刀成绝响,人世不见楚留香,彻底删除完成!\n”);
- }
- if(sta <= 0) printf(“彻底删除无法完成!\n”);
- break;
- case 10:
- EmptyRecover(link);
- break;
- default:
- printf(“你的输入不合法,请重新输入!\n”);
- break;
- }
- }
- SaveAddressBook(L);//保存通讯录到本地文件
- SaveLinkList(link);//保存回收站到本地文件
- }
test3_Seq.h
- status InitAddressBook(Seqlist &L)
- {
- L.seq = (elemtype *)malloc(sizeof(elemtype)*MAXSIZE);
- FILE *fp;
- fp=fopen(“AddressBook.txt”,“r”);
- if (fp==NULL)
- {
- return INFEASIBLE;
- }
- elemtype x;
- //printf(“%d\n”,L.lenght);
- while(!feof(fp))
- {
- fscanf(fp,“%s %s %s”,x.name,x.tel,x.email);
- L.seq[L.lenght++] = x;
- //printf(“%d\n”,L.lenght);
- }
- fclose(fp);
- return OK;
- }
- status BrowseAddressBook(Seqlist L)
- {
- if(L.lenght==0)
- {
- printf(“通讯录内容为空!”);
- return ERROR;
- }
- else printf(“Num\t\tName\t\tTel\t\t\tEmail\n”);
- for(int i = 0;i<L.lenght;i++)
- {
- printf(“%d\t\t%s\t\t%s\t\t%s\n”,i,L.seq[i].name,L.seq[i].tel,L.seq[i].email);
- }
- return OK;
- }
- status InsertAddressBook(Seqlist &L)
- {
- int P,i;
- printf(“请输入你要插入的位置:”);
- scanf(“%d”,&P);
- if ( L.lenght == MAXSIZE-1) {
- printf(“表满,”);
- return OVERFLOW;
- }
- if ( P<0 || P>L.lenght ) {
- printf(“位置不合法,”);
- return INFEASIBLE;
- }
- printf(“请输入你要插入的信息:”);
- elemtype x;
- scanf(“%s %s %s”,x.name,x.tel,x.email);
- for( i=L.lenght-1; i>=P; i– )
- L.seq[i+1] = L.seq[i];
- L.seq[P] = x;
- L.lenght++;
- return OK;
- }
- status DeleteAddressBook(Seqlist &L,node &link)
- {
- int P,i;
- printf(“请输入你要删除的位置:”);
- scanf(“%d”,&P);
- if ( P<0 || P>=L.lenght ){
- printf(“位置不合法,”);
- return INFEASIBLE;
- }
- elemtype x;
- x = L.seq[P];
- InsertRecover(link,x);
- for(i = P;i < L.lenght-1;i++)
- {
- L.seq[i] = L.seq[i+1];
- }
- L.lenght–;
- return OK;
- }
- status ChangeAddressBook(Seqlist &L)
- {
- int P,i;
- printf(“请输入你要修改的位置:”);
- scanf(“%d”,&P);
- if ( P<0 || P>=L.lenght ){
- printf(“位置不合法,”);
- return INFEASIBLE;
- }
- else
- {
- printf(“0.NAME 1.TEL 2.EMAIL\n请输入你要修改的信息选项:”);
- scanf(“%d”,&i);
- //getchar();
- switch (i)
- {
- case 0:
- scanf(“%s”,L.seq[P].name);
- break;
- case 1:
- scanf(“%s”,L.seq[P].tel);
- break;
- case 2:
- scanf(“%s”,L.seq[P].tel);
- break;
- default:
- printf(“没有该选项,”);
- return INFEASIBLE;
- }
- //printf(“%s %s %s”,L.seq[P].name,L.seq[P].tel,L.seq[P].email);
- return OK;
- }
- }
- status SearchAddressBook(Seqlist &L)
- {
- char name[20];
- if(L.lenght==0)
- {
- printf(“通讯录内容为空!”);
- return ERROR;
- }
- else
- {
- printf(“请输入你要查询的名字:”);
- scanf(“%s”,name);
- for(int i = 0;i<L.lenght;i++)
- {
- if(strcmp(name,L.seq[i].name)==0)
- {
- printf(“%d\t\t%s\t\t%s\t\t%s\n查找完成!\n”,i,L.seq[i].name,L.seq[i].tel,L.seq[i].email);
- return OK;
- }
- }
- printf(“通讯录未录入该姓名”);
- return FALSE;
- }
- }
- void SaveAddressBook(Seqlist L)
- {
- FILE *fp;
- fp=fopen(“AddressBook.txt”,“w”);
- if (fp==NULL)
- {
- exit(1);
- }
- for (int i=0;i<L.lenght-1;i++)
- fprintf(fp,“%s %s %s\n”,L.seq[i].name,L.seq[i].tel,L.seq[i].email);
- fprintf(fp,”%s %s %s”,L.seq[L.lenght-1].name,L.seq[L.lenght-1].tel,L.seq[L.lenght-1].email);
- fclose(fp);
- }
test3_link.h
- status InitRecover(node &link)
- {
- FILE *fp;
- fp=fopen(“Recover.txt”,“r”);
- if (fp==NULL)
- {
- return ERROR;
- }
- elemtype x;
- struct node * temp=NULL;
- while(!feof(fp))
- {
- if (fscanf(fp,“%s %s %s”,x.name,x.tel,x.email)!=EOF)
- {
- temp = (node *)malloc(sizeof(node));
- temp->date = x;
- temp->next = link.next;
- link.next = temp;
- //printf(“%s %s %s\n”,x.name,x.tel,x.email);
- //printf(“%s %s %s\n”,temp->date.name,temp->date.tel,temp->date.email);
- }
- }
- fclose(fp);
- /*struct node * test = link.next;
- while(test != NULL)
- {
- printf(“%s %s %s\n”,test->date.name,test->date.tel,test->date.email);
- test = test->next;
- }*/
- }
- status InsertRecover(node &link,elemtype x)
- {
- //printf(“%s %s %s\n”,x.name,x.tel,x.email);
- struct node *add=NULL;
- add = (node *)malloc(sizeof(node));
- add->date = x;
- add->next = link.next;
- link.next = add;
- //printf(“%s %s %s\n”,add->date.name,add->date.tel,add->date.email);
- }
- status BrowseRecover(node &link)
- {
- if(link.next==NULL)
- {
- printf(“查询的内容为空!”);
- return ERROR;
- }
- else printf(“Name\t\tTel\t\t\tEmail\n”);
- struct node * test = link.next;
- while(test != NULL)
- {
- printf(“%s\t\t%s\t\t%s\n”,test->date.name,test->date.tel,test->date.email);
- test = test->next;
- }
- return OK;
- }
- status SearchRecover(node link,struct node* &s)
- {
- s = link.next;
- if(s == NULL)
- {
- printf(“回收站为空,”);
- return FALSE;
- }
- printf(“请输入联系人姓名:”);
- char name[20];
- scanf(“%s”,name);
- //printf(“%s”,name);
- while(s!=NULL)
- {
- if(strcmp(name,s->date.name)==0)return TURE;
- else s = s->next;
- }
- printf(“回收站未收录该姓名,”);
- return FALSE;
- }
- DeleteRecover(struct node &link,struct node* &s)
- {
- node *temp = &link;
- if(link.next == s)
- {
- link.next = s->next;
- free(s);
- return OK;
- }
- //printf(“%s %s %s”,s->next->date.name,s->next->date.tel,s->next->date.email);
- while(temp->next!=s)
- {
- temp = temp->next;
- }
- temp->next = s->next;
- free(s);
- return OK;
- }
- status RecoveryRecover(Seqlist &L,node &link)
- {
- if(L.lenght>MAXSIZE)
- {
- printf(“通讯录已满,”);
- return OVERFLOW;
- }
- node* s;
- status sta = 0;
- sta = SearchRecover(link,s);
- if(sta < 1) return ERROR;
- else
- {
- L.seq[L.lenght] = s->date;
- L.lenght++;
- DeleteRecover(link,s);
- }
- return OK;
- }
- status EmptyRecover(node &link)
- {
- node *temp = NULL;
- while(link.next!=NULL)
- {
- temp = link.next;
- link.next = temp->next;
- free(temp);
- }
- printf(“回收站已清空!\n”);
- return OK;
- }
- void SaveLinkList(node link)
- {
- FILE *fp;
- fp=fopen(“Recover.txt”,“w”);
- if (fp==NULL)
- {
- exit(1);
- }
- node *p=link.next;
- while(p->next)
- {
- fprintf(fp,“%s %s %s\n”,p->date.name,p->date.tel,p->date.email);
- p=p->next;
- }
- fprintf(fp,”%s %s %s”,p->date.name,p->date.tel,p->date.email);
- fclose(fp) ;
- }