qq_24955509 2014-12-31 02:15 采纳率: 0%
浏览 1876

请问这是怎么回事?(有关内存越界)

以下是代码,进入后选择某些功能会出现以下警告:图片说明
有没有能帮忙改一下的,谢谢。

include

include

include

typedef struct link
{
char number[100];
char bookname[100];
char author[100];
char publish[100];
char time[100];
float price;
char status[100];
struct link *next;
}link;

link *Cbook()
{
int n=0;
link *p,*q,*head;
FILE *fp;
fp=fopen("图书信息.txt","r+");
if(fp==NULL)
printf("没有找到文件,请检查……");
p=(link *)malloc(sizeof(link));
if(p==NULL)
printf("申请内存出错!!!\n");
fscanf(fp,"%s%s%s%s%s",p->number,p->bookname,p->author,p->publish,p->time);
fscanf(fp,"%f",&p->price);
fscanf(fp,"%s",p->status);
while(feof(fp)==0)
{
n++;
if(n==1)
{
head=p;
}
else
{
q=p;
p=(link *)malloc(sizeof(link));
if(p==NULL)
printf("申请内存出错!!!\n");
fscanf(fp,"%s%s%s%s%s",p->number,p->bookname,p->author,p->publish,p->time);
fscanf(fp,"%f",&p->price);
fscanf(fp,"%s",p->status);
q->next=p;
}
}
p->next=NULL;
fclose(fp);
printf("信息已录入!!!");
return head;
}

