C语言实现简单学生信息管理系统

 更新时间:2022年07月25日 08:56:44   作者:自然的像植物  
这篇文章主要为大家详细介绍了C语言实现简单学生信息管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

学生信息管理系统的功能有,也可以自己增加或者改进一些函数功能。

在main函数里调用这8个函数

学生信息包含姓名、年龄、学号、成绩,需要定义一个结构体(结构体是全局变量,所以需要全局声明):

typedef struct _student{
    char name[20];
    int age;
    int stuNum;
    int score;
}student;

需要有一个存储数据的空间,所以使用单链表存储,定义如下:

typedef struct _Node{
    student stu1;
    struct _Node* pNext;
}Node;

此时需要给链表一个头:

Node *g_head=NULL;

录入学生信息:

1)创建一个新节点,结点里包括结构体数据,和下一个指针。
2)需要判断头结点是不是空的,如果是空的,则此时需要将新节点付给头结点,如果不是空的,那么该结点的下一个指针=头结点(头插法,能用就行)。
3)然后就输入数据scanf("%s",&p->stu1.age);
4)最后提示一下该学生信息输入完毕!
5)现在只输入了一个学生信息,此时只需要在主函数里写一个循环就可以无限调用该函数进行数据录入。

void input(){
    printf("请输入学生信息\t\n");
    Node *pNewNode=(Node*)malloc(sizeof(Node));//创建一个新节点。
    pNewNode->pNext=NULL;
    
    if(g_head==NULL){
        g_head=pNewNode;
    }
    else{
        pNewNode->pNext=g_head;
        g_head=pNewNode;
    }
    printf("请输入姓名: ");
    scanf("%s",pNewNode->stu1.name);
    printf("请输入年龄: ");
    scanf("%d",&pNewNode->stu1.age);
    printf("请输入学号 : ");
    scanf("%d",&pNewNode->stu1.stuNum);
    printf("请输入成绩 : ");
    scanf("%d",&pNewNode->stu1.score);
printf("该学生信息输入完毕!\n\n");
}

查看信息:

1)需要对链表进行遍历然后printf;
2)新建一个结点指针然后 Node* p=g_head; while(p!=NULL){ printf p=p->next}(注:以上代码只是功能的描述)

代码:

void printdate(){
    Node* p=g_head;
    printf("\t姓名\t年龄\t学号\t成绩\n");
    while(p!=NULL)
    {
        printf("\t%s\t,%d\t,%d\t,%d\t\n",p->stu1.name,p->stu1.age,p->stu1.stuNum,p->stu1.score);
        p=p->pNext;
    }
}

保存信息:

1)先用文件指针指向一个要使用的文件,
2)便利所有链表的数据,将遍历的数据写入文件指针指向的文件里

void save(){
    FILE *fp=fopen("E:\\stu.data","w");
    if(fp==NULL){
        printf("文件打开失败");
        return;
    }
    Node* p=g_head;
    while(p!=NULL){
        fwrite(&p->stu1,1,sizeof(student),fp);
        p=p->pNext;
    }
    fclose(fp);
    printf("文件保存成功!\n");
}

文件读取:

1)先用文件指针指向一个使用的文件,
2)这里需要开辟链表来将读取到的数据写入链表里的数据里的结构体里,
3)需要判断一下是否读取到了数据,将文件里的数据先读到结构体数组里
4)然后while循环里 ,,将结构体数组里的数据复制给链表,
5)需要将每一个节点排好队列,这里用头插法,每复制一个就会开辟一个结点,

代码:

void rs(){
    FILE* fp=fopen("E:\\stu.data","r");
    if(fp==NULL){
        printf("文件打开失败");
    }
    printf("文件读取成功!\n 查看文件请按2\n");
    student stu;
    
    while(fread(&stu,1,sizeof(student),fp)){
        Node* pNewNode=(Node*)malloc(sizeof(Node));
        pNewNode->pNext=NULL;

        memcpy(pNewNode,&stu,sizeof(student));

        if(g_head==NULL){
            g_head=pNewNode;

        }
        else{
            pNewNode->pNext=g_head;
            g_head=pNewNode;

        }
    }
    }

统计人数:

1)遍历链表的时候定义一个整型数组自加最后输出

void count(){
    int a=0;
    FILE* fp=fopen("E:\\stu.data","r");
    if(fp==NULL){
        printf("文件打开失败");
        return;
    }
    Node* p=g_head;
    while(p!=NULL){
        p=p->pNext;
        a++;
    }
    printf("总人数%d",a);
}

查找学生:

1)定义整型数据,输入学号,先遍历再if判断输入的学号和p->stu1.num是否相等,相等的话输出:

