求解答为啥程序会停止运行

icon172433 2017-01-06 11:16:54
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
#include<conio.h>
#define recperpage 10
#define FILENAME "c:\\comlists"
struct address
{
char name[20];
char email[20];
char phone[20];
struct address *next;
} *head=NULL;
struct address *addnew (struct address *head,char *name,char *email,char *phone)
{
struct address *p0,*p1;
p0=(struct address *)malloc(sizeof(struct address));
strcpy(p0->name,name);
strcpy(p0->email,email);
strcpy(p0->phone,phone);
if(head==NULL)
head=p0;
else
{
p1=head;
while(p1->next!=NULL)
p1=p1->next;
p1->next=p0;
}
p1->next=NULL;
return(head);
}
struct address *delrec(char *name)
{
struct address *p1,*p2;
if(head==NULL) return NULL;
p1=head;
while(strcmp(p1->name,name)!=0&&p1->next!=NULL)
{
p2=p1;p1=p1->next;
}
if(strcmp(p1->name,name)==0)
{
if(p1==head)
head=p1->next;
else
p2->next=p1->next;
printf("\nDelete OK! \n");
}
else
printf("\nNot find! \n");
return head;
}
void saverec(struct address *head,char *filename)
{
struct address *p=head;
FILE *fp;
if((fp=fopen(filename,"wb"))==NULL)
{
printf("cannot open file\n");
return;
}
if(p!=NULL)
do
{
if(fwrite(p,sizeof(struct address),1,fp)!=1)
{
printf("file write error\n");
fclose(fp);
break;
}
p=p->next;
}while(p!=NULL);
fclose(fp);
}
/*显示链表的数据内容*/
void printrec(struct address *head)
{
int n=0,key;
struct address *p=head;
prints:
printf("\n\nPress 0 to Exit,others to continue!\n");
printf(" ID Name E_Mail Phone\n");
if(p!=NULL)
do
{
printf("%3d",n+1);
printf("%20s",p->name);
printf("%20s",p->email);
printf("%20s",p->phone);
n++;
p=p->next;
if(p==NULL) break;
if(n%recperpage==0)
{
scanf("%d",&key);
if(key==0) break;
else goto prints;
}
}while(p!=NULL);
}
/*从文件中读取数据内容到链表中*/
struct address *readrec(char *filename)
{
char c;
struct address *p,*p1,*head=NULL;
FILE *fp;
if((fp=fopen(filename,"rb"))==NULL)
{
printf("cannot open file\n");
return NULL;
}
while(!feof(fp))
{
c=fgetc(fp); /*预读一个字节,然后再向前定位一个字节*/
if(feof(fp)) break;
fseek(fp,-1,SEEK_CUR);
p=(struct address *)malloc(sizeof(struct address));
fread(p,sizeof(struct address),1,fp);
if(head==NULL)
head=p;
else
p1->next=p;
p1=p;
}
fclose(fp);
p1->next=NULL;
return(head);
} /*交换两个节点的内容*/
void exchangenode(struct address *p,struct address *p1)
{
char cdata[20];
strcpy(cdata,p1->name);
strcpy(p1->name,p1->name);
strcpy(p1->name,cdata);
strcpy(cdata,p1->email);
strcpy(p1->email,p1->email);
strcpy(p1->email,cdata);
strcpy(cdata,p1->phone);
strcpy(p1->phone,p1->phone);
strcpy(p1->phone,cdata);
}
/*按照姓名排序*/
void sortbyname(struct address *head)
{
struct address *p1,*p2,*p;
if(head==NULL) return;
p1=head;
do
{
p=p1;
p2=p1->next;
while(p2!=NULL)
{
if(strcmp(p->name,p2->name)>0)
p=p2;
p2=p2->next;
}
if(p!=p1)
exchangenode(p,p1);
p1=p1->next;
}while(p!=NULL);
}
/*按姓名进行查询并显示查询结果*/
void querybyname(struct address *h,char *qname)
{
struct address *p,*p0=NULL;
p=h;
if(p==NULL)
{
printf("Linklist is NULL,not find!");
return;
}
while(p!=NULL)
{
/*找到查找内容则加入到新的链表中,用于打印*/
if(strcmp(p->name,qname)==0)
p0=addnew(p0,p->name,p->email,p->phone);
p=p->next;
}
/*显示检索出来的链表内容*/
if(p0==NULL)
{
printf("Sorry,Not find!");
return;
}
else
printrec(p0);
}
void mainmenu()
{
printf("\n\n\nWelcome to use this program!\n");
printf("Add New Record,press 1\n");
printf("Delete One Record ,press 2\n");
printf("Modify Record,press 3\n");
printf("Save Record,press 4\n");
printf("Read Record,press 5\n");
printf("Display Record,press 6\n");
printf("Query Record,press 7\n");
printf("Sort Record,press 8\n");
printf("Exit,press 0\n");
}
int main()
{
int key,key1;
char name[20]={0},email[20]={0},phone[20]={0};
mainmenu();
scanf("%d",&key);
while(key!=0)
{
switch(key)
{
case 1: /*添加新纪录*/
printf("Please Input User Name:");scanf("%s",name);
printf("Please Input User E_Mail:");scanf("%s",email);
printf("Please Input User phone:");scanf("%s",phone);
head=addnew(head,name,email,phone); /*调用函数*/
break;
case 2: /*删除一条记录*/
printf("Input User Name to Del:");scanf("%s",name);
head=delrec(name);
break;
case 3: /*修改一条记录*/
printf("input user name to modify:");scanf("%s",name);
head=delrec(name);
break;
case 4: /*保存链表数据到数据文件中*/
saverec(head,FILENAME);
break;
case 5: /*读取数据文件中的记录到链表*/
head=readrec(FILENAME);
break;
case 6: /*显示链表中的数据内容*/
printrec(head);
break;
case 7: /*按照姓名进行查找*/
printf("input user name to Query:");
scanf("%s",name);
querybyname(head,name);
break;
case 8: /*按照姓名排序*/
sortbyname(head);
break;
case 0:
printf("\nThanks,Good_bye!Press any key to Quit\n");
getch();

}
mainmenu();
scanf("%d",&key);
}
return 0;
}
...全文
337 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2017-01-09
  • 打赏
  • 举报
