| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1254 人关注过本帖
标题:各位楼主帮我看看那里出问题了
只看楼主 加入收藏
啊蒋
Rank: 1
等 级:新手上路
帖 子:31
专家分:0
注 册:2017-2-26
结帖率:70%
收藏
 问题点数:0 回复次数:3 
各位楼主帮我看看那里出问题了
  #include<stdio.h>
#include<stdlib.h>
#define MAX 20
#define SUB 3
typedef struct Student Student;
typedef struct Class Class;



struct Student
{
    char name[MAX];
    double subject[SUB];

    double student_average;
};
struct Class
{
    char class_name[MAX];
    Student** student;
    Class* pnext;
    double class_average[SUB];
    int student_number;
   

};
int main()
{
    Class *class_one;
    FILE* pfile=NULL;
    int i=0;
    char* filename="D:\\蒋臣健\\学生成绩.bin";       //这个文件里存了一些学生的成绩,我写这个程序就是先创建个结构,先给它赋初值,然后再从文件中读取一个结构出来,这个文件里面的数据,也是 我编                   了另外一个结构,存进去的,用的结构成员跟这个程序的结构成员是一样的
    class_one=NULL;
    if(!(pfile=fopen(filename,"rb")))
    {
        printf("Failed to open");
        exit(1);
    }
    if(setvbuf(pfile,NULL,_IOFBF,BUFSIZ))
    {
        printf("file buffering failed");
        exit(2);
    }
    class_one=(Class*)calloc(1,sizeof(Class));
    class_one->student=calloc(MAX,sizeof(Student*));

    if(!(class_one->student))
    {
        printf("失败1");
        exit(1);
    }

   
    class_one->student[0]=(Student*)calloc(1,sizeof(Student));
    if(!(class_one->student[0]))
    {
        printf("shibai");
        exit(2);
    }
    printf("请输入");
    scanf("%s",class_one->student[0]->name );

   
    printf("请输入");
    scanf("%lf",&class_one->student[0]->student_average);
    printf("请输入");
    fflush(stdin);
    if(fread(class_one,sizeof(Class),1,pfile)<1)
        printf("shibai2");
   

        printf("%s ",class_one->student[0]->name);
   
    for(i=0;i<MAX;i++)
{
        free(class_one->student[i]);
        class_one->student[0]=NULL;

   
    }

    free(class_one->student);
    class_one->student=NULL;
    free(class_one);
    class_one=NULL;
    fclose(pfile);
    pfile=NULL;
    return 0;
}
图片附件: 游客没有浏览图片的权限,请 登录注册
搜索更多相关主题的帖子: include double 
2017-04-01 00:10
wp231957
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:神界
等 级:贵宾
威 望:423
帖 子:13688
专家分:53332
注 册:2012-10-18
收藏
得分:0 
你把那个文本文件发出来  再说一下你这个程序想实现神马目标

DO IT YOURSELF !
2017-04-01 12:05
啊蒋
Rank: 1
等 级:新手上路
帖 子:31
专家分:0
注 册:2017-2-26
收藏
得分:0 
//就是用这个程序把结构写到这个文件中的,这个程序我试了好几次都是可以运行的,上面那个程序就不行了

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
#define MAX 20
#define SUB 3

typedef struct Student Student;
typedef struct Class Class;



struct Student
{
    char name[MAX];
    double subject[SUB];     

    double student_average;
};
struct Class
{
    char class_name[MAX];
    Student** student;      
    Class* pnext;
    double class_average[SUB];
    int student_number;
   

};

FILE* pfile=NULL;
char* filename="D:\\蒋臣健\\学生成绩.bin";


double get_student(Student* student,double class_average[],size_t n);   //返回一个学生的三个科目的分数平均值
Class* get_class(void);
void free_memory(Class* pfirst);
void list_class(Class* first);
int camp(char* name,Class* first);
Class* get_class(void)
{

    int number=0;
    char ch='a';
    Class* pclass=NULL;
    if(!(pclass=calloc(1,sizeof(Class))))
    {
        printf("班级分配内存失败");
        exit(1);
    }
    pclass->student=calloc(MAX,sizeof(Student*));
    if(!(pclass->student))
    {
        printf("存放结构的指针的内存失败");
        exit(1);
    }

   
    printf("请输入班级名字:");
    scanf("%s",pclass->class_name);
    do
    {
        pclass->student[number]=calloc(1,sizeof(Student));
        if(!(pclass->student[number]))
        {
            printf("学生结构内存分配失败");
            exit(2);
        }
        pclass->student[number]->student_average=get_student(pclass->student[number],pclass->class_average,MAX);
        ++number;
   
        ++pclass->student_number;
        fflush(stdin);
        printf("还要输入学生成绩吗?");
        scanf("%c",&ch);
    }while(ch=='y');
    return pclass;

}
double get_student(Student* student,double class_average[],size_t n)
{
        int i=0;
   
   double average=0.0;
   double sum=0.0;
    fflush(stdin);



    printf("请输入学生姓名:");
    scanf("%s",student->name);

        for(i=0;i<SUB;i++)
        {
            switch(i)
            {
                case 0:
                    printf("please enter %s Chinese scores",student->name);
                    
                    break;

                case 1:
                    printf("please enter %s Maths scores",student->name);
                    break;
                case 2:
                    printf("please enter %s English scores",student->name);
                    break;
                default:
                break;
            }
        
            scanf("%lf",&student->subject[i]);
            class_average[i]+=student->subject[i];
   
        
        
        sum+=student->subject[i];
        
        }
    average=sum/SUB;
    return average;
 
}

    void list_class(Class* first)
    {
        Class* pcurrent=first;
        int b=0;
        int i=0;
        while(pcurrent!=NULL)
        {

            printf("%s  ",pcurrent->class_name);
            for(b=0;b<pcurrent->student_number;b++)
            {

                for(i=0;i<SUB;i++)
                {

                    switch(i)
                    {
                        case 0:
                        printf("%s的语文成绩为%.2lf  ",pcurrent->student[b]->name,pcurrent->student[b]->subject[i]);
                        
                        break;

                        case 1:
                        printf("%s的数学成绩为%.2lf  ",pcurrent->student[b]->name,pcurrent->student[b]->subject[i]);
                        break;
                        case 2:
                        printf("%s的英语成绩为%.2lf\n",pcurrent->student[b]->name,pcurrent->student[b]->subject[i]);
                        break;
                         default:
                        break;
                    }
            
               
                }
             printf("%s的平均成绩为%.2lf\n",pcurrent->student[b]->name,pcurrent->student[b]->student_average);
            }
            
            for(i=0;i<SUB;i++)
            {
                switch(i)
                {
                    case 0:
                        printf("%s的语文总平均分为%.2lf  ",pcurrent->class_name,pcurrent->class_average[i]);
                        break;
                    case 1:
                        printf("%s的数学总平均分为%.2lf  ",pcurrent->class_name,pcurrent->class_average[i]);
                        break;
                    case 2:
                        printf("%s的英文总平均分为%.2lf\n",pcurrent->class_name,pcurrent->class_average[i]);
                        break;
                    default:
                        break;
                }
            }
            
            printf("班级总人数%d",pcurrent->student_number);
               
            pcurrent=pcurrent->pnext;
        
        }
    }


    void free_memory(Class* pfirst)
        {
            int b=0;
            Class* pcurrent=pfirst;
   
            while(pcurrent!=NULL)
            {
                Class* ptemp=NULL;
                ptemp=pcurrent;
                for(b=0;b<pcurrent->student_number;b++)
                {
                    free(pcurrent->student[b]);
                    pcurrent->student[b]=NULL;
                }
                free(pcurrent->student);
                pcurrent->student=NULL;
                pcurrent=pcurrent->pnext;
                free(ptemp);
                ptemp=NULL;
            }
        }


