| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 650 人关注过本帖, 1 人收藏
标题:求救高手,我这程序就是执行其中一个功能老是出错,能不能指正一下
只看楼主 加入收藏
johnsy1
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2008-9-17
收藏(1)
 问题点数:0 回复次数:8 
求救高手,我这程序就是执行其中一个功能老是出错,能不能指正一下
执行功能5老是出错,请高手帮忙指正一下



#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <stdlib.h>
#define LEN sizeof(struct student)


 

char schoolwork[5][10]={"math","c_prog","eng","polity","sport"};

struct student /* 学生的数据结构 */
{
    char class_0[20];
    int num;
    char name[8];
    float score[5];    
    float total;
    float ave;
    struct student *next;
    int order;
}stu;

int n,i;

/* 函数功能:创建链表,完成数据录入功能 */
struct student *create(void)
{
    struct student *p1,*p2,*head;
    n=0;
    p1=p2=(struct student *)malloc(LEN);
    printf("input number of student:");
    printf("if number is 0,stop inputing.\n");
    scanf("%d",&p1->num);
    printf("class:");
    scanf("%s",p1->class_0);
    printf("name:");
    scanf("%s",p1->name);
    printf("score:");
    for(i=0;i<5;i++)
    {
        printf("%s:",schoolwork[i]);
        scanf("%f",&p1->score[i]);
    }
    head=NULL;
    while(p1->num!=0)
    {
        n=n+1;
        if(n==1)
            head=p1;
        else
            p2->next=p1;
        p2=p1;
        p1=(struct student *)malloc(LEN);
        printf("input number of student:");
        printf("if number is 0,stop inputing.\n");
        scanf("%d",&p1->num);
        if(p1->num==0)
            goto end;
        printf("class:");
        scanf("%s",p1->class_0);
        printf("name:");
        scanf("%s",p1->name);
        printf("score:");
        for(i=0;i<5;i++)
        {
            printf("%s:",schoolwork[i]);
            scanf("%f",&p1->score[i]);
        }
    }
    end:
    p2->next=NULL;
    return(head);
}

/* 函数功能:计算每个学生的5门功课的总成绩和平均成绩 */
void TotalScore(struct student *head)
{
    struct student *p1;
    p1=head;
    while (p1!= NULL)              /*若不是表尾,则循环*/
    {
        p1->total=0;
        for (i=0;i<5;i++)
        {
            p1->total+=p1->score[i];
        }
        p1->ave=p1->total/5;
        p1=p1->next;              /*让p指向下一个节点*/
    }
}

/*函数功能:交换两个整型数*/
void IntSwap(int *pt1, int *pt2)
{
    int temp;
    temp = *pt1;
    *pt1 = *pt2;
    *pt2 = temp;
}

/*函数功能:交换两个实型数*/
void FloatSwap(float *pt1, float *pt2)
{
    float temp;
    temp = *pt1;
    *pt1 = *pt2;
    *pt2 = temp;
}

/*函数功能:交换两个字符串*/
void CharSwap(char *pt1, char *pt2)
{
    char temp[15];
    strcpy(temp, pt1);
    strcpy(pt1, pt2);
    strcpy(pt2, temp);
}

/* 函数功能:对学生成绩排序并确定名次 */
void SortScore(struct student *head)
{
    student *pt;
    int flag=0,a=0;
    if(head==NULL)
    {
        printf("\nlist null!\n");
        goto end;
    }
    
    do{
        flag=0;
        pt=head;
        /*若后一个节点的总成绩比前一个节点的总成绩高,则交换两个节点中的数据
        注意只交换节点数据,而节点顺序不变,即节点next指针内容不进行交换*/
        while(pt->next!=NULL)
        {
            if (pt->next->total>pt->total)
            {
                CharSwap(pt->class_0, pt->next->class_0);
                IntSwap(&pt->num, &pt->next->num);
                CharSwap(pt->name, pt->next->name);
                for (i=0; i<5; i++)
                {
                FloatSwap(&pt->score[i], &pt->next->score[i]);
                }
                FloatSwap(&pt->total, &pt->next->total);
                FloatSwap(&pt->ave, &pt->next->ave);
                flag=1;
            }
            pt=pt->next;
        }
    }while(flag);
    pt=head;
    while(pt!=NULL)
    {
        a++;
        pt->order=a;
        pt=pt->next;
    }
    end:
    printf("");
}

