专栏首页张泽旭的专栏用链表编写学生管理系统(涵盖文件的输入输出)

用链表编写学生管理系统(涵盖文件的输入输出)

学过链表的大家都知道,它是一种动态的数据结构,但是它理解起来是比较困难的,但是用链表存储数据是非常好用的,可以使我们的程序更加具有健壮性。为了初学者理解单项链表更加快,今天就用链表和文件操作写了一控制台的学生管理系统,希望可以帮助大家更好的理解链表。

程序实现的功能:

源代码如下

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void AppendNode(long long studentID, char studentName[10], char studentSex[4], int score[4]);//添加数据
void DisplayNode(struct link *head);//显示数据
void InsertNode(long long studentID, char studentName[10], char studentSex[4], int score[4]);//插入一条数据;按总分的从大到小排序
void InsertNodeNumber(long long studentID, char studentName[10], char studentSex[4], int score[4]);//按学号的从小到大排序
void NumberSorting(int flag);//按从大到小对数据进行排序;1、对总分进行排序,2、对学号进行排序
void DeleteNodeID();//输入学号删除
void DeleteNodeName();//输入学号删除
void DeleteMemory(struct link *head);//删除聊表所占用的内存;
void Save();//保存数据
void Open();//打开数据
void FindID();//按学号查找学生
void FindName();//按姓名查找学生;
void Menu();
typedef struct student
{
    long long studentID;
    char studentName[10];
    char studentSex[4];
    int score[4];
}STU;
struct link
{
    STU student;
    struct link *next;
};
struct link *head = NULL;//保存输入的学生信息数据
struct link *head1 = NULL;//保存排序后的学生信息数据
int main()
{
    long long studentID;
    char studentName[10];
    char studentSex[4];
    int score[4];//定义要输入学生信息的变量;
    char c;
    int menu;//保存要进行的选项;
    Open();
    while (1)
    {
        system("pause");
        Menu();
        printf("请输入要进行的操作:");
        scanf("%d", &menu);
        switch (menu)
        {
        case 0:exit(0);
            break;
        case 1:
            printf("请输入Y或y来添加数据\n");
            scanf(" %c", &c);
            while (c == 'y' || c == 'Y')
            {
                printf("请输入学生学号:");
                scanf("%lld", &studentID);
                printf("请输入学生姓名:");
                scanf("%s", &studentName);
                printf("请输入学生性别:");
                scanf("%s", &studentSex);
                int i = 0;
                printf("请分别输入学生四门课的成绩:");
                for (i = 0; i < 4; i++)
                {
                    scanf("%d", &score[i]);
                }
                AppendNode(studentID, studentName, studentSex, score);
                printf("请输入Y或y来添加数据\n");
                scanf(" %c", &c);
            }
            DisplayNode(head);
            break;
        case 2:
             FindID();
            break;
        case 3:
             FindName();
            break;
        case 4:
             DeleteNodeID();
            break;
        case 5:
             DeleteNodeName();
        case 6:
            NumberSorting(1);
            DeleteMemory(head1);
            head1 = NULL;
            break;
        case 7:
            NumberSorting(2);
            DeleteMemory(head1);
            head1 = NULL;
            break;
        default:
            printf("输入有误!请重新输入");
        }
    }
    Save();
    DeleteMemory(head);
    DeleteMemory(head1);
    system("pause");
}
void AppendNode(long long studentID, char studentName[10], char studentSex[4], int score[4])
{
    struct link *p = NULL, *pr = head;
    p = (struct link *) malloc(sizeof(struct link));
    if (p == NULL)
    {
        printf("申请内存失败");
        return;
    }
    if (head == NULL)
    {
        head = p;
    }
    else {
        while (pr->next != NULL)
        {
            pr = pr->next;
        }
        pr->next = p;
    }
    p->student.studentID = studentID;
    strcpy(p->student.studentName, studentName);
    strcpy(p->student.studentSex, studentSex);
    p->student.score[0] = score[0];
    p->student.score[1] = score[1];
    p->student.score[2] = score[2];
    p->student.score[3] = score[3];
    p->next = NULL;
    return;
}
void NumberSorting(int flag)
{
    struct link *p = head;
    struct link *p1 = head1;
    int sum = 0;
        if(p == NULL)
        {
            printf("没有数据,无法排序");
            return;
        }
        while (p!=NULL)
        {
            switch (flag)
            {
            case 1:
                InsertNode(p->student.studentID, p->student.studentName, p->student.studentSex, p->student.score);
                break;
            case 2 :
                InsertNodeNumber(p->student.studentID, p->student.studentName, p->student.studentSex, p->student.score);
                break;
            default:
                printf("程序异常,请重试!");
                break;
            }
            p = p->next;    
        }
        DisplayNode(head1);
}
void DisplayNode(struct link *head)
{
    struct link *p = head;
    if (p == NULL)
    {
        return;
    }
    printf("%lld", p->student.studentID);
    printf("%10s", p->student.studentName);
    printf("%5s", p->student.studentSex);
    {
        int i = 0;
        for (i = 0; i < 4; i++)
        {
            printf("%4d", p->student.score[i]);
        }
    }
    printf("\n");
    DisplayNode(p->next);
}
void DeleteMemory(struct link *head)
{
    struct link *p = head, *pr = NULL;
    while (p != NULL)
    {
        pr = p;
        p = p->next;
        free(pr);
    }
    
}
void InsertNodeNumber(long long studentID, char studentName[10], char studentSex[4], int score[4])
{
    struct link *pr = head1, *p = head1, *temp = NULL;
    p = (struct link *)malloc(sizeof(struct link));
    if (p == NULL)
    {
        printf("内存申请失败");
        return;
    }
    p->next = NULL;
    p->student.studentID = studentID;
    strcpy(p->student.studentName, studentName);
    strcpy(p->student.studentSex, studentSex);
    p->student.score[0] = score[0];
    p->student.score[1] = score[1];
    p->student.score[2] = score[2];
    p->student.score[3] = score[3];
    if (head1 == NULL)
    {
        head1 = p;
    }
    else
    {
        while (pr->student.studentID < studentID&&pr->next != NULL)
        {
            temp = pr;
            pr = pr->next;    
        }
        if (pr->student.studentID >= studentID)
        {
            if (pr == head1)
            {
                p->next = head1;
                head1 = p;
            }
            else
            {
                pr = temp;
                p->next = pr->next;
                pr->next = p;
            }
        }
        else
        {
            pr->next = p;
        }
    }
    return;
}

