| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 447 人关注过本帖
标题:] 为什么不能运行自定义load函数,调用保存的数据
只看楼主 加入收藏
冬天的梦
Rank: 1
等 级:新手上路
帖 子:5
专家分:5
注 册:2014-12-16
结帖率:66.67%
收藏
已结贴  问题点数:15 回复次数:1 
] 为什么不能运行自定义load函数,调用保存的数据
程序代码:
// 实训4.cpp : 定义控制台应用程序的入口点。
//

// 实训1.cpp : 定义控制台应用程序的入口点。
//

// 实训2.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "stdlib.h"
#include "string.h"
struct STU
{
    int xuehao;
    char name[30];
    float daolun;
    float cyuyan;
    float english;
    float math;
    float sum;
    float avg;
    struct STU *next;
};
struct STU * input (void);
void list (struct STU * p);
struct STU * save(struct STU *p);
struct STU * load(struct STU *p);
void clear_list();

 void find(struct STU *p);
void total(struct STU *p);
struct STU *head,*tail=NULL;
int n=0;
struct STU *p=NULL;//p是头节点
struct STU *q=NULL;//保存文件地址
struct STU *a=NULL;//加载后文件地址
int _tmain(int argc, _TCHAR* argv[])
{
    while(1)
    {
    printf("Hello Worlf!\n");
    printf("\n1:载入成绩");
    printf("\n2:列出成绩");
    printf("\n3:录入成绩");
    printf("\n4:查询成绩");
    printf("\n5:成绩统计");
    printf("\n6:修改成绩");
    printf("\n7:另存为成绩");
    printf("\n8:加密成绩");
    printf("\n9:排名次");
    printf("\n0:退出");
    
    
    
    int v;
    printf("请选择");
    scanf("%d",&v);

 fflush(stdin);
    switch(v){
        case 1:
            p=input();
            
            break;
case 2:list(p);break;
case 3:q=save(p);break;
case 4:a=load(q);break;
case 5:find(p);break;
case 6:total(p);break;
    }
    }
    return 0;
}


struct STU * input (void)
{

    
    while(true)
    {
            printf("\n第%d位同学:",n+1);
            struct STU *p=(struct STU *)malloc(sizeof(struct STU ));//临时节点,每个同学就新建一个节点
            p->next=NULL;
            printf("\n输入学号");
scanf("%f",&p->xuehao);//当输%d再输出%s,必须getchar();,不然直接跳过
getchar();
printf("\n输入姓名");
gets(p->name);
printf("\n输入导论");
scanf("%f",&p->daolun);
printf("\n输入c语言");
scanf("%f",&p->cyuyan);
printf("\n输入英语");
scanf("%f",&p->english);
printf("\n输入数学");
scanf("%f",&p->math);
//加入链表中
                if (head==NULL)//链表还是空的
                {
                    tail=head=p;
                }
                else//链表中已有信息
                {
                    tail->next=p;
                    tail = p;
                }
                n++;
                
                getchar();//去掉缓存中的内容

                char c;
                printf("\n\n还要输入吗(Y/N)");
                scanf("%c", &c);
                if (c!='y' && c!='Y')
                    break;

                getchar();//去掉缓存中的内容                

    }
    return p;
}
void list (struct STU * p)
{
    p = head;//p指向链表头
            //遍历链表中的每一个元素,并打印其信息
            while (p!=NULL)
            {
                    printf("\n输入学号%.0f",p->xuehao);

printf("\t输入姓名%s\t",p->name);

printf("\n输入导论%.1f",p->daolun);

printf("\t输入c语言%.1f",p->cyuyan);

printf("\t输入英语%.1f",p->english);

printf("\t输入数学%.1f",p->math);



                p=p->next;
            }
        }
struct STU * save(struct STU *p)
    {//保存            
            FILE* fs;
            fs=fopen("d:\\score.dat","wb");

            fprintf(fs,"%d",n);//先保存人数到文件

        p = head;//p指向链表头
            //遍历链表中的每一个元素,分别把它们写入到文件中
            while (p!=NULL)
            {
                //把p所指的结构体写入文件中
                int len = sizeof(struct STU);
                fwrite(p, len ,1 , fs);    //把p所指向的内存块(长为len)的1块存入fs指定的文件        
                p=p->next;
            }            
            
            fclose(fs);
            return q;
        }

struct STU *load(struct STU *q)

    {//载入
            clear_list();//清除原有的链表中数据

            FILE* fs;
            fs=fopen("c:\\score.dat","rb");

            fscanf(fs,"%d",&n);//读入人数

            //要逐个读入n个块的结构内容
            for(int i=0; i<n;i++)
            {
                //分配一个结构空间,用来接收从文件读入的一个结构数据
                struct STU*q =  (struct STU*)  malloc( sizeof(struct STU));
                int len = sizeof(struct STU);
                fread(q,len,1,fs);

            fclose(fs);
            return a;
            }
        }
void clear_list()
{

    //清除所有的成绩 (从头开始清除)
    struct STU  *p = head;//p指向链表头
    while (p!=NULL)
    {
        head = head->next;
        free(p);
        p=head;
    }
    n=0;
}

 void find(struct STU *p)
    {//删除一个同学的信息
            char st_name[20];
            printf("\n输入要查询同学的姓名:");
            gets(st_name);

            p = head;//p指向链表头
            struct STU *q=NULL;//指向上一个无素
            //遍历链表中的元素,找到这同学的信息结构体(指针p指向它)
            while (p!=NULL)
            {
                //是不是要找的学生
                if (strcmp(st_name, p->name)==0)
                {
                    printf("\n输入学号%.0f",p->xuehao);

printf("\t输入姓名%s\t",p->name);

printf("\n输入导论%.1f",p->daolun);

printf("\t输入c语言%.1f",p->cyuyan);

printf("\t输入英语%.1f",p->english);

printf("\t输入数学%.1f",p->math);


                    //把找到的这个同学的结构体从链表中删除,并回收其内存
                    //if (q==NULL)//说明:p指向的结构体是头结构体
                    //{
                    //    head=head->next;//把头指针指向新的头结构
                    //    free(p);
                    //}
                    //else
                    //{
                    //    q->next=p->next;
                    //    free(p);
                    //}
                    break;
                }
                q=p;
                p=p->next;
            }
        }
  void total(struct STU *p)
        {
    p = head;//p指向链表头
            //遍历链表中的每一个元素,并打印其信息
    p->sum=0;
            while (p!=NULL)
            {
                
                    p->sum+=p->math;



                p=p->next;
            }
            printf("%.1f",p->sum);
        }
搜索更多相关主题的帖子: 应用程序 控制台 实训 
2015-01-14 22:34
yahwei
Rank: 7Rank: 7Rank: 7
来 自:湖~
等 级:黑侠
威 望:3
帖 子:145
专家分:644
注 册:2011-11-10
收藏
得分:15 
回复 楼主 冬天的梦
这是你自己写的程序代码?请仔细看load函数的返回语句是什么?那个 a 是怎么来的?

[qq]949654600[/qq]
2015-01-15 12:21
快速回复:] 为什么不能运行自定义load函数,调用保存的数据
数据加载中...
 
   



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

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