int main()
{
    char word='y';
    Class class_one;
    do
    {
        

        Class* pfirst=NULL;
   
        Class* previous=NULL;
        Class* pcurrent=NULL;
        int i=0;
        int b=0;
        char ch=0;
        int student_number=0;
        int dex=0;
        char name[MAX];
        if(!(pfile=fopen(filename,"wb")))
        {
            printf("Failed to open");
            exit(1);
        }
        if(setvbuf(pfile,NULL,_IOFBF,BUFSIZ))
        {
            printf("file buffering failed");
            exit(2);
        }

        do
        {

            
            pcurrent=get_class();
            if(fwrite(pcurrent,sizeof(Class),1,pfile)<1)
                printf("写入失败");
            for(b=0;b<SUB;b++)
        
            pcurrent->class_average[b]=pcurrent->class_average[b]/pcurrent->student_number;

            printf("sizeof(pcurrent)=%d",sizeof(Student));
            
            

            

            if(!pfirst)
                pfirst=previous=pcurrent;
            else
            {
                previous->pnext=pcurrent;
                previous=pcurrent;
                previous->pnext=NULL;
            }

   
        
        
            
        
        fflush(stdin);
            printf("还想输入班级吗");
            scanf("%c",&ch);
        }while(ch=='y');

    list_class(pfirst);
    fflush(stdin);
    printf("请输入你想要找的学生:");
    scanf("%s",name);
    if(!(dex=camp(name,pfirst)))
        printf("没有这个人");
    pfirst=NULL;
        fclose(pfile);
    if(!(pfile=fopen(filename,"rb")))
        {
            printf("Failed to open");
            exit(1);
        }
        if(setvbuf(pfile,NULL,_IOFBF,BUFSIZ))
        {
            printf("file buffering failed");
            exit(2);
        }
        fread(&class_one,sizeof(Class),1,pfile);
        printf("sizeof=%d",sizeof(class_one));
        for(i=0;i<class_one.student_number;i++)
        {
            printf("%lf ",class_one.student[i]->student_average);
        }

   
    free_memory(pfirst);
        pfirst=NULL;
        fclose(pfile);
        pfile=NULL;
        fflush(stdin);
        printf("还想再来一次");
        scanf("%c",&word);

   
    }while(tolower(word)=='y');
        
        return 0;
}
int camp(char* name,Class* first)
{
    Class* pcurrent=first;
    int i=0;
    int b=0;
    while(pcurrent!=NULL)
    {
        for(b=0;b<pcurrent->student_number;b++)
        {
            if(strcmp(name,pcurrent->student[b]->name)==0)
            {
                printf("%s在%s\n",pcurrent->student[b]->name,pcurrent->class_name);
                for(i=0;i<SUB;i++)
                {
                        switch(i)
                    {
                        case 0:
                        printf("%s的语文成绩为%.2lf  ",pcurrent->student[b]->name,pcurrent->student[b]->subject[i]);
                        
                        break;

                        case 1:
                        printf("%s的数学成绩为%.2lf  ",pcurrent->student[b]->name,pcurrent->student[b]->subject[i]);
                        break;
                        case 2:
                        printf("%s的英语成绩为%.2lf\n",pcurrent->student[b]->name,pcurrent->student[b]->subject[i]);
                        break;
                         default:
                        break;
                    }
                }
                return 1;
            }
        }
        pcurrent=pcurrent->pnext;
    }
    return 0;

}
2017-04-01 22:33
啊蒋
Rank: 1
等 级:新手上路
帖 子:31
专家分:0
注 册:2017-2-26
收藏
得分:0 
回复 2楼 wp231957
//就是用这个程序把结构写到这个文件中的,这个程序我试了好几次都是可以运行的,上面那个程序就不行了

#include<stdio.h>     
#include<stdlib.h>
#include<ctype.h>
#include<string.h>        
#define MAX 20   //
#define SUB 3    //科目数

typedef struct Student Student;
typedef struct Class Class;



struct Student     //定义一个学生的结构
{
    char name[MAX];    //学生的名字
    double subject[SUB];     //三个科目的分数

    double student_average;   //一个学生的的平均分
 };
struct Class       //定义一个班级
{
    char class_name[MAX];  //班级名字
    Student** student;       //存储学生结构的指针
    Class* pnext;            
    double class_average[SUB];  
    int student_number;     //一个班级的人数
   

};

FILE* pfile=NULL;
char* filename="D:\\蒋臣健\\学生成绩.bin";   //储存班级结构的文件


