(全部积分奉上) 第一次写“大”程序花了一个多星期了,但还是这么糟糕,求助高手~~~~~~
这个程序算法应该没什么问题,错误可能是出在一些具体的小地方,哪位高手帮帮小弟吧,分不多但是是我的全部积分,多谢了!!!!!
//一个简单的电子通讯录
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
#include <conio.h>
#define LEN sizeof(struct person)
#define LEN0 sizeof(struct person0)
int j=1;/*结点*/
FILE *fp;
struct person *creat ();/*添加联系人*/
struct person *del_person();/*删除联系人*/
int show_all();/*显示所有联系人*/
struct person *alter();/*修改信息*/
struct person *search();/*查询联系人*/
int array();/*排序*/
int save(struct person *);/*保存数据于磁盘中*/
struct person *move(FILE *);//将文件里的信息转移到链表中去
struct person
{
char name[20];/*姓名*/
char relation[20];/*关系*/
char address[20];/*地址*/
char tel_number[20];/*电话号码*/
char post[20];/*邮编*/
struct person *next;
};
struct person0
{
char name0[20];/*姓名*/
char relation0[20];/*关系*/
char address0[20];/*地址*/
char tel_number0[20];/*电话号码*/
char post0[20];/*邮编*/
}person[100];//结构体数组用于文件读存的中间转换
/******************************************************************************/
int save(struct person *head)/*保存记录*/
{
struct person *p1;
struct person0 *p2;
if((fp=fopen("tel_book.txt","wb"))==NULL)
{printf("cannot open file\n");
exit(0);
}
p1=head;
p2=person;
strcpy(p2->name0,p1->name);
strcpy(p2->relation0,p1->relation);
strcpy(p2->address0,p1->address);
strcpy(p2->tel_number0,p1->tel_number);
strcpy(p2->post0,p1->post);
if(fwrite(p2,LEN0,1,fp)!=1)
printf("file write error\n");
rewind(fp);
free(p1);
return 0;
}
/******************************************************************************/
struct person *move(FILE *fp0)//将文件里的信息转移到链表中去
{
struct person *p1=NULL;
struct person *p2=NULL;
struct person *head=NULL;
struct person0 *p3;
int i=0;
p3=person;
p1=p2=(struct person *)malloc(LEN);
while(1)
{fread(p3+i,LEN0,1,fp0);
if(feof(fp0))break;
strcpy(p1->name,(p3+i)->name0);
strcpy(p1->relation,(p3+i)->relation0);
strcpy(p1->address,(p3+i)->address0);
strcpy(p1->tel_number,(p3+i)->tel_number0);
strcpy(p1->post,(p3+i)->post0);
if(i==0)//首结点
head=p1;
else
{p2->next=p1;
p1=p2;
p1=(struct person *)malloc(LEN);//前插法
}
i++;
j++;
}
p2->next=NULL;
rewind(fp);
free(p1);
return (head);
}
/******************************************************************************/
struct person *creat (void)/*添加联系人*/
{
int n,m=1;
char p;
struct person *p1,*p2;
struct person *head;
struct person0 *p3;
while(m)
{
if((fp=fopen("tel_book.txt","rb"))==NULL)
{printf("you need Create a new document!\n");
fp=fopen("Tel_book.txt","w+");
p1=(struct person *)malloc(LEN);
printf("Now input the inserted record:\n");//输入数据
while(getchar()!='\n');
printf("The name:\n");
scanf("%s",p1->name);
printf("The relation:\n");
scanf("%s",p1->relation);
printf("The address:\n");
scanf("%s",p1->address);
printf("the tel_number:\n");
scanf("%s",p1->tel_number);
printf("The post:\n");
scanf("%s",p1->post);
p1->next=NULL;
head=p1;
j=1;
save(head);//保存信息
printf("你添加的信息保存在tel_book.txt文件中!\n");
}
else/*前插法添加结点*/
{
fp=fopen("tel_book.txt","rb");
p2=(struct person *)malloc(LEN);
printf("Now input the inserted record:\n");/*输入数据*/
while(getchar()!='\n');
printf("Name:\n");
scanf("%s",p1->name);
printf("Relation:\n");
scanf("%s",p1->relation);
printf("Address:\n");
scanf("%s",p1->address);
printf("Tel_number:\n");
scanf("%s",p1->tel_number);
printf("post:\n");
scanf("%s",p1->post);
p2->next=head;/*前插法添加结点*/
head=p2;
j++;
save(head);//保存信息
printf("你添加的信息保存在tel_book.txt文件中!\n");
}
fclose(fp);
printf(" continue add?(y / n)\n");
scanf("%c",&p);
if(p!='y')m=0;
while(getchar()!='\n');
system ("cls");
}
return 0;
}
/******************************************************************************/
struct person *creat (void)/*添加联系人*/
{
int n,m=1;
char p;
struct person *p1,*p2;
struct person *head;
struct person0 *p3;
while(m)
{
if((fp=fopen("tel_book.txt","rb"))==NULL)
{printf("you need Create a new document!\n");
fp=fopen("Tel_book.txt","w+");
p1=(struct person *)malloc(LEN);
printf("Now input the inserted record:\n");//输入数据
while(getchar()!='\n');
printf("The name:\n");
scanf("%s",p1->name);
printf("The relation:\n");
scanf("%s",p1->relation);
printf("The address:\n");
scanf("%s",p1->address);
printf("the tel_number:\n");
scanf("%s",p1->tel_number);
printf("The post:\n");
scanf("%s",p1->post);
p1->next=NULL;
head=p1;
j=1;
save(head);//保存信息
printf("你添加的信息保存在tel_book.txt文件中!\n");
}
else/*前插法添加结点*/
{
fp=fopen("tel_book.txt","rb");
p2=(struct person *)malloc(LEN);
printf("Now input the inserted record:\n");/*输入数据*/
while(getchar()!='\n');
printf("Name:\n");
scanf("%s",p1->name);
printf("Relation:\n");
scanf("%s",p1->relation);
printf("Address:\n");
scanf("%s",p1->address);
printf("Tel_number:\n");
scanf("%s",p1->tel_number);
printf("post:\n");
scanf("%s",p1->post);
p2->next=head;/*前插法添加结点*/
head=p2;
j++;
save(head);//保存信息
printf("你添加的信息保存在tel_book.txt文件中!\n");
}
fclose(fp);
printf(" continue add?(y / n)\n");
scanf("%c",&p);
if(p!='y')m=0;
while(getchar()!='\n');
system ("cls");
}
return 0;
}
/******************************************************************************/
struct person *del_person (struct person *head)/*删除联系人*/
{
struct person *p1,*p2;
struct person0 *p;
char name[20];
int m=1;
char t;
while(m)
{
fp=fopen("tel_book.txt","rb");//打开文件
if(fp==NULL){printf("the list is null\n");//空文件
exit(0);
}
else
{
p=person;
fread(p,LEN0,1,fp);
if(!feof(fp)){printf("tel_book.txt is empty file!\n");
fclose(fp);
return 0;
}
rewind(fp);
head=move(fp);//将磁盘信息转换到链表
p1=head;
printf("\n Please input the Name for deletion:");
scanf("%s",p1->name);
while(strcmp(name,p1->name)!=0&&p1->next!=NULL)//未找到,并且后面还有结点
{
p2=p1;p1=p1->next;
}
if(strcmp(name,p1->name)==0)
{
if(p1==head)head=p1->next;//p1指向首结点,把第二个结点位置赋予head
else
{p2->next=p1->next;//将下一个结点地址赋给前一结点地址
printf("you have deleted:%s",name);
}
j--;
}
else printf("Can't find the record");
save(head);//保存修改后的信息
}
fclose(fp);
printf(" continue delete?(y / n)\n");//是否继续执行本函数
while(getchar()!='\n');
scanf("%c",&t);
if(t!='y')m=0;
return(0);
}
}