| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 698 人关注过本帖
标题:学生管理系统 排序功能无法使用 求大佬帮忙 其他功能都能使用 第7个功能用不 ...
只看楼主 加入收藏
sergio-
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2023-1-3
结帖率:50%
收藏
已结贴  问题点数:20 回复次数:7 
学生管理系统 排序功能无法使用 求大佬帮忙 其他功能都能使用 第7个功能用不了
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>

typedef struct _Stduent
{
    int num;  //学号
    char name[20];  //名字
    int score;  //成绩
}Student;

typedef struct _Node
{
    struct _Stduent student;
    struct _Node* next;
}Node;

void project();

void inputstudent(Node* head);

void outputstudent(Node* head);

void countstudent(Node* head);

void findstduent(Node* head);

void savestudent(Node* head);

void loadstudent(Node* head);

void changestudent(Node* head);

void deletestudent(Node* head);

void paixu(Node* head);

int main()
{
    //创建头节点;
    Node* head = (Node*)malloc(sizeof(Node));
    head->next = NULL;
    loadstudent(head);
    while(1){
        project();
        char c = _getch();
        
        switch (c)
        {
        case '1': //录入
            inputstudent(head);
            break;
        case '2': //打印
            outputstudent(head);
            break;
        case '3': //统计
            countstudent(head);
            break;
        case '4': //查找
            findstduent(head);
            break;
        case '5': //修改
            changestudent(head);
            break;
        case '6': //删除
            deletestudent(head);
            break;
        case '7'://排序
            paixu(head);
            break;
        case '8'://退出
            system("cls");
            printf("BYE BYE");
            exit(0);
            break;
        default:
            printf("请重新输入:");
            system("pause");//暂停程序;
            system("cls");//清空控制台;
            break;
        }
 }

    return 0;
}

void project() {
    printf("*********************************\n");
    printf("*\t学生成绩管理系统\t*\n");
    printf("*********************************\n");
    printf("*\t请选择功能列表\t\t*\n");
    printf("*********************************\n");
    printf("*\t1.录入学生信息\t\t*\n");
    printf("*\t2.打印学生信息\t\t*\n");
    printf("*\t3.统计学生人数\t\t*\n");
    printf("*\t4.查找学生信息\t\t*\n");
    printf("*\t5.修改学生信息\t\t*\n");
    printf("*\t6.删除学生信息\t\t*\n");
    printf("*\t7.按成绩排序\t\t*\n");
    printf("*\t8.退出系统\t\t*\n");
    printf("*********************************\n");
}

void inputstudent(Node *head)
{
    Node* fresh=(Node*)malloc(sizeof(Node));
    fresh->next = NULL;
    printf("请输入学生的学号,姓名,成绩:");
        scanf("%d%s%d", &fresh->student.num,fresh->student.name,&fresh->student.score);

        Node* move = head;
        while (move->next != NULL) {
            move = move->next;
        }//链到最后的节点
        //将学生插入到尾部;
        move->next = fresh;

        savestudent(head);
        
          system("pause");//暂停程序;
       system("cls");//清空控制台;
}

void outputstudent(Node *head) {
    Node* move = head->next;
    while (move != NULL) {
        printf("学号:%d 姓名:%s 成绩:%d\n", move->student.num, move->student.name, move->student.score);
        move = move->next;
    }
    system("pause");//暂停程序;
    system("cls");//清空控制台;
}

void countstudent(Node* head) {
    int count = 0;
    Node* move = head->next;
    while (move != NULL) {
        count++;
        move = move->next;
    }
    printf("学生的总人数为%d\n", count);
    system("pause");//暂停程序;
    system("cls");//清空控制台;
}

void findstduent(Node* head) {
    int stunumber=0;
    printf("请输入要查找的学生学号");
    scanf("%d", &stunumber);
      Node* move = head->next;
        while (move != NULL) {
            if (stunumber == move->student.num)
            {
                printf("学号:%d姓名:%s成绩:%d\n", move->student.num, move->student.name, move->student.score);
                system("pause");//暂停程序;
                system("cls");//清空控制台;
                return;
            }
                move = move->next;
        }
        printf("未找到学生信息\n");
        system("pause");//暂停程序;
        system("cls");//清空控制台;
}