/* 函数的功能:显示所有已经建立好的节点的节点号和该节点中数据项内容 */
void PrintScore(struct student *head)
{
    struct student *p=head;
printf("\n****************************学生成绩统计系统********************************\n");
    printf("|class|num|name|math|c_prog| eng | polity | sport |总分| ave | order | \n");
   while (p!=NULL)    /*若不是表尾,则循环打印*/
    {
       printf("\n%-5s%-5d%-5s", p->class_0, p->num,p->name);
        for(i=0;i<5;i++)
        {    
            printf("%-7.1f", p->score[i]);
        }
        printf("%-7.1f%-7.1f%d", p->total, p->ave,p->order);
        p=p->next;      /*让p指向下一个节点*/
    }
}

/* 函数功能:按学号查找一个节点数据 */
void SearchStudent(struct student *head)
{
    struct student *p=head;  
    int b;        /*存放学号的变量*/
    printf("输入您要查找学生的学号:\n");
    scanf("%d",&b); /*输入学号*/
    if(head==NULL)
    {
        printf("\nlist null!\n");
        goto end;
    }
    while(p!=NULL)/*当记录的指针不为空时*/
    {
        if(p->num==b)/*显示找到的记录信息*/
        {
            printf("\n****************************所查找学生的数据**********************************\n");
            printf("|class|num|name|math|c_prog| eng | polity | sport |总分| ave | order | \n");
            printf("\n%-5s%-5d%-5s", p->class_0, p->num,p->name);
            for(i=0;i<5;i++)
            {    
                printf("%-7.1f", p->score[i]);
            }
            printf("%-7.1f%-7.1f%d", p->total, p->ave,p->order);
            printf("\n********************************end*******************************\n");
            break;
        }
        else
            p=p->next; /*移动指针,指向下一结点,继续查找*/
    }
        
    if(p==NULL) /*指针为空,说明未能找到所要的结点*/
    printf("\n找不到所需的学号 %d\n",b);
    end:
    printf("");

}

/* 函数功能:按学号查找并修改一个节点数据  */
void ModifyStudent(struct student *head)
{
    struct student *p=head;
    int b;        /*存放学号的变量*/
    printf("输入您要查找学生的学号:\n");
    scanf("%d",&b); /*输入学号*/
    if(head==NULL)
    {
        printf("\nlist null!\n");
        goto end;
    }
    while(p!=NULL)/*当记录的指针不为空时*/
    {
        if(p->num==b)
        {
            printf("请输入新数据:");
            printf("input number of student:");
            scanf("%d",&p->num);
            printf("class:");
            scanf("%s",p->class_0);
            printf("name:");
            scanf("%s",p->name);
            printf("score:");
            for(i=0;i<5;i++)
            {
                printf("%s:",schoolwork[i]);
                scanf("%f",&p->score[i]);
            }
            TotalScore(p);
            break;
        }
        else
            p=p->next; /*移动指针,指向下一结点,继续查找*/
        }
    if(p==NULL) /*指针为空,说明未能找到所要的结点*/
    printf("\n找不到所需的学号 %d\n",b);
    end:
    printf("");
}

