大一期末大作业—学生成绩管理系统

应老师要求,在期末考试前肝完了大作业,历程坎坷。

系统介绍:

1、这是一个基本的学生成绩管理系统,成绩管理中的数据存有学生学号、姓名、性别、高数成绩、英语成绩、c语言成绩。

2、项目运行的软、硬件huanj1:Windows操作系统以及相应的软硬件、Microsoft、

codeblocks。

设计方案:

不足:

1、缺少查重处理

2、部分功能未开发

3、其他存在的还没发现的bug

备注:

1、“唐朝学子.txt”和“宋朝学子.txt”两个数据文件需要提前建立,因为程序里有对两个文件是进行的“a”追读追写操作。

2、本系统的排序功能存在一点延迟,就是可能你使用一次排序功能后打开文件看发现是空的(我也不知道是什么原因),这时只要在多运行几遍就能成功写入文件了。

源代码:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define SIZE 500          //最大存储容量
#define N sizeof(struct Node)

void inputdata(char);
void printdata(char);
void savedata(char);
void searchdata(char);
void adddata(char);
void changedata(char);
void deletedata(char);
void course_sort(char);
void score_sort(char);
void aver_sort(char);
void name_sort(char);
void num_sort(char);
void countdata(char);
void menu();


struct Student
{
    int num;
    char name[20];
    char sex[20];
    float score[3];
    float aver;//三科平均分
    float total;//三科总分
}stu[SIZE];

//还要定义链表节点
struct Node
{
    struct Student stu;
    struct Node *next;
};
struct Node *head=NULL;//需要先定义一个头节点


//开始主函数