void mainmenu()
{
printf("\n*****************************************\n");
printf("\n** 欢迎使用图书管理系统 \n");
printf("\n
1.进入系统 0.退出系统 \n");
printf("\n
***************************************\n");
printf("\n 请选择");
}

void menu1()
{
printf("\n*****************************************\n");
printf("\n** 1.进入图书查询系统 \n");
printf("\n
2.进入图书借还系统 \n");
printf("\n
3.保存所有图书信息 \n");
printf("\n
4.返回上级菜单 \n");
printf("\n
***************************************\n");
printf("\n请选择");
}

void cxmenu()
{
printf("\n*****************************************\n");
printf("\n** 1.显示所有图书信息 \n");
printf("\n
2.按编号查询图书 \n");
printf("\n
3.按书名查询图书 \n");
printf("\n
4.按作者查询图书 \n");
printf("\n
5.返回上级菜单 \n");
printf("\n
**************************************\n");
printf("\n请选择");
}

void jhmenu()
{
printf("\n*****************************************\n");
printf("\n** 1.借书 \n");
printf("\n
2.还书 \n");
printf("\n
3.返回上级菜单 \n");
printf("\n
***************************************\n");
printf("\n请选择");

}

void print(link head)
{
link *p;
p=head;
printf("\n");
printf("\n
*************************************************\n");
printf("\n编号\t\t书名\t\t作者\t\t出版社\t\t出版时间\t\t价格\t\t状态\n");
while(p!=NULL)
{

printf("\n");
printf("%s\t%s\t%s\t%s\t%s",p->number,p->bookname,p->author,p->publish,p->time);
printf("\t\t%.2f",p->price);
printf("\t\t%s",p->status);
printf("\n");
p=p->next;
}
}

void hold(link *head)
{
link *p;
FILE *fp;
fp=fopen("图书信息.txt","w+");
if(fp==NULL)
{
printf("文件操作出错!!!");
exit(1);

}
p=head;
for(;p!=NULL;p=p->next)
{

fprintf(fp,"%s\t%s\t%s\t%s\t%s",p->number,p->bookname,p->author,p->publish,p->time);
fprintf(fp,"\t\t%.2f",p->price);
fprintf(fp,"\t\t%s",p->status);
fprintf(fp,"\n");

}
fclose(fp);
printf("信息已保存。");
}

void holdcx(link t)
{
link *p=NULL;
FILE *fp;
fp=fopen("查询.txt","a+");
p=t;
fprintf(fp,"查询到的信息为:\n");
fprintf(fp,"\n");
fprintf(fp,"\n
*************************************************\n");
fprintf(fp,"\n编号\t\t书名\t\t作者\t\t出版社\t\t出版时间\t\t价格\t\t状态\n");
fprintf(fp,"\n");
fprintf(fp,"%s\t%s\t%s\t%s\t%s",p->number,p->bookname,p->author,p->publish,p->time);
fprintf(fp,"\t\t%.2f",p->price);
fprintf(fp,"\t\t%s",p->status);
fprintf(fp,"\n");
fclose(fp);
printf("信息已保存!!!");
}

link *findnumber(link *head)
{
char key[100];
link *h,*t1=NULL;
printf("请输入图书的编号:");
getchar();
gets(key);
for(h=head;h!=NULL;h=h->next)
{
if(strcmp(key,h->number)==0)
{
t1=h;
break;
}
}
return t1;
}

link *findboookname(link *head)
{
char key[100];
link *h,*t2=NULL;
printf("请输入图书的书名:");
getchar();
gets(key);
for(h=head;h!=NULL;h=h->next)
{
if(strcmp(key,h->bookname)==0)
{
t2=h;
break;
}
}
return t2;
}

link *findauthor(link *head)
{
char key[100];
link *h,*t3=NULL;
printf("请输入作者姓名:");
getchar();
gets(key);
for(h=head;h!=NULL;h=h->next)
{
if(strcmp(key,h->author)==0)
{
t3=h;
break;
}
}
return t3;
}

link *sortnumber(link *head)
{
link *p,*q,*temp;
temp=(link *)malloc(sizeof(link));
if(temp==NULL)
{
printf("申请内存出错!!!");
exit(1);
}
for(p=head;p!=NULL;p=p->next)
{
for(q=p->next;q!=NULL;q=q->next)
{
if(strcmp(p->number,q->number)>0)
{
strcpy(temp->number,p->number);
strcpy(temp->bookname,p->bookname);
strcpy(temp->author,p->author);
strcpy(temp->publish,p->publish);
strcpy(temp->time,p->time);
temp->price=p->price;
strcpy(temp->status,p->status);

         strcpy(p->number,q->number);
         strcpy(p->bookname,q->bookname);
         strcpy(p->author,q->author);
         strcpy(p->publish,q->publish);
         strcpy(p->time,q->time);
         p->price=q->price;
         strcpy(p->status,temp->status); 

         strcpy(q->number,temp->number);
         strcpy(q->bookname,temp->bookname);
         strcpy(q->author,temp->author);
         strcpy(q->publish,temp->publish);
         strcpy(q->time,temp->time);
         q->price=temp->price;
         strcpy(q->status,temp->status);
      }  
      }
  }

return head;
}

link borrowbook(link *head)
{
link *h,*p;
char ch,ch1[100]="在库",ch2[100]="不在库";
h=head;
printf("\n
*************************************************\n");
printf("\t\t欢迎使用借书系统!!!\n");
p=findboookname(h);
if(p==NULL)
printf("借书失败,书库中没有这本书!!!");
else if(strcmp(p->status,ch2)==0)
printf("借书失败,图书不在库!!!");
else
{
printf("确认借出?Y/N:");
scanf("%c",&ch);
getchar();
if(ch=='Y'||ch=='y')
{

strcpy(p->status,ch2);
printf("借书成功,请返回上级菜单保存信息!!!");
}
if(ch=='N'||ch=='n')
{
printf("你没有借任何书!!!");
}
}
return head;
}

link returnbook(link *head)
{
link *h,*p;
char ch,ch1[100]="在库",ch2[100]="不在库";
h=head;
printf("\n
*************************************************\n");
printf("\t\t欢迎使用还书系统!!!\n");
p=findboookname(h);
if(p==NULL)
printf("还书失败,这不是本书库的书!!");
else
{
printf("确认还书?Y/N:");
scanf("%c",&ch);
getchar();
if(ch=='Y'||ch=='y')
{

strcpy(p->status,ch1);
printf("还书成功,请返回上级菜单保存信息!!!");
}
if(ch=='N'||ch=='n')
{
printf("你没有还书!!!");
}
}
return head;
}

void main()
{
int a,b,c,d;
link h,*t;
L:system("cls");
mainmenu();
while(1)
{
scanf("%d",&a);
switch(a)
{
case 1:
{
B: system("cls");
menu1();
while(1)
{
scanf("%d",&b);
switch(b)
{
case 1:
{
system("cls");
cxmenu();
while(1)
{
scanf("%d",&c);
switch(c)
{
case 1:
{
print(h);
}break;
case 2:
{
t=findnumber(h);
if(t==NULL)
printf("没有要查询的图书!!!");
if(t!=NULL)
{
printf("查询的图书为:");
printf("\n");
printf("\n
*************************************************\n");
printf("\n编号\t\t书名\t\t作者\t\t出版社\t\t出版时间\t\t价格\t\t状态\n");

            printf("\n");
printf("%s\t%s\t%s\t%s\t%s",t->number,t->bookname,t->author,t->publish,t->time);
            printf("\t\t%.2f",t->price);
            printf("\t\t%s",t->status);
            printf("\n");
           }
              }break;
           case 3: 
           {
           t=findboookname(h);
           if(t==NULL)
           printf("没有要查询的图书!!!");
           if(t!=NULL)
           {
            printf("查询的图书为:");
            printf("\n");

printf("\n**************************************************\n");
printf("\n编号\t\t书名\t\t作者\t\t出版社\t\t出版时间\t\t价格\t\t状态\n");
printf("\n");
printf("%s\t%s\t%s\t%s\t%s",t->number,t->bookname,t->author,t->publish,t->time);
printf("\t\t%.2f",t->price);
printf("\t\t%s",t->status);
printf("\n");
}
}break;
case 4:
{
t=findauthor(h);
if(t==NULL)
printf("没有要查询的图书!!!");
if(t!=NULL)
{
printf("查询的图书为:");
printf("\n");
printf("\n**************************************************\n");
printf("\n编号\t\t书名\t\t作者\t\t出版社\t\t出版时间\t\t价格\t\t状态\n");
printf("\n");
printf("%s\t%s\t%s\t%s\t%s",t->number,t->bookname,t->author,t->publish,t->time);
printf("\t\t%.2f",t->price);
printf("\t\t%s",t->status);
printf("\n");
}
}
break;
case 5:
{
goto B;
}break;
}
}
}break;
case 2:
{
system("cls");
jhmenu();
while(1)
{
scanf("%d",&d);
switch(d)
{
case 1:
{
borrowbook(h);
}break;
case 2:
{
returnbook(h);
}break;
case 3:
{
goto B;
}break;
}
}
}break;
case 3:
{
hold(h);
}break;
case 4:
{
goto L;
}break;

}

}
}break;
case 0:
{
printf("\t\t\t感谢使用,再见!!!");
exit(0);
}
}
}
}

  • 写回答

4条回答 默认 最新

  • threenewbee 2014-12-31 02:26
    关注

    代码不全,建议你贴在 codepad.org 上,然后把链接写在这里。便于调试。

    0xcccccccc 很典型是你的指针没有初始化造成的。

    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题