库里斯托 2023-03-01 13:39 采纳率: 91.1%
浏览 60
已结题

关于#学生成绩管理系统#的问题,如何解决?

#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++) {     //输入4科成绩并计算总分
        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;
    }
}

帮忙完成一下switch函数中 5 6 7 8

  • 写回答

2条回答 默认 最新

  • qzjhjxj 2023-03-01 16:48
    关注

    全功能的实现如下,供参考:

    #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   line;    //排名
    }STU;
    typedef struct node //创建结点类型
    {
        STU stu;        //数据域
        struct node* next;//指向下一个节点的指针
    }NODE;
    NODE* head = NULL; //定义头指针
    
    void start();
    void inputstudent();
    void savestudent();
    void readstudent();
    void printfstudent(NODE* p = NULL);
    void rank();//排名次函数
    NODE* find_id(long id);
    NODE* find_name(char*);
    void swap(STU& stu1, STU& stu2);
    void paixu1();
    void paixu2();
    
    int main()
    {
        int exit = 0; long id; char name[10]; NODE* pt = NULL;
        while (1)
        {
            start();
            char ch = _getch();
            switch (ch)
            {
            case '1': //录入学生信息
                inputstudent();
                rank();
                break;
            case '2':  //保存学生信息
                savestudent();
                break;
            case '3'://读取学生信息
                readstudent();
                break;
            case '4':  //打印学生信息
                printfstudent();
                break;
            case'5':  //根据学号查询学生
                printf("请输入学号:");
                scanf_s("%ld", &id);
                pt = find_id(id);
                if (pt)
                    printfstudent(pt);
                else
                    printf("未找到该学号学生的记录!\n");
                break;
            case'6':  //根据姓名查询学生
                printf("请输入姓名:");
                scanf_s("%s", name, 10);
                pt = find_name(name);
                if (pt)
                    printfstudent(pt);
                else
                    printf("未找到该姓名学生的记录!\n");
                break;
            case '7':  //按学号由小到大排出成绩表
                paixu1();
                printfstudent();
                break;
            case '8':  //按姓名字典顺序排序排出成绩表
                paixu2();
                printfstudent();
                break;
            case '0':
                printf("再见!\n");
                exit = 1;
                break;
            default:
                printf("菜单选择错误,请重新选择!\n");
                break;
            }
            if (exit)  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("*0.退出系统!                           *\n");
        printf("*****************************************\n");
    }
    
    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++) { //输入4科成绩并计算总分
            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(NODE* p)
    {
        int i;
        NODE* P = NULL;
        if (!p)
            P = head;
        else
            P = p;
        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);
            if (!p)
                P = P->next;
            else
                break;
        }
        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;
        }
    }
    
    NODE* find_id(long id) //按学号查找
    {
        NODE* p = head;
        while (p) {
            if (p->stu.stuID == id)
                return p;
            p = p->next;
        }
        return NULL;
    }
    
    NODE* find_name(char* name)//按姓名查找
    {
        NODE* p = head;
        while (p) {
            if (strcmp(p->stu.stuname, name) == 0)
                return p;
            p = p->next;
        }
        return NULL;
    }
    void swap(STU& stu1, STU& stu2)
    {
        STU temp;
        temp = stu1;
        stu1 = stu2;
        stu2 = temp;
    }
    
    void paixu1()
    {
        NODE* p = NULL, * pre = NULL;
        if (!head)  return;
        for (p = head; p->next != NULL; p = p->next)
            for (pre = p->next; pre != NULL; pre = pre->next)
                if (p->stu.stuID > pre->stu.stuID)  //按学号从小到大排序
                    swap(p->stu, pre->stu);
    }
    
    void paixu2()
    {
        NODE* p = NULL, * pre = NULL;
        if (!head)  return;
        for (p = head; p->next != NULL; p = p->next)
            for (pre = p->next; pre != NULL; pre = pre->next)
                if (strcmp(p->stu.stuname, pre->stu.stuname) > 0) //按姓名字典顺序排序
                    swap(p->stu, pre->stu);
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 3月9日
  • 已采纳回答 3月1日
  • 创建了问题 3月1日

悬赏问题

  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错