/*函数功能:按学号插入一个节点数据*/
struct student *InsertStudent(struct student *head)
{
    struct student *p1,*p2,*info,*k; /*p指向插入位置,q是其前驱,info指新插入记录*/
    int n=0;
    p1=head;
    printf("\ninput a new record:\n");
    info=(struct student *)malloc(LEN); /*申请空间*/
    if(!info)
    {
        printf("\nOut of memory"); /*如没有申请到,内存溢出*/
        return NULL; /*返回空指针*/
    }
    printf("input number of student:");
    printf("if number is 0,stop inputing.\n");
    scanf("%d",&p1->num);
    printf("class:");
    scanf("%s",p1->class_0);
    printf("name:");
    scanf("%s",p1->name);
    printf("score:");
    for(i=0;i<5;i++)
    {
        printf("%s:",schoolwork[i]);
        scanf("%f",&p1->score[i]);
    }
    TotalScore(info);
    info->next=NULL; /*设后继指针为空*/
    p1=p2=head; /*将指针赋值给p1,p2*/
    if(head==NULL)/*链表为空*/
    {
        head=info;
        info->next=NULL;
    }
    else
    {
        while((info->total<p1->total)&&(p1->next!=NULL))
        {
            p2=p1; /*使p2指向p1刚才的结点*/
            p1=p1->next;
        } /*使p1后移一个结点*/
        if(info->total>=p1->total)
        {
            if(head==p1)
            {
                head=info;
                info->next=p1;
            } /*info作为头指针,p1作为info的后继指针*/
            else
            {
                p2->next=info; /*p2的后继指针是info*/
                info->next=p1;
            }
        } /*info的后继指针是p1*/
        else
        {
            p1->next=info;
            info->next=NULL;
        }/*p1的后继指针是info,info的后继指针是空*/
    }
    k=head;
    while(k!=NULL) /*重新赋予排序号(排序号因为被插入新数据而改变)*/
    {
        n++;
        k->order=n;
        k=k->next;
    }
    printf("\n ----have inserted the student----\n");
    return(head);       /*返回头指针*/
}  

/*函数功能:按学号删除一个节点数据*/
struct student *DeleteStudent(struct student *head)
{
    struct student *p1,*p2;
    int number;
    printf("请输入要删除学生的学号:");
    scanf("%d",&number);
    if(head==NULL)
    {
        printf("\nlist null!\n");
        goto end;
    }
    p1=head;
    while(number!=p1->num&&p1->next!=NULL) /*p1指向的不是所要找的结点,并且后面还有结点*/
    {
        p2=p1;
        p1=p1->next;
    }/*p1向后移一个结点*/
    if(number==p1->num)
    {
        if(p1==head)
            head=p1->next; /*若p1指向的是首结点,把第二个结点地址赋给head*/
        else
            p2->next=p1->next; /*否则将下一结点地址付给前一结点地址*/
        printf("已经删除学号为%d的学生。",number);
        n=n-1;
    }
    else printf("找不到学号为%d的学生。\n",number);/*找不到该结点*/
    end:
    return(head);
}

/* 函数功能:显示菜单并获得用户键盘输入的选项 */
int menu_select(void)
{
    int digit;
    printf("\n      *************学生成绩管理系统*************\n");
    printf("      &            1.添加学生的数据            &\n");
    printf("      &           2.输出全部学生数据           &\n");
    printf("      &           3.查询一个学生数据           &\n");
    printf("      &           4.修改一个学生数据           &\n");
    printf("      &           5.插入一个学生数据           &\n");
    printf("      &           6.删除一个学生数据           &\n");
    printf("      &               7.保存数据               &\n");
    printf("      &               8.读取数据               &\n");
    printf("      &           0.退出学生记录系统           &\n");
    printf("      ******************************************\n");
    printf("请选择:");
    scanf("%d", &digit); /*在%c前面加一个空格,将存于缓冲区中的回车符读入*/
    return digit;
}

 
/*函数功能:保存数据到文件模块*/
void SaveFile(struct student *head)
{
    FILE *fp; /*定义指向文件的指针*/
    struct student *p; /* 定义移动指针*/
    char outfile[20]; /*保存输出文件名*/
    printf("Enter outfile name,for example G:\\f1\\score.txt:\n");
    scanf("%s",outfile);
    if((fp=fopen(outfile,"wb"))==NULL) /*为输出打开一个二进制文件,如没有则建立*/
    {
        printf("Can not open file\n");
        return;
    }
    printf("\nSaving file......\n"); /*打开文件,提示正在保存*/
    p=head; /*移动指针从头指针开始*/
    while(p!=NULL) /*如p不为空*/
    {
        fwrite(p,LEN,1,fp);  /*写入一条数据*/
        p=p->next;  /*指针后移*/
    }
    fclose(fp); /*关闭文件*/
    printf("-----Save success!!-----\n"); /*显示保存成功*/
}


/*导入信息模块*/
struct student *LoadFile(void)
{
    struct student *p,*q,*h=NULL; /*定义记录指针变量*/
    FILE *fp; /* 定义指向文件的指针*/
    char infile[20]; /*保存文件名*/
    printf("Enter infile name,for example G:\\f1\\score.txt:\n");
    scanf("%s",infile); /*输入文件名*/
    if((fp=fopen(infile,"rb"))==NULL) /*打开一个二进制文件,为读方式*/
    {
        printf("Can not open file\n"); /*如不能打开,返回头指针*/
        return h;
    }
    printf("\n -----Loading file!-----\n");
    p=(struct student *)malloc(LEN); /*申请空间*/
    if(!p)
    {
        printf("Out of memory!\n"); /*如没有申请到,则内存溢出*/
        return h; /*返回空头指针*/
    }
    h=p; /*申请到空间,将其作为头指针*/
    while(!feof(fp)) /*循环读数据直到文件尾结束*/
    {
        if(1!=fread(p,LEN,1,fp))
        break; /*如果没读到数据,跳出循环*/
    p->next=(struct student *)malloc(LEN); /*为下一个结点申请空间*/
    if(!p->next)
    {
        printf("Out of memory!\n"); /*如没有申请到,则内存溢出*/
        return h;
    }
    q=p; /*保存当前结点的指针,作为下一结点的前驱*/
    p=p->next; /*指针后移,新读入数据链到当前表尾*/
    }
    q->next=NULL; /*最后一个结点的后继指针为空*/

    fclose(fp); /*关闭文件*/
    printf("Load success!!!\n");
    return h; /*返回头指针*/
}


void main()
{
    struct student *head=NULL; /*链表定义头指针*/
    int digit;
    for(;;) /*无限循环*/
    {
        digit=menu_select();
        switch(digit)/*调用主菜单函数,返回值整数作开关语句的条件*/
        {
            case 1:head=create();TotalScore(head);SortScore(head);break; /*创建链表*/
            case 2:SortScore(head);PrintScore(head);break; /*显示全部记录*/
            case 3:SearchStudent(head);break; /*查找记录*/
            case 4:ModifyStudent(head);break; /*修改记录*/
            case 5:head=InsertStudent(head); break; /*插入记录*/
            case 6:head=DeleteStudent(head);break; /*删除记录*/
            case 7:SaveFile(head);break; /*保存数据*/
            case 8:head=LoadFile();break; /*读取数据*/
            case 0:exit(0); /*如菜单返回值为0程序结束*/
        }
    }
}
搜索更多相关主题的帖子: include 
2008-09-17 23:07
hjselena
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2007-5-31
收藏
得分:0 
info=(struct student *)malloc(LEN); /*申请空间*/
申请空间的是info
 scanf("%d",&p1->num);
    printf("class:");
    scanf("%s",p1->class_0);
    printf("name:");
    scanf("%s",p1->name);
    printf("score:");
    for(i=0;i<5;i++)
    {
        printf("%s:",schoolwork[i]);
        scanf("%f",&p1->score[i]);
    }
赋值的却是p1,自然就不正确了
2008-09-18 14:02
johnsy1
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2008-9-17
收藏
得分:0 
那要怎么修改啊
那要怎么修改啊
2008-09-18 23:15
f319
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2008-5-13
收藏
得分:0 
如2L所说p1指针没有赋值就开始使用它了。
2008-09-18 23:30
johnsy1
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2008-9-17
收藏
得分:0 
修改后变成这样还是不行啊
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <stdlib.h>
#define LEN sizeof(struct student)


 