void savestudent(Node* head) {
    FILE* file=fopen(".\\student.info","w");    //  ".\\"表示当前目录 相对路径
    Node* move = head->next;
    while (move != NULL) {
        if (fwrite(&move->student, sizeof(Student), 1, file) != 1) {
            printf("写入失败\n");
            return;
        }
        move = move->next;
    }
    fclose(file);
}
   
void loadstudent(Node* head) {
    FILE* file=fopen(".\\student.info", "r");
    if (!file) {
        printf("没有学生信息,跳过读取\n");
        return;
    }
    Node* fresh = (Node*)malloc(sizeof(Node));
    fresh->next = NULL;
    Node* move = head;
    while(fread(&fresh->student, sizeof(Student), 1, file) == 1) {
        move->next = fresh;
        move = fresh;
        fresh = (Node*)malloc(sizeof(Node));
        fresh->next = NULL;
    }
    free(fresh);
    fclose(file);
    printf("读取成功\n");
}
void changestudent(Node* head) {
    printf("请输入要修改的学生学号");
    int number;
    scanf("%d", &number);  
    Node* move = head->next;
    while (move != NULL) {
        if (move->student.num == number) {
            printf("请输入修改的学生姓名,成绩\n");
            scanf("%s%d", move->student.name, &move->student.score);
            savestudent(head);
            printf("修改成功");
            system("pause");//暂停程序;
            system("cls");//清空控制台;
            return;
        }
        move = move->next;
    }
    printf("未找到学生信息");
    system("pause");//暂停程序;
    system("cls");//清空控制台;
}

void deletestudent(Node* head) {
    printf("请输入要删除的学生学号:");
    int number = 0;
    scanf("%d", &number);
    Node* move = head;
    while (move->next!= NULL) {
        if (move->next->student.num == number) {
            Node* tmp = move->next;
            move->next = move->next->next; //链这个指针的后一个;
            free(tmp);//释放删除的节点;
            tmp = NULL;
            savestudent(head);
            printf("删除成功");
            system("pause");//暂停程序;
            system("cls");//清空控制台;
            return;
     }
        move = move->next;
    }
    printf("未找到学生");
    system("pause");//暂停程序;
    system("cls");//清空控制台;
}

void paixu(Node* head) {
    Node* turn = head->next;
    Node* move = head->next;
    Node* save = NULL;
    for (turn;turn->next!=NULL;turn=turn->next) {
        for (move;move->next!= save;move=move->next) {
            if (move->student.score<move->next->student.score) {
                Student tmp = move->student;
                move->student=move->next->student;
                move->next->student=tmp;
            }
        }
        save=move;
    }
    outputstudent(head);
    system("pause");//暂停程序;
    system("cls");//清空控制台;
}







搜索更多相关主题的帖子: student next Node head printf 
2023-04-16 13:14
东海ECS
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:Python
等 级:版主
威 望:32
帖 子:412
专家分:1646
注 册:2023-1-24
收藏
得分:0 
第七个功能是啥

会当凌绝顶,一览众山小.
2023-04-16 14:28
东海ECS
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:Python
等 级:版主
威 望:32
帖 子:412
专家分:1646
注 册:2023-1-24
收藏
得分:20 