回复
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack即“调用堆栈”里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处,看不懂时双击下一行,直到能看懂为止
小灸舞 版主 2017-01-09
  • 打赏
  • 举报
回复
代码功能归根结底不是别人帮自己看或讲解或注释出来的;而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。 提醒:再牛×的老师也无法代替学生自己领悟和上厕所! 单步调试和设断点调试(VS IDE中编译连接通过以后,按F10或F11键单步执行,按Shift+F11退出当前函数;在某行按F9设断点后按F5执行停在该断点处。)是程序员必须掌握的技能之一。
lunat 2017-01-09
  • 打赏
  • 举报
回复
自己动手,遇到具体问题点了再问。要让别人看到你的努力。 直接帮你做好了,说实话没有任何价值,其实是在助长你的懒惰。 你这帖子要是在stackoverflow,反对票能淹死你。
YXTS122 2017-01-09
  • 打赏
  • 举报
回复
只帮你改了下面这个函数 struct address *addnew(struct address *head,char *name,char *email,char *phone) { struct address *p0,*p1; p0=(struct address *)malloc(sizeof(struct address)); strcpy(p0->name,name); strcpy(p0->email,email); strcpy(p0->phone,phone); p0->next=NULL; if(head==NULL) head=p0; else { p1=head; while(p1->next!=NULL) p1=p1->next; p1->next=p0; } return (head); }
YXTS122 2017-01-08
  • 打赏
  • 举报
回复
代码好长,好没心情去看啊!
ck2333 2017-01-07
  • 打赏
  • 举报
回复
因为你程序出问题了啊,程序不能正常运行,你单步调试下。
小竹浮生 2017-01-07
  • 打赏
  • 举报
回复
不要做伸手党
孙建飞的博客 2017-01-07
  • 打赏
  • 举报
回复
你至少应该描述下程序功能及运行异常 信息
icon172433 2017-01-07
  • 打赏
  • 举报
回复
因为刚刚接触c语言,所以还不会调试,,这个程序的作用就相当于是建立一个简单的通讯录系统
paschen 2017-01-07
  • 打赏
  • 举报
回复
p1 p2可能没有初始化就使用了

69,382

社区成员

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

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