⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sheji.c

📁 学生成绩管理系统的开发
💻 C
字号:
 /* sheji.c:按总成绩循序排序*/
#include "stdio.h"
void SortByheji()
 {
    int i,j,k;
    student TmpS;   /*定义进行操作时的临时结构体变量*/
    student s[SIZE];/*SIZE,在shead.h头文件中定义的常量,值为100  */
    int recNumber = 0;
    char DataFile[40] = ""; /*DataFile存储学生成绩信息的文件名*/
    FILE *fp;/*====fp指针指向存储数据的文件名====*/
    /*提示用户输入要进行排序的文件名*/
    printf("\nplease input the name of file where data is stored,end with enter key.\n");
    gets(DataFile);
    /*提示用户输入要进行排序的文件名*/
    while(*DataFile == ('\0'))
    {
        printf("\nplease input the name of file where data is stored,end with enter key.\n");
        gets(DataFile);
    }
    /*以读的方式打开文件,如文件不存在,提示错误*/
    fp=fopen(DataFile,"rb");
    if (fp == NULL)
    {
        printf("\nOpen file %s fail!End with any key\n",DataFile);
        perror("Open file fail");
        getch();
        exit(1);
    }
   /*将文件中要排序的信息存入结构体数组*/
    while((fread(&TmpS,sizeof(student),1,fp)) != (int)NULL)
    {
        s[recNumber].Number = TmpS.Number;
        strcpy(s[recNumber].Name, TmpS.Name);
        s[recNumber].chinesescore = TmpS.chinesescore;
        s[recNumber].mathscore = TmpS.mathscore;
        s[recNumber].heji=TmpS.heji;
        recNumber++;
    }
    fclose(fp);
    /*====如果文件中有记录,则将各条记录按总成绩值排序===*/
    if(recNumber>1)
    {
        /*====用选择排序法进行按总成绩的排序====*/
        for(i=0;i<recNumber-1;i++)
        {
            k = i;
            for(j=i+1;j<recNumber;j++)
            {
                if(s[k].heji<s[j].heji) k = j;
            }
            TmpS.Number = s[k].Number;
            strcpy(TmpS.Name,s[k].Name);
            TmpS.chinesescore = s[k].chinesescore;
            TmpS.mathscore = s[k].mathscore;
            TmpS.heji = s[k].heji;


            s[k].Number = s[i].Number;
            strcpy(s[k].Name,s[i].Name);
            s[k].chinesescore = s[i].chinesescore;
            s[k].mathscore = s[i].mathscore;
            s[k].heji = s[i].heji;

            s[i].Number = TmpS.Number;
            strcpy(s[i].Name,TmpS.Name);
            s[i].chinesescore = TmpS.chinesescore;
            s[i].mathscore = TmpS.mathscore;
            s[i].heji = TmpS.heji;
        }

        /*====将排序好的结构体记录写入文件====*/
        fp=fopen(DataFile,"wb+");
        if (fp == NULL)
        {
            printf("\nSet up file %sfail !end with anykey.\n",DataFile);
            perror("Set up fail");
            getch();
            exit(1);
        }
        for(i=0; i<recNumber; i++)
        {
            if(fwrite(&s[i],sizeof(student),1,fp)!=1)
            {
                printf("\nWrite file %s fail!end with anykey.\n",DataFile);
                perror("Write file fail!");
                    getch();
                    exit(1);
            }
        }
        fclose(fp);
    }
     /*====显示排序后的文件====*/
    printf("the Student's Score in file %s is as flow:.\n",DataFile);
    fp=fopen(DataFile,"rb");
    if (fp == NULL)
    {
        printf("\nOpen file%sfail!End with any key \n",DataFile);
        perror("Open file fail");
        getch();
        exit(1);
    }
    printf("\nNumber \tName\tchinesescore\tmathscore\theji\n");
    while(fread(&TmpS,sizeof(student),1,fp) != (int)NULL)
    {
        printf("\n%ld\t%s\t%4.2f\t%4.2f\t%4.2f\n",TmpS.Number,TmpS.Name,TmpS.chinesescore,TmpS.mathscore,TmpS.heji);
    }
    fclose(fp);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -