#include<stdio.h>
#include<stdlib.h>
#include <conio.h>
#include<string.h>
struct pep
{
char num[20]; char name[30]; char dizi[40]; struct pep *next; }people;
FILE *fp;
char name[30];
struct pep * p1,* p2,* head,*wei; void addfile()
{
system("cls"); int shu=0; char jx='y'; struct pep *temp; p2=temp=NULL; while((jx!='n')&&(jx!='N')) { p1=(struct pep *)malloc(sizeof(struct pep)); if(shu++==0) wei=p1; printf("\n请输入联系人姓名:"); gets(p1->name); printf("请输入联系人号码:"); gets(p1->num); printf("请输入联系人住址:"); gets(p1->dizi); printf("\t是否继续(y/n):"); jx=getch(); while(jx!='y'&&jx!='n') { printf("\t是否继续(y/n):"); } jx=getchar(); temp=p1; p1->next=p2; p2=temp; } printf("\n是否保存(y/n):");
} jx=getch(); if((jx!='n')&&(jx!='N')) { wei->next=head; head=temp; }
void sanchu(struct pep *p )
{
p2=head; if(p==p2) head=p2->next; else while(p2->next!=p&&p2->next!=NULL) p2=p2->next; if(p2->next!=NULL) { } p2->next=p->next; printf("记录删除成功按任意键返回"); getch();
}
void xiugai(struct pep *p)
{
system("cls");
printf("请输入联系人姓名:");
} scanf("%s",p->name); printf("请输入联系人号码:"); scanf("%s",p->num); printf("请输入联系人住址:"); scanf("%s",p->dizi); printf("记录修改成功按任意键返回"); getch();
void findname()
{
int i=0,j=0,n; struct pep *name[100]; system("cls"); char cazao[20],jx; p1=p2=head; printf("输入查找人姓名:"); scanf("%s",cazao); while(p1!=NULL)
{ } if(strstr(p1->name,cazao)!=NULL) { printf("%d\n",++j); printf("姓名:%s\n",p1->name); } else p1=p1->next; printf("号码:%s\n",p1->num); printf("地址:%s\n",p1->dizi); printf("\n"); name[i++]=p1; p1=p1->next; if(i==0) { printf("没有找到是否重新输入(y/n);"); getchar(); jx=getchar(); if(jx!='n'&&jx!='N') findname(); } else { while(1) { printf("选择联系人:"); scanf("%d",&n); if(n<=j) break; } n=n-1; printf("\n选择操作\t1.删除\t2.修改\t3.退出\n"); getchar(); jx=getch(); switch(jx) { case '1': sanchu(name[n]);break; case '2': xiugai(name[n]);break; case '3': return;break; } } }
void findnum()
{
int i=0,j=0,n; p1=p2=head; char h[20],jx; struct pep *num[100]; system("cls"); printf("请输入要查找的号码:"); scanf("%s",h); while(p1!=NULL) { if(strstr(p1->num,h)!=NULL) { } else printf("%d\n",++j); printf("姓名:%s\n",p1->name); printf("号码:%s\n",p1->num); printf("地址:%s\n",p1->dizi); printf("\n"); num[i++]=p1; p1=p1->next; p1=p1->next; } if(i==0) { printf("没有找到是否重新输入(y/n);"); } else { while(1) { } n=n-1; printf("\n选择操作\t1.删除\t2.修改\t3.退出\n"); getchar(); jx=getch(); printf("选择联系人:"); scanf("%d",&n); if(n<=j) break; getchar(); jx=getchar(); if(jx!='n'&&jx!='N') findnum();
} switch(jx) { case '1': sanchu(num[n]);break; case '2': xiugai(num[n]);break; case '3': return;break; } }
void find() {
} char xz; system("cls"); while(1) { printf("\n\n\n\n\t 微型通讯录程序"); printf("\n\t printf("\n\t printf("\n\t printf("\n\t *********************************************************"); ************************查找菜单*************************"); ** 1.按姓名 2.按号码 **"); ** 3.返回主菜单 **"); printf("\n\t *********************************************************"); printf("\n\t *********************************************************"); printf("\n\t 选择:"); xz=getch(); switch(xz) { case '1': findname();break; case '2': findnum();break; case '3': return;break; } system("cls"); }
void xianshi() {
char sd; struct pep a,*jl[100]; int i,j,n; p1=p2=head; if(p1!=NULL) for(;;p1=p1->next) { for(p2=p1;;p2=p2->next) { if(strcmp(p1->name,p2->name)>0)
} { strcpy(a.num,p1->num); strcpy(a.dizi,p1->dizi); strcpy(a.name,p1->name); strcpy(p1->num,p2->num); strcpy(p1->dizi,p2->dizi); strcpy(p1->name,p2->name); strcpy(p2->num,a.num); strcpy(p2->dizi,a.dizi); strcpy(p2->name,a.name); } if(p2->next==NULL) break; if(p1->next==NULL) break; } p1=head; j=i=0; while(p1!=NULL) { if(i==5||i==0) { i=0; jl[j++]=p1; } i++; p1=p1->next; } n=j; system("cls"); char df; p1=head; j=i=0; if(p1!=NULL) while(1) { i++; printf("姓名:%s\n",p1->name); printf("号码:%s\n",p1->num); printf("地址:%s\n",p1->dizi); printf("\n"); p1=p1->next; if(i==5||p1==NULL)
{ } p1=jl[j]; system("cls"); i=0; printf("\t\t%d\n",j+1); if(p1==NULL) printf("已到最后一页\n"); printf("\t\t\t\t\t\t按(j/k)键翻页\n\t\t\t\t\t\t按s删除全部"); df=getch(); if(df=='j') { j--; if(j==-1) j=0; } else if(df=='k') { } { } else break; system("cls"); printf("真的要删除吗?(y/n)"); df=getch(); while(df!='y'&&df!='n') { df=getch(); printf("真的要删除吗?(y/n)"); j++; if(j==n) j=n-1; else if(df=='s') } sd=df; if(sd=='y') { } else return; head=NULL; return;
} } else { printf("没有记录按任意键返回"); df=getch(); }
void ex() {
if ((fp=fopen(name,"wb"))==NULL) { printf("\n\t\t打开失败"); exit(0); } p1=head; while(p1!=NULL) { strcpy(people.dizi,p1->dizi); strcpy(people.name,p1->name); strcpy(people.num,p1->num); if(fwrite(&people,sizeof(struct pep),1,fp)!=1) { } printf("文件写入错误"); exit(0); p1=p1->next; } printf("\n\n\n\t\t保存成功"); getch(); exit(0); }
void main() {
char xz; printf("\n请输入通讯录文件名:"); scanf("%s",name); getchar(); p2=head=NULL; if((fp=fopen(name,"rb"))==NULL) { printf("\n通讯录文件不存在,新建通讯录文件"); p1=(struct pep *)malloc(sizeof(struct pep)); if ((fp=fopen(name,"wb"))==NULL) {
printf("\n\t\t建立失败"); exit(0); } } else { } while(!feof(fp)) { } if(fread(&people,sizeof(struct pep),1,fp)==1) { } p1=(struct pep *)malloc(sizeof(struct pep)); strcpy(p1->dizi,people.dizi); strcpy(p1->name,people.name); strcpy(p1->num,people.num); head=p1; p1->next=p2; p2=head; printf("\n通讯录文件已建立"); printf("\n按任意键进入主菜单"); getch(); fclose(fp); system("cls"); while(1) { printf("\n\n\n\n\t 微型通讯录程序"); printf("\n\t *********************************************************"); printf("\n\t *************************主菜单**************************"); printf("\n\t ** 1.添加 2.查找 **"); printf("\n\t ** 3.显示记录 4.保存并退出 **"); printf("\n\t *********************************************************"); printf("\n\t *********************************************************"); printf("\n\t 选择:"); xz=getch(); switch(xz) { case '1': addfile();break; case '2': find();break; case '3': xianshi();break; case '4': ex();break; } system("cls");
} }