int main()
{
    char FIRST;
    printf("\t\t有两个学生成绩管理系统:A-唐朝学子,B-宋朝学子\n");
    printf("\t\t请选择你要进入的系统:");
    scanf("%c",&FIRST);//在最开始对用户选择的想要进行操作的系统进行定义

    if(FIRST!='A'&&FIRST!='B')
    {
        printf("\n\t\t输入错误!\n");
        exit(0);
    }

    while(1)//为了在每次选项结束后可以回到主菜单
    {
        int n;
        menu();
        printf("\n\t\t请选择:");
        scanf("%d",&n);
        if(n>=0&&n<=13)
        {
            switch(n)
            {
            case 1:inputdata(FIRST);break;
            case 2:printdata(FIRST);break;
            case 3:savedata(FIRST);break;
            case 4:searchdata(FIRST);break;
            case 5:adddata(FIRST);break;
            case 6:changedata(FIRST);break;
            case 7:deletedata(FIRST);break;
            case 8:course_sort(FIRST);break;
            case 9:score_sort(FIRST);break;
            case 10:aver_sort(FIRST);break;
            case 11:name_sort(FIRST);break;
            case 12:num_sort(FIRST);break;
            case 13:countdata(FIRST);break;
            case 0:
                printf("\n\t\t\t    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
                printf("\t\t\t                   bye!");
                printf("\n\t\t\t    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
                exit(0);
            }
        }
        else
        {
            printf("\n\t\t\t    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
            printf("\t\t\t                   输入错误!");
            printf("\n\t\t\t    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
            exit(0);
        }

    }
    return 0;
}


//以下是打印菜单的函数定义
void menu()
{
    printf("\t\t________________________________________________________\n");
    printf("\t\t|\t欢迎使用山东师范大学学生成绩管理系统\t\t|\n");
    printf("\t\t|\t\t本系统有以下功能\t\t\t|\n");
    printf("\t\t|\t\t1、录入学生信息\t\t\t\t|\n");
    printf("\t\t|\t\t2、打印学生信息\t\t\t\t|\n");
    printf("\t\t|\t\t3、保存学生信息\t\t\t\t|\n");
    printf("\t\t|\t\t4、查找学生信息\t\t\t\t|\n");
    printf("\t\t|\t\t5、增加学生信息\t\t\t\t|\n");
    printf("\t\t|\t\t6、修改学生信息\t\t\t\t|\n");
    printf("\t\t|\t\t7、删除学生信息\t\t\t\t|\n");
    printf("\t\t|\t\t8、按某门科目排序\t\t\t|\n");
    printf("\t\t|\t\t9、按总分排序\t\t\t\t|\n");
    printf("\t\t|\t\t10、按平均分排序\t\t\t|\n");
    printf("\t\t|\t\t11、按名字排序\t\t\t\t|\n");
    printf("\t\t|\t\t12、按学号排序\t\t\t\t|\n");
    printf("\t\t|\t\t13、统计数据\t\t\t\t|\n");
    printf("\t\t|\t\t0、停止,退出系统\t\t\t|\n");
    printf("\t\t_________________________________________________________\n");
}


//以下是"1、录入学生信息"函数inputdata的定义
void inputdata(char FIRST)
{
    system("cls");//为了屏幕看着更加简洁,先清屏

    struct Node *newnode=(struct Node*)malloc(N);//为插入成绩先设置一个新节点
    newnode->next=NULL;

    FILE *ptr=NULL;
    if(FIRST=='A')
        ptr=fopen("唐朝学子.txt","a");//应该是追加信息
    else
        ptr=fopen("宋朝学子.txt","a");

    struct Node *p=head;
    while(head!=NULL&&p->next!=NULL)
    {
        p=p->next;
    }

    if(head==NULL)
    {
        head=newnode;
    }
    else
    {
        p->next=newnode;
    }

    //成功插入后开始录入信息
    printf("学生的学号:");
    scanf("%d",&newnode->stu.num);
    printf("学生的姓名:");
    scanf("%s",newnode->stu.name);
    printf("学生的性别:");
    scanf("%s",newnode->stu.sex);
    printf("高数成绩:");
    scanf("%f",&newnode->stu.score[0]);
    printf("英语成绩:");
    scanf("%f",&newnode->stu.score[1]);
    printf("c语言成绩:");
    scanf("%f",&newnode->stu.score[2]);

    int n;
    printf("是否保存?\n1-是,2-否\n");
    scanf("%d",&n);
    if(n==1)
        {
            fprintf(ptr,"%d %s %s %.2f %.2f %.2f\n",
                    newnode->stu.num,
                    newnode->stu.name,
                    newnode->stu.sex,
                    newnode->stu.score[0],
                    newnode->stu.score[1],
                    newnode->stu.score[2]);
        }

    fclose(ptr);
    if(n==1)
    printf("\n\t信息已保存\n");
    printf("\n\t按任意键回到主菜单\n");
    system("pause");
    system("cls");
}


//以下是"2、打印学生信息"函数printdata的定义

void printdata(char FIRST)
{
    system("cls");//为了屏幕看着更加简洁,先清屏

    FILE *ptr=NULL;
    if(FIRST=='A')
        {
            if((ptr=fopen("唐朝学子.txt","a+"))==NULL)//应该是追加信息
            {
                printf("\t打开文件失败!\n");
                exit(0);
             }
        }
    else
        {

            if((ptr=fopen("宋朝学子.txt","a+"))==NULL)
            {
                printf("\t打开文件失败!\n");
                exit(0);
            }
        }

    printf("\t________________________________________________________________________________________________\n");
    printf("\t|\t\t\t\t欢迎使用山东师范大学学生成绩管理系统\t\t\t\t|\n");
    printf("\t________________________________________________________________________________________________\n");
    printf("\t|\t学号\t|      姓名\t|      性别\t|    高数成绩\t|    英语成绩\t|   c语言成绩\t|\n");
    printf("\t________________________________________________________________________________________________\n");

    struct Node *first,*p,*tail;
    first=tail=NULL;
    while(!feof(ptr))
    {
       p = (struct Node *)malloc(sizeof(struct Node));
        fscanf(ptr,"%d\t%s\t%s\t%f\t%f\t%f\n",
               &p->stu.num,
               p->stu.name,
               p->stu.sex,
               &p->stu.score[0],
               &p->stu.score[1],
               &p->stu.score[2]);
         printf("\t|\t%d\t|%10s\t|\t%s\t|\t%.2f\t|\t%.2f\t|\t%.2f\t|\n",
               p->stu.num,
               p->stu.name,
               p->stu.sex,
               p->stu.score[0],
               p->stu.score[1],
               p->stu.score[2]);

        if(first==NULL)
            first=p;
        else
            tail->next=p;
        tail=p;
    }
    printf("\t________________________________________________________________________________________________\n");

    fclose(ptr);
    printf("\n\t按任意键回到主菜单\n");

    system("pause");
    system("cls");

}


//以下是"3、保存学生信息"函数savedata的定义

void savedata(char FIRST)
{
    FILE *ptr;

    if(FIRST=='A')
    {
        if((ptr=fopen("唐朝学子.txt","a"))==NULL)
    {
        printf("\t\t打开文件失败!\n");
        exit(0);
    }
    }
    else
    {
        if((ptr=fopen("宋朝学子.txt","a"))==NULL)
    {
        printf("\t\t打开文件失败!\n");
        exit(0);
    }
    }

    struct Node *p=head;
    while(p!=NULL)
    {
        fprintf(ptr,"%d %s %s %.2f %.2f %.2f\n",
                p->stu.num,
                p->stu.name,
                p->stu.sex,
                p->stu.score[0],
                p->stu.score[1],
                p->stu.score[2]);
        p=p->next;
    }
    fclose(ptr);//一定要注意关闭文件!!
    printf("\n\t\t信息已保存\n");
    system("pause");
    system("cls");
}


//以下是"4、查找学生信息"函数searchdata的定义

void searchdata(char FIRST)
{
    system("cls");

    int stunum,flag=0,choose;//这里对stunum的初始化可能存在点问题
    char name[20];

    //设置选项
    printf("\t有以下两种选择:1-根据学号来查找,2-根据姓名来查找\n");
    printf("\t请输入选项:");
    scanf("%d",&choose);

    if(choose==1)
    {
        printf("\t你要查找的学生是(输入学号):");
        scanf("%d",&stunum);
    }
    else if(choose==2)
    {
        printf("\t你要查找的学生是(输入姓名):");
        scanf("%s",name);
    }
    else
    {
        printf("\n\t输入错误!\n");
        flag=1;
    }

    FILE *ptr;

    if(FIRST=='A')
    {
        if((ptr=fopen("唐朝学子.txt","r"))==NULL)
    {
        printf("\t打开文件失败!\n");
        exit(0);
    }
    }
    else
    {
        if((ptr=fopen("宋朝学子.txt","r"))==NULL)
    {
        printf("\t打开文件失败!\n");
        exit(0);
    }
    }

    struct Node *first,*p,*tail;
    first=tail=NULL;
    while(!feof(ptr))//遍历文件全部数据来查找
    {
        p = (struct Node *)malloc(sizeof(struct Node));//每一次循环都开辟一块新内存来存放数据
        fscanf(ptr,"%d\t%s\t%s\t%f\t%f\t%f\n",
               &p->stu.num,
               p->stu.name,
               p->stu.sex,
               &p->stu.score[0],
               &p->stu.score[1],
               &p->stu.score[2]);
        if(p->stu.num==stunum||strcmp(p->stu.name,name)==0)
        {
             printf("\t________________________________________________________________________________________________\n");
             printf("\t|\t学号\t|      姓名\t|      性别\t|    高数成绩\t|    英语成绩\t|    c语言成绩\t|\n");
             printf("\t________________________________________________________________________________________________\n");
             printf("\t|\t%d\t|%10s\t|\t%s\t|\t%.2f\t|\t%.2f\t|\t%.2f\t|\n",
               p->stu.num,
               p->stu.name,
               p->stu.sex,
               p->stu.score[0],
               p->stu.score[1],
               p->stu.score[2]);
             printf("\t________________________________________________________________________________________________\n");
             flag=1;//标志已经找到
             break;
        }
       if(first==NULL)
            first=p;
        else
            tail->next=p;
        tail=p;
    }
    if(flag==0)
        printf("\n\t输入学生信息出误或暂无该学生信息!\n");

    fclose(ptr);
    printf("\n\t按任意键回到主菜单\n");

    system("pause");
    system("cls");
}


//以下是"5、增加学生信息"函数adddata的定义
//(同录入信息一样的原理)

void adddata(char FIRST)
{
    system("cls");//为了屏幕看着更加简洁,先清屏

    struct Node *newnode=(struct Node*)malloc(N);//为插入成绩先设置一个新节点
    newnode->next=NULL;

    FILE *ptr=NULL;
    if(FIRST=='A')
        ptr=fopen("唐朝学子.txt","a");//应该是追加信息
    else
        ptr=fopen("宋朝学子.txt","a");

    struct Node *p=head;
    while(head!=NULL&&p->next!=NULL)
    {
        p=p->next;
    }

    if(head==NULL)
    {
        head=newnode;
    }
    else
    {
        p->next=newnode;
    }

    //成功插入后开始录入信息
    printf("学生的学号:");
    scanf("%d",&newnode->stu.num);
    printf("学生的姓名:");
    scanf("%s",newnode->stu.name);
    printf("学生的性别:");
    scanf("%s",newnode->stu.sex);
    printf("高数成绩:");
    scanf("%f",&newnode->stu.score[0]);
    printf("英语成绩:");
    scanf("%f",&newnode->stu.score[1]);
    printf("c语言成绩:");
    scanf("%f",&newnode->stu.score[2]);

    int n;
    printf("是否保存?\n1-是,2-否\n");
    scanf("%d",&n);
    if(n==1)
        {
             fprintf(ptr,"%d %s %s %.2f %.2f %.2f\n",
                    newnode->stu.num,
                    newnode->stu.name,
                    newnode->stu.sex,
                    newnode->stu.score[0],
                    newnode->stu.score[1],
                    newnode->stu.score[2]);
        }

    fclose(ptr);
    if(n==1)
    printf("\n\t信息已保存\n");
    printf("\n\t按任意键回到主菜单\n");
    system("pause");
    system("cls");
}


//以下是"6、改变学生信息"函数changedata的定义

void changedata(char w)
{
    system("cls");

    int stunum,flag=0,i,choose=0;
    int num;
    char name[20];

    printf("\t有以下两种选择:1-根据学号来查找,2-根据姓名来查找\n");
    printf("\t请输入选项:");
    scanf("%d",&choose);

    if(choose==1)
    {
        printf("\t你要查找的学生是(输入学号):");
        scanf("%d",&stunum);
    }
    else if(choose==2)
    {
        printf("\t你要查找的学生是(输入姓名):");
        scanf("%s",name);
    }
    else
    {
        printf("\n\t输入错误!\n");
        flag=1;
        exit(0);
    }

    FILE *ptr,*tem;
    if(w=='A')
   {
       if((ptr=fopen("唐朝学子.txt","r"))==NULL)
    {
        printf("\t打开文件失败!\n");
        exit(0);
    }
   }
   else
   {
       if((ptr=fopen("宋朝学子.txt","r"))==NULL)
    {
        printf("\t打开文件失败!\n");
        exit(0);
    }
   }
    tem=fopen("temp.txt","w");//先设置一个用于替代的文件
    struct Node *p,*first,*tail;
    first=tail=NULL;
    while(!feof(ptr))//先将文件数据遍历读取
    {
        p = (struct Node *)malloc(sizeof(struct Node));
        fscanf(ptr,"%d\t%s\t%s\t%f\t%f\t%f\n",
               &p->stu.num,
               p->stu.name,
               p->stu.sex,
               &p->stu.score[0],
               &p->stu.score[1],
               &p->stu.score[2]);

        if(p->stu.num==stunum||strcmp(p->stu.name,name)==0)
        {

//先将文件里的数据遍历输入到用于替代的文件中,
//如果遇到了需要修改的学生信息,则进入if句后,修改后用continue结束本次循环

             printf("\t________________________________________________________________________________________________\n");
             printf("\t|\t学号\t|      姓名\t|      性别\t|    高数成绩\t|    英语成绩\t|  c语言成绩\t|\n");
             printf("\t________________________________________________________________________________________________\n");
             printf("\t|\t%d\t|%10s\t|\t%s\t|\t%.2f\t|\t%.2f\t|\t%.2f\t|\n",
               p->stu.num,
               p->stu.name,
               p->stu.sex,
               p->stu.score[0],
               p->stu.score[1],
               p->stu.score[2]);
             printf("\t________________________________________________________________________________________________\n");
             printf("\t是否更改该学生信息?1-是,2-否\n");
             printf("\t请输入选项序号(1或0):");
             scanf("%d",&i);
             if(i==1)
             {
                 flag=1;
             printf("\t请输入修改后的学生的学号:");
             scanf("%d",&p->stu.num);
             printf("\t请输入修改后的学生的姓名:");
             scanf("%s",p->stu.name);
             printf("\t请输入修改后的学生的性别:");
             scanf("%s",p->stu.sex);
             printf("\t请输入修改后的高数成绩:");
             scanf("%f",&p->stu.score[0]);
             printf("\t请输入修改后的英语成绩:");
             scanf("%f",&p->stu.score[1]);
             printf("\t请输入修改后的c语言成绩:");
             scanf("%f",&p->stu.score[2]);

             fprintf(tem,"%d %s %s %.2f %.2f %.2f\n",//输入替代文件中
                     p->stu.num,
                     p->stu.name,
                     p->stu.sex,
                     p->stu.score[0],
                     p->stu.score[1],
                     p->stu.score[2]);

             continue;//continue需要在if(i==1)的语句里,保证如果不修改的话还是会继续把辕信息写入替代文件
             }
        }
        fprintf(tem,"%d %s %s %.2f %.2f %.2f\n",
                p->stu.num,
                p->stu.name,
                p->stu.sex,
                p->stu.score[0],
                p->stu.score[1],
                p->stu.score[2]);

        if(first==NULL)
            first=p;
        else
            tail->next=p;
        tail=p;
    }

    fclose(ptr);//要先关闭文件
    fclose(tem);
    if(flag==0)
        printf("\t输入学生信息出误或暂无该学生信息!\n");

//        然后将原本的旧文件删除,将用于替代的文件改名成原本文件的名字“studentgrades.txt”

    else
    {
        if(w=='A')
       {
        remove("唐朝学子.txt");
        char oldname[]="temp.txt";
        char newname[]="唐朝学子.txt";
        rename(oldname,newname);//关于rename函数的用法
       }
       else
       {
           remove("宋朝学子.txt");
        char oldname[]="temp.txt";
        char newname[]="宋朝学子.txt";
        rename(oldname,newname);//关于rename函数的用法
       }

        printf("\n\t已将更改后的信息存入文件,可进行查看\n");
    }

    printf("\n\t按任意键回到主菜单\n");
    system("pause");
    system("cls");

}


//以下是"7、删除学生信息"函数deletedata的定义
//(同上述修改数据几乎一样,就是在遇到需要删除的数据直接跳过本次循环就行)

void deletedata(char w)
{
    system("cls");
    int stunum,flag=0,i,choose=0;
    char name[20];

    printf("\t有以下两种选择:1-根据学号来查找,2-根据姓名来查找\n");
    printf("\t请输入选项:");
    scanf("%d",&choose);

    if(choose==1)
    {
        printf("\t你要查找的学生是(输入学号):");
        scanf("%d",&stunum);
    }
    else if(choose==2)
    {
        printf("\t你要查找的学生是(输入姓名):");
        scanf("%s",name);
    }
    else
    {
        printf("\n\t输入错误!\n");
        flag=1;
        exit(0);
    }

    FILE *ptr,*tem;
    if(w=='A')
    {
        if((ptr=fopen("唐朝学子.txt","r"))==NULL)
    {
        printf("打开文件失败!\n");
        exit(0);
    }
    }
    else
    {
        if((ptr=fopen("宋朝学子.txt","r"))==NULL)
    {
        printf("打开文件失败!\n");
        exit(0);
    }

    }
    tem=fopen("temp.txt","w");

    struct Node *first,*p,*tail;
    first=tail=NULL;
    while(!feof(ptr))
    {
        p = (struct Node *)malloc(sizeof(struct Node));
        fscanf(ptr,"%d\t%s\t%s\t%f\t%f\t%f\n",
               &p->stu.num,
               p->stu.name,
               p->stu.sex,
               &p->stu.score[0],
               &p->stu.score[1],
               &p->stu.score[2]);
        if(p->stu.num==stunum||strcmp(p->stu.name,name)==0)
        {
             flag=1;

             printf("\t________________________________________________________________________________________________\n");
             printf("\t|\t学号\t|      姓名\t|      性别\t|    高数成绩\t|    英语成绩\t|  c语言成绩\t|\n");
             printf("\t________________________________________________________________________________________________\n");
             printf("\t|\t%d\t|%10s\t|\t%s\t|\t%.2f\t|\t%.2f\t|\t%.2f\t|\n",
               p->stu.num,
               p->stu.name,
               p->stu.sex,
               p->stu.score[0],
               p->stu.score[1],
               p->stu.score[2]);
             printf("\t________________________________________________________________________________________________\n");

             printf("\t是否删除?1-是,2-否\n");
             do
             {
                 printf("\t请输入选序号(1或2):");
                 scanf("%d",&i);
             }while(i!=1&&i!=2);
             if(i==1)
             {
                 continue;
             }
             else
             {
                 flag=2;
             }
        }

        fprintf(tem,"%d %s %s %.2f %.2f %.2f\n",
                p->stu.num,
                p->stu.name,
                p->stu.sex,
                p->stu.score[0],
                p->stu.score[1],
                p->stu.score[2]);

        if(first==NULL)
            first=p;
        else
            tail->next=p;
        tail=p;
    }

    fclose(ptr);
    fclose(tem);

    if(flag==0)
        printf("\n\t没有找到该学生信息或学生信息不存在\n");
    else if(flag==1)
    {
        if(w=='A')
       {
        remove("唐朝学子.txt");
        char oldname[]="temp.txt";
        char newname[]="唐朝学子.txt";
        rename(oldname,newname);//关于rename函数的用法
       }
       else
       {
           remove("宋朝学子.txt");
        char oldname[]="temp.txt";
        char newname[]="宋朝学子.txt";
        rename(oldname,newname);//关于rename函数的用法
       }

        printf("\n\t已删除该学生信息,可进行查看\n");
    }
    else
    {
        printf("\t根据用户选择没有删除该学生信息!\n");
    }

    printf("\t按任意键回到主菜单\n");
    system("pause");
    system("cls");
}


//以下是"8、按某门成绩排序"函数course_sort的定义(从高到低)

void course_sort(char w)
{
    void select_sort(int a,int b,char w);

    int a,b;
    printf("\t\t你想进行哪门科目的排序(1-高数,2-英语,3-c语言)\n");
    printf("\t\t请输入选项:");
    scanf("%d",&a);
    while(a<1||a>3)
    {
        printf("\t\t选择错误!请按规则选择:");
        scanf("%d",&a);
    }
    printf("\n\t\t有两种排序模式(1-从低到高,2-从高到低)\n");
    printf("\t\t请输入选项:");
    scanf("%d",&b);
    while(b<1||b>3)
    {
        printf("\t\t选择错误!请按规则选择:");
        scanf("%d",&a);
    }

    select_sort(a,b,w);

    system("pause");
    system("cls");
}


void select_sort(int a,int b,char w)
{
    FILE *ptr;
     if(w=='A')
    {
        if((ptr=fopen("唐朝学子.txt","r"))==NULL)
    {
        printf("打开文件失败!\n");
        exit(0);
    }
    }
    else
    {
        if((ptr=fopen("宋朝学子.txt","r"))==NULL)
    {
        printf("打开文件失败!\n");
        exit(0);
    }

    }

    FILE *pw;//根据需要排序的科目来打开相应的文件

    if(w=='A')
    {
    if(a==1)pw=fopen("唐朝学子高数成绩.txt","w");
    if(a==2)pw=fopen("唐朝学子英语成绩.txt","w");
    if(a==3)pw=fopen("唐朝学子c语言成绩.txt","w");
    }
    else
    {
    if(a==1)pw=fopen("宋朝学子高数成绩.txt","w");
    if(a==2)pw=fopen("宋朝学子英语成绩.txt","w");
    if(a==3)pw=fopen("宋朝学子c语言成绩.txt","w");
    }

    int i,j,n=0;
    float x,y;

    while(!feof(ptr))
    {
         fscanf(ptr,"%d\t%s\t%s\t%f\t%f\t%f\n",
                &stu[n].num,
                stu[n].name,
                stu[n].sex,
                &stu[n].score[0],
                &stu[n].score[1],
                &stu[n].score[2]);
         n++;
    }

    fclose(ptr);

     for(i=0;i<n;i++)
    {
        for(j=i+1;j<n;j++)
        {
//            根据需要排序的科目来设置相应的变量
            if(a==1){x=stu[i].score[0],y=stu[j].score[0];}
            else if(a==2){x=stu[i].score[1],y=stu[j].score[1];}
            else if(a==3){x=stu[i].score[2],y=stu[j].score[2];}

            if(b==1)
            {
                if(x>y)
            {
                struct Student temp;
                temp=stu[i];
                stu[i]=stu[j];
                stu[j]=temp;
            }

            }
            else
            {
                if(x<y)
            {
                struct Student temp;
                temp=stu[i];
                stu[i]=stu[j];
                stu[j]=temp;
            }

            }
        }
    }

    printf("\t\t排序后:\n");
    system("cls");

    //在屏幕上打印
    printf("\t________________________________________________________________\n");
    if(a==1)printf("\t|\t学号\t|      姓名\t|      性别\t|    高数成绩\t|\n");
    if(a==2)printf("\t|\t学号\t|      姓名\t|      性别\t|    英语成绩\t|\n");
    if(a==3)printf("\t|\t学号\t|      姓名\t|      性别\t|    c语言成绩\t|\n");
    printf("\t________________________________________________________________\n");

    //在文件里打印
    fprintf(pw,"______________________________________\n");
    if(a==1)fprintf(pw,"|学号  |     姓名     |   性别  |  高数成绩   |\n");
    if(a==2)fprintf(pw,"|学号  |     姓名     |   性别  |  英语成绩   |\n");
    if(a==3)fprintf(pw,"|学号  |     姓名     |   性别  |  c语言成绩  |\n");
    fprintf(pw,"______________________________________\n");

     for(i=0;i<n;i++)
    {
        if(a==1)x=stu[i].score[0];
        else if(a==2)x=stu[i].score[1];
        else if(a==3)x=stu[i].score[2];
       fprintf(pw," %d\t%s\t%s\t%.2f\t\n",stu[i].num,stu[i].name,stu[i].sex,x);
       printf("\t|\t%d\t|%10s\t|\t%s\t|\t%.2f\t|\n",stu[i].num,stu[i].name,stu[i].sex,x);
    }
    printf("\t________________________________________________________________\n");
    fprintf(pw,"_______________________________________\n");

    printf("\n\t已排序并存入文件!可打开文件查看\n");
    printf("\t按任意键回到主菜单\n");

    fclose(pw);

}


//以下是"9、按总分排序"函数score_sort的定义

void score_sort(char w)
{
    FILE *p1,*p2;
    if(w=='A')
    {
        if((p1=fopen("唐朝学子.txt","r"))==NULL)
    {
        printf("打开文件失败!\n");
        exit(0);
    }
    }
    else
    {
        if((p1=fopen("宋朝学子.txt","r"))==NULL)
    {
        printf("打开文件失败!\n");
        exit(0);
    }
    }


    if(w=='A')
    {
        if((p2=fopen("唐朝学子总分排名.txt","w"))==NULL)
    {
        printf("打开文件失败!\n");
        exit(0);
    }
    }
    else
    {
        if((p2=fopen("宋朝学子总分排名.txt","w"))==NULL)
    {
        printf("打开文件失败!\n");
        exit(0);
    }
    }

    int i,j,n=0;
    int b;

    printf("\n\t\t有两种排序模式(1-从低到高,2-从高到低)\n");
    printf("\t\t请输入选项:");
    scanf("%d",&b);

    while(!feof(p1))
    {
        fscanf(p1,"%d\t%s\t%s\t%f\t%f\t%f\n",
               &stu[n].num,
               stu[n].name,
               stu[n].sex,
               &stu[n].score[0],
               &stu[n].score[1],
               &stu[n].score[2]);
        stu[n].total=stu[n].score[0]+stu[n].score[1]+stu[n].score[2];
        n++;
    }

    fclose(p1);

    for(i=0;i<n;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(b==1)
            {
                if(stu[i].total>stu[j].total)
            {
                struct Student temp;
                temp=stu[i];
                stu[i]=stu[j];
                stu[j]=temp;
            }

            }
            else
            {
                if(stu[i].total<stu[j].total)
            {
                struct Student temp;
                temp=stu[i];
                stu[i]=stu[j];
                stu[j]=temp;
            }
            }
        }
    }

    system("cls");
    printf("\t排序后:\n");
    printf("   _____________________________________________________________________________________________________________\n");
   fprintf(p2,"_________________________________________________________________________\n");
    printf("   |\t学号\t|      姓名\t|      性别\t|    高数成绩\t|    英语成绩\t|     c语言成绩\t|      总分\t|\n");
    fprintf(p2,"|学号  |     姓名     |   性别  |  高数成绩   |  英语成绩   |   c语言成绩   |   总分  |\n");
    printf("   _____________________________________________________________________________________________________________\n");
   fprintf(p2,"_________________________________________________________________________\n");

    for(i=0;i<n;i++)
    {
       fprintf(p2," %d\t%s\t%s\t%.2f\t%.2f\t%.2f\t%.2f\t\n",
               stu[i].num,
               stu[i].name,
               stu[i].sex,
               stu[i].score[0],
               stu[i].score[1],
               stu[i].score[2],
               stu[i].total);

        printf("   |\t%d\t|%10s\t|\t%s\t|\t%.2f\t|\t%.2f\t|\t%.2f\t|\t%.2f\t|\n",
               stu[i].num,
               stu[i].name,
               stu[i].sex,
               stu[i].score[0],
               stu[i].score[1],
               stu[i].score[2],
               stu[i].total);
    }

    printf("   _____________________________________________________________________________________________________________\n");
    fprintf(p2,"_________________________________________________________________________\n");

    fclose(p2);

    printf("\n\t已排序并存入文件!可打开文件查看\n");
    printf("\t按任意键回到主菜单\n");

    system("pause");
    system("cls");
}


//以下是"10、按三科平均分排序"函数aver_sort的定义

void aver_sort(char w)
{
    FILE *p1,*p2;
    if(w=='A')
    {
        if((p1=fopen("唐朝学子.txt","r"))==NULL)
    {
        printf("打开文件失败!\n");
        exit(0);
    }
    }
    else
    {
        if((p1=fopen("宋朝学子.txt","r"))==NULL)
    {
        printf("打开文件失败!\n");
        exit(0);
    }
    }


    if(w=='A')
    {
        if((p2=fopen("唐朝学子三科平均分排名.txt","w"))==NULL)
    {
        printf("打开文件失败!\n");
        exit(0);
    }
    }
    else
    {
        if((p2=fopen("宋朝学子三科平均分排名.txt","w"))==NULL)
    {
        printf("打开文件失败!\n");
        exit(0);
    }
    }

    int i,j,n=0;
    int b;

    printf("\n\t\t有两种排序模式(1-从低到高,2-从高到低)\n");
    printf("\t\t请输入选项:");
    scanf("%d",&b);

    while(!feof(p1))
    {
        fscanf(p1,"%d\t%s\t%s\t%f\t%f\t%f\n",
               &stu[n].num,
               stu[n].name,
               stu[n].sex,
               &stu[n].score[0],
               &stu[n].score[1],
               &stu[n].score[2]);
        stu[n].total=stu[n].score[0]+stu[n].score[1]+stu[n].score[2];
        stu[n].aver=stu[n].total/3;
        n++;
    }

    fclose(p1);

    for(i=0;i<n;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(b==1)
            {
                if(stu[i].aver>stu[j].aver)
            {
                struct Student temp;
                temp=stu[i];
                stu[i]=stu[j];
                stu[j]=temp;
            }

            }
            else
            {
                if(stu[i].aver<stu[j].aver)
            {
                struct Student temp;
                temp=stu[i];
                stu[i]=stu[j];
                stu[j]=temp;
            }
            }
        }
    }

    system("cls");
    printf("\t排序后:\n");
    printf("   _____________________________________________________________________________________________________________\n");
    fprintf(p2,"___________________________________________________________________________\n");
    printf("   |\t学号\t|      姓名\t|      性别\t|    高数成绩\t|    英语成绩\t|     c语言成绩\t|      平均分\t|\n");
    fprintf(p2,"|学号  |     姓名     |   性别  |  高数成绩   |  英语成绩   |   c语言成绩   |   平均分  |\n");
    printf("   _____________________________________________________________________________________________________________\n");
    fprintf(p2,"___________________________________________________________________________\n");
    for(i=0;i<n;i++)
    {
       fprintf(p2," %d\t%s\t%s\t%.2f\t%.2f\t%.2f\t%.2f\t\n",
               stu[i].num,
               stu[i].name,
               stu[i].sex,
               stu[i].score[0],
               stu[i].score[1],
               stu[i].score[2],
               stu[i].aver);

        printf("   |\t%d\t|%10s\t|\t%s\t|\t%.2f\t|\t%.2f\t|\t%.2f\t|\t%.2f\t|\n",
               stu[i].num,
               stu[i].name,
               stu[i].sex,
               stu[i].score[0],
               stu[i].score[1],
               stu[i].score[2],
               stu[i].aver);
    }

    printf("   _____________________________________________________________________________________________________________\n");
    fprintf(p2,"___________________________________________________________________________\n");

    fclose(p2);

    printf("\n\t已排序并存入文件!可打开文件查看\n");
    printf("\t按任意键回到主菜单\n");

    system("pause");
    system("cls");
}



//以下是"11、按名字排序"函数name_sort的定义

void name_sort(char w)
{
    FILE *p1,*p2;
    if(w=='A')
    {
        if((p1=fopen("唐朝学子.txt","r"))==NULL)
    {
        printf("打开文件失败!\n");
        exit(0);
    }
    }
    else
    {
        if((p1=fopen("宋朝学子.txt","r"))==NULL)
    {
        printf("打开文件失败!\n");
        exit(0);
    }
    }


    if(w=='A')
    {
        if((p2=fopen("唐朝学子姓名排名.txt","w"))==NULL)
    {
        printf("打开文件失败!\n");
        exit(0);
    }
    }
    else
    {
        if((p2=fopen("宋朝学子姓名排名.txt","w"))==NULL)
    {
        printf("打开文件失败!\n");
        exit(0);
    }
    }

    int i,j,n=0;
    int b;

    printf("\n\t\t有两种排序模式(1-顺序,2-倒序)\n");
    printf("\t\t请输入选项:");
    scanf("%d",&b);

    while(!feof(p1))
    {
        fscanf(p1,"%d\t%s\t%s\t%f\t%f\t%f\n",&stu[n].num,stu[n].name,stu[n].sex,&stu[n].score[0],&stu[n].score[1],&stu[n].score[2]);
        n++;
    }

    fclose(p1);

    for(i=0;i<n;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(b==1)
            {
                if(strcmp(stu[i].name,stu[j].name)>0)
            {
                struct Student temp;
                temp=stu[i];
                stu[i]=stu[j];
                stu[j]=temp;
            }

            }
            else
            {
                if(strcmp(stu[i].name,stu[j].name)<0)
            {
                struct Student temp;
                temp=stu[i];
                stu[i]=stu[j];
                stu[j]=temp;
            }
            }
        }
    }

    system("cls");
    printf("\t排序后:\n");
    printf("   _____________________________________________________________________________________________\n");
    fprintf(p2,"_________________________________________________________________\n");
    printf("   |\t学号\t|      姓名\t|      性别\t|    高数成绩\t|    英语成绩\t|     c语言成绩\t|\n");
    fprintf(p2,"|学号  |     姓名     |   性别  |  高数成绩   |  英语成绩   |   c语言成绩   |\n");
    printf("   _____________________________________________________________________________________________\n");
    fprintf(p2,"_________________________________________________________________\n");

    for(i=0;i<n;i++)
    {
       fprintf(p2," %d\t%s\t%s\t%.2f\t%.2f\t%.2f\t\n",
               stu[i].num,
               stu[i].name,
               stu[i].sex,
               stu[i].score[0],
               stu[i].score[1],
               stu[i].score[2]);

        printf("   |\t%d\t|%10s\t|\t%s\t|\t%.2f\t|\t%.2f\t|\t%.2f\t|\n",
               stu[i].num,
               stu[i].name,
               stu[i].sex,
               stu[i].score[0],
               stu[i].score[1],
               stu[i].score[2]);
    }

    printf("   _____________________________________________________________________________________________\n");
    fprintf(p2,"_________________________________________________________________\n");

    printf("\n\t已排序并存入文件!可打开文件查看\n");
    printf("\t按任意键回到主菜单\n");

    system("pause");
    system("cls");
}


//以下是"12、按学号排序"函数num_sort的定义

void num_sort(char w)
{

    FILE *p1,*p2;
    if(w=='A')
    {
        if((p1=fopen("唐朝学子.txt","r"))==NULL)
    {
        printf("打开文件失败!\n");
        exit(0);
    }
    }
    else
    {
        if((p1=fopen("宋朝学子.txt","r"))==NULL)
    {
        printf("打开文件失败!\n");
        exit(0);
    }
    }


    if(w=='A')
    {
        if((p2=fopen("唐朝学子学号排名.txt","w"))==NULL)
    {
        printf("打开文件失败!\n");
        exit(0);
    }
    }
    else
    {
        if((p2=fopen("宋朝学子学号排名.txt","w"))==NULL)
    {
        printf("打开文件失败!\n");
        exit(0);
    }
    }

    int i,j,n=0;
    int b;

    printf("\n\t\t有两种排序模式(1-顺序,2-倒序)\n");
    printf("\t\t请输入选项:");
    scanf("%d",&b);

    while(!feof(p1))
    {
        fscanf(p1,"%d\t%s\t%s\t%f\t%f\t%f\n",&stu[n].num,stu[n].name,stu[n].sex,&stu[n].score[0],&stu[n].score[1],&stu[n].score[2]);
        n++;
    }

    fclose(p1);

    for(i=0;i<n;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(b==1)
            {
                if(stu[i].num>stu[j].num)
            {
                struct Student temp;
                temp=stu[i];
                stu[i]=stu[j];
                stu[j]=temp;
            }

            }
            else
            {
                if(stu[i].num<stu[j].num)
            {
                struct Student temp;
                temp=stu[i];
                stu[i]=stu[j];
                stu[j]=temp;
            }
            }
        }
    }

    system("cls");
    printf("\t排序后:\n");
    printf("   _____________________________________________________________________________________________\n");
    fprintf(p2,"_________________________________________________________________\n");
    printf("   |\t学号\t|      姓名\t|      性别\t|    高数成绩\t|    英语成绩\t|     c语言成绩\t|\n");
    fprintf(p2,"|学号  |     姓名     |   性别  |  高数成绩   |  英语成绩   |   c语言成绩   |\n");
    printf("   _____________________________________________________________________________________________\n");
    fprintf(p2,"_________________________________________________________________\n");

    for(i=0;i<n;i++)
    {
       fprintf(p2," %d\t%s\t%s\t%.2f\t%.2f\t%.2f\t\n",
               stu[i].num,
               stu[i].name,
               stu[i].sex,
               stu[i].score[0],
               stu[i].score[1],
               stu[i].score[2]);

        printf("   |\t%d\t|%10s\t|\t%s\t|\t%.2f\t|\t%.2f\t|\t%.2f\t|\n",
               stu[i].num,
               stu[i].name,
               stu[i].sex,
               stu[i].score[0],
               stu[i].score[1],
               stu[i].score[2]);
    }

    printf("   _____________________________________________________________________________________________\n");
    fprintf(p2,"_________________________________________________________________\n");

    fclose(p2);

    printf("\n\t已排序并存入文件!可打开文件查看\n");
    printf("\t按任意键回到主菜单\n");

    system("pause");
    system("cls");
}


