标题:如代码所示,连续运行‘b’,‘c’程序出现崩溃,但是单步执行程序却不出现 ...
只看楼主
骑猪看世界
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2017-7-26
结帖率:0
已结贴  问题点数:20 回复次数:4 
如代码所示,连续运行‘b’,‘c’程序出现崩溃,但是单步执行程序却不出现崩溃现象
程序代码:
#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();
    
  
}


搜索更多相关主题的帖子: struct student head next printf 
2017-07-26 14:27
吹水佬
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:432
帖 子:10064
专家分:41463
注 册:2014-5-20
得分:20 
//链表单元定义,链表相关变量
unsigned int         Prin_n;

struct student
{
    int id;
    float score;
    char name[20];
    char sex[20];
    struct student *next;
} *head,*pthis;

void print(struct student *head);
struct student *insert(struct student *stu_2,struct student *head);
void KeepMessage(struct student *head);
2017-07-26 14:46
骑猪看世界
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2017-7-26
得分:0 
回复 2楼 吹水佬
为什么这样呀??请问可以解释一下吗
2017-07-26 14:54
骑猪看世界
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2017-7-26
得分:0 
回复 2楼 吹水佬
还是崩溃的。?怎么回事
2017-07-26 15:03
吹水佬
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:432
帖 子:10064
专家分:41463
注 册:2014-5-20
得分:0 
代码是自己写的吗?为何写了那么多才发现问题?
如果代码不是自己写,最好先阅读代码,哪里有读不明的再提出来,待都读明白时,问题就不是问题了。
2017-07-26 19:28



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-479622-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.104077 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved