| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1120 人关注过本帖, 1 人收藏
标题:求解答,问题描述附件一,出问题的代码附件二,学生成绩管理
只看楼主 加入收藏
blackshep
Rank: 2
等 级:论坛游民
帖 子:8
专家分:25
注 册:2017-12-20
结帖率:50%
收藏(1)
已结贴  问题点数:10 回复次数:4 
求解答,问题描述附件一,出问题的代码附件二,学生成绩管理
#include    <stdio.h>
#include    <stdlib.h>
#include    <string.h>

typedef struct student
{
    double id;
    char name[50];
    int score[3];
    int ave;
    int sum;
}STU;
STU s[48];
STU t;
int main()
{
    FILE * fp;
    int i,j,max;
    if((fp=fopen("stud.txt","r"))==NULL)           //读取原始数据
    {
        printf("Can not open the file !!!\n");
        exit(0);
    }
    for(i=0;i<48;i++)                              //原始数据暂存结构体
    {
        fscanf(fp,"%lf%s%d%d%d",&s[i].id,s[i].name,&s[i].score[0],&s[i].score[1],&s[i].score[2]);
        s[i].ave=(s[i].score[0]+s[i].score[1]+s[i].score[2])/3;
        s[i].sum=s[i].score[0]+s[i].score[1]+s[i].score[2];
    }
    fclose(fp);
    for(i=0;i<48;i++)
    {
        printf("%.0f\t%s\t%d\t%d\t%d\t%d\t%d\n",s[i].id,s[i].name,s[i].score[0],s[i].score[1],s[i].score[2],s[i].ave,s[i].sum);
    }



    if((fp=fopen("stud.dat","wb"))==NULL)                   //数据存储二进制文件
    {
        printf("Can not open the file stud.dat!!\n");
        exit(0);
    }
    fwrite(s,sizeof(STU),48,fp);
    fclose(fp);
    for(i=0;i<47;i++)
    {
        max=i;
        for(j=i+1;j<48;j++)
        {
            if(s[max].sum<s[j].sum)
                max=j;
        }
        t=s[i];
        s[i]=s[max];
        s[max]=t;
    }


    if((fp=fopen("score_sorted.txt","w"))==NULL)            // 总成绩排序存储文件
    {
        printf("Can not open the file score_sorted.txt!!!\n");
        exit(0);
    }
    for(i=0,j=1;i<48;i++,j++)
    {
        fprintf(fp,"%d\t\t%.0f\t%s\t%d\t%d\t%d\t%d\t%d\n",j,s[i].id,s[i].name,s[i].score[0],s[i].score[1],s[i].score[2],s[i].ave,s[i].sum);
    }
    if((fp=fopen("score_sorted.dat","wb"))==NULL)                   //二进制文件
    {
        printf("Can not open the file score_sorted.dat!!\n");
        exit(0);
    }
    fwrite(s,sizeof(STU),48,fp);
    fclose(fp);


    if((fp=fopen("Chinese_sorted.txt","w"))==NULL)          //语文成绩排序存储文件
    {
        printf("Can not open the file Chinese_sorted.txt!!!\n");
        exit(0);
    }
    for(i=0;i<47;i++)
    {
        max=i;
        for(j=i+1;j<48;j++)
        {
            if(s[max].score[0]<s[j].score[0])
                max=j;
        }
        t=s[i];
        s[i]=s[max];
        s[max]=t;
    }
    for(i=0,j=1;i<48;i++,j++)
    {
        fprintf(fp,"%d\t\t%.0f\t%s\t%d\t%d\t%d\t%d\t%d\n",j,s[i].id,s[i].name,s[i].score[0],s[i].score[1],s[i].score[2],s[i].ave,s[i].sum);

    }
    if((fp=fopen("Chinese_sorted.dat","wb"))==NULL)                   //二进制文件
    {
        printf("Can not open the file Chinese_sorted.dat!!\n");
        exit(0);
    }
    fwrite(s,sizeof(STU),48,fp);
    fclose(fp);


    if((fp=fopen("Math_sorted.txt","w"))==NULL)            //数学成绩排序存储文件
    {
        printf("Can not open the file Math_sorted.txt!!!\n");
        exit(0);
    }
    for(i=0;i<47;i++)
    {
        max=i;
        for(j=i+1;j<48;j++)
        {
            if(s[max].score[1]<s[j].score[1])
                max=j;
        }
        t=s[i];
        s[i]=s[max];
        s[max]=t;
    }
    for(i=0,j=1;i<48;i++,j++)
    {
        fprintf(fp,"%d\t\t%.0f\t%s\t%d\t%d\t%d\t%d\t%d\n",j,s[i].id,s[i].name,s[i].score[0],s[i].score[1],s[i].score[2],s[i].ave,s[i].sum);

    }
    if((fp=fopen("Math_sorted.dat","wb"))==NULL)                   //二进制文件
    {
        printf("Can not open the file Math_sorted.dat!!\n");
        exit(0);
    }
    fwrite(s,sizeof(STU),48,fp);
    fclose(fp);


    if((fp=fopen("English_sorted.txt","w"))==NULL)          //英语成绩排序存储文件
    {
        printf("Can not open the file English_sorted.txt!!!\n");
        exit(0);
    }
    for(i=0;i<47;i++)
    {
        max=i;
        for(j=i+1;j<48;j++)
        {
            if(s[max].score[2]<s[j].score[2])
                max=j;
        }
        t=s[i];
        s[i]=s[max];
        s[max]=t;
    }
    for(i=0,j=1;i<48;i++,j++)
    {
        fprintf(fp,"%d\t\t%.0f\t%s\t%d\t%d\t%d\t%d\t%d\n",j,s[i].id,s[i].name,s[i].score[0],s[i].score[1],s[i].score[2],s[i].ave,s[i].sum);

    }
    if((fp=fopen("English_sorted.dat","wb"))==NULL)                   //二进制文件
    {
        printf("Can not open the file English_sorted.dat!!\n");
        exit(0);
    }
    fwrite(s,sizeof(STU),48,fp);
    fclose(fp);



    double N;
    char M[20];
    int a;
    printf("Please put in the the student's ID and the subject(Chinese or Math or English or Total) that you want to have a look:\n");
    scanf("%lf",&N);
    scanf("%s",M);
    getchar();
                if((fp=fopen(M,"r"))==NULL)
                {
                    printf("Can not open the file %s!!!\n",M);
                    exit(0);
                }
            for(i=0;i<48;i++)
                {
                    fscanf(fp,"%d\t\t%lf%s%d%d%d%d%d",&a,&t.id,t.name,&t.score[0],&t.score[1],&t.score[2],&t.ave,&t.sum);
                    if(N==t.id)
                    {
                        printf("%d\t\t%.0f\t%s\t%d\t%d\t%d\t%d\t%d\n",a,t.id,t.name,t.score[0],t.score[1],t.score[2],t.ave,t.sum);
                        break;
                    }

                }

            fclose(fp);

    return 0;
}
图片附件: 游客没有浏览图片的权限,请 登录注册
图片附件: 游客没有浏览图片的权限,请 登录注册
搜索更多相关主题的帖子: score max printf the for 
2017-12-21 21:21
吹水佬
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:451
帖 子:10539
专家分:42927
注 册:2014-5-20
收藏
得分:10 
有什么问题?
2017-12-21 22:20
blackshep
Rank: 2
等 级:论坛游民
帖 子:8
专家分:25
注 册:2017-12-20
收藏
得分:0 
回复 2楼 吹水佬
加入那些语句后所有通过这个程序生成的文件全部没有了内容
stud.rar (1.01 KB)
2017-12-22 17:20
吹水佬
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:451
帖 子:10539
专家分:42927
注 册:2014-5-20
收藏
得分:0 
内容都几多,有空可以写个参考。
2017-12-23 14:51
吹水佬
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:451
帖 子:10539
专家分:42927
注 册:2014-5-20
收藏
得分:0 
内容多,没时间测试修改了。
就按自己思路简单写个参考。

/*
    学生成绩管理(从文件导入数据)
    要求:
    从stud.txt导入生成stud.dat(二进制文件),计算总分和平均分。
    分别按总分和各科分数排序(降序)并计算名次,分别生成txt和dat类型文件。
    按选择科目和学号查询并输出各项数据。
*/
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>

typedef struct student
{
    long long id;
    char name[50];
    int score[3];
    int ave;
    int sum;
    int num;
} STU, *PSTU;

typedef struct stulist
{
    STU st;
    struct stulist *next;
} STULIST, *PSTULIST;

FILE *_fopen(char *path, char *mode)
{
    FILE *fp = fopen(path, mode);
    if (fp == NULL)
    {
        printf("打开文件失败!\n");
        exit(0);
    }
    return fp;
}

void *_malloc(int size)
{
    void *p = malloc(size);
    if (p == NULL)
    {
        printf("分配内存失败!\n");
        exit(0);
    }
    return p;
}

void _free(PSTULIST h)
{
    PSTULIST p=h, q;
    while (p)
    {
        q = p->next;
        free(p);
        p = q;
    }
}

void _txt_dat(char *path_txt, char *path_dat)
{
    FILE *fs = _fopen(path_txt, "r");
    FILE *fd = _fopen(path_dat, "wb");
    STU st;
    while (fscanf(fs, "%lld%s%d%d%d", &st.id, st.name, &st.score[0], &st.score[1], &st.score[2]) == 5)
    {
        st.sum = st.score[0] + st.score[1] + st.score[2];
        st.ave = st.sum/3;
        fwrite(&st, sizeof(STU), 1, fd);
    }
    fclose(fs);
    fclose(fd);
}

PSTULIST _read(char *path)
{
    FILE *fp = _fopen(path, "rb");
    STULIST st;
    PSTULIST head=NULL, p, q;
    if (fread(&st, sizeof(STU), 1, fp) == 1)
    {
        head = (PSTULIST)_malloc(sizeof(STULIST));
        q = head;
        *q = st;
        q->next = NULL;
    }
    while (fread(&st, sizeof(STU), 1, fp) == 1)
    {
        p = (PSTULIST)_malloc(sizeof(STULIST));
        *p = st;
        p->next = NULL;
        q->next = p;
        q = p;
    }
    fclose(fp);
    return head;
}

void _write_txt(PSTULIST h, char *path)
{
    FILE *fp = _fopen(path, "w");
    while (h)
    {
        fprintf(fp, "%lld\t%s\t%d\t%d\t%d\t%d\t%d\t%d\n",
                h->st.id, h->st.name,
                h->st.score[0], h->st.score[1], h->st.score[2],
                h->st.ave, h->st.sum, h->st.num);
        h = h->next;
    }
    fclose(fp);
}

void _write_dat(PSTULIST h, char *path)
{
    FILE *fp = _fopen(path, "wb");
    while (h)
    {
        fwrite(h, sizeof(STU), 1, fp);
        h = h->next;
    }
    fclose(fp);
}

PSTULIST _insert_sort(PSTULIST h, PSTULIST x, int offset)
{
    PSTULIST p, q, s;
    p = h;
    q = NULL;
    s = (PSTULIST)_malloc(sizeof(STULIST));
    *s = *x;
    s->next = NULL;
    while (p)
    {
        if (*(int*)((void*)s+offset) >= *(int*)((void*)p+offset)) //降序。
        {
            s->next = p;
            break;
        }
        q = p;
        p = p->next;
    }
    if (q)              //如果上一个记录存在
        q->next = s;    //新记录接上一个记录
    else                //否则
        h = s;          //新记为首记录
    return h;           //返回首地址
}

PSTULIST _sort(PSTULIST h, int offset)
{
    PSTULIST hs = _insert_sort(NULL, h, offset);
    for (h=h->next; h; h=h->next)
        hs = _insert_sort(hs, h, offset);
    return hs;
}

void _num(PSTULIST h, int offset)
{
    int n=1, x = *(int*)((void*)h+offset);
    while (h)
    {
        if (*(int*)((void*)h+offset) != x)
        {
            x = *(int*)((void*)h+offset);
            h->st.num = ++n;
        }
        else
            h->st.num = n;
        h = h->next;
    }
}

void _write_file(PSTULIST h, int offset, char *path_txt, char *path_dat)
{
    PSTULIST hs = _sort(h, offset);
    _num(hs, offset);
    _write_txt(hs, path_txt);
    _write_dat(hs, path_dat);
    _free(hs);
}