//以下是"13、统计数据"函数countdata的定义

void countdata(char w)
{
    float sort_(int a,int n,char w);

    FILE *p1;
    if(w=='A')
    {
        if((p1=fopen("唐朝学子.txt","r"))==NULL)
    {
        printf("打开文件失败!\n");
        exit(0);
    }
    }
    else
    {
        if((p1=fopen("宋朝学子.txt","r"))==NULL)
    {
        printf("打开文件失败!\n");
        exit(0);
    }
    }

    int n=0;
    double aver=0,aver0,aver1,aver2,sum=0,sum0=0,sum1=0,sum2=0;
    float mathmax,ymax,cmax,mathmin,ymin,cmin;

    while(!feof(p1))
    {
        fscanf(p1,"%d\t%s\t%s\t%f\t%f\t%f\n",&stu[n].num,stu[n].name,stu[n].sex,&stu[n].score[0],&stu[n].score[1],&stu[n].score[2]);
        sum0+=stu[n].score[0];
        sum1+=stu[n].score[1];
        sum2+=stu[n].score[2];
        sum+=(stu[n].score[0]+stu[n].score[1]+stu[n].score[2]);
        n++;
    }

    fclose(p1);

    aver=sum/n;
    aver0=sum0/n;
    aver1=sum1/n;
    aver2=sum2/n;
    mathmax=sort_(1,1,w);
    ymax=sort_(2,1,w);
    cmax=sort_(3,1,w);
    mathmin=sort_(1,2,w);
    ymin=sort_(2,2,w);
    cmin=sort_(3,2,w);

    system("cls");

    printf("\t学生总人数为:%d\n",n);
    printf("\t总分平均分为:%.2lf\n",aver);
    printf("\t高数最高分为:%.2f\t高数最低分为:%.2f\t高数平均分为:%.2lf\n",mathmax,mathmin,aver0);
    printf("\t英语最高分为:%.2f\t英语最低分为:%.2f\t英语平均分为:%.2lf\n",ymax,ymin,aver1);
    printf("\tc语言最高分为:%.2f\tc语言最低分为:%.2f\tc语言平均分为:%.2lf\n",cmax,cmin,aver2);

    printf("\n\t按任意键回到主菜单\n");

    system("pause");
    system("cls");
}