void find(){
    int num;
    printf("请输入要查找的学生学号: \n");
    scanf("%d",&num);
    Node* p=g_head;
    while(p!=NULL){
        if(p->stu1.stuNum==num){
            printf("\t%s\t,%d\t,%d\t,%d\t\n",p->stu1.name,p->stu1.age,p->stu1.stuNum,p->stu1.score);
        }
        p=p->pNext;
    }
printf("无该学生信息");
}

修改信息:

1)定义整型数据,输入学号,先遍历再if判断输入的学号和p->stu1.num是否相等,相等的话重新输入:

void change(){
    int num;
    printf("请输入要修改的学生的学号: ");
    scanf("%d",&num);
    Node* p=g_head;
    while(p!=NULL){

        if(p->stu1.stuNum==num){
            printf("请输入姓名: \n");
            scanf("%s",p->stu1.name);
            printf("请输入年龄: \n");
            scanf("%d",&p->stu1.age);
            printf("请输入学号: \n");
            scanf("%d",&p->stu1.stuNum);
            printf("请输入成绩: \n");
            scanf("%d",&p->stu1.score);
            printf("信息更改完毕!");
        }

        p=p->pNext;
    }
    if(p==NULL){
        printf("该学生不存在!\n");
    }

}

删除信息:

1)思路是先遍历,找到了free就可以了,
2)需要定义两个节点指针,如果找到了是头结点直接将头结点付给定义的节点,然后free,
如果不是头结点,将该节点付给定义的节点,p->next=p->next->next;
然后free

void del(){
    int num;

    printf("请输入要删除的学号");
    scanf("%d",&num);
    Node* p=g_head;
    Node*p1,*p2;
    if(p->stu1.stuNum==num){
        p1=p->pNext;

        free(p1);

    }
    if(p->pNext!=NULL){
        p2=p->pNext;
        p->pNext=p->pNext->pNext;
        free(p2);
    }
printf("学号为%d的信息删除成功!\n",num);

}

下面是完整代码:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
typedef struct _student{
    char name[20];
    int age;
    int stuNum;
    int score;
}student;

typedef struct _Node{
    student stu1;
    struct _Node* pNext;
}Node;

Node *g_head=NULL;
void menu(){
    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("退出不要直接点叉,请按0退出!\n查看文件之前请先读取文件!\n");
}
void input(){
    printf("请输入学生信息\t\n");
    Node *pNewNode=(Node*)malloc(sizeof(Node));//创建一个新节点。
    pNewNode->pNext=NULL;
    
    if(g_head==NULL){
        g_head=pNewNode;
    }
    else{
        pNewNode->pNext=g_head;
        g_head=pNewNode;
    }
    printf("请输入姓名: ");
    scanf("%s",pNewNode->stu1.name);
    printf("请输入年龄: ");
    scanf("%d",&pNewNode->stu1.age);
    printf("请输入学号 : ");
    scanf("%d",&pNewNode->stu1.stuNum);
    printf("请输入成绩 : ");
    scanf("%d",&pNewNode->stu1.score);
printf("该学生信息输入完毕!\n\n");
}

void printdate(){
    Node* p=g_head;
    printf("\t姓名\t年龄\t学号\t成绩\n");
    while(p!=NULL)
    {
        printf("\t%s\t,%d\t,%d\t,%d\t\n",p->stu1.name,p->stu1.age,p->stu1.stuNum,p->stu1.score);
        p=p->pNext;
    }
}


void save(){
    
    FILE *fp=fopen("E:\\stu.data","w");
    if(fp==NULL){
        printf("文件打开失败");
        return;
    }
    Node* p=g_head;
    while(p!=NULL){
        fwrite(&p->stu1,1,sizeof(student),fp);
        p=p->pNext;

    }
    fclose(fp);
    printf("文件保存成功!\n");
}


void rs(){
    FILE* fp=fopen("E:\\stu.data","r");
    if(fp==NULL){
        printf("文件打开失败");
    }
    printf("文件读取成功!\n 查看文件请按2\n");
    student stu;
    
    while(fread(&stu,1,sizeof(student),fp)){
        Node* pNewNode=(Node*)malloc(sizeof(Node));
        pNewNode->pNext=NULL;

        memcpy(pNewNode,&stu,sizeof(student));

        if(g_head==NULL){
            g_head=pNewNode;

        }
        else{
            pNewNode->pNext=g_head;
            g_head=pNewNode;

        }
    }
    }

