- #include "stdio.h"
- typedef struct
- {
- char name[50];
- char address[50];
- char phonenum[20];
- char email[20];
- char remark[50];
- struct mylist *next;
- }mylist;
- void addlist(){
- mylist list;
- FILE *fp;
- printf("请输入信息->>\n\t");
- printf("姓名:");
- scanf("%s",list.name);
- printf("\n\t地址:");
- scanf("%s",list.address);
- printf("\n\t电话号码:");
- scanf("%s",list.phonenum);
- printf("\n\te-mail:");
- scanf("%s",list.email);
- printf("\n\t备注:");
- scanf("%s",list.remark);
- list.next=NULL;
- fp=fopen("MyAddressList","ab+");
-
- fwrite(&list,sizeof(mylist),1,fp);
- fclose(fp);
- }
- mylist *readlist(int j){
- mylist *head=NULL,*r=NULL,*s;
- int i,n;
- char ch;
- long k;
- FILE *fp;
-
- fp=fopen("MyAddressList","rb");
-
- for(i=1;1;i++){
- n=fgetc(fp);
- if(n==EOF)
- break;
- k=i-1;
- fseek(fp,k*sizeof(mylist),SEEK_SET);
- s=malloc(sizeof(mylist));
- fread(s,sizeof(mylist),1,fp);
- if(head==NULL){
- head=s;
- }else
- r->next=s;
- r=s;
- }
-
- if(j==1){
- s=head;
- for(i=0;s->next!=NULL;i++){
- printf("姓名:%s\n地址:%s\n电话号码:%s\nEmail:%s\n备注:%s\n\n",s->name,s->address,s->phonenum,s->email,s->remark);
- s=s->next;
- printf("\n______按任意键继续>\n");
- getch();
- }
- printf("姓名:%s\n地址:%s\n电话号码:%s\nEmail:%s\n备注:%s\n\n",s->name,s->address,s->phonenum,s->email,s->remark);
- printf("\n按任意键继续>\n");
- getch();
- }
- return(head);
- fclose(fp);
-
- }
- void search(){
- mylist listu;
- mylist *p,*coun;
- char ch;
- p=readlist(0);
- coun=p;
- while(1){
- printf("请输入查询条件:\n");
- printf("***********************\n");
- printf("\t1、姓名\n");
- printf("\t2、手机号\n");
- printf("\t3、备注\n");
- printf("\t4、退出\n");
- printf("***********************\n");
- ch=getch();
- switch(ch){
- case '1': p=coun;
- printf("请输入联系人名字:");
- scanf("%s",listu.name);
- for(;p->next!=NULL;p=p->next){
- if(strcmp(listu.name,p->name)==0){
- printf("姓名:%s\n地址:%s\n电话号码:%s\nEmail:%s\n备注:%s\n\n",p->name,p->address,p->phonenum,p->email,p->remark);
- }
- }
- if(strcmp(listu.name,p->name)==0){
- printf("姓名:%s\n地址:%s\n电话号码:%s\nEmail:%s\n备注:%s\n\n",p->name,p->address,p->phonenum,p->email,p->remark);
- }
- break;
- case '2': p=coun;
- printf("请输入联系人手机号:");
- scanf("%s",listu.phonenum);
- for(;p->next!=NULL;p=p->next){
- if(strcmp(listu.phonenum,p->phonenum)==0){
- printf("姓名:%s\n地址:%s\n电话号码:%s\nEmail:%s\n备注:%s\n\n",p->name,p->address,p->phonenum,p->email,p->remark);
- }
- }
- if(strcmp(listu.phonenum,p->phonenum)==0){
- printf("姓名:%s\n地址:%s\n电话号码:%s\nEmail:%s\n备注:%s\n\n",p->name,p->address,p->phonenum,p->email,p->remark);
- }
- break;
- case '3': p=coun;
- printf("请输入联系人备注信息:");
- scanf("%s",listu.remark);
- for(;p->next!=NULL;p=p->next){
- if(strcmp(listu.remark,p->remark)==0){
- printf("姓名:%s\n地址:%s\n电话号码:%s\nEmail:%s\n备注:%s\n\n",p->name,p->address,p->phonenum,p->email,p->remark);
- }
- }
- if(strcmp(listu.remark,p->remark)==0){
- printf("姓名:%s\n地址:%s\n电话号码:%s\nEmail:%s\n备注:%s\n\n",p->name,p->address,p->phonenum,p->email,p->remark);
- }
- break;
- case '4':return;
-
- default:printf("请输入数字(1or2or3)\n");
- continue;
- }
- }
-
- }
- void dellist(){
- FILE *fp;
- char nameu[10];
- int i=0;
- mylist *head,*r,*p,*s;
- head=readlist(0);
- r=head;
-
- if(head==NULL)
- printf("通讯录为空!");
- else{
- printf("请输入要删除的联系人名字:");
- scanf("%s",nameu);
-
- r=head;
- if(strcmp(r->name,nameu)==0)
- { r=r->next;
- head=r;
- }
- else
- {
- p=r->next;
- while(strcmp(p->name,nameu)!=0){
- r=r->next;
- p=r->next;
- }
- s=r->next;
- r->next=s->next;
- free(s);
- }
- p=r=head;
- if(strcmp(p->name,nameu)==0)
- r->next=NULL;
- fp=fopen("MyAddressList","wb+");
- r=head;
- fwrite(r,sizeof(mylist),1,fp);
- r=r->next;
- while(r!=NULL){
- fwrite(r,sizeof(mylist),1,fp);
- r=r->next;
- }
-
- fclose(fp);
- }
-
- }
- int chlist(){
- mylist *head,*p;
- FILE *fp;
- char ch[10];
- head=readlist(0);
- p=head;
- printf("请输入要修改人的姓名:");
- scanf("%s",ch);
- for(;head!=NULL;head=head->next){
- if(strcmp(head->name,ch)==0){
- printf("姓名:%s\n地址:%s\n电话号码:%s\nEmail:%s\n备注:%s\n\n",head->name,head->address,head->phonenum,head->email,head->remark);
- printf("请输入修改后信息..........\n");
- printf("\n\t姓名:");
- scanf("%s",head->name);
- printf("\n\t地址:");
- scanf("%s",head->address);
- printf("\n\t电话号码:");
- scanf("%s",head->phonenum);
- printf("\n\te-mail:");
- scanf("%s",head->email);
- printf("\n\t备注:");
- scanf("%s",head->remark);
- fp=fopen("MyAddressList","wb+");
-
-
- while(p!=NULL){
- fwrite(p,sizeof(mylist),1,fp);
- p=p->next;
- }
- fclose(fp);
- return(1);
- }
-
- }
- }
- void main(){
- char ch;
- int i;
- while(1){
- printf("*********我的通讯录************\n");
- printf("\t1、添加联系人\n");
- printf("\t2、删除联系人\n");
- printf("\t3、查找联系人\n");
- printf("\t4、更改联系人信息\n");
- printf("\t5、显示所有联系人\n");
- printf("\t6、退出\n");
- printf("*********我的通讯录************\n");
- ch=getch();
- switch(ch){
- case '1':
- addlist();
- continue;
- case '2':
- dellist();
- continue;
- case '3':
- search();
- continue;
- case '4':
- i=chlist();
- if(i)
- printf("修改成功\n");
- else
- printf("修改失败\n");
- continue;
- case '5':
- readlist(1);
- continue;
- case '6':
- exit(0);
- }
- }
- }
复制代码 |
|