double get_student(Student* student,double class_average[],size_t n);   //返回一个学生的三个科目的分数平均值
Class* get_class(void);       //创建一个班级结构
void free_memory(Class* pfirst);  //释放堆内存
void list_class(Class* first);    //列出班级结构
int camp(char* name,Class* first);    //比较输入的名字在不在班级里面                                                                                                                                                                                                                                                                 
Class* get_class(void)
{

    int number=0;  //初始化一个班级的学生数目
    char ch='a';
    Class* pclass=NULL;
    if(!(pclass=calloc(1,sizeof(Class))))
    {
        printf("班级分配内存失败");
        exit(1);
    }
    pclass->student=calloc(MAX,sizeof(Student*));   //存储20个指向学生结构指针的内存
    if(!(pclass->student))
    {
        printf("存放结构的指针的内存失败");
        exit(1);
    }

   
    printf("请输入班级名字:");
    scanf("%s",pclass->class_name);
    do   //用循环输入一个班级的成员
    {
        pclass->student[number]=calloc(1,sizeof(Student));   //存储一个学生结构
        if(!(pclass->student[number]))
        {
            printf("学生结构内存分配失败");
            exit(2);
        }
        pclass->student[number]->student_average=get_student(pclass->student[number],pclass->class_average,MAX);   //调用输入一个学生结构的函数,把返回值存到一个学生的平均分
        ++number;
   
        ++pclass->student_number;    //记录班级人数
        fflush(stdin);
        printf("还要输入学生成绩吗?");
        scanf("%c",&ch);
    }while(ch=='y');
    return pclass;      

}
double get_student(Student* student,double class_average[],size_t n)
{
        int i=0;
   
   double average=0.0;         
   double sum=0.0;
    fflush(stdin);



    printf("请输入学生姓名:");
    scanf("%s",student->name);

        for(i=0;i<SUB;i++)     //在一个循环里输入一个学生的三个科目的分数
        {
            switch(i)         //控制三个科目分数的顺序
            {
                case 0:
                    printf("please enter %s Chinese scores",student->name);
                    
                    break;

                case 1:
                    printf("please enter %s Maths scores",student->name);
                    break;
                case 2:
                    printf("please enter %s English scores",student->name);
                    break;
                default:
                break;
            }
        
            scanf("%lf",&student->subject[i]);
            class_average[i]+=student->subject[i];   // 记录一个班级每个科目的平均分
   
        
        
        sum+=student->subject[i];   //累加一个学生科目的分数
        
        }
    average=sum/SUB;      //记录一个学生的平均分
    return average;
 
}

    void list_class(Class* first)
    {
        Class* pcurrent=first;      //把头指针复制给临时变量
        int b=0;
        int i=0;
        while(pcurrent!=NULL)    //在一个循环里输出班级的成员
        {

            printf("%s  ",pcurrent->class_name);
            for(b=0;b<pcurrent->student_number;b++)  //for循环输出一个班级的学生,学生的每科分数
            {

                for(i=0;i<SUB;i++)
                {

                    switch(i)   //控制输入每科成绩的顺序      
                    {
                        case 0:
                        printf("%s的语文成绩为%.2lf  ",pcurrent->student[b]->name,pcurrent->student[b]->subject[i]);
                        
                        break;

                        case 1:
                        printf("%s的数学成绩为%.2lf  ",pcurrent->student[b]->name,pcurrent->student[b]->subject[i]);
                        break;
                        case 2:
                        printf("%s的英语成绩为%.2lf\n",pcurrent->student[b]->name,pcurrent->student[b]->subject[i]);
                        break;
                         default:
                        break;
                    }
            
               
                }
             printf("%s的平均成绩为%.2lf\n",pcurrent->student[b]->name,pcurrent->student[b]->student_average);
            }
            
            for(i=0;i<SUB;i++)  //输出一个班级的每个科目的总平均分
            {
                switch(i)
                {
                    case 0:
                        printf("%s的语文总平均分为%.2lf  ",pcurrent->class_name,pcurrent->class_average[i]);
                        break;
                    case 1:
                        printf("%s的数学总平均分为%.2lf  ",pcurrent->class_name,pcurrent->class_average[i]);
                        break;
                    case 2:
                        printf("%s的英文总平均分为%.2lf\n",pcurrent->class_name,pcurrent->class_average[i]);
                        break;
                    default:
                        break;
                }
            }
            
            printf("班级总人数%d",pcurrent->student_number);
               
            pcurrent=pcurrent->pnext;  
        
        }
    }


    void free_memory(Class* pfirst)
        {
            int b=0;
            Class* pcurrent=pfirst;
   
            while(pcurrent!=NULL)
            {
                Class* ptemp=NULL;
                ptemp=pcurrent;
                for(b=0;b<pcurrent->student_number;b++)
                {
                    free(pcurrent->student[b]);
                    pcurrent->student[b]=NULL;
                }
                free(pcurrent->student);
                pcurrent->student=NULL;
                pcurrent=pcurrent->pnext;
                free(ptemp);
                ptemp=NULL;
            }
        }