char schoolwork[5][10]={"math","c_prog","eng","polity","sport"};

struct student /* 学生的数据结构 */
{
    char class_0[20];
    int num;
    char name[8];
    float score[5];    
    float total;
    float ave;
    struct student *next;
    int order;
}stu;

int n,i;

/* 函数功能:创建链表,完成数据录入功能 */
struct student *create(void)
{
    struct student *p1,*p2,*head;
    n=0;
    p1=p2=(struct student *)malloc(LEN);
    printf("input number of student:");
    printf("if number is 0,stop inputing.\n");
    scanf("%d",&p1->num);
    printf("class:");
    scanf("%s",p1->class_0);
    printf("name:");
    scanf("%s",p1->name);
    printf("score:");
    for(i=0;i<5;i++)
    {
        printf("%s:",schoolwork[i]);
        scanf("%f",&p1->score[i]);
    }
    head=NULL;
    while(p1->num!=0)
    {
        n=n+1;
        if(n==1)
            head=p1;
        else
            p2->next=p1;
        p2=p1;
        p1=(struct student *)malloc(LEN);
        printf("input number of student:");
        printf("if number is 0,stop inputing.\n");
        scanf("%d",&p1->num);
        if(p1->num==0)
            goto end;
        printf("class:");
        scanf("%s",p1->class_0);
        printf("name:");
        scanf("%s",p1->name);
        printf("score:");
        for(i=0;i<5;i++)
        {
            printf("%s:",schoolwork[i]);
            scanf("%f",&p1->score[i]);
        }
    }
    end:
    p2->next=NULL;
    return(head);
}

/* 函数功能:计算每个学生的5门功课的总成绩和平均成绩 */
void TotalScore(struct student *head)
{
    struct student *p1;
    p1=head;
    while (p1!= NULL)              /*若不是表尾,则循环*/
    {
        p1->total=0;
        for (i=0;i<5;i++)
        {
            p1->total+=p1->score[i];
        }
        p1->ave=p1->total/5;
        p1=p1->next;              /*让p指向下一个节点*/
    }
}

/*函数功能:交换两个整型数*/
void IntSwap(int *pt1, int *pt2)
{
    int temp;
    temp = *pt1;
    *pt1 = *pt2;
    *pt2 = temp;
}

/*函数功能:交换两个实型数*/
void FloatSwap(float *pt1, float *pt2)
{
    float temp;
    temp = *pt1;
    *pt1 = *pt2;
    *pt2 = temp;
}

/*函数功能:交换两个字符串*/
void CharSwap(char *pt1, char *pt2)
{
    char temp[15];
    strcpy(temp, pt1);
    strcpy(pt1, pt2);
    strcpy(pt2, temp);
}

/* 函数功能:对学生成绩排序并确定名次 */
void SortScore(struct student *head)
{
    student *pt;
    int flag=0,a=0;
    if(head==NULL)
    {
        printf("\nlist null!\n");
        goto end;
    }
    
    do{
        flag=0;
        pt=head;
        /*若后一个节点的总成绩比前一个节点的总成绩高,则交换两个节点中的数据
        注意只交换节点数据,而节点顺序不变,即节点next指针内容不进行交换*/
        while(pt->next!=NULL)
        {
            if (pt->next->total>pt->total)
            {
                CharSwap(pt->class_0, pt->next->class_0);
                IntSwap(&pt->num, &pt->next->num);
                CharSwap(pt->name, pt->next->name);
                for (i=0; i<5; i++)
                {
                FloatSwap(&pt->score[i], &pt->next->score[i]);
                }
                FloatSwap(&pt->total, &pt->next->total);
                FloatSwap(&pt->ave, &pt->next->ave);
                flag=1;
            }
            pt=pt->next;
        }
    }while(flag);
    pt=head;
    while(pt!=NULL)
    {
        a++;
        pt->order=a;
        pt=pt->next;
    }
    end:
    printf("");
}

/* 函数的功能:显示所有已经建立好的节点的节点号和该节点中数据项内容 */
void PrintScore(struct student *head)
{
    struct student *p=head;
printf("\n****************************学生成绩统计系统********************************\n");
    printf("|class|num|name|math|c_prog| eng | polity | sport |总分| ave | order | \n");
   while (p!=NULL)    /*若不是表尾,则循环打印*/
    {
       printf("\n%-5s%-5d%-5s", p->class_0, p->num,p->name);
        for(i=0;i<5;i++)
        {    
            printf("%-7.1f", p->score[i]);
        }
        printf("%-7.1f%-7.1f%d", p->total, p->ave,p->order);
        p=p->next;      /*让p指向下一个节点*/
    }
}

/* 函数功能:按学号查找一个节点数据 */
void SearchStudent(struct student *head)
{
    struct student *p=head;  
    int b;        /*存放学号的变量*/
    printf("输入您要查找学生的学号:\n");
    scanf("%d",&b); /*输入学号*/
    if(head==NULL)
    {
        printf("\nlist null!\n");
        goto end;
    }
    while(p!=NULL)/*当记录的指针不为空时*/
    {
        if(p->num==b)/*显示找到的记录信息*/
        {
            printf("\n****************************所查找学生的数据**********************************\n");
            printf("|class|num|name|math|c_prog| eng | polity | sport |总分| ave | order | \n");
            printf("\n%-5s%-5d%-5s", p->class_0, p->num,p->name);
            for(i=0;i<5;i++)
            {    
                printf("%-7.1f", p->score[i]);
            }
            printf("%-7.1f%-7.1f%d", p->total, p->ave,p->order);
            printf("\n********************************end*******************************\n");
            break;
        }
        else
            p=p->next; /*移动指针,指向下一结点,继续查找*/
    }
        
    if(p==NULL) /*指针为空,说明未能找到所要的结点*/
    printf("\n找不到所需的学号 %d\n",b);
    end:
    printf("");

}

/* 函数功能:按学号查找并修改一个节点数据  */
void ModifyStudent(struct student *head)
{
    struct student *p=head;
    int b;        /*存放学号的变量*/
    printf("输入您要查找学生的学号:\n");
    scanf("%d",&b); /*输入学号*/
    if(head==NULL)
    {
        printf("\nlist null!\n");
        goto end;
    }
    while(p!=NULL)/*当记录的指针不为空时*/
    {
        if(p->num==b)
        {
            printf("请输入新数据:");
            printf("input number of student:");
            scanf("%d",&p->num);
            printf("class:");
            scanf("%s",p->class_0);
            printf("name:");
            scanf("%s",p->name);
            printf("score:");
            for(i=0;i<5;i++)
            {
                printf("%s:",schoolwork[i]);
                scanf("%f",&p->score[i]);
            }
            TotalScore(p);
            break;
        }
        else
            p=p->next; /*移动指针,指向下一结点,继续查找*/
        }
    if(p==NULL) /*指针为空,说明未能找到所要的结点*/
    printf("\n找不到所需的学号 %d\n",b);
    end:
    printf("");
}

