早起的月光 发表于 2008-3-2 23:10

小菜,求助学生分数管理系统的改错

我编了个学生分数管理系统但总是出现内存分配错误,请问下问题出在哪?
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
#include<malloc.h>
struct student{
        char name[20];
        int score;
        struct student *next;
};
struct student *ptr,*head,*this,*prev;
void ini_func(void);
void insert_func(void);
void sort_func(void);
void delete_func(void);
void modify_func(void);
void display_func(void);
void anykey_func(void);
int main()
{
        char option;
        ini_func();
        while(1){
                printf("*********************\n");
                printf("1               insert\n");
                printf("2               delete\n");
                printf("3              display\n");
                printf("4               modify\n");
                printf("5                 quit\n");
                printf("*********************\n");
                printf("please enter your choose");
                option=getche();
                printf("\n");
                switch(option){
                        case '1':
                                insert_func();
                                break;
                        case '2':
                                delete_func();
                                break;
                        case '3':
                                display_func();
                                break;
                        case '4':
                                modify_func();
                                break;
                        case '5':
                                exit(0);
                        defult:
                                printf("error!please enter again\n");
                }
        }
return 0;
}
void ini_func(void)
{
        head=(struct student*)malloc(sizeof(struct student));
        head->next=NULL;
}
void insert_func(void)
{
        char s_temp[4];
        ptr=(struct student*)malloc(sizeof(struct student));
        if(!ptr)exit(0);
        printf("plese enter the students name ");
        gets(ptr->name);
        printf("please enter the students store ");
        gets(s_temp);
        ptr->score=atoi(s_temp);
       
        sort_func();
}
void sort_func(void)
{
        prev=head;
        this=head->next;
        while(!this&&(ptr->score>this->score)){
                prev=this;
                this=this->next;
        }
        ptr->next=this;
        prev->next=ptr;
}
void delete_func(void)
{
        char de_name[20];
        printf("delete student name\n");
        gets(de_name);
        prev=head;
        this=head->next;
        while(!this&&strcmp(this->name,de_name)){
                prev=this;
                this=this->next;
        }
        if(this){
                prev->next=this->next;
                free(this);
        }else{
                printf("no the name\n");
        }
        anykey_func();
}
void modify_func(void)
{
        char mo_name[20],mo_score[4];
        printf("modify student name");
        gets(mo_name);
        prev=head;
        this=head->next;
        while(!this&&strcmp(this->name,mo_name)!=0){
                prev=this;
                this=this->next;
        }
        if(this){
                printf("************************\n");
                printf("  the student name is %s",this->name);
                printf("  the student score is %d",this->score);
                printf("*************************\n");
                printf(" please enter new score:");
                gets(mo_score[4]);
                this->score=atoi(mo_score[4]);
                printf("%s student record is modified %d\n",this->name,this->score);
    }else{
            printf("no this student\n");
                   
    }
    anykey_func();
}
void display_func(void)
{
        if(!head->next)
                printf("there is no student");
        while(!this){
                printf("%-20s the score is %3d",this->name,this->score);
                this=this->next;
        }
        printf("_______________________\n");
        anykey_func();
}
void anykey_func(void)
{
        printf("press any key to continue\n");
        getch();
        printf("\n");
}
我估计是sort和anykey函数有问题?请指教


页: [1]

编程论坛