如代码所示,连续运行‘b’,‘c’程序出现崩溃,但是单步执行程序却不出现崩溃现象

骑猪看世界 2017-07-26 02:30:12

#include <stdio.h>
#include <malloc.h>
#include <conio.h>
#include <stdlib.h>
void print(struct student *head);
struct student *insert(struct student *stu_2,struct student *head);
void KeepMessage(struct student *head);
//链表单元定义,链表相关变量
unsigned int Prin_n;
struct student
{
int id;
float score;
char name[20];
char sex[20];
struct student *next;
} *head,*pthis;

//输入数据创建链表
void input()
{
struct student *tmp;
FILE *fp;
printf("\n\n请输入学生的信息以学号为0结束!!:\n");
if(!(fp=fopen("student_list","wb")))//打开文本
{
printf("cannot\n");
return;
}
do
{

printf("ID\t成绩\t姓名\t性别\t以Tab或者回车输入\n");

if ((tmp=(struct student *)malloc(sizeof(struct student)))==NULL)//申请内存
{
printf("\n错误!不能申请所需的内存!\n");
exit(0);
}
scanf("%d\t%f\t%s\t%s",&tmp->id,&tmp->score,tmp->name,tmp->sex);//输入信息
fwrite(tmp,sizeof(struct student),1,fp);
tmp->next=NULL;

if (tmp->id!=0)
{

if (head==NULL)
{
head=tmp;
pthis=head;
}
else
{
pthis->next=tmp;
pthis=pthis->next;
}
}
else
{
tmp->next=NULL;
}


} while (tmp->id!=0);

fclose(fp); //关闭文件
free(tmp);//释放内存地址

}

void KeepMessage(struct student *head)//保存数据函数存入二进制文件
{
FILE *fp;
struct student *bianl;
bianl=head;
if(!(fp=fopen("student_list","wb")))
{
printf("cannot\n");
return;
}
do{
fwrite(bianl,sizeof(struct student),1,fp);
bianl=bianl->next;
}while(bianl!=NULL);

fclose(fp);
// return temp;

}

//搜索链表找到第一个符合条件的项目输出
void search(int id)
{
FILE *fp;
printf("\n\n查询结果\n");
printf("ID\t成绩\n");
printf("-------------------------------\n");

if(!(fp=fopen("student_list","r")))//防止无文件
{
printf("不能读写\n");
return ;
}
if (head==NULL)
{
printf("\n错误!没有数据!\n");
return;
}
pthis=head;
while (pthis!=NULL)
{
if (pthis->id==id)
{
printf("%d\t%.2f %s %s\n",pthis->id,pthis->score,pthis->name,pthis->sex);
return;
}
else
{
pthis=pthis->next;
}
}
printf("\n没有找到!\n");
}

//列表输出链表中的所有项排序
void list()
{
struct student *view_sort_chinese(struct student *h);
FILE *fp;
void print(struct student *head);
if(!(fp=fopen("student_list","r")))
{
printf("不能读写\n");
return ;
}
printf("\n\n数据列表\n");
printf("ID\t成绩\t姓名\t性别\t\n");
printf("-------------------------------\n");
printf("按学号排序如下\n");
printf("-------------------------------\n");


if (head==NULL)
{
printf("错误,没有数据!\n");
return;
}
print (view_sort_chinese(head));


}
struct student *view_sort_chinese(struct student *h) //冒泡排序
{

//在调用函数里面判断head=NULL,这里不判断。
struct student *endpt,*u,*v,*p;
u = (struct student *)malloc(sizeof(head));
u->next=h;
h=u;

for(endpt=NULL;endpt!=h;endpt=p)
{
for(p=u=h;u->next->next!=endpt;u=u->next)
{
if(u->next->id > u->next->next->id)
{
v=u->next->next;
u->next->next=v->next;
v->next=u->next;
u->next=v;
p=u->next->next;
}
}
}
u=h;
h=h->next;
head=h;
return head;
}

void print(struct student *head)//打印文本
{
struct student *p;
p=head;
if(head)
{
do
{
printf("%d\t%.2f %s %s\n",p->id,p->score,p->name,p->sex);
p=p->next;
}while(p!=NULL);//只打印大于0的数;&&(p->id>0)
}
else
{
printf("打印错误,文件为空\n");
}

}



//追加数据
struct student *insert(struct student *hea,struct student *stu)//
{
struct student *p0,*p1;

p1 = hea;
p0 = stu;
if(NULL == hea)
{
head=p0;
p0->next=NULL;
return head;
}
else
{
p0->next=hea;
head=p0;

}
KeepMessage(head);
return head;
}

//删除数据
struct student *del(int num)
{
struct student *p1,*p2;
if(NULL==head)
{
printf("\nthis list is null!\n");
return head;
}

p1=head;
while(p1->id != num && p1->next != NULL)
{
p2=p1;
p1=p1->next;
}
if(num==p1->id)
{
if(p1==head)
{
head=p1->next;
}
else
{
p2->next=p1->next;
}
printf("\n删除成员:%d 成功!\n",num);
}
else
{
printf("%d没有找到!\n",num);
}

KeepMessage(head);
return head;

}