/*函数功能:按学号插入一个节点数据*/
struct student *InsertStudent(struct student *head)
{
    struct student *p1,*p2,*info,*k; /*p指向插入位置,q是其前驱,info指新插入记录*/
    int n=0;
    p1=head;
    printf("\ninput a new record:\n");
    info=(struct student *)malloc(LEN); /*申请空间*/
    if(!info)
    {
        printf("\nOut of memory"); /*如没有申请到,内存溢出*/
        return NULL; /*返回空指针*/
    }
    printf("input number of student:");
    printf("if number is 0,stop inputing.\n");
    scanf("%d",&info->num);
    printf("class:");
    scanf("%s",info->class_0);
    printf("name:");
    scanf("%s",info->name);
    printf("score:");
    for(i=0;i<5;i++)
    {
        printf("%s:",schoolwork[i]);
        scanf("%f",&info->score[i]);
    }
    TotalScore(info);
    info->next=NULL; /*设后继指针为空*/
    p1=p2=head; /*将指针赋值给p1,p2*/
    if(head==NULL)/*链表为空*/
    {
        head=info;
        info->next=NULL;
    }
    else
    {
        while((info->total<p1->total)&&(p1->next!=NULL))
        {
            p2=p1; /*使p2指向p1刚才的结点*/
            p1=p1->next;
        } /*使p1后移一个结点*/
        if(info->total>=p1->total)
        {
            if(head==p1)
            {
                head=info;
                info->next=p1;
            } /*info作为头指针,p1作为info的后继指针*/
            else
            {
                p2->next=info; /*p2的后继指针是info*/
                info->next=p1;
            }
        } /*info的后继指针是p1*/
        else
        {
            p1->next=info;
            info->next=NULL;
        }/*p1的后继指针是info,info的后继指针是空*/
    }
    k=head;
    while(k!=NULL) /*重新赋予排序号(排序号因为被插入新数据而改变)*/
    {
        n++;
        k->order=n;
        k=k->next;
    }
    printf("\n ----have inserted the student----\n");
    return(head);       /*返回头指针*/
}  

/*函数功能:按学号删除一个节点数据*/
struct student *DeleteStudent(struct student *head)
{
    struct student *p1,*p2;
    int number;
    printf("请输入要删除学生的学号:");
    scanf("%d",&number);
    if(head==NULL)
    {
        printf("\nlist null!\n");
        goto end;
    }
    p1=head;
    while(number!=p1->num&&p1->next!=NULL) /*p1指向的不是所要找的结点,并且后面还有结点*/
    {
        p2=p1;
        p1=p1->next;
    }/*p1向后移一个结点*/
    if(number==p1->num)
    {
        if(p1==head)
            head=p1->next; /*若p1指向的是首结点,把第二个结点地址赋给head*/
        else
            p2->next=p1->next; /*否则将下一结点地址付给前一结点地址*/
        printf("已经删除学号为%d的学生。",number);
        n=n-1;
    }
    else printf("找不到学号为%d的学生。\n",number);/*找不到该结点*/
    end:
    return(head);
}

/* 函数功能:显示菜单并获得用户键盘输入的选项 */
int menu_select(void)
{
    int digit;
    printf("\n      *************学生成绩管理系统*************\n");
    printf("      &            1.添加学生的数据            &\n");
    printf("      &           2.输出全部学生数据           &\n");
    printf("      &           3.查询一个学生数据           &\n");
    printf("      &           4.修改一个学生数据           &\n");
    printf("      &           5.插入一个学生数据           &\n");
    printf("      &           6.删除一个学生数据           &\n");
    printf("      &               7.保存数据               &\n");
    printf("      &               8.读取数据               &\n");
    printf("      &           0.退出学生记录系统           &\n");
    printf("      ******************************************\n");
    printf("请选择:");
    scanf("%d", &digit); /*在%c前面加一个空格,将存于缓冲区中的回车符读入*/
    return digit;
}

 
/*函数功能:保存数据到文件模块*/
void SaveFile(struct student *head)
{
    FILE *fp; /*定义指向文件的指针*/
    struct student *p; /* 定义移动指针*/
    char outfile[20]; /*保存输出文件名*/
    printf("Enter outfile name,for example G:\\f1\\score.txt:\n");
    scanf("%s",outfile);
    if((fp=fopen(outfile,"wb"))==NULL) /*为输出打开一个二进制文件,如没有则建立*/
    {
        printf("Can not open file\n");
        return;
    }
    printf("\nSaving file......\n"); /*打开文件,提示正在保存*/
    p=head; /*移动指针从头指针开始*/
    while(p!=NULL) /*如p不为空*/
    {
        fwrite(p,LEN,1,fp);  /*写入一条数据*/
        p=p->next;  /*指针后移*/
    }
    fclose(fp); /*关闭文件*/
    printf("-----Save success!!-----\n"); /*显示保存成功*/
}