void count(){
    int a=0;
    FILE* fp=fopen("E:\\stu.data","r");
    if(fp==NULL){
        printf("文件打开失败");
        return;
    }
    Node* p=g_head;
    while(p!=NULL){
        p=p->pNext;
        a++;
    }
    printf("总人数%d",a);
}


void find(){
    int num;
    printf("请输入要查找的学生学号: \n");
    scanf("%d",&num);
    Node* p=g_head;
    while(p!=NULL){
        if(p->stu1.stuNum==num){
            printf("\t%s\t,%d\t,%d\t,%d\t\n",p->stu1.name,p->stu1.age,p->stu1.stuNum,p->stu1.score);
        }
        p=p->pNext;
    }
printf("have not");
}

void change(){
    int num;
    printf("请输入要修改的学生的学号: ");
    scanf("%d",&num);
    Node* p=g_head;
    while(p!=NULL){

        if(p->stu1.stuNum==num){
            printf("请输入姓名: \n");
            scanf("%s",p->stu1.name);
            printf("请输入年龄: \n");
            scanf("%d",&p->stu1.age);
            printf("请输入学号: \n");
            scanf("%d",&p->stu1.stuNum);
            printf("请输入成绩: \n");
            scanf("%d",&p->stu1.score);
            printf("信息更改完毕!");
        }

        p=p->pNext;
    }
    if(p==NULL){
        printf("该学生不存在!\n");
    }

}

void del(){
    int num;

    printf("请输入要删除的学号");
    scanf("%d",&num);
    Node* p=g_head;
    Node*p1,*p2;
    if(p->stu1.stuNum==num){
        p1=p->pNext;

        free(p1);

    }
    if(p->pNext!=NULL){
        p2=p->pNext;
        p->pNext=p->pNext->pNext;
        free(p2);
    }
printf("学号为%d的信息删除成功!\n",num);

}

int main()
{
    menu();
    while(1)
    {
        char ch=getch();
        switch(ch){
        case '1':input();break;
        case '2':printdate();break;
        case '3':save();break;
        case '4':rs();break;


        case '5':count();break;
        case '6':find();break;
        case '7':change();break;
        case '8':del();break;

        case '0':exit(0);
        
        }
    }

return 0;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • C++11中longlong超长整型和nullptr初始化空指针

    C++11中longlong超长整型和nullptr初始化空指针

    本文介绍 C++11 标准中新添加的 long long 超长整型和 nullptr 初始化空指针,在 C++11 标准下,相比 NULL 和 0,使用 nullptr 初始化空指针可以令我们编写的程序更加健壮,本文结合示例代码给大家详细讲解,需要的朋友跟随小编一起看看吧
    2022-12-12
  • OpenCV实现车牌定位(C++)

    OpenCV实现车牌定位(C++)

    这篇文章主要为大家详细介绍了OpenCV实现车牌定位,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-11-11
  • C++ vector操作实现

    C++ vector操作实现

    这篇文章主要介绍了C++ vector操作实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-12-12
  • C++ 标准模板类详解

    C++ 标准模板类详解

    今天小编就为大家分享一篇关于C++标准模板类的介绍与使用讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2021-09-09
  • C++多继承多态的实例详解

    C++多继承多态的实例详解

    这篇文章主要介绍了C++多继承多态的实例详解的相关资料,需要的朋友可以参考下
    2017-06-06
  • C语言实现输入ascii码,输出对应的字符方式

    C语言实现输入ascii码,输出对应的字符方式

    这篇文章主要介绍了C语言实现输入ascii码,输出对应的字符方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-01-01
  • C++ 容器 Vector 的使用方法

    C++ 容器 Vector 的使用方法

    这篇文章主要介绍了C++ 容器 Vector 的使用方法,Vector 是一个能够存放任意类型的动态数组,有点类似数组,是一个连续地址空间,下文更多详细内容的介绍,需要的小伙伴可以参考一下
    2022-06-06
  • 二分图匹配实例代码及整理

    二分图匹配实例代码及整理

    这篇文章主要介绍了二分图匹配实例代码及整理的相关资料,这里提供了三种方法包括匈牙利算法,KM算法,多重匹配,需要的朋友可以参考下
    2017-07-07
  • 一些C语言中字符串的算法问题解决实例小结

    一些C语言中字符串的算法问题解决实例小结

    这篇文章主要介绍了一些C语言中字符串的算法问题解决实例小结,包括将字符串转化为int类型的数及旋转字符串等操作,需要的朋友可以参考下
    2016-03-03
  • C语言实现flappy bird游戏

    C语言实现flappy bird游戏

    这篇文章主要为大家详细介绍了C语言实现flappy bird小游戏,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-12-12

最新评论