(全部积分奉上) 第一次写“大”程序花了一个多星期了,但还是这么糟糕,求助高手~~~~~~

sj307639429 2008-06-19 08:38:34
这个程序算法应该没什么问题,错误可能是出在一些具体的小地方,哪位高手帮帮小弟吧,分不多但是是我的全部积分,多谢了!!!!!




//一个简单的电子通讯录
#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);
}
}
...全文
204 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
windriverer 2008-06-20
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 ddddisadog 的回复:]
问个白痴点的问题!这么长的程序是怎么写出来的呢?望赐教!谢谢!我也想写!就是不知道从哪下手啊!郁闷!
[/Quote]
先分析需求 分模块 定义函数
笼统的说就是先设计后编码

sj307639429 2008-06-19
  • 打赏
  • 举报
回复
学c一段时间了,只是想搞个东西做下,看下自己学的怎么样了,呵呵~~~~~~
ddddisadog 2008-06-19
  • 打赏
  • 举报
回复
问个白痴点的问题!这么长的程序是怎么写出来的呢?望赐教!谢谢!我也想写!就是不知道从哪下手啊!郁闷!
sj307639429 2008-06-19
  • 打赏
  • 举报
回复
定义person0是为了把数据写入磁盘,没有必要用么??
sj307639429 2008-06-19
  • 打赏
  • 举报
回复
多谢多谢~~~~大家说的真精辟啊
program2050 2008-06-19
  • 打赏
  • 举报
回复
学习




!!
K行天下 2008-06-19
  • 打赏
  • 举报
回复
还有,你的添加联系人程序也有问题,
if((fp=fopen("tel_book.txt","rb"))==NULL)
{
。。。
}
else/*前插法添加结点*/
{
fp=fopen("tel_book.txt","rb"); // 这里又打开了一次,既然是else,表示文件已经代开了,应该删去
baihacker 2008-06-19
  • 打赏
  • 举报
回复
#define LEN sizeof(struct person)
#define LEN0 sizeof(struct person0)

命名不懂

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];//结构体数组用于文件读存的中间转换

一般认为姓名这些是不必要的注释
另外person0的命名也...
另外定义person0也是多余的

{printf("cannot open file\n");
exit(0);
}
格式要一致



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);

如果不管next的话是可以直接用=进行赋值的,因为里面是数组类型
如果要考虑next,写个赋值函数

.......
其它的不多说了.
K行天下 2008-06-19
  • 打赏
  • 举报
回复
然后就是 主菜单只能执行一个操作,这样不行,应该采取一个循环, 当不输入0(退出)时,可以继续执行其它操作!

把主菜单写为一个函数:

void showMenu()
{
printf("***************************************************************\n");
printf("***************************************************************\n");
printf("* *\n");
printf("* 迷你通讯录 *\n");
printf("*-------------------------------------------------------------*\n");
printf("* 1.添加联系人 4.修改信息 *\n");
printf("* 2.删除联系人 5.查询联系人 *\n");
printf("* 3.显示所有记录 0.关闭通讯录 *\n");
printf("*-------------------------------------------------------------*\n");
printf("* ----- 苏俊制作*\n");
printf("***************************************************************\n");
printf("***************************************************************\n");
printf("请输入一个数字(0~5) \n");
}

int main()
{
int m = 1;
struct person *head=NULL;
while(m)
{
//system ("cls");
showMenu();
scanf("%d",&m);
system ("cls");
switch(m)
{
case 1:
{
head=creat();/*执行添加函数*/
array();
}/*添加完成后排序*/
break;
case 2:
{
head=move(fp);//读取文件
head=del_person(head);/*执行删除函数*/
array();
}/*执行完成后排序*/
break;
case 3:show_all();/*执行显示所有函数*/
break;
case 4:{alter(head);/*执行修改信息函数*/
array();}/*修改信息后排序*/
break;
case 5:
search(head);/*执行查询函数*/
break;
case 0:
break;/*关闭通讯录*/
defalt:
printf("Your choice is wrong,please choose one more time\n");
}
}

//getch();
}
9527他大爷 2008-06-19
  • 打赏
  • 举报
回复
好大的程序啊,眼睛都看花了
K行天下 2008-06-19
  • 打赏
  • 举报
回复
void search(struct person *head)这里也要改

另外,要添加装入链表的程序,如果文件中有通讯录,运行程序要将通讯录装入内存, 这样选择5就可以查询通讯录了!
sj307639429 2008-06-19
  • 打赏
  • 举报
回复
飞雪再给点意见啊~~~~~~~
sj307639429 2008-06-19
  • 打赏
  • 举报
回复
对对
那里有个错误
多谢飞雪~~~~~~~~
baihacker 2008-06-19
  • 打赏
  • 举报
回复
/******************************************************************************/ 
void alter(struct person *head)/*修改信息*/

返回类型改成void
sj307639429 2008-06-19
  • 打赏
  • 举报
回复
哦!!不好意思 crate函数写重了
sj307639429 2008-06-19
  • 打赏
  • 举报
回复
只有400多行哦
conio.h 没有也没关系吧
我的程序错误主要是处在 对于内存调用方面
fallening 2008-06-19
  • 打赏
  • 举报
回复
很想帮你,但是俺的系统里边没有conio.h 这个文件…………
airhiphop 2008-06-19
  • 打赏
  • 举报
回复
比我那个1千行的程序还牛X,太长了。怕看
sj307639429 2008-06-19
  • 打赏
  • 举报
