完全自主写的,呵呵,比较简陋,还请各位多多指教
/*********************简 易 通 讯 录******************************/
/*********************程 序 设 计 :张 洋**************************/
/*********************烟台大学计算机学院041-2****************************/
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#include<conio.h>
#define SIZE sizeof(struct person)
#define NULL 0
struct person
{
char name[11];/*姓名*/
char age[3];/*年龄*/
char sex[3];/*性别*/
char phone[21];/*电话号码*/
char address[101];/*地址*/
char email[51];/*电子邮件*/
struct person *next;
};
void inti()/*初始化函数。用来检测数据文件是否存在,如不存在则新建*/
{
FILE *fp;
if(!(fp=fopen("data.dat","rb")))/*如果不存在*/
{
if(!(fp=fopen("data.dat","wb+")));/*建立*/
{
printf("程序初始化失败!\n");
exit(0);
}
fclose(fp);
}
}
void save(struct person *head)/*保存函数。用来将head指向的链表写入数据文件*/
{
FILE *fp;
remove("data.dat");/*删除原数据文件*/
if(!(fp=fopen("data.dat","wb+")))/*建立*/
{
printf("程序错误:无法保存数据!\n");
exit(0);
}
while(head->next)/*把链表存储的数据写入文件*/
{
fwrite(head,SIZE,1,fp);
head=head->next;
}
fwrite(head,SIZE,1,fp);/*解决尾结点问题*/
fclose(fp);
}
void view(struct person *head)/*查看函数。用来查看通讯录中所有的记录*/
{
int i=0;
if(!head)
{
printf("文件里没有任何记录!\n");
return;
}
while(head->next)
{
if(i==2)/*控制两个为一组,按任意键继续*/
{
getch();
i=0;
}
printf("姓名:%s\n",head->name);
printf("年龄:%s\n",head->age);
printf("性别:%s\n",head->sex);
printf("电话号码:%s\n",head->phone);
printf("地址:%s\n",head->address);
printf("E-mail:%s\n",head->email);
printf("\n");
printf("\n");
head=head->next;
i++;
}
printf("姓名:%s\n",head->name);/*解决尾结点问题*/
printf("年龄:%s\n",head->age);
printf("性别:%s\n",head->sex);
printf("电话号码:%s\n",head->phone);
printf("地址:%s\n",head->address);
printf("E-mail:%s\n",head->email);
}
void add()/*建立填加函数,用来建立一个新的链表或向文件追加数据*/
{
FILE *fp;
struct person *head,*p1,*p2;
int n=0;
p1=p2=(struct person *)malloc(SIZE);/*以下是建立链表*/
head=NULL;
printf("请输入(姓名 年龄 性别 电话 地址 E-mail):\n");
scanf("%s %s %s %s %s %s",p1->name,p1->age,p1->sex,p1->phone,p1->address,p1->email);
while(strcmp(p1->name,"0\0"))/*判断name是否为0,是则结束链表,否则继续添加*/
{
n++;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct person *)malloc(SIZE);
printf("请输入(姓名 年龄 性别 电话 地址 E-mail):\n");
scanf("%s %s %s %s %s %s",p1->name,p1->age,p1->sex,p1->phone,p1->address,p1->email);
}
p2->next=NULL;/*到此为止*/
if(!(fp=fopen("data.dat","ab")))/*打开数据文件*/
{
printf("程序错误:无法打开数据文件!\n");
exit(0);
}
while(head->next)/*写文件*/
{
fwrite(head,SIZE,1,fp);
head=head->next;
}
fwrite(head,SIZE,1,fp);
fclose(fp);
}
void search(struct person *head)/*查询函数。用来查询指定姓名的人的资料*/
{
char name[11];/*存储用户输入的姓名*/
printf("请输入姓名进行查找:");
scanf("%s",name);
while(head->next)
{
if(!strcmp(name,head->name))/*比较*/
{
printf("您所要查找的资料为:\n");
printf("姓名:%s\n",head->name);
printf("年龄:%s\n",head->age);
printf("性别:%s\n",head->sex);
printf("电话号码:%s\n",head->phone);
printf("地址:%s\n",head->address);
printf("E-mail:%s\n",head->email);
getch();
return;
}else
{
head=head->next;
}
}
if(strcmp(name,head->name)&&!head->next)
{
printf("对不起!没有找到您所需要的数据。\n");/*如果没有指定的人,则运行这*/
getch();
}else
{
printf("您所要查找的资料为:\n");/*解决尾结点问题*/
printf("姓名:%s\n",head->name);
printf("年龄:%s\n",head->age);
printf("性别:%s\n",head->sex);
printf("电话号码:%s\n",head->phone);
printf("地址:%s\n",head->address);
printf("E-mail:%s\n",head->email);
getch();
}
}
void del(struct person *head,struct person **head1)/*删除函数。用来删除某指定姓名的人的资料*/
{
struct person *p;
char name[11];
char c;
p=head;
printf("请输入要删除人的姓名:");
scanf("%s",name);
while(head->next)/*如果链表不到结尾,则继续*/
{
if(!strcmp(name,head->name))/*如果找到指定的人*/
{
while(p!=head&&p->next!=head)/*p指向要删除的人的前面一个结点*/
{
p=p->next;
}
printf("您确定要删除%s的资料吗?(y/n)",name);/*确认删除*/
c=getchar();
while(c!='y'&&c!='n')
{
c=getchar();
}
if(c=='y')/*以下是删除程序*/
{
if(p==head)
{
*head1=head->next;
free(head);
}
else if(!head->next)/*如果是最后一个结点*/
{
p->next=NULL;
free(head);
}else/*不是最后一个结点*/
{
p->next=head->next;
free(head);
}
printf("删除成功!\n");
getch();
return;
}else
{
return;
}
}else
{
head=head->next;
}
}
if(strcmp(name,head->name)&&!head->next)/*没有找到指定姓名的人*/
{
printf("对不起!没有此人的资料。\n");
getch();
}else
{
while(p!=head&&p->next!=head)/*解决尾结点问题*/
{
p=p->next;
}
printf("您确定要删除%s的资料吗?(y/n)",name);
c=getchar();
while(c!='y'&&c!='n')
{
c=getchar();
}
if(c=='y')
{
if(!head->next)
{
p->next=NULL;
free(head);
}else
{
p->next=head->next;
free(head);
}
printf("删除成功!\n");
getch();
}
}
}
struct person *openfile()/*打开文件,建立链表函数。打开数据文件,读入所有内容并建立链表*/
{
FILE *fp;
struct person *head,*p1,*p2;
int n=0;
p1=p2=(struct person *)malloc(SIZE);/*以下是建立链表程序*/
head=NULL;
if(!(fp=fopen("data.dat","rb")))/*打开文件*/
{
printf("程序错误:无法打开数据文件!\n");
exit(0);
}
fread(p1,SIZE,1,fp);/*读入*/
while(!feof(fp))/*如果不到文件末尾则继续读入*/
{
n++;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct person *)malloc(SIZE);
fread(p1,SIZE,1,fp);
}
p2->next=NULL;/*到此为止*/
fclose(fp);
return(head);/*返回建立链表的头指针*/
}
main()
{
struct person *head;
char flag;/*用来记录用户输入的执行代码*/
inti();
head=openfile();
while(1)/*永真循环实现界面的多次显示*/
{
clrscr();/*清屏*/
printf("\n");/*开始打印界面*/
printf(" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n");
printf("\n");
printf(" 简 易 通 讯 录\n");
printf("\n");
printf(" 程序设计:张洋\n");
printf(" 烟台大学计算机学院041-2\n");
printf(" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n");
printf("\n");
printf("\n");
printf("\n");
printf(" 1.查 看 通 讯 录\n");
printf("\n");
printf(" 2.添 加 记 录\n");
printf("\n");
printf(" 3.按 姓 名 查 找\n");
printf("\n");
printf(" 4.删 除 记 录\n");
printf("\n");
printf(" 5.退 出 程 序\n");
printf("\n");
printf("请输入执行代码:");/*界面打印完成*/
switch(flag=getchar())/*判断输入的执行代码*/
{
case '1' : getchar();view(head);getchar();break;/*查看通讯录,getchar()用来接受回车符,下同*/
case '2' : getchar();add();head=openfile();getchar();break;/*添加记录,并重新读入更新后的数据文件*/
case '3' : getchar();search(head);getchar();break;/*查询*/
case '4' : getchar();del(head,&head);getchar();save(head);break;/*删除并保存入数据文件*/
case '5' : getchar();exit(0);/*结束程序*/
}
}
} |