//以下是对某门科目进行排序选出最高分的函数的定义

float sort_(int a,int b,char w)
{
    FILE *ptr;

    if(w=='A')
    {
        if((ptr=fopen("唐朝学子.txt","r"))==NULL)
    {
        printf("打开文件失败!\n");
        exit(0);
    }
    }
    else
    {
        if((ptr=fopen("宋朝学子.txt","r"))==NULL)
    {
        printf("打开文件失败!\n");
        exit(0);
    }
    }

    FILE *pw;//根据需要排序的科目来打开相应的文件
    if(w=='A')
    {
    if(a==1)pw=fopen("唐朝学子高数成绩.txt","w");
    if(a==2)pw=fopen("唐朝学子英语成绩.txt","w");
    if(a==3)pw=fopen("唐朝学子c语言成绩.txt","w");
    }
    else
    {
    if(a==1)pw=fopen("宋朝学子高数成绩.txt","w");
    if(a==2)pw=fopen("宋朝学子英语成绩.txt","w");
    if(a==3)pw=fopen("宋朝学子c语言成绩.txt","w");
    }

    int i,j,n=0;
    float x,y,coursemax,coursemin;

    while(!feof(ptr))
    {
         fscanf(ptr,"%d\t%s\t%s\t%f\t%f\t%f\n",
                &stu[n].num,
                stu[n].name,
                stu[n].sex,
                &stu[n].score[0],
                &stu[n].score[1],
                &stu[n].score[2]);
         n++;
    }

    fclose(ptr);

    //要对两个最值进行初始化
    if(a==1){coursemax=stu[0].score[0],coursemin=stu[0].score[0];}
    else if(a==2){coursemax=stu[0].score[1],coursemin=stu[0].score[1];}
    else if(a==3){coursemax=stu[0].score[2],coursemax=stu[0].score[2];}

     for(i=0;i<n;i++)
    {
        //每一次循环都要进行比较,所以变量x要放在for循环里进行赋值
            if(a==1){x=stu[i].score[0];}
            else if(a==2){x=stu[i].score[1];}
            else if(a==3){x=stu[i].score[2];}
            if(b==1)
            {
                if(x>coursemax)
                    coursemax=x;
            }
            if(b==2)
            {
                if(x<coursemin)
                    coursemin=x;
            }
    }

    if(b==1)
        return coursemax;
    else
        return coursemin;
}




















鉴于笔者的能力不足,有很多地方还不够完善,运行效率方面也很低下,仅供参考。

 

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值