当前位置:   article > 正文

C语言课程设计||学生管理系统(简单版,超详细,看这一篇就够了)_学生管理系统c语言代码

学生管理系统c语言代码

前言

嗨嗨嗨!!这里是是等等(。・∀・)ノ゙经过大一上学期的学习,老师希望我们能运用所学知识来编写一个学生管理系统,我也是磕磕碰碰完成啦。代码呢,总共500来行,用链表做的,总体是跟着视频来的,链接我放在后面啦,需要的小伙伴可以去看看(视频里用到的编译器是VS,我用到的编译器是CLion,所以可能有的地方不太一样)。那么来看看代码吧(ง •_•)ง

一、需求分析

我也是挑了一个比较简单的,但是功能还是很全的o( ̄▽ ̄)o

一个学生通讯管理系统,学生信息包括职学号、姓名、班级、住址、电话、QQ,其中学号不重复。该系统具有以下功能:
(1)学生通讯信息录入(储存在文件中);
(2)学生通讯信息浏览
(3)查询功能:包括按学号查询、按姓名查询,按班级查询;
(4)学生通讯信息删除:按学号删除、按姓名删除;
(5)学生通讯信息修改:按学号修改、按姓名修改;
(6)学生通讯信息排序:按学号升序、按学号降序;
(7)学生通讯信息统计:统计各班人数、统计各省份人数;

二、代码解析

1、总体框架

定义一个结构体存放学生通讯信息;定义节点,节点的数据域为存放学生信息的结构体;创建节点和创建表单独写一个函数,方便调用;

#include<stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 6 //班级数量

//数据设计
struct student{
    int num;
    char name[20];
    int class;
    char addr[50];
    char tel[20];
    char QQ[50];
};

struct Node{
    struct student data;
    struct Node *next;
};
//创建表
struct Node *createList(){
    struct Node* headNode = (struct Node*)malloc(sizeof(struct Node));
    headNode->next = NULL;
    return headNode;
}
//创建节点
struct Node *createNode(struct student data){
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = data;
    newNode->next = NULL;
    return newNode;
}
//函数声明
void menu();//菜单
void keyDown();//用户交互
void keydown1();//用户交互
void keydown2();//用户交互
void keydown3();//用户交互
void keydown4();//用户交互
void keydown5();//用户交互
void insertNodeByHead(struct Node* headNode,struct student data);//1.录入
void printList(struct Node* headNode);//2.浏览
void searchInfoByNum(struct Node* headNode,int num);//3.按学号查询
void searchInfoByName(struct Node* headNode,char *name);//3.按名字查询
void searchInfoByClass(struct Node* headNode,int class);//3.按班级查询
void deleteNumNode(struct Node* headNode,int num);//4.按学号删除
void deleteNameNode(struct Node* headNode,char *name);//4.按姓名删除
void alterNumNode(struct Node* headNode,int num);//5.按学号修改
void alterNameNode(struct Node* headNode,char* name);//5.按姓名修改
void sortUp(struct Node* headNode);//6.按学号升序排序
void sortDrop(struct Node* headNode);//6.按学号降序排序
void countClass(struct Node* headNode);//7.统计各班人数
void countProvince(struct Node* headNode);//7.统计各省份人数
void readInFromFile(struct Node* headNode,char* filename);//文件读操作
void writeInFromFile(struct Node* headNode,char* filename);//文件写操作

struct Node* list = NULL;//创建链表,在主函数内赋值

int main()
{
    list = createList();
    readInFromFile(list,"student.txt");
    while (1)
    {
        menu();
        keyDown();
        system("pause");
        system("cls");
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70

2、菜单和用户交互

这里就没什么可说的啦,为了方便大家理解,我做了个树状图( ̄▽ ̄)*

在这里插入图片描述

//菜单
void menu(){
    printf("****************【学生通讯录管理系统】**************\n");
    printf("*\t\t1.学生通讯信息录入                 *\n");
    printf("*--------------------------------------------------*\n");
    printf("*\t\t2.学生通讯信息浏览                 *\n");
    printf("*--------------------------------------------------*\n");
    printf("*\t\t3.查询功能                         *\n");
    printf("*--------------------------------------------------*\n");
    printf("*\t\t4.学生通讯信息删除                 *\n");
    printf("*--------------------------------------------------*\n");
    printf("*\t\t5.学生通讯信息修改                 *\n");
    printf("*--------------------------------------------------*\n");
    printf("*\t\t6.学生通讯信息排序                 *\n");
    printf("*--------------------------------------------------*\n");
    printf("*\t\t7.学生通讯信息统计                 *\n");
    printf("*--------------------------------------------------*\n");
    printf("*\t\t0.退出系统                         *\n");
    printf("****************************************************\n");
}
//用户交互
void keyDown(){
    struct student data;
    int choice = 0;
    scanf("%d",&choice);
    switch(choice){
        case 0:
            printf("正常退出。");
            system("pause");
            exit(0);
            break;
        case 1:
            printf("----------------【学生通讯信息录入】----------------\n");
            printf("请输入学生的学号,姓名,班级,地址,电话,QQ:\n");
            fflush(stdin);
            scanf("%d %s %d %s %s %s",&data.num,data.name,&data.class,data.addr,data.tel,data.QQ);
            insertNodeByHead(list,data);
            printf("录入成功!\n");
            break;
        case 2:
            printf("----------------【学生通讯信息浏览】----------------\n");
            printList(list);
            break;
        case 3:
            printf("----------------【查询功能】----------------\n");
            printf("\t\t1.按学号查询\n");
            printf("\t\t2.按姓名查询\n");
            printf("\t\t3.按班级查询\n");
            keydown1();
            break;
        case 4:
            printf("----------------【学生通讯信息删除】----------------\n");
            printf("\t\t1.按学号删除\n");
            printf("\t\t2.按姓名删除\n");
            keydown2();
            break;
        case 5:
            printf("----------------【学生通讯信息修改】----------------\n");
            printf("\t\t1.按学号查找并修改\n");
            printf("\t\t2.按姓名查找并修改\n");
            keydown3();
            break;
        case 6:
            printf("----------------【学生通讯信息排序】----------------\n");
            printf("\t\t1.按学号升序排序\n");
            printf("\t\t2.按学号降序排序\n");
            keydown4();
            break;
        case 7:
            printf("----------------【学生通讯信息统计】----------------\n");
            printf("\t\t1.统计各班人数\n");
            printf("\t\t2.统计各省份人数\n");
            keydown5();
            break;
        default:
            printf("选择错误,请重新输入......\n");
            system("pause");
    }
    writeInFromFile(list,"student.txt");
}
void keydown1(){
    int choice = 0;
    scanf("%d",&choice);
    struct student data;
    switch(choice){
        case 1:
            printf("----------------【按学号查询】----------------\n");
            printf("请输入学号:");
            scanf("%d",&data.num);
            searchInfoByNum(list,data.num);
            break;
        case 2:
            printf("----------------【按姓名查询】----------------\n");
            printf("请输入姓名:");
            scanf("%s",data.name);
            searchInfoByName(list,data.name);
            break;
        case 3:
            printf("----------------【按班级查询】----------------\n");
            printf("请输入班级:");
            scanf("%d",&data.class);
            searchInfoByClass(list,data.class);
            break;
        default:
            printf("选择错误,请重新输入......\n");
            system("pause");
            break;
    }
}
void keydown2(){
    struct student data;
    int choice = 0;
    scanf("%d",&choice);
    switch(choice){
        case 1:
            printf("----------------【按学号删除】----------------\n");
            printf("请输入删除学生的学号:\n");
            scanf("%d",&data.num);
            deleteNumNode(list,data.num);
            break;
        case 2:
            printf("----------------【按姓名删除】----------------\n");
            printf("请输入删除学生的姓名:\n");
            scanf("%s",data.name);
            deleteNameNode(list,data.name);
            break;
        default:
            printf("选择错误,请重新输入......\n");
            system("pause");
            break;
    }
}
void keydown3() {
    struct student data;
    int choice = 0;
    scanf("%d", &choice);
    switch (choice) {
        case 1:
            printf("----------------【按学号查找并修改】----------------\n");
            printf("请输入需要修改学生的学号:\n");
            scanf("%d", &data.num);
            alterNumNode(list, data.num);
            break;
        case 2:
            printf("----------------【按姓名查找并修改】----------------\n");
            printf("请输入需要修改学生的姓名:\n");
            scanf("%s", data.name);
            alterNameNode(list,data.name);
            break;
        default:
            printf("选择错误,请重新输入......\n");
            system("pause");
            break;
    }
}
void keydown4(){
    int choice = 0;
    scanf("%d", &choice);
    switch (choice) {
        case 1:
            printf("----------------【按学号升序排序】----------------\n");
            sortUp(list);
            break;
        case 2:
            printf("----------------【按学号降序排序】----------------\n");
            sortDrop(list);
            break;
        default:
            printf("选择错误,请重新输入......\n");
            system("pause");
            break;
    }
}
void keydown5(){

    int choice = 0;
    scanf("%d", &choice);
    switch (choice) {
        case 1:
            printf("----------------【统计各班人数】----------------\n");
            countClass(list);
            break;
        case 2:
            printf("----------------【统计各省份人数】----------------\n");
            countProvince(list);
            break;
        default:
            printf("选择错误,请重新输入......\n");
            system("pause");
            break;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192

运行界面
在这里插入图片描述

3、录入和浏览

录入:将学生通讯信息储存在链表中。
浏览:遍历链表。

//录入
void insertNodeByHead(struct Node* headNode,struct student data)
{
    struct Node* newNode = createNode(data);
    newNode->next = headNode->next;
    headNode->next = newNode;
}
//浏览
void printList(struct Node* headNode){
    struct Node* pMove = headNode->next;
    printf("学号\t姓名\t班级\t地址\t电话\t\tQQ\n");
    while (pMove){
        printf("%d\t%s\t%d\t%s\t%s\t%s\n",pMove->data.num,pMove->data.name,pMove->data.class,pMove->data.addr,pMove->data.tel,pMove->data.QQ);
        pMove = pMove->next;
    }
    printf("\n");
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

运行界面
在这里插入图片描述

在这里插入图片描述

4、查询

遍历链表,如该学生存在,则打印该学生信息,不存在则退出。

//按学号查询
void searchInfoByNum(struct Node* headNode,int num){
    struct Node* pMove = headNode;
    while (pMove){
        if(pMove -> data.num == num){
            printf("学号\t姓名\t班级\t地址\t电话\t\tQQ\n");
            printf("%d\t%s\t%d\t%s\t%s\t%s\n", pMove->data.num, pMove->data.name, pMove->data.class, pMove->data.addr,
                   pMove->data.tel, pMove->data.QQ);
            break;
        }
        pMove = pMove -> next;
    }
    if(pMove == NULL){
        printf("学号不存在,无法查找!\n ");
        return ;
    }

}
//按名字查询
void searchInfoByName(struct Node* headNode,char *name){
    struct Node* pMove = headNode;
    while (pMove){
        if(strcmp(pMove -> data.name,name) == 0){
            printf("学号\t姓名\t班级\t地址\t电话\t\tQQ\n");
            printf("%d\t%s\t%d\t%s\t%s\t%s\n", pMove->data.num, pMove->data.name, pMove->data.class, pMove->data.addr,
                   pMove->data.tel, pMove->data.QQ);
            break;
        }
        pMove = pMove -> next;
    }
    if(pMove == NULL){
        printf("名字不存在,无法查找!\n ");
        return ;
    }
}
//按班级查询
void searchInfoByClass(struct Node* headNode,int class){
    struct Node* pMove = headNode;
    printf("学号\t姓名\t班级\t地址\t电话\t\tQQ\n");
    while (pMove){
        if(pMove -> data.class == class){
            printf("%d\t%s\t%d\t%s\t%s\t%s\n", pMove->data.num, pMove->data.name, pMove->data.class, pMove->data.addr,
                   pMove->data.tel, pMove->data.QQ);
        }
        pMove = pMove -> next;
    }

    if(pMove == NULL){
        printf("班级不存在,无法查找!\n ");
        return ;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52

运行介面
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

5、删除

遍历链表,如存在该学生,释放节点(即删除),不存在则退出。

//按学号删除
void deleteNumNode(struct Node* headNode,int num){
    struct Node* posNode = headNode -> next;
    struct Node* posFrontNode = headNode;
    while(posNode -> data.num != num)
    {
        posFrontNode = posNode;
        posNode = posNode -> next;
    }
    if(posNode == NULL){
        printf("未找学号位置,无法删除!");
        return;
    }
    else{
        posFrontNode -> next = posNode -> next;
        free(posNode);
        printf("删除成功!");
    }

}
//按名字删除
void deleteNameNode(struct Node* headNode,char *name){
    struct Node* posNode = headNode -> next;
    struct Node* posFrontNode = headNode;
    if(posNode == NULL){
        printf("名字不存在,无法删除!\n ");
        return;
    }
    while(strcmp(posNode -> data.name,name))
    {
        posFrontNode = posNode;
        posNode = posNode -> next;
    }
    if(posNode == NULL){
        printf("未找名字位置,无法删除!");
        return;
    }
    else{
        posFrontNode -> next = posNode -> next;
        free(posNode);
        printf("删除成功!");
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

运行介面
在这里插入图片描述
在这里插入图片描述

6、修改

先删除再重新录入,结合删除功能和录入功能。

//按学号修改
void alterNumNode(struct Node* headNode,int num){
    struct Node* posNode = headNode -> next;
    struct Node* posFrontNode = headNode;
    while(posNode -> data.num != num)
    {
        posFrontNode = posNode;
        posNode = posNode -> next;
    }
    if(posNode == NULL){
        printf("未找该学生,无法修改!");
        return;
    }
    else{
        struct student data;
        posFrontNode -> next = posNode -> next;
        free(posNode);
        printf("请重新输入学生的学号,姓名,班级,地址,电话,QQ:\n");
        fflush(stdin);
        scanf("%d %s %d %s %s %s",&data.num,data.name,&data.class,data.addr,data.tel,data.QQ);
        insertNodeByHead(list,data);
        printf("修改成功!");
    }
}
//按姓名修改
void alterNameNode(struct Node* headNode,char* name){
    struct Node* posNode = headNode -> next;
    struct Node* posFrontNode = headNode;
    while(strcmp(posNode -> data.name,name))
    {
        posFrontNode = posNode;
        posNode = posNode -> next;
    }
    if(posNode == NULL){
        printf("未找该学生,无法修改!");
        return;
    }
    else{
        struct student data;
        posFrontNode -> next = posNode -> next;
        free(posNode);
        printf("请重新输入学生的学号,姓名,班级,地址,电话,QQ:\n");
        fflush(stdin);
        scanf("%d %s %d %s %s %s",&data.num,data.name,&data.class,data.addr,data.tel,data.QQ);
        insertNodeByHead(list,data);
        printf("修改成功!");
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

运行界面
在这里插入图片描述
在这里插入图片描述

7、排序

采用了冒泡排序法。

//按学号升序排序
void sortUp(struct Node* headNode){
    struct Node* turn;
    struct Node* move;
    struct student temp;
    for(turn = headNode -> next;turn -> next != NULL;turn = turn -> next){
        for(move = headNode -> next;move -> next != NULL;move = move -> next){
            if(move -> data.num > move -> next -> data.num){
                temp = move -> data;
                move -> data = move -> next -> data;
                move -> next -> data = temp;
            }
        }
    }
    printList(list);
}
//按学号降序排序
void sortDrop(struct Node* headNode){
    struct Node* turn;
    struct Node* move;
    struct student temp;
    for(turn = headNode -> next;turn -> next != NULL;turn = turn -> next){
        for(move = headNode -> next;move -> next != NULL;move = move -> next){
            if(move -> data.num < move -> next -> data.num){
                temp = move -> data;
                move -> data = move -> next -> data;
                move -> next -> data = temp;
            }
        }
    }
    printList(list);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

运行介面
在这里插入图片描述
在这里插入图片描述

8、统计

遍历链表,定义计数数组,用来存放各班级人数或各省份人数,最后将统计结果输出。

//统计各班级人数
void countClass(struct Node* headNode){
    int i;
    int count[N + 1] = {0};
    struct Node* pMove = headNode;
    while (pMove){
        for( i = 1;i <= N; i++){
            if (pMove->data.class == i ) count[i]++;
        }
        pMove = pMove -> next;
    }
    for (int i = 1;i <= N; i++) {
        printf("%d班的人数为:%d\n",i,count[i]);
    }
}
//统计各省份人数
void countProvince(struct Node* headNode){
    char province[34][100] = {"Hebei","Shanxi","Liaoning","Jilin","Heilongjiang","Jiangsu","Zhejiang","Anhui","Fujian","Jiangxi","Shandong","Henan","Hubei","Hunan","Guangdong","Hannan","Sichuan","Guizhou","Yunnan","Shanxi","Gansu","Qinghai","Taiwan","Neimenggu","Guangxi","Xizang","Ningxia","Xinjiang","Beijiang","Tianjing","Shanghai","Chongqing","Xianggang","Aomen"};
    int i;
    int count[34] ={0};
    struct Node* pMove = headNode;
    while (pMove){
        for( i = 0;i < 34; i++){
            if (strcmp(pMove->data.addr,province[i]) == 0 ) count[i]++;
        }
        pMove = pMove -> next;
    }
    for (int i = 0;i < 34; i++) {
        if(count[i] != 0)
            printf("地址为%s的人数为%d\n",province[i],count[i]);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

运行介面
在这里插入图片描述
在这里插入图片描述

三、总代码展示

#include<stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 6

//数据设计
struct student{
    int num;
    char name[20];
    int class;
    char addr[50];
    char tel[20];
    char QQ[50];
};

struct Node{
    struct student data;
    struct Node *next;
};
//创建表
struct Node *createList(){
    struct Node* headNode = (struct Node*)malloc(sizeof(struct Node));
    headNode->next = NULL;
    return headNode;
}
//创建结点
struct Node *createNode(struct student data){
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = data;
    newNode->next = NULL;
    return newNode;
}
//函数声明
void menu();//菜单

void keyDown();//用户交互
void keydown1();//用户交互
void keydown2();//用户交互
void keydown3();//用户交互
void keydown4();//用户交互
void keydown5();//用户交互
void insertNodeByHead(struct Node* headNode,struct student data);//1.录入
void printList(struct Node* headNode);//2.浏览
void searchInfoByNum(struct Node* headNode,int num);//3.按学号查询
void searchInfoByName(struct Node* headNode,char *name);//3.按名字查询
void searchInfoByClass(struct Node* headNode,int class);//3.按班级查询
void deleteNumNode(struct Node* headNode,int num);//4.按学号删除
void deleteNameNode(struct Node* headNode,char *name);//4.按姓名删除
void alterNumNode(struct Node* headNode,int num);//5.按学号修改
void alterNameNode(struct Node* headNode,char* name);//5.按姓名修改
void sortUp(struct Node* headNode);//6.按学号升序排序
void sortDrop(struct Node* headNode);//6.按学号降序排序
void countClass(struct Node* headNode);//7.统计各班人数
void countProvince(struct Node* headNode);//7.统计各省份人数
void readInFromFile(struct Node* headNode,char* filename);//文件读操作
void writeInFromFile(struct Node* headNode,char* filename);//文件写操作

struct Node* list = NULL;

int main()
{
    list = createList();
    readInFromFile(list,"student.txt");
    while (1)
    {
        menu();
        keyDown();
        system("pause");
        system("cls");
    }
}
//菜单
void menu(){
    printf("****************【学生通讯录管理系统】**************\n");
    printf("*\t\t1.学生通讯信息录入                 *\n");
    printf("*--------------------------------------------------*\n");
    printf("*\t\t2.学生通讯信息浏览                 *\n");
    printf("*--------------------------------------------------*\n");
    printf("*\t\t3.查询功能                         *\n");
    printf("*--------------------------------------------------*\n");
    printf("*\t\t4.学生通讯信息删除                 *\n");
    printf("*--------------------------------------------------*\n");
    printf("*\t\t5.学生通讯信息修改                 *\n");
    printf("*--------------------------------------------------*\n");
    printf("*\t\t6.学生通讯信息排序                 *\n");
    printf("*--------------------------------------------------*\n");
    printf("*\t\t7.学生通讯信息统计                 *\n");
    printf("*--------------------------------------------------*\n");
    printf("*\t\t0.退出系统                         *\n");
    printf("****************************************************\n");
}
//用户交互
void keyDown(){
    struct student data;
    int choice = 0;
    scanf("%d",&choice);
    switch(choice){
        case 0:
            printf("正常退出。");
            system("pause");
            exit(0);
            break;
        case 1:
            printf("----------------【学生通讯信息录入】----------------\n");
            printf("请输入学生的学号,姓名,班级,地址,电话,QQ:\n");
            fflush(stdin);
            scanf("%d %s %d %s %s %s",&data.num,data.name,&data.class,data.addr,data.tel,data.QQ);
            insertNodeByHead(list,data);
            printf("录入成功!");
            break;
        case 2:
            printf("----------------【学生通讯信息浏览】----------------\n");
            printList(list);
            break;
        case 3:
            printf("----------------【查询功能】----------------\n");
            printf("\t\t1.按学号查询\n");
            printf("\t\t2.按姓名查询\n");
            printf("\t\t3.按班级查询\n");
            keydown1();
            break;
        case 4:
            printf("----------------【学生通讯信息删除】----------------\n");
            printf("\t\t1.按学号删除\n");
            printf("\t\t2.按姓名删除\n");
            keydown2();
            break;
        case 5:
            printf("----------------【学生通讯信息修改】----------------\n");
            printf("\t\t1.按学号查找并修改\n");
            printf("\t\t2.按姓名查找并修改\n");
            keydown3();
            break;
        case 6:
            printf("----------------【学生通讯信息排序】----------------\n");
            printf("\t\t1.按学号升序排序\n");
            printf("\t\t2.按学号降序排序\n");
            keydown4();
            break;
        case 7:
            printf("----------------【学生通讯信息统计】----------------\n");
            printf("\t\t1.统计各班人数\n");
            printf("\t\t2.统计各省份人数\n");
            keydown5();
            break;
        default:
            printf("选择错误,请重新输入......\n");
            system("pause");
    }
    writeInFromFile(list,"student.txt");
}
void keydown1(){
    int choice = 0;
    scanf("%d",&choice);
    struct student data;
    switch(choice){
        case 1:
            printf("----------------【按学号查询】----------------\n");
            printf("请输入学号:");
            scanf("%d",&data.num);
            searchInfoByNum(list,data.num);
            break;
        case 2:
            printf("----------------【按姓名查询】----------------\n");
            printf("请输入姓名:");
            scanf("%s",data.name);
            searchInfoByName(list,data.name);
            break;
        case 3:
            printf("----------------【按班级查询】----------------\n");
            printf("请输入班级:");
            scanf("%d",&data.class);
            searchInfoByClass(list,data.class);
            break;
        default:
            printf("选择错误,请重新输入......\n");
            system("pause");
            break;
    }
}
void keydown2(){
    struct student data;
    int choice = 0;
    scanf("%d",&choice);
    switch(choice){
        case 1:
            printf("----------------【按学号删除】----------------\n");
            printf("请输入删除学生的学号:\n");
            scanf("%d",&data.num);
            deleteNumNode(list,data.num);
            break;
        case 2:
            printf("----------------【按姓名删除】----------------\n");
            printf("请输入删除学生的姓名:\n");
            scanf("%s",data.name);
            deleteNameNode(list,data.name);
            break;
        default:
            printf("选择错误,请重新输入......\n");
            system("pause");
            break;
    }
}
void keydown3() {
    struct student data;
    int choice = 0;
    scanf("%d", &choice);
    switch (choice) {
        case 1:
            printf("----------------【按学号查找并修改】----------------\n");
            printf("请输入需要修改学生的学号:\n");
            scanf("%d", &data.num);
            alterNumNode(list, data.num);
            break;
        case 2:
            printf("----------------【按姓名查找并修改】----------------\n");
            printf("请输入需要修改学生的姓名:\n");
            scanf("%s", data.name);
            alterNameNode(list,data.name);
            break;
        default:
            printf("选择错误,请重新输入......\n");
            system("pause");
            break;
    }
}
void keydown4(){
    int choice = 0;
    scanf("%d", &choice);
    switch (choice) {
        case 1:
            printf("----------------【按学号升序排序】----------------\n");
            sortUp(list);
            break;
        case 2:
            printf("----------------【按学号降序排序】----------------\n");
            sortDrop(list);
            break;
        default:
            printf("选择错误,请重新输入......\n");
            system("pause");
            break;
    }
}
void keydown5(){

    int choice = 0;
    scanf("%d", &choice);
    switch (choice) {
        case 1:
            printf("----------------【统计各班人数】----------------\n");
            countClass(list);
            break;
        case 2:
            printf("----------------【统计各省份人数】----------------\n");
            countProvince(list);
            break;
        default:
            printf("选择错误,请重新输入......\n");
            system("pause");
            break;
    }
}
//录入
void insertNodeByHead(struct Node* headNode,struct student data)
{
    struct Node* newNode = createNode(data);
    newNode->next = headNode->next;
    headNode->next = newNode;
}
//浏览
void printList(struct Node* headNode){
    struct Node* pMove = headNode->next;
    printf("学号\t姓名\t班级\t地址\t电话\t\tQQ\n");
    while (pMove){
        printf("%d\t%s\t%d\t%s\t%s\t%s\n",pMove->data.num,pMove->data.name,pMove->data.class,pMove->data.addr,pMove->data.tel,pMove->data.QQ);
        pMove = pMove->next;
    }
    printf("\n");
}
//按学号查询
void searchInfoByNum(struct Node* headNode,int num){
    struct Node* pMove = headNode;
    while (pMove){
        if(pMove -> data.num == num){
            printf("学号\t姓名\t班级\t地址\t电话\t\tQQ\n");
            printf("%d\t%s\t%d\t%s\t%s\t%s\n", pMove->data.num, pMove->data.name, pMove->data.class, pMove->data.addr,
                   pMove->data.tel, pMove->data.QQ);
            break;
        }
        pMove = pMove -> next;
    }
    if(pMove == NULL){
        printf("学号不存在,无法查找!\n ");
        return ;
    }

}
//按名字查询
void searchInfoByName(struct Node* headNode,char *name){
    struct Node* pMove = headNode;
    while (pMove){
        if(strcmp(pMove -> data.name,name) == 0){
            printf("学号\t姓名\t班级\t地址\t电话\t\tQQ\n");
            printf("%d\t%s\t%d\t%s\t%s\t%s\n", pMove->data.num, pMove->data.name, pMove->data.class, pMove->data.addr,
                   pMove->data.tel, pMove->data.QQ);
            break;
        }
        pMove = pMove -> next;
    }
    if(pMove == NULL){
        printf("名字不存在,无法查找!\n ");
        return ;
    }
}
//按班级查询
void searchInfoByClass(struct Node* headNode,int class){
    struct Node* pMove = headNode;
    printf("学号\t姓名\t班级\t地址\t电话\t\tQQ\n");
    while (pMove){
        if(pMove -> data.class == class){
            printf("%d\t%s\t%d\t%s\t%s\t%s\n", pMove->data.num, pMove->data.name, pMove->data.class, pMove->data.addr,
                   pMove->data.tel, pMove->data.QQ);
        }
        pMove = pMove -> next;
    }

    if(pMove == NULL){
        printf("班级不存在,无法查找!\n ");
        return ;
    }
}
//按学号删除
void deleteNumNode(struct Node* headNode,int num){
    struct Node* posNode = headNode -> next;
    struct Node* posFrontNode = headNode;
    while(posNode -> data.num != num)
    {
        posFrontNode = posNode;
        posNode = posNode -> next;
    }
    if(posNode == NULL){
        printf("未找学号位置,无法删除!");
        return;
    }
    else{
        posFrontNode -> next = posNode -> next;
        free(posNode);
        printf("删除成功!");
    }

}
//按名字删除
void deleteNameNode(struct Node* headNode,char *name){
    struct Node* posNode = headNode -> next;
    struct Node* posFrontNode = headNode;
    if(posNode == NULL){
        printf("名字不存在,无法删除!\n ");
        return;
    }
    while(strcmp(posNode -> data.name,name))
    {
        posFrontNode = posNode;
        posNode = posNode -> next;
    }
    if(posNode == NULL){
        printf("未找名字位置,无法删除!");
        return;
    }
    else{
        posFrontNode -> next = posNode -> next;
        free(posNode);
        printf("删除成功!");
    }

}
//按学号修改
void alterNumNode(struct Node* headNode,int num){
    struct Node* posNode = headNode -> next;
    struct Node* posFrontNode = headNode;
    while(posNode -> data.num != num)
    {
        posFrontNode = posNode;
        posNode = posNode -> next;
    }
    if(posNode == NULL){
        printf("未找该学生,无法修改!");
        return;
    }
    else{
        struct student data;
        posFrontNode -> next = posNode -> next;
        free(posNode);
        printf("请重新输入学生的学号,姓名,班级,地址,电话,QQ:\n");
        fflush(stdin);
        scanf("%d %s %d %s %s %s",&data.num,data.name,&data.class,data.addr,data.tel,data.QQ);
        insertNodeByHead(list,data);
        printf("修改成功!");
    }
}
//按姓名修改
void alterNameNode(struct Node* headNode,char* name){
    struct Node* posNode = headNode -> next;
    struct Node* posFrontNode = headNode;
    while(strcmp(posNode -> data.name,name))
    {
        posFrontNode = posNode;
        posNode = posNode -> next;
    }
    if(posNode == NULL){
        printf("未找该学生,无法修改!");
        return;
    }
    else{
        struct student data;
        posFrontNode -> next = posNode -> next;
        free(posNode);
        printf("请重新输入学生的学号,姓名,班级,地址,电话,QQ:\n");
        fflush(stdin);
        scanf("%d %s %d %s %s %s",&data.num,data.name,&data.class,data.addr,data.tel,data.QQ);
        insertNodeByHead(list,data);
        printf("修改成功!");
    }
}
//按学号升序排序
void sortUp(struct Node* headNode){
    struct Node* turn;
    struct Node* move;
    struct student temp;
    for(turn = headNode -> next;turn -> next != NULL;turn = turn -> next){
        for(move = headNode -> next;move -> next != NULL;move = move -> next){
            if(move -> data.num > move -> next -> data.num){
                temp = move -> data;
                move -> data = move -> next -> data;
                move -> next -> data = temp;
            }
        }
    }
    printList(list);
}
//按学号降序排序
void sortDrop(struct Node* headNode){
    struct Node* turn;
    struct Node* move;
    struct student temp;
    for(turn = headNode -> next;turn -> next != NULL;turn = turn -> next){
        for(move = headNode -> next;move -> next != NULL;move = move -> next){
            if(move -> data.num < move -> next -> data.num){
                temp = move -> data;
                move -> data = move -> next -> data;
                move -> next -> data = temp;
            }
        }
    }
    printList(list);
}
//统计各班级人数
void countClass(struct Node* headNode){
    int i;
    int count[N + 1] = {0};
    struct Node* pMove = headNode;
    while (pMove){
        for( i = 1;i <= N; i++){
            if (pMove->data.class == i ) count[i]++;
        }
        pMove = pMove -> next;
    }
    for (int i = 1;i <= N; i++) {
        printf("%d班的人数为:%d\n",i,count[i]);
    }
}
//统计各省份人数
void countProvince(struct Node* headNode){
    char province[34][100] = {"Hebei","Shanxi","Liaoning","Jilin","Heilongjiang","Jiangsu","Zhejiang","Anhui","Fujian","Jiangxi","Shandong","Henan","Hubei","Hunan","Guangdong","Hannan","Sichuan","Guizhou","Yunnan","Shanxi","Gansu","Qinghai","Taiwan","Neimenggu","Guangxi","Xizang","Ningxia","Xinjiang","Beijiang","Tianjing","Shanghai","Chongqing","Xianggang","Aomen"};
    int i;
    int count[34] ={0};
    struct Node* pMove = headNode;
    while (pMove){
        for( i = 0;i < 34; i++){
            if (strcmp(pMove->data.addr,province[i]) == 0 ) count[i]++;
        }
        pMove = pMove -> next;
    }
    for (int i = 0;i < 34; i++) {
        if(count[i] != 0)
            printf("地址为%s的人数为%d\n",province[i],count[i]);
    }
}
//文件读操作
void readInFromFile(struct Node* headNode,char* filename){
    struct student data;
    FILE *fp;
    fp = fopen(filename,"r");
    if(fp == NULL){
        printf("文件打开失败!");
    }
    while (fscanf(fp,"%d\t%s\t%d\t%s\t%s\t%s\n",&data.num,data.name,&data.class,data.addr,data.tel,data.QQ) != EOF){
        insertNodeByHead(headNode,data);
    }

    fclose(fp);
}
//文件写操作
void writeInFromFile(struct Node* headNode,char* filename){
    FILE *fp = fopen(filename,"w");
    struct Node* pMove = headNode -> next;
    while(pMove){
        fprintf(fp,"%d\t%s\t%d\t%s\t%s\t%s\n",pMove->data.num,pMove->data.name,pMove->data.class,pMove->data.addr,pMove->data.tel,pMove->data.QQ);
        pMove = pMove -> next;
    }
    fclose(fp);
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
  • 409
  • 410
  • 411
  • 412
  • 413
  • 414
  • 415
  • 416
  • 417
  • 418
  • 419
  • 420
  • 421
  • 422
  • 423
  • 424
  • 425
  • 426
  • 427
  • 428
  • 429
  • 430
  • 431
  • 432
  • 433
  • 434
  • 435
  • 436
  • 437
  • 438
  • 439
  • 440
  • 441
  • 442
  • 443
  • 444
  • 445
  • 446
  • 447
  • 448
  • 449
  • 450
  • 451
  • 452
  • 453
  • 454
  • 455
  • 456
  • 457
  • 458
  • 459
  • 460
  • 461
  • 462
  • 463
  • 464
  • 465
  • 466
  • 467
  • 468
  • 469
  • 470
  • 471
  • 472
  • 473
  • 474
  • 475
  • 476
  • 477
  • 478
  • 479
  • 480
  • 481
  • 482
  • 483
  • 484
  • 485
  • 486
  • 487
  • 488
  • 489
  • 490
  • 491
  • 492
  • 493
  • 494
  • 495
  • 496
  • 497
  • 498
  • 499
  • 500
  • 501
  • 502
  • 503
  • 504
  • 505
  • 506
  • 507
  • 508
  • 509
  • 510
  • 511
  • 512
  • 513

好了,大概就是这样了,后期我还增加了登录和注册功能(比较简单的),需要的小伙伴可以找我要( ̄▽ ̄)",如果程序上有什么问题,希望大家可以帮忙指出,我会及时更正的(☆▽☆)。
视频链接在这,老师讲得很好,大家可以看看。C/C++项目教程:学生信息管理系统,大学里老师布置的作业你会做吗?基础不牢固的赶紧学习一下。

下面这个是我测试程序用到的数据,分享给大家啦,省得大家一个个敲(~o ̄3 ̄)~

10001 Lily 1 Beijing 13836763922 5467334567
10002 Sophia 1 Shanxi 13987654890 8894563208
10003 Olivia 1 Shanxi 19176490875 3456998711
10004 Amelia 1 Tianjin 13887490654 3746529087
10005 Freya 1 Fujian 13399878956 3647589022
10006 Aria 2 Nanjing 13399568956 3847590865
10007 Navbar 2 Guangxi 13890878956 3748598877
10008 Mila 2 Beijing 13399864956 3377287653
10009 Grace 2 Fujian 13397898956 3748595999
10010 Layla 2 Hebei 19909878956 3374727177
10011 Maryam 3 Beijing 13996328956 4758349993
10012 Willow 3 Hubei 13399878943 3894859020
10013 Elsie 3 Qinghai 13988956654 3848569652
10014 Bailey 3 Hebei 13399878546 5748399302
10015 Blew 3 Gansu 13399648956 3847539534
10016 Chukka 3 Nanjing 19176496780 5345329464
10017 Dock 4 Hebei 19176887875 3465364536
10018 String 4 Nanjing 19176490543 3246524645
10019 Fibula 4 Sichuan 19176490098 5432543465
10020 Grain 4 Sichaun 19176490567 3465236534
10021 High 4 Guizhou 19176490453 3456326455
10022 Janara 5 Taiwan 19176490875 6346456346
10023 Jannean 5 Nanjing 19176095875 3462456234
10024 Kayne 5 Beijing 19176497645 8437536754
10025 Lalaine 5 Shanxi 19176490634 6345734766
10026 Mikeia 5 Taiwan 19176478984 3475357349
10027 Mieshia 5 Sichuan 19176490556 4357347568
10028 Narali 6 Nanjing 19109890875 4373765826
10029 Enemy 6 Taiwan 19176498794 6427358754
10030 Conchie 6 Hebei 19176494444 3475684675

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/783308
推荐阅读
相关标签
  

闽ICP备14008679号