struct student *load1()
{
FILE *fp;

struct student *temp;
head=NULL;
if(!(fp=fopen("student_list","r")))
{
printf("不能读写,或无文件\n");
return 0;
}

do
{
if ((temp=(struct student *)malloc(sizeof(struct student)))==NULL)
{
printf("\n错误!不能申请所需的内存!\n");
exit(0);
}
fread(temp,sizeof(struct student),1,fp);
temp->next=NULL;
if(!(!feof(fp)&&(temp->id>0)))
{
break;
}
if (head==NULL)
{
head=temp;
pthis=head;
}
else
{
pthis->next=temp;
pthis=pthis->next;
}
}while(!feof(fp)&&(temp->id>0));

fclose(fp);
return head;

}
struct student *load()
{
FILE *fp;
struct student *temp;
head=NULL;
if(!(fp=fopen("student_list","r")))
{
printf("不能读写\n");
return 0;
}
do
{
if ((temp=(struct student *)malloc(sizeof(struct student)))==NULL)
{
printf("\n错误!不能申请所需的内存!\n");
exit(0);
}
fread(temp,sizeof(struct student),1,fp);
temp->next=NULL;

if (temp->id!=0)
{

if (head==NULL)
{
head=temp;
pthis=head;
}
else
{
pthis->next=temp;
pthis=pthis->next;
}
}

}
while (temp->id!=0);

fclose(fp);
return head;
}


void Dere_list()
{
struct student *stu_2;
char command=0;
int id=0;
int n;
int i;
do {
printf("\n\n\t 菜单\n");
printf("-------------------------------\n");
printf("\ta,输入数据\n");
printf("\tc,排列顺序\n");
printf("\td,插入数据\n");
printf("\te,查询数据\n");
printf("\tf,删除记录\n");
printf("\th,载入文本数据\n");
printf("\tg,退出系统\n");
printf("-------------------------------\n");
printf("\t请选择:");
command=getch();
printf("\n");
//命令处理
switch (command)
{
case 'a':
if (head==NULL)
{
input();
break;
}
else
{
printf("\n\n数据已经存在!\n");
break;
}
case 'c':
list();
break;
case 'd':
if ((stu_2=(struct student *)malloc(sizeof(struct student)))==NULL)//申请stu_2内存,可以连续输入,以占用上次。
{
printf("\n错误!不能申请所需的内存!\n");
exit(0);
}
printf("\nID\t成绩\t姓名\t性别\t以Tab或者回车输入\n");
scanf("%d\t%f\t%s\t%s",&stu_2->id,&stu_2->score,stu_2->name,stu_2->sex);
if(stu_2->id<0)
{
printf("输入有误!\n");
}
print(insert(head,stu_2));
// free(stu_2);

break;
case 'e':
scanf("%d",&i);
search(i);
//print(head);

break;
case 'f':
printf("请输入学号ID\n");
scanf("%d",&n);
print(del(n));

break;
case 'h':
load1();
print(head);
}
} while (command!='g');

printf("按任意键退出\n");
}

//程序主函数
void main()
{

//主循环
Dere_list();


}


...全文
136 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
骑猪看世界 2017-07-26
  • 打赏
  • 举报
回复
是排序有问题,但是我单步调试发现没问题,也不知道错在哪,而且单步调试可以运行但是一运行就崩溃
自信男孩 2017-07-26
  • 打赏
  • 举报
回复
重点看一下你的排序,排序有问题。
骑猪看世界 2017-07-26
  • 打赏
  • 举报
回复
不太懂,,可以说详细点吗?小白一枚,。。。
赵4老师 2017-07-26
  • 打赏
  • 举报
回复
判断是否越界访问,可以在数组的最后一个元素之后对应的地址处设置数据读写断点。如果该地址对应其它变量干扰判断,可将数组多声明一个元素,并设置数据读写断点在该多出元素对应的地址上。
#include <time.h>
#include <stdlib.h>
#include <windows.h>
int main() {
    int a,b[11];//本来是b[10],为判断哪句越界,故意声明为b[11]

    srand((unsigned int)time(NULL));//按两次F11,等黄色右箭头指向本行时,调试、新建断点、新建数据断点,地址:&b[10],字节计数:4,确定。
    while (1) {//按F5,会停在下面某句,此时a的值为10,b[10]已经被修改为对应0..4之一。
        b[(a=rand()%11)]=0;
        Sleep(100);
        b[(a=rand()%11)]=1;
        Sleep(100);
        b[(a=rand()%11)]=2;
        Sleep(100);
        b[(a=rand()%11)]=3;
        Sleep(100);
        b[(a=rand()%11)]=4;
        Sleep(100);
    }
    return 0;
}

69,369

社区成员

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

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