int main()
{
    char word='y';
    Class class_one;
    do
    {
        

        Class* pfirst=NULL;
   
        Class* previous=NULL;
        Class* pcurrent=NULL;
        int i=0;
        int b=0;
        char ch=0;
        int student_number=0;
        int dex=0;            //储存比较函数的返回值
        char name[MAX];
        if(!(pfile=fopen(filename,"wb")))
        {
            printf("Failed to open");
            exit(1);
        }
        if(setvbuf(pfile,NULL,_IOFBF,BUFSIZ))
        {
            printf("file buffering failed");
            exit(2);
        }

        do    //用循环生成班级结构链表
        {

            
            pcurrent=get_class();     //获取一个班级结构
            if(fwrite(pcurrent,sizeof(Class),1,pfile)<1)   //把班级结构写入文件
                printf("写入失败");
     ;
            
            

            

            if(!pfirst)
                pfirst=previous=pcurrent;
            else
            {
                previous->pnext=pcurrent;
                previous=pcurrent;
                previous->pnext=NULL;
            }

   
        
        
            
        
        fflush(stdin);    //清楚输入缓存区
            printf("还想输入班级吗");
            scanf("%c",&ch);
        }while(ch=='y');

    list_class(pfirst);                                      
    fflush(stdin);
    printf("请输入你想要找的学生:");
    scanf("%s",name);
    if(!(dex=camp(name,pfirst)))
        printf("没有这个人");
    pfirst=NULL;
        fclose(pfile);
    if(!(pfile=fopen(filename,"rb")))      
        {
            printf("Failed to open");
            exit(1);
        }
        if(setvbuf(pfile,NULL,_IOFBF,BUFSIZ))
        {
            printf("file buffering failed");
            exit(2);
        }
        fread(&class_one,sizeof(Class),1,pfile);   //从文件中读取结构
        printf("sizeof=%d",sizeof(class_one));
        for(i=0;i<class_one.student_number;i++)
        {
            printf("%lf ",class_one.student[i]->student_average);
        }

   
    free_memory(pfirst);
        pfirst=NULL;
        fclose(pfile);
        pfile=NULL;
        fflush(stdin);
        printf("还想再来一次");
        scanf("%c",&word);

   
    }while(tolower(word)=='y');
        
        return 0;
}
int camp(char* name,Class* first)
{
    Class* pcurrent=first;
    int i=0;
    int b=0;
    while(pcurrent!=NULL)     //在班级链表循环里比较输入的学生名字是否在班级里
    {
        for(b=0;b<pcurrent->student_number;b++)    //用循环遍历一个班级的所有学生跟输入学生作比较
        {
            if(strcmp(name,pcurrent->student[b]->name)==0)  //如果有的话就输出这个学生的成绩
            {
                printf("%s在%s\n",pcurrent->student[b]->name,pcurrent->class_name);
                for(i=0;i<SUB;i++)
                {
                        switch(i)
                    {
                        case 0:
                        printf("%s的语文成绩为%.2lf  ",pcurrent->student[b]->name,pcurrent->student[b]->subject[i]);
                        
                        break;

                        case 1:
                        printf("%s的数学成绩为%.2lf  ",pcurrent->student[b]->name,pcurrent->student[b]->subject[i]);
                        break;
                        case 2:
                        printf("%s的英语成绩为%.2lf\n",pcurrent->student[b]->name,pcurrent->student[b]->subject[i]);
                        break;
                         default:
                        break;
                    }
                }
                return 1;
            }
        }
        pcurrent=pcurrent->pnext;
    }
    return 0;

}
2017-04-03 21:25
快速回复:各位楼主帮我看看那里出问题了
数据加载中...
 
   



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

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