void InsertNode(long long studentID, char studentName[10], char studentSex[4], int score[4])
{
    struct link *pr = head1, *p = head1, *temp = NULL;
    int sum, sum1;
    p = (struct link *)malloc(sizeof(struct link));
    if (p == NULL)
    {
        printf("内存申请失败");
        return;
    }
    p->next = NULL;
    p->student.studentID = studentID;
    strcpy(p->student.studentName, studentName);
    strcpy(p->student.studentSex, studentSex);
    p->student.score[0] = score[0];
    p->student.score[1] = score[1];
    p->student.score[2] = score[2];
    p->student.score[3] = score[3];
    sum = p->student.score[0] + p->student.score[1] + p->student.score[2] + p->student.score[3];
    if (head1 == NULL)
    {
        head1 = p;
    }
    else
    {
        sum1 = pr->student.score[0] + pr->student.score[1] + pr->student.score[2] + pr->student.score[3];
        while (sum1 > sum&&pr->next != NULL)
        {
            temp = pr;
            pr = pr->next;
            sum1 = pr->student.score[0] + pr->student.score[1] + pr->student.score[2] + pr->student.score[3];
        }
        if (sum1 <= sum)
        {
            if (pr == head1)
            {
                p->next = head1;
                head1 = p;
            }
            else
            {
                pr = temp;
                p->next = pr->next;
                pr->next = p;
            }
        }
        else
        {
            pr->next = p;
        }
    }
    return;
}

