标题:求大佬救救孩子 为啥我写的c语言 学生管理系统 只能运行一次。第二次打开 几 ...
只看楼主
sergio-
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2023-1-3
结帖率:50%
已结贴  问题点数:20 回复次数:5 
求大佬救救孩子 为啥我写的c语言 学生管理系统 只能运行一次。第二次打开 几个功能就跑不了了 是存放数据出错了吗
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>

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

typedef struct _Node
{
    Student 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");//清空控制台;
}
搜索更多相关主题的帖子: Node Student next printf head 
2023-01-03 21:10
sergio-
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2023-1-3
得分:0 
求帮忙
2023-01-03 21:11
yahooboby
Rank: 2
等 级:论坛游民
威 望:2
帖 子:6
专家分:40
注 册:2016-4-27
得分:10 
    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;   //可能是这行多了一个等号,改成fresh->next = NULL;后正常
    }
    free(fresh);
    fclose(file);
2023-01-04 15:59
不会游泳的虾
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:24
帖 子:101
专家分:643
注 册:2021-7-1
得分:10 
代码里排序函数有问题的,重新写了排序函数,其他没什么大问题,修改如下,供参考:
程序代码:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>

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

typedef struct _Node
{
    Student 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 Sort(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'://排序
            Sort(head);  //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;  //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 Sort(Node* head)       //修改
{
    Node* p = NULL, * q = NULL, * t;
    t = NULL;
    while (head->next != t) 
    {
        p = head;
        q = head->next;
        while (q->next != t)
        {
            if (q->student.score < q->next->student.score)
            {
                p->next = q->next;
                q->next = q->next->next;
                p->next->next = q;
                q = p->next;
            }
            q = q->next;
            p = p->next;
        }
        t = q;
    }
    outputstudent(head);
    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");//清空控制台;
}
2023-01-05 16:30
sergio-
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2023-1-3
得分:0 
回复 3楼 yahooboby
谢谢 解决了 感谢
2023-01-05 21:48
sergio-
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2023-1-3
得分:0 
回复 4楼 不会游泳的虾
特别谢谢 谢谢帮忙
2023-01-05 21:51



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-511044-1-1.html




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

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