/*导入信息模块*/
struct student *LoadFile(void)
{
    struct student *p,*q,*h=NULL; /*定义记录指针变量*/
    FILE *fp; /* 定义指向文件的指针*/
    char infile[20]; /*保存文件名*/
    printf("Enter infile name,for example G:\\f1\\score.txt:\n");
    scanf("%s",infile); /*输入文件名*/
    if((fp=fopen(infile,"rb"))==NULL) /*打开一个二进制文件,为读方式*/
    {
        printf("Can not open file\n"); /*如不能打开,返回头指针*/
        return h;
    }
    printf("\n -----Loading file!-----\n");
    p=(struct student *)malloc(LEN); /*申请空间*/
    if(!p)
    {
        printf("Out of memory!\n"); /*如没有申请到,则内存溢出*/
        return h; /*返回空头指针*/
    }
    h=p; /*申请到空间,将其作为头指针*/
    while(!feof(fp)) /*循环读数据直到文件尾结束*/
    {
        if(1!=fread(p,LEN,1,fp))
        break; /*如果没读到数据,跳出循环*/
    p->next=(struct student *)malloc(LEN); /*为下一个结点申请空间*/
    if(!p->next)
    {
        printf("Out of memory!\n"); /*如没有申请到,则内存溢出*/
        return h;
    }
    q=p; /*保存当前结点的指针,作为下一结点的前驱*/
    p=p->next; /*指针后移,新读入数据链到当前表尾*/
    }
    q->next=NULL; /*最后一个结点的后继指针为空*/

    fclose(fp); /*关闭文件*/
    printf("Load success!!!\n");
    return h; /*返回头指针*/
}


void main()
{
    struct student *head=NULL; /*链表定义头指针*/
    int digit;
    for(;;) /*无限循环*/
    {
        digit=menu_select();
        switch(digit)/*调用主菜单函数,返回值整数作开关语句的条件*/
        {
            case 1:head=create();TotalScore(head);SortScore(head);break; /*创建链表*/
            case 2:SortScore(head);PrintScore(head);break; /*显示全部记录*/
            case 3:SearchStudent(head);break; /*查找记录*/
            case 4:ModifyStudent(head);break; /*修改记录*/
            case 5:head=InsertStudent(head); break; /*插入记录*/
            case 6:head=DeleteStudent(head);break; /*删除记录*/
            case 7:SaveFile(head);break; /*保存数据*/
            case 8:head=LoadFile();break; /*读取数据*/
            case 0:exit(0); /*如菜单返回值为0程序结束*/
        }
    }
}
2008-09-19 00:06
随心
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:12
帖 子:2577
专家分:250
注 册:2007-8-12
收藏
得分:0 
这么长,建议你把问题缩小一下再发上来
否则你会错失很多人的问答

天之道,利而不害。圣人之道,为而不争。信言不美,美言不信。善者不辩,辩者不善。知者不博,博者不知。
2008-09-19 02:22
johnsy1
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2008-9-17
收藏
得分:0 
/*函数功能:按学号插入一个节点数据*/ 这一部分无法执行
/*函数功能:按学号插入一个节点数据*/ 这一部分无法执行
2008-09-19 07:33
hjselena
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2007-5-31
收藏
得分:0 
还是不行是因为在赋值后接着调用了TotalScore(info);把info->next=NULL; /*设后继指针为空*/ 这一句话移到TotalScore(info);前就OK了。这段代码并不是实现按学号插入一个节点数据,而是按平均成绩来插入,请问楼主是否仔细读过代码?如果这样的代码都不能读下来,以后何况是写代码呢
2008-09-19 13:12
johnsy1
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2008-9-17
收藏
得分:0 
太感谢你了,刚学没多久,不太会链表
2008-09-19 17:34
快速回复:求救高手,我这程序就是执行其中一个功能老是出错,能不能指正一下
数据加载中...
 
   



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

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