回复
/******************************************************************************/
struct person *alter(struct person *head)/*修改信息*/
{
struct person *p1,*p2;
p1=head;
int m=1;
char b;
char name[20];
printf("please input the name that you want to change:\n");
scanf("%s",p1->name);
if(head==NULL)
{
printf("list null!\n");
}
while(strcmp(p1->name,name)!=0&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(strcmp(p1->name,name)==0)
{
printf("*****************************\n");
printf("the name:%s\n",p1->name);
printf("the relation:%s\n",p1-> relation);
printf("the address:%s\n",p1->address);
printf("the tel_number:%s\n",p1-> tel_number);
printf("the post:%s\n",p1-> post);

printf("*****************************\n");
while(1)
{
printf("*********************************\n");
printf("** please choose the option **\n");
printf("** 1-change the name **\n");
printf("** 2-change the relation **\n");
printf("** 3-change the address **\n");
printf("** 4-change the tel_number **\n");
printf("** 5-change the post **\n");

printf("** 6-exit **\n");
printf("*********************************\n");
scanf("%c",&b);
switch(b)
{
case '1': printf("NOW yuo will change the name:\n");
scanf("%s",p1->name);
break;
case '2':printf("NOW you will change the relation :\n");
scanf("%s",p1->relation);
break;
case '3':printf("NOW you will change the address:\n");
scanf("%s",p1->address);
break;
case '4':printf("NOW you will change the tel_number :\n");
scanf("%s",p1->tel_number);
break;
case '5':printf("NOW you will change the post :\n");
scanf("%s",p1->post);
break;

case'6':break;
default:printf("Error\n");
}
printf(" continue add?(y / n)\n");
while(getchar()!='\n');
scanf("%c",&b);
if(b!='y')m=0;
while(getchar()!='\n');
system ("cls");
}
}
}
/******************************************************************************/
struct person *search(struct person *head)/*查询联系人*/
{
struct person *p1;
char name[20];
printf("please input the name whose you want to see:\n");
gets(name);
if(head==NULL)
{
printf("list is null!\n");
}
p1=head;
for(;p1!=NULL;p1=p1->next)
{
if(strcmp(p1->name,name)==0)
{printf("*****************************\n");
printf("the name:%s\n",p1->name);
printf("the relation :%s\n",p1->relation );
printf("the address:%s\n",p1->address);
printf("the tel_number:%s\n",p1->tel_number);
printf("the post:%s\n",p1->post);
printf("*****************************\n");
}
else
printf("Don’t have the person!!!\n");
}
return(0);
}
/******************************************************************************/
int array()/*排序*/
{
struct person *head;
struct person *p1,*p2;
char name[20];
char relation[20];
char address[20];
char tel_number[20];
char post[20];
head=move(fp);
for(p1=head;p1!=NULL;p1=p1->next)
{
for(p2=p1->next;p2!=NULL;p2=p2->next)
{
if((strcmp(p1->name,p2->name))>0)
{
strcpy(name,p1->name);
strcpy(relation,p1-> relation);
strcpy(address,p1-> address);
strcpy(tel_number,p1-> tel_number);
strcpy(post,p1->post);
strcpy(p1->name,p2->name);
strcpy(p1-> relation,p2-> relation);
strcpy(p1-> address,p2-> address);
strcpy(p1-> tel_number,p2-> tel_number);
strcpy(p1-> post,p2-> post);
strcpy(p2->name,name);
strcpy(p2-> relation, relation);
strcpy(p2-> address, address);
strcpy(p2-> tel_number, tel_number);
strcpy(p2-> post, post);
}
}
}
save(head);/*排序后保存信息于磁盘中*/
return(0);
}
/******************************************************************************/
int show_all()/*显示所有联系人*/
{
struct person *head;
struct person *p1;
struct person0 *p;
p=person;
int i=0;
p1=head;
fp=fopen("tel_book.txt","rb");
if(fp==NULL){printf("the file is null\n");
exit(0);
}
else
{for(;i<j;i++)
{
fread((p+i),LEN0,1,fp);
printf(" The name is: %s \n",(p+i)->name0);
printf(" The relation is: %s \n",(p+i)->relation0);
printf(" The address is: %s \n",(p+i)->address0);
printf(" The tel_number is: %s \n",(p+i)->tel_number0);
printf(" The post is:%s\n",(p+i)->post0);

}
}
fclose(fp);
return 0;
}
/******************************************************************************/
int main()
{
int m;
struct person *head=NULL;
printf("***************************************************************\n");
printf("***************************************************************\n");
printf("* *\n");
printf("* 迷你通讯录 *\n");
printf("*-------------------------------------------------------------*\n");
printf("* 1.添加联系人 4.修改信息 *\n");
printf("* 2.删除联系人 5.查询联系人 *\n");
printf("* 3.显示所有记录 0.关闭通讯录 *\n");
printf("*-------------------------------------------------------------*\n");
printf("* ----- 苏俊制作*\n");
printf("***************************************************************\n");
printf("***************************************************************\n");
printf("请输入一个数字(0~5) \n");
scanf("%d",&m);
system ("cls");
switch(m)
{
case 1:{head=creat();/*执行添加函数*/
array();}/*添加完成后排序*/
break;
case 2:{head=move(fp);//读取文件
head=del_person(head);/*执行删除函数*/
array();}/*执行完成后排序*/
break;
case 3:show_all();/*执行显示所有函数*/
break;
case 4:{alter(head);/*执行修改信息函数*/
array();}/*修改信息后排序*/
break;
case 5:search(head);/*执行查询函数*/
break;
case 0:break;/*关闭通讯录*/
defalt:printf("Your choice is wrong,please choose one more time\n");
}

}

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