修改函数需要输入修改的学生姓名和成绩,但是缺少对应的printf语句,需要添加:
printf("请输入修改的学生姓名,成绩:");
scanf("%s%d", move->student.name, &move->student.score);
删除函数在删除完节点后,应当显示删除成功并退出函数,而不应该继续执行函数,因此需要添加return语句:
printf("删除成功\n");
savestudent(head);
return;
排序函数的第一个for循环少了一个条件turn->next!=NULL,应该改成:
for (turn; turn->next != NULL; turn = turn->next) {
排序函数的第二个for循环判断条件不正确,应该改成move!=save:
for (move; move != save; move = move->next) {
在排序函数中,每次交换两个节点的数据,最好使用指针交换,这样效率更高。可以改成以下代码:
Student* tmp = &(move->student);
move->student = move->next->student;
move->next->student = *tmp;

会当凌绝顶,一览众山小.
2023-04-16 14:33
sergio-
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2023-1-3
收藏
得分:0 
回复 3楼 东海ECS
非常感谢你的回复和帮助
这是我修改后的
但是排序功能还是使用不了

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>

typedef struct _Stduent
{
    int num;  //学号
    char name[20];  //名字
    int score;  //成绩
}Student;

typedef struct _Node
{
    struct _Stduent student;
    struct _Node* next;
}Node;

void project();

void inputstudent(Node* head);

void outputstudent(Node* head);

void countstudent(Node* head);

void findstduent(Node* head);

void savestudent(Node* head);

void loadstudent(Node* head);

void changestudent(Node* head);

void deletestudent(Node* head);

void paixu(Node *&head);

int main()
{
    //创建头节点;
    Node* head = (Node*)malloc(sizeof(Node));
    head->next = NULL;
    loadstudent(head);
    while(1){
        project();
        char c = _getch();
        
        switch (c)
        {
        case '1': //录入
            inputstudent(head);
            break;
        case '2': //打印
            outputstudent(head);
            break;
        case '3': //统计
            countstudent(head);
            break;
        case '4': //查找
            findstduent(head);
            break;
        case '5': //修改
            changestudent(head);
            break;
        case '6': //删除
            deletestudent(head);
            break;
        case '7'://排序
            paixu(head);
            break;
        case '8'://退出
            system("cls");
            printf("BYE BYE");
            exit(0);
            break;
        default:
            printf("请重新输入:");
            system("pause");//暂停程序;
            system("cls");//清空控制台;
            break;
        }
 }

    return 0;
}

void project() {
    printf("*********************************\n");
    printf("*\t学生成绩管理系统\t*\n");
    printf("*********************************\n");
    printf("*\t请选择功能列表\t\t*\n");
    printf("*********************************\n");
    printf("*\t1.录入学生信息\t\t*\n");
    printf("*\t2.打印学生信息\t\t*\n");
    printf("*\t3.统计学生人数\t\t*\n");
    printf("*\t4.查找学生信息\t\t*\n");
    printf("*\t5.修改学生信息\t\t*\n");
    printf("*\t6.删除学生信息\t\t*\n");
    printf("*\t7.按成绩排序\t\t*\n");
    printf("*\t8.退出系统\t\t*\n");
    printf("*********************************\n");
}

void inputstudent(Node *head)
{
    Node* fresh=(Node*)malloc(sizeof(Node));
    fresh->next = NULL;
    printf("请输入学生的学号,姓名,成绩:");
        scanf("%d%s%d", &fresh->student.num,fresh->student.name,&fresh->student.score);

        Node* move = head;
        while (move->next != NULL) {
            move = move->next;
        }//链到最后的节点
        //将学生插入到尾部;
        move->next = fresh;

        savestudent(head);
        
          system("pause");//暂停程序;
       system("cls");//清空控制台;
}

void outputstudent(Node *head) {
    Node* move = head->next;
    while (move != NULL) {
        printf("学号:%d 姓名:%s 成绩:%d\n", move->student.num, move->student.name, move->student.score);
        move = move->next;
    }
    system("pause");//暂停程序;
    system("cls");//清空控制台;
}

void countstudent(Node* head) {
    int count = 0;
    Node* move = head->next;
    while (move != NULL) {
        count++;
        move = move->next;
    }
    printf("学生的总人数为%d\n", count);
    system("pause");//暂停程序;
    system("cls");//清空控制台;
}

void findstduent(Node* head) {
    int stunumber=0;
    printf("请输入要查找的学生学号");
    scanf("%d", &stunumber);
      Node* move = head->next;
        while (move != NULL) {
            if (stunumber == move->student.num)
            {
                printf("学号:%d姓名:%s成绩:%d\n", move->student.num, move->student.name, move->student.score);
                system("pause");//暂停程序;
                system("cls");//清空控制台;
                return;
            }
                move = move->next;
        }
        printf("未找到学生信息\n");
        system("pause");//暂停程序;
        system("cls");//清空控制台;
}

void savestudent(Node* head) {
    FILE* file=fopen(".\\student.info","w");    //  ".\\"表示当前目录 相对路径
    Node* move = head->next;
    while (move != NULL) {
        if (fwrite(&move->student, sizeof(Student), 1, file) != 1) {
            printf("写入失败\n");
            return;
        }
        move = move->next;
    }
    fclose(file);
}
   
void loadstudent(Node* head) {
    FILE* file=fopen(".\\student.info", "r");
    if (!file) {
        printf("没有学生信息,跳过读取\n");
        return;
    }
    Node* fresh = (Node*)malloc(sizeof(Node));
    fresh->next = NULL;
    Node* move = head;
    while(fread(&fresh->student, sizeof(Student), 1, file) == 1) {
        move->next = fresh;
        move = fresh;
        fresh = (Node*)malloc(sizeof(Node));
        fresh->next = NULL;
    }
    free(fresh);
    fclose(file);
    printf("读取成功\n");
}
void changestudent(Node* head) {
    printf("请输入要修改的学生学号");
    int number;
    scanf("%d", &number);  
    Node* move = head->next;
    while (move != NULL) {
        if (move->student.num == number) {
            printf("请输入修改的学生姓名,成绩\n");
            scanf("%s%d", move->student.name, &move->student.score);
            savestudent(head);
            printf("修改成功");
            system("pause");//暂停程序;
            system("cls");//清空控制台;
            return;
        }
        move = move->next;
    }
    printf("未找到学生信息");
   
    system("pause");//暂停程序;
    system("cls");//清空控制台;
}

void deletestudent(Node* head) {
    printf("请输入要删除的学生学号:");
    int number = 0;
    scanf("%d", &number);
    Node* move = head;
    while (move->next!= NULL)
     {
        if (move->next->student.num == number)
        {
            Node* tmp = move->next;
            move->next = move->next->next; //链这个指针的后一个;
            free(tmp);//释放删除的节点;
            tmp = NULL;
            savestudent(head);
            printf("删除成功");
            system("pause");//暂停程序;
            system("cls");//清空控制台;
            return;
        }
        move = move->next;
    }
    printf("未找到学生");
    system("pause");//暂停程序;
    system("cls");//清空控制台;
}

void paixu(Node *&head) {
    Node* turn = head->next;
    Node* move = head->next;
    Node* save = NULL;
    for (turn;turn->next!=NULL;turn=turn->next) {
        for (move;move!= save;move=move->next) {
            if (move->student.score<move->next->student.score) {
                Student *tmp =&(move->student);
                move->student=move->next->student;
                move->next->student=*tmp;
            }
        }
        save=move;
    }
    outputstudent(head);
    system("pause");//暂停程序;
    system("cls");//清空控制台;
}







2023-04-16 15:08
sergio-
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2023-1-3
收藏
得分:0 
回复 3楼 东海ECS
感谢 已解决 move定义错了
2023-04-16 16:05
forever74
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:CC
等 级:版主
威 望:58
帖 子:1705
专家分:4345
注 册:2007-12-27
收藏
得分:0 
嗯,建议不要满足于看上去正确。
链表的排序讲究的是数据不动,只动next 。
动了数据效率就上不去了。

对宇宙最严谨的描述应该就是宇宙其实是不严谨的
2023-04-16 16:29
sergio-
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2023-1-3
收藏
得分:0 
回复 5楼 sergio-
好的 受教
2023-04-16 21:18
cheetah
Rank: 3Rank: 3
等 级:论坛游侠
威 望:3
帖 子:120
专家分:118
注 册:2013-6-29
收藏
得分:0 
以下是引用forever74在2023-4-16 16:29:07的发言:

嗯,建议不要满足于看上去正确。
链表的排序讲究的是数据不动,只动next 。
动了数据效率就上不去了。

不会吧,只是交换一下里面的值也会影响效率吗?

天道酬勤
2023-04-17 22:01
快速回复:学生管理系统 排序功能无法使用 求大佬帮忙 其他功能都能使用 第7个功 ...
数据加载中...
 
   



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

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