void DeleteNodeID()//输入学号删除
{
    struct link *p = head, *pr = head;
    long long studentID = 0;
    if (head == NULL) {
        printf("对不起,没有数据可以删除");
        return;
    }
    printf("请输入你要删除的学生的学号");
    scanf("%s", studentID);
    while (studentID != p->student.studentID&&p->next != NULL)
    {
        pr = p;
        p = p->next;
    }
    if (studentID == p->student.studentID)
    {
        if (p == head) {
            head = p->next;
        }
        else
        {
            pr->next = p->next;
        }
        free(p);
    }
    else
    {
        printf("你要查找的这个数据未找到");
    }
    return;
}
void DeleteNodeName()//输入姓名删除
{
    struct link *p = head, *pr = head;
    char studentName[10] = "";
    if (head == NULL) {
        printf("对不起,没有数据可以删除");
        return;
    }
    printf("请输入你要删除的学生的姓名:");
    scanf("%s", studentName);
    while (0!=strcmp(studentName,p->student.studentName)&&p->next != NULL)
    {
        pr = p;
        p = p->next;
    }
    if (0==strcmp(studentName, p->student.studentName))
    {
        if (p == head) {
            head = p->next;
        }
        else
        {
            pr->next = p->next;
        }
        free(p);
    }
    else
    {
        printf("你要查找的这个数据未找到");
    }
    return;
}
void Save()//保存链表中的数据
{
    FILE *fp;
    struct link *p = head;
    if ((fp = fopen("demo.txt", "w")) == NULL)
    {
        printf("打开文件失败");
        return;
    }
    while (p != NULL)
    {
        fprintf(fp, "%20lld%10s%5s%4d%4d%4d%4d", p->student.studentID, p->student.studentName, p->student.studentSex,
            p->student.score[0], 
            p->student.score[1], 
            p->student.score[2], 
            p->student.score[3]);
        p = p->next;
    }
    fclose(fp);
    return;
}
void Open()//将文件中获得的数据写入到链表中
{
    long long studentID;
    char studentName[10];
    char studentSex[4];
    int score[4];
    FILE *fp;
    char c;
    if ((fp = fopen("demo.txt", "r")) == NULL)
    {
        printf("文件打开失败");
        return;
    }
    while ((c = fgetc(fp))!=EOF)
    {
        fscanf(fp, "%20lld", &studentID);
        fscanf(fp, "%10s", studentName);
        fscanf(fp, "%5s", studentSex);
        fscanf(fp, "%4d", &score[0]);
        fscanf(fp, "%4d", &score[1]);
        fscanf(fp, "%4d", &score[2]);
        fscanf(fp, "%4d", &score[3]);
        AppendNode(studentID, studentName, studentSex, score);
    }
    fclose(fp);
}
void FindID()
{
    struct link *p = head;
    long long studentID=0;
    if (head == NULL)
    {
        printf("没有数据查找");
        return;
    }
    printf("请输入你要查找的学生的学号:");
    scanf("%lld", &studentID);
    while (studentID != p->student.studentID&&p->next != NULL)
    {
        p = p->next;
    }
    if (studentID == studentID)
    {
        printf("%lld", p->student.studentID);
        printf("%10s", p->student.studentName);
        printf("%5s", p->student.studentSex);
        {
            int i = 0;
            for (i = 0; i < 4; i++)
            {
                printf("%4d", p->student.score[i]);
            }
        }
    }
    else{
        printf("没有你要查找的数据");
    }
    return;
}
void FindName()
{
    struct link *p = head;
    char studentName[10] = "";
    if (head == NULL)
    {
        printf("没有数据查找");
        return;
    }
    printf("请输入你要查找的学生的姓名:");
    scanf("%s", studentName);
    while (0!=strcmp(studentName,p->student.studentName)&&p->next != NULL)
    {
        p = p->next;
    }
    if (0==strcmp(studentName,p->student.studentName))
    {
        printf("%lld", p->student.studentID);
        printf("%10s", p->student.studentName);
        printf("%5s", p->student.studentSex);
        {
            int i = 0;
            for (i = 0; i < 4; i++)
            {
                printf("%4d", p->student.score[i]);
            }
        }
    }
    else{
        printf("没有你要查找的数据");
    }
    return;
}
void Menu()
{
    system("cls");//清平操作; 
    printf("\n\n\n\n\n");//输入回车,形成格式; 
    printf("\t\t|...........学生管理系统..............|\n");//
    printf("\t\t|\t 0.退出。                     |\n");//“/t”水平制表,相当于一个tab键; 
    printf("\t\t|\t 1.添加学生成绩信息!         |\n");
    printf("\t\t|\t 2.查找学生(按学号)信息!     |\n");
    printf("\t\t|\t 3.查找学生(按姓名)信息!     |\n");
    printf("\t\t|\t 4.删除学生成绩(按学号)信息! |\n");
    printf("\t\t|\t 5.删除学生成绩(按姓名)信息! |\n");
    printf("\t\t|\t 6.按总分排序!               |\n");
    printf("\t\t|\t 7.按学号排序!               |\n");
    printf("\t\t|...........学生管理系统..............|\n");//
}

如对程序有什么好的建议欢迎评论指教。。。。。。

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • 信息管理系统(java)

    该项目开发的软件为网络QQ账户信息管理系统软件,是鉴于目前人们QQ的使用领域的增加,QQ信息呈爆炸性增长的前提下人们对自己的QQ信息管理的自动化与准确化的要求日...

    张泽旭
  • java JVM 报C [libresolv.so.2+0x7e7d] __libc_res_nquery+0x4dd 解决

    quartz 进行Quartz version update check failed 导致

    张泽旭
  • springcloud gateway 自定义协议转化实现

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

    张泽旭
  • Gartner年度:中国81位CIO预计IT预算平均增长12%

    【新智元导读】在GDP增速的放缓的情况下,中国多数首席信息官认为,2016年的IT预算将平均增长12%,高于2015年度8.5%的预期增幅。数字化能力在未来讲深...

    新智元
  • Spark学习之基于MLlib的机器学习

    Spark学习之基于MLlib的机器学习 1. 机器学习算法尝试根据训练数据(training data)使得表示算法行为的数学目标最大化,并以此来进行预测或作...

    王小雷
  • Java锁的那些事儿

    Java多线程开发中,如果涉及到共享资源操作场景,那就必不可少要和Java锁打交道。

    有赞coder
  • mac eclipse 安装svn插件步骤

    subclipse官网:http://subclipse.tigris.org 选择适合的版本。

    故久
  • 吴恩达机器学习笔记24-正规方程法求最优参数

    “Linear Regression with multiple variables——Normal equation”

    讲编程的高老师
  • 搞定GTD - 掌控流程之二:明确意义

    如果所有事物长期占据着收集箱,则心情会更加焦虑,所以需要定时清空收集箱,而这一步就是明确意义。 从收集箱中拿出一件物品,问一下自己”这是什么”?这件事能对个人成...

    申龙斌
  • 对 Kotlin 与 Java 编程语言的思考

    近期学习了下 Kotlin,不得不说 Kotlin 比 Java 简洁不少,个人感觉代码量能减少 50%。到现在已经使用Java语言做服务端开发两年半的时间了,...

    Yano_nankai

扫码关注云+社区

领取腾讯云代金券