void _create_file()
{
    _txt_dat("stud.txt", "stud.dat");
    PSTULIST head = _read("stud.dat");
    _write_file(head, offsetof(STULIST,st.sum), "score_sorted.txt", "score_sorted.dat");
    _write_file(head, offsetof(STULIST,st.score[0]), "Chinese_sorted.txt", "Chinese_sorted.dat");
    _write_file(head, offsetof(STULIST,st.score[1]), "Math_sorted.txt", "Math_sorted.dat");
    _write_file(head, offsetof(STULIST,st.score[2]), "English_sorted.txt", "English_sorted.dat");
    _free(head);
}

void _search()
{
    char filename[4][260]={"Chinese_sorted.dat","Math_sorted.dat","English_sorted.dat","score_sorted.dat"};
    char subject[4][5]={"语文","数学","英语","总分"};
    PSTULIST head;
    long long id;
    int sel=1;
    while (sel)
    {
        printf("选择科目 (1.语文  2.数学  3.英语  4.总分  0.退出): ");
        scanf("%d", &sel);
        while (getchar()!='\n') NULL;
        if (sel<1 || sel>4)
            continue;
        head = _read(filename[sel-1]);
        id = 1;
        while (id)
        {
            printf("输入学号 (0.退出): ");
            scanf("%lld", &id);
            while (getchar()!='\n') NULL;
            if (id < 1)
                continue;
            PSTULIST p = head;
            while (p)
            {
                if (p->st.id == id)
                {
                    printf("学号            姓名      语文      数学      英语      平均分    总分      %s名次\n", subject[sel-1]);
                    printf("%-16lld%-10s%-10d%-10d%-10d%-10d%-10d%-10d\n",
                           p->st.id, p->st.name,
                           p->st.score[0], p->st.score[1], p->st.score[2],
                           p->st.ave, p->st.sum, p->st.num);
                    break;
                }
                p = p->next;
            }
            if (!p)
                printf("找不到学号 %lld 的记录\n", id);
        }
        _free(head);
    }
}

main()
{
    _create_file();
    _search();
}

/* stud.txt
251005180041     章紫东    15    71    89
251005180035     石凌燕    16    71    87
251005180039     徐升    19    75    86
251005180038     王燕    20    65    82
251005180040     李颖    26    79    72
251005180042     俞嘉琳    36    83    79
251005180037     苏燕    49    77    82
251005180034     刘靖    57    67    83
251005180036     王欢    52    82    76
251005180010     雷莹    50    85    76
251005180045     赵水华    74    78    83
251005180047     丰宇    66    70    71
251005180013     于洁    62    69    77
251005180033     黄桂花    66    68    74
251005180030     万梦琪    62    76    87
251005180008     徐星    58    74    69
251005180014     郑文文    50    64    73
251005180031     严风霞    73    74    75
251005180011     陈文凤    65    76    75
251005180043     王欣    77    73    66
251005180028     金乐    62    81    61
251005180007     张文霞    71    75    70
251005180015     占彩红    63    77    71
251005180023     边婷婷    67    65    63
251005180018     吴美华    76    65    70
251005180022     王维    71    70    62
251005180016     程海霞    79    66    55
251005180019     程淑静    81    73    62
251005180017     石锦    67    77    71
251005180026     周瑛    71    71    70
251005180009     陈海燕    69    79    56
251005180012     胡涛英    73    74    75
251005180029     王桂琴    69    67    67
251005180032     严芳芳    72    67    62
251005180003     王茜    72    62    71
251005180020     盛玲玲    85    75    66
251005180002     张继鹏    78    57    64
251005180001     杨根华    75    70    65
251005180021     吴小娟    84    71    62
251005180006     余斌    74    68    54
251005180025     钟婵    75    73    51
251005180024     胡甜甜    81    52    69
251005180046     王鑫栋    81    62    65
251005180004     伍颖    87    69    49
251005180048     熊少波    81    62    67
251005180027     李伟    78    70    61
251005180044     朱火兰    78    76    65
251005180005     龚鑫豪    85    71    69
*/
2017-12-23 22:20
快速回复:求解答,问题描述附件一,出问题的代码附件二,学生成绩管理
数据加载中...
 
   



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

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