想在122-125行之间实现实时给学生的总分数排名词

#include
#include
#include
#include 
typedef struct stu
{
    long stuID;     //学号
    char stuname[10];    //名字
    char stusex;     //性别
    int score[4];    //分数
    int total;   //总分
    int aver;    //平均分
    int line;   //排名
}STU;
typedef struct node   //创建结点类型
{
    STU stu;                //数据域
    struct node* next;   //指向下一个节点的指针
}NODE;
NODE* head = NULL;  //定义头指针

NODE* creatlist();


void start();
void inputstudent(); //void inputstudent(STU stu[]);  修改
void savestudent();
void readstudent();
void printfstudent();


//#define N 30                   修改 
int main()
{
    int arr[100] = { 1 };    //存每名学生的名次  
    //NODE* list = creatlist(); //修改
    //STU stu[N];               //修改
    int m = 4;
    while (1)
    {
        start();
        char ch = _getch();
        switch (ch)
        {
        case '1': //录入学生信息
            inputstudent();  //inputstudent(stu);  修改
            break;
        case '2':  //保存学生信息
            savestudent();
            break;
        case '3'://读取学生信息
            readstudent();
            break;
        case '4':  //打印学生成绩
            printfstudent();
            break;
        case '5':  //按总分由高到低排出名次
          
            break;
        case '6':  //按总分由低到高排出名次
            break;
        case '7':  //按学号由小到大排出成绩表
            break;
        case '8':  //按姓名字典顺序排序排出成绩表
            break;
        }
    }
    return 0;
}


void start()
{
    printf("*****************************************\n");
    printf("欢迎使用学生成绩管理系统                *\n");
    printf("*1.录入学生信息                         *\n");
    printf("*2.保存学生信息                         *\n");
    printf("*3.读取学生信息                         *\n");
    printf("*4.打印学生信息                         *\n");
    printf("*5.按总分由高到低排出名次               *\n");
    printf("*6.按总分由低到高排出名次               *\n");
    printf("*7.按学号由小到大排出成绩表             *\n");
    printf("*8.按姓名字典顺序排序排出成绩表         *\n");
    printf("*9.根据学号查询学生成绩及排名           *\n");
    printf("*0.根据姓名查询学生成绩及排名           *\n");
    printf("*****************************************\n");
}

NODE* creatlist()    //创建表头表示整个链表即创建链表(表头可以是头结点,也可以是数据结点,通常是头结点)
{
    NODE* headNode = (NODE*)malloc(sizeof(NODE));
    headNode->next = NULL;
    return headNode;
}


void inputstudent()   //void inputstudent(STU stu[])  修改
{
   
    NODE* newnode = (NODE*)malloc(sizeof(NODE)); //创建一个新结点来作头结点,使newnode这个指针可以通过->来当作结构体变量来用
    newnode->next = NULL;
    if (head == NULL)   //遍历
    {
        head = newnode;
    }
    else
    {
        newnode->next = head;
        head = newnode;
    }
    printf("学号:");
    scanf_s("%ld", &newnode->stu.stuID);
    printf("姓名:");
    scanf_s("%s", newnode->stu.stuname, 10);
    printf("性别:");
    scanf_s(" %c", &newnode->stu.stusex, 1);
    printf("成绩:");
    scanf_s("%d %d %d %d", &newnode->stu.score[0], &newnode->stu.score[1], &newnode->stu.score[2], &newnode->stu.score[3]);
    newnode->stu.total = newnode->stu.score[0] + newnode->stu.score[1] + newnode->stu.score[2] + newnode->stu.score[3];
    printf("总分:%d\n", newnode->stu.total);
    newnode->stu.aver = (newnode->stu.score[0] + newnode->stu.score[1] + newnode->stu.score[2] + newnode->stu.score[3]) / 4.0;
    printf("平均分:%d\n", newnode->stu.aver);
     // if (newnode->next->stu.total > newnode->stu.total)
     //   newnode->next->stu.line++;
    printf("名次:%d", newnode->stu.line);
}

