哪位高手能帮小弟我看看小弟我这个程序是如何了,能编译和执行,就是运行过程中系统提示出错了
谁能帮我看看我这个程序是怎么了,能编译和执行,就是运行过程中系统提示出错了!
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct {
char num[5];
char name[5];
char call[8];
char phone[11];
}datatype;
typedef struct LNode{
struct LNode *next;
datatype data;
}LNode,*Linklist;
void create(Linklist,int);
void find(Linklist);
void modify(Linklist);
void main()
{ Linklist head=NULL,tail=NULL;
int n=0;//员工总数
head=(Linklist)malloc(sizeof(LNode));
tail=head;
printf("输入员工总数:\n");
scanf("%d",&n);
create(head,n);
find(head);
modify(head);
}
void create(Linklist tail1,int n)
{ Linklist p;
int i=0;
for(i=0;i<n;i++)
{
p=(Linklist)malloc(sizeof(LNode));
printf("请输入第%d位员工通讯录信息。\n",i+1);
printf("姓名:");scanf("%s",&(p->data.name));
printf("编号:");scanf("%s",&(p->data.num));
printf("电话:");scanf("%s",&(p->data.call));
printf("手机号:");scanf("%s",&(p->data.phone));
tail1->next=p;
tail1=p;
}
}
void find(Linklist head1)
{
char num1[5];
printf("输入员工编号:\n");
scanf("%s",num1);
head1=head1->next;
while((head1->data).num[5]!=num1[5]&&(head1!=NULL)) head1=head1->next;
if(head1)printf("输入有误!");
else {
printf("姓名:%s",(head1->data).name);
printf("编号:%s",(head1->data).num);
printf("电话:%s",(head1->data).call);
printf("手机:%s",(head1->data).phone);
}
}
void modify(Linklist head1)
{ char num1[5];
printf("输入员工编号:\n");
scanf("%s",num1);
head1=head1->next;
while((head1->data).num[5]!=num1[5]&&(head1!=NULL)) head1=head1->next;
if(head1)printf("输入有误!");
else {
printf("姓名:");scanf("%s",&(head1->data.name));
printf("编号:");scanf("%s",&(head1->data.num));
printf("电话:");scanf("%s",&(head1->data.call));
printf("手机号:");scanf("%s",&(head1->data.phone));
}
}
------解决方案--------------------
head=(Linklist)malloc(sizeof(LNode));没有用free来释放内存。
------解决方案--------------------
经过调试,find()和modify()函数中while((head1->data).num[5]!=num1[5]&&(head1!=NULL)) head1=head1->next;这句报错。这条语句有问题。员工编号是char[]类型,进行比较的话不可以用(head1->data).num[5]!=num1[5]。用函数strcmp吧。
------解决方案--------------------
单步调试和设断点调试是程序员必须掌握的技能之一。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct {
char num[5];
char name[5];
char call[8];
char phone[11];
}datatype;
typedef struct LNode{
struct LNode *next;
datatype data;
}LNode,*Linklist;
void create(Linklist,int);
void find(Linklist);
void modify(Linklist);
void main()
{ Linklist head=NULL,tail=NULL;
int n=0;//员工总数
head=(Linklist)malloc(sizeof(LNode));
tail=head;
printf("输入员工总数:\n");
scanf("%d",&n);
create(head,n);
find(head);
modify(head);
}
void create(Linklist tail1,int n)
{ Linklist p;
int i=0;
for(i=0;i<n;i++)
{
p=(Linklist)malloc(sizeof(LNode));
printf("请输入第%d位员工通讯录信息。\n",i+1);
printf("姓名:");scanf("%s",&(p->data.name));
printf("编号:");scanf("%s",&(p->data.num));
printf("电话:");scanf("%s",&(p->data.call));
printf("手机号:");scanf("%s",&(p->data.phone));
tail1->next=p;
tail1=p;
}
}
void find(Linklist head1)
{
char num1[5];
printf("输入员工编号:\n");
scanf("%s",num1);
head1=head1->next;
while((head1->data).num[5]!=num1[5]&&(head1!=NULL)) head1=head1->next;
if(head1)printf("输入有误!");
else {
printf("姓名:%s",(head1->data).name);
printf("编号:%s",(head1->data).num);
printf("电话:%s",(head1->data).call);
printf("手机:%s",(head1->data).phone);
}
}
void modify(Linklist head1)
{ char num1[5];
printf("输入员工编号:\n");
scanf("%s",num1);
head1=head1->next;
while((head1->data).num[5]!=num1[5]&&(head1!=NULL)) head1=head1->next;
if(head1)printf("输入有误!");
else {
printf("姓名:");scanf("%s",&(head1->data.name));
printf("编号:");scanf("%s",&(head1->data.num));
printf("电话:");scanf("%s",&(head1->data.call));
printf("手机号:");scanf("%s",&(head1->data.phone));
}
}
------解决方案--------------------
head=(Linklist)malloc(sizeof(LNode));没有用free来释放内存。
------解决方案--------------------
经过调试,find()和modify()函数中while((head1->data).num[5]!=num1[5]&&(head1!=NULL)) head1=head1->next;这句报错。这条语句有问题。员工编号是char[]类型,进行比较的话不可以用(head1->data).num[5]!=num1[5]。用函数strcmp吧。
------解决方案--------------------
单步调试和设断点调试是程序员必须掌握的技能之一。