void savestudent()    //保存学生信息
{
    FILE* pf = fopen("pph.txt", "w"); //创建并打开文件
    if (pf == NULL) //判断打开文件是否失败
    {
        printf("打开文件失败\n");
        return;
    }
    NODE* p = head;
    while (p != NULL)
    {
        fwrite(&p->stu, sizeof(STU), 1, pf);   //修改
        //fwrite(&p->stu, 1, sizeof(STU), pf); //修改
        p = p->next;
    }
    fclose(pf);
    printf("数据保存成功\n");
}


void readstudent()   //读取学生信息
{
    FILE* pf = fopen("pph.txt", "r");  //打开文件
    if (pf == NULL) //判断打开文件是否失败
    {
        printf("err!\n");
        return;
    }
    while (1)  //(!feof(pf))   修改
    {
        NODE* newnode = (NODE*)malloc(sizeof(NODE));
        newnode->next = NULL;
        if (fread(&newnode->stu, sizeof(STU), 1, pf) != 1) { //fread(&newnode->stu, 1, sizeof(STU), pf);
            free(newnode);                                   //修改
            break;
        }                                                   //修改
        //头插法
        if (head == NULL)
        {
            head = newnode;
        }
        else
        {
            newnode->next = head;
            head = newnode;
        }
    }
    printf("加载数据成功\n");
    fclose(pf);
}
void printfstudent()
{
    NODE* P = head;   
    while (P != NULL)
    {
        printf("%ld %s %c %d %d %d %d %d %d %d", P->stu.stuID, P->stu.stuname, P->stu.stusex, 
            P->stu.score[0], P->stu.score[1], P->stu.score[2], P->stu.score[3],P->stu.total, P->stu.aver); //修改
        P = P->next;
        break;
    }
    system("pause");
}

想在122-125行之间实现实时给学生的总分数排名词,如何实现?

新写一个排名次函数 void rank() ,实现按总分从高到低排名,总分相同的排名相同,因为链表是动态的,随时增加记录,所以排名次放在输入函数里不合适。同时建议将 void savestudent() 保存学生信息函数不单列在菜单里,将它放在退出程序前运行更合理,将 void readstudent() 读取学生信息函数移动到程序代码的开始处,加载一次即可,同时,已将这两个函数及其他函数做了完善修改。供参考:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#define N 4        //修改 课程数目 
typedef struct stu
{
    long  stuID;     //学号
    char  stuname[10]; //名字
    char  stusex;  //性别
    int   score[N]; //分数
    int   total;   //总分
    float aver;    //平均分  int
    int   line;    //排名
}STU;
typedef struct node   //创建结点类型
{
    STU stu;                //数据域
    struct node* next;   //指向下一个节点的指针
}NODE;
NODE* head = NULL;  //定义头指针

//NODE* creatlist();

void start();
void inputstudent();
void savestudent();
void readstudent();
void printfstudent();
void rank();         //排名次函数

int main()
{
    //int arr[100] = { 1 };//存每名学生的名次   修改
    int m = 4;
    while (1)
    {
        start();
        char ch = _getch();
        switch (ch)
        {
        case '1': //录入学生信息
            inputstudent();  //inputstudent(stu);  修改
            rank();
            break;
        case '2':  //保存学生信息
            savestudent();
            break;
        case '3'://读取学生信息
            readstudent();
            break;
        case '4':  //打印学生成绩
            printfstudent();
            break;
        case '5':  //按总分由高到低排出名次

            break;
        case '6':  //按总分由低到高排出名次
            break;
        case '7':  //按学号由小到大排出成绩表
            break;
        case '8':  //按姓名字典顺序排序排出成绩表
            break;
        }
    }
    return 0;
}

void start()
{
    printf("*****************************************\n");
    printf("欢迎使用学生成绩管理系统                *\n");
    printf("*1.录入学生信息                         *\n");
    printf("*2.保存学生信息                         *\n");
    printf("*3.读取学生信息                         *\n");
    printf("*4.打印学生信息                         *\n");
    printf("*5.按总分由高到低排出名次               *\n");
    printf("*6.按总分由低到高排出名次               *\n");
    printf("*7.按学号由小到大排出成绩表             *\n");
    printf("*8.按姓名字典顺序排序排出成绩表         *\n");
    printf("*9.根据学号查询学生成绩及排名           *\n");
    printf("*0.根据姓名查询学生成绩及排名           *\n");
    printf("*****************************************\n");
}

NODE* creatlist()    //创建表头表示整个链表即创建链表(表头可以是头结点,也可以是数据结点,通常是头结点)
{
    NODE* headNode = (NODE*)malloc(sizeof(NODE));
    headNode->next = NULL;
    return headNode;
}

void inputstudent()  
{
    int  i;
    NODE* newnode = (NODE*)malloc(sizeof(NODE));
    newnode->next = NULL;
    printf("学号:");
    scanf_s("%ld", &newnode->stu.stuID);
    printf("姓名:");
    scanf_s("%s", newnode->stu.stuname, 10);
    printf("性别:");
    scanf_s(" %c", &newnode->stu.stusex, 1);
    printf("%d门课程成绩:", N);
    for (i = 0, newnode->stu.total = 0; i < N; i++) {
        scanf_s("%d", &newnode->stu.score[i]);
        newnode->stu.total += newnode->stu.score[i];
    }
    newnode->stu.aver = (float)(newnode->stu.total / N);

    if (head == NULL)//以下代码,实现输入每个学生信息后,按总分从高到低链入链表
        head = newnode;
    else{
        NODE* p = head; 
        while (p->next && p->next->stu.total > newnode->stu.total)  p = p->next;
        if (p == head && p->stu.total < newnode->stu.total) {
            newnode->next = head;
            head = newnode;
        }
        else {
            newnode->next = p->next;
            p->next = newnode;
        }
    }
}

void savestudent()    //保存学生信息
{
    FILE* pf = fopen("pph.txt", "w"); //创建并打开文件
    if (pf == NULL) //判断打开文件是否失败
    {
        printf("打开文件失败\n");
        return;
    }
    NODE* p = head;
    while (p != NULL)
    {
        fwrite(&p->stu, sizeof(STU), 1, pf);  
        p = p->next;
    }
    fclose(pf);
    printf("数据保存成功\n");
}


void readstudent()   //读取学生信息
{
    FILE* pf = fopen("pph.txt", "r");  //打开文件
    if (pf == NULL) //判断打开文件是否失败
    {
        printf("err!\n");
        return;
    }
    NODE* pt = NULL;
    while (1)  
    {
        NODE* newnode = (NODE*)malloc(sizeof(NODE));
        newnode->next = NULL;
        if (fread(&newnode->stu, sizeof(STU), 1, pf) != 1) { 
            free(newnode);                                   
            break;
        }                                                   
        if (head == NULL)
            head = newnode;
        else
        {
            if (!pt) {
                pt = head;
                while (pt->next) pt = pt->next;
            }
            pt->next = newnode;
            pt = newnode;
        }
    }
    fclose(pf);
    printf("加载数据成功\n");
}
void printfstudent()
{
    int i;
    NODE* P = head;
    while (P != NULL)
    {
        printf("%ld %s %c", P->stu.stuID, P->stu.stuname, P->stu.stusex);
        for (i = 0; i < N; i++)
            printf(" %d", P->stu.score[i]);
        printf(" %d %.2f %d\n", P->stu.total, P->stu.aver, P->stu.line);
        P = P->next;
    }
    system("pause");
}

void rank()   // 排名次函数
{
    int   k = 0;
    NODE* P = head, * pre = NULL;
    while (P != NULL)
    {
        if (P == head)
            k++;
        else if (pre->stu.total != P->stu.total) {
            k++;
        }
        P->stu.line = k;
        pre = P;
        P = P->next;
    }
}

你可以在所有的分数中找比他小的个数,然后输出个数+1