zoukankan      html  css  js  c++  java
  • C语言学生信息管理系统

    一、需求分析

    学生信息管理系统:

    学生信息管理有许多使用功能,使用非常普遍。

    需要有的功能是输入、输出、查找、排序、删除和修改等。

    用菜单实现,界面功能简洁明了,需要容错,考虑人性化的设计。

    二、总体设计

    学生包含许多属性,考虑使用结构体来存储。由于不确定学生人数,还有要对信息增加、修改、删除等,使用动态链表实现较为方便。

    菜单的设计根据用户的输入来执行不同的函数,有的函数中还要包括子菜单。

    总体选择用switch-case

    遇到的问题:学生信息管理系统最关键的就是录入信息,需要加容错

    解决:输入单独写一个函数

    遇到的问题:输入的学号不能重复

    解决:用一个数组放学号,如果已经存在就置1,未存在为0

    三、详细设计

    编写的函数清单

     函数调用关系图

    •  Student *add(Student *head);//录入信息

    该函数用来添加信息,里面调用了int input(Student *p)输入函数

    形参是链表的头指针,返回值是添加数据后的头指针

    Student* add(Student *head)
    {
        Student *p1;
        while((p1=(Student *)malloc(LEN))&&input(p1))
        {
    
            while(a[p1->num-190400])
            {
                system("cls");
                print(head);
                printf("输入的学号%ld重复,请重新输入!
    ",p1->num);
                input(p1);
            }
            a[p1->num-190400]=1;
            n++;
            if(head==NULL)
            {
                head=p1;
                p1->next=NULL;
            }
            else
            {
                p1->next=head;
                head=p1;
            }
            system("cls");
        }
        system("cls");
        free(p1);
        return head;
    }
    View Code
    • void search(Student *head);//查找信息

    形参是链表的头指针,返回值是void

    查找信息采用两种方式:按学号查找和按姓名查找

    调用查找菜单函数void search_menu();

    void search(Student *head)
    {
        Student *p1=head;
        int choice=1;
        long num;
        char s[20];
        while(choice)
        {
                search_menu();
                printf("请输入选择:");
                scanf("%d",&choice);
                if(choice==0)
                {
                    system("cls");
                    break;
                }
                else if(choice==1)
                {
                    system("cls");
                    p1=head;//如果不初始化,第二次查找会出错
                    printf("请输入学号查找 :");
                    scanf("%ld",&num);
                    while(p1->num!=num&&p1->next!=NULL)
                    {
                        p1=p1->next;
                    }
                    if(p1->num==num)
                    {
                        printf("找到了!
    ");
                        printf("学号      姓名    年龄   性别
    ");
                        printf("%4ld %6s %7d %5c
    ",p1->num,p1->name,p1->age,p1->sex);
                    }
                    else
                    {
                        system("cls");
                        print(head);
                        printf("学号:%ld不存在!
    ",num);
                    }
    
                }
                else if(choice==2)
                {
                    system("cls");
                    p1=head;
                    printf("请输入姓名查找 :");
                    scanf("%s",s);
                    while(strcmp(p1->name,s)!=0&&p1->next!=NULL)
                    {
                        p1=p1->next;
                    }
                    if(strcmp(p1->name,s)==0)
                    {
                        printf("找到了!
    ");
                        printf("学号      姓名    年龄   性别
    ");
                        printf("%4ld %6s %7d %5c
    ",p1->num,p1->name,p1->age,p1->sex);
                    }
                    else
                    {
                        system("cls");
                        print(head);
                        printf("学生:%s不存在!
    ",s);
                    }
    
                }
                else
                {
                    system("cls");
                    printf("请根据菜单重新输入!
    ");
                }
        }
    }
    View Code
    • Student *del(Student *head);//删除信息

    删除一条信息,用到链表的删除操作

    形参是链表的头指针,返回值是删除数据后的头指针

    删除方式有两种:输入学号或姓名

    调用删除菜单函数:void del_menu();

    Student *del(Student *head)
    {
        Student *p1=head,*p2;
        int choice=1;
        long num;
        char s[20];
        while(choice)
        {
            del_menu();
            printf("请输入选择:");
            scanf("%d",&choice);
            if(choice==0)
            {
                system("cls");
                break;
            }
            else if(choice==1)
            {
                system("cls");
                if(head==NULL)
                {
                    printf("没有任何信息!
    ");
                    return head;
                }
                p1=head;
                printf("请输入要删除的学号:");
                scanf("%ld",&num);
                while(p1->num!=num&&p1->next!=NULL)
                {
                    p2=p1;
                    p1=p1->next;
                }
                if(p1->num==num)//找到了要删除的数字
                {
                    if(p1==head)//删除头结点
                        head=p1->next;
                    else//删除中间或尾结点
                    {
                        p2->next=p1->next;
                    }
                    a[p1->num-190400]=0;
                    p1=p1->next;
                    printf("删除成功!
    ");
                    n--;
                }
                else
                {
                    printf("没找到要删除的学号%ld!请重新输入
    ",num);
                }
            }
            else if(choice==2)
            {
                system("cls");
                if(head==NULL)
                {
                    printf("没有任何信息!
    ");
                    return head;
                }
                p1=head;
                printf("请输入要删除的姓名:");
                scanf("%s",s);
                while(strcmp(p1->name,s)!=0&&p1->next!=NULL)
                {
                    p2=p1;
                    p1=p1->next;
                }
                if(strcmp(p1->name,s)==0)//找到了要删除的数字
                {
                    if(p1==head)//删除头结点
                        head=p1->next;
                    else//删除中间或尾结点
                    {
                        p2->next=p1->next;
                    }
                    a[p1->num-190400]=0;
                    p1=p1->next;
                    printf("删除成功!
    ");
                    n--;
                }
                else
                {
                    printf("没找到要删除的姓名%s!请重新输入
    ",s);
                }
            }
            else
            {
                system("cls");
                printf("请根据菜单重新输入!
    ");
            }
        }
        return head;
    }
    View Code
    • Student *modify(Student *head);//修改信息

    形参是链表的头指针,返回值是修改数据后的头指针

    先输入要修改的学号或姓名,找到之后再修改信息

    调用修改菜单函数:void modify_menu();

    Student *modify(Student *head)
    {
        Student *p1=head;
        int choice,choice1;
        long num;
        char s[20];
        int age;
        char sex;
        if(head==NULL)
        {
            printf("没有任何信息!
    ");
            return head;
        }
        while(1)
        {
            printf("┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
    ");
            printf("┃____________0.退出______________________________┃
    ");
            printf("┃____________1.输入要修改的学生的学号____________┃
    ");
            printf("┃____________2.输入要修改的学生的姓名____________┃
    ");
            printf("┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
    ");
            printf("请输入选择:");
            scanf("%d",&choice1);
            if(choice1==0)
            {
                system("cls");
                return head;
            }
            else if(choice1==1)
            {
                printf("要修改的学生的学号:");
                scanf("%ld",&num);
                p1=head;
                while(p1->num!=num&&p1->next!=NULL)
                {
                    p1=p1->next;
                }
                if(p1->num==num)
                {
                    printf("当前信息:
    ");
                    printf("学号      姓名    年龄   性别
    ");
                    printf("%4ld %6s %7d %5c
    ",p1->num,p1->name,p1->age,p1->sex);
                    while(1)
                    {
                        modify_menu();
                        printf("请输入选择:");
                        scanf("%d",&choice);
                        getchar();
                        if(choice==0)
                        {
                            system("cls");
                            break;
                        }
                        switch(choice)
                        {
                            case 1:
                                system("cls");
                                a[num-190400]=0;//修改这个学号要置零
                                printf("请输入修改后的学号:");
                                scanf("%ld",&num);
                                while(a[num-190400])
                                {
                                    printf("输入的学号%ld已经存在,请重新输入 !
    ",num);
                                    printf("请输入修改后的学号:");
                                    scanf("%ld",&num);
                                }
                                a[num-190400]=1;//修改后要置1
                                p1->num=num;
                                printf("修改成功!
    ");
                                printf("学号      姓名    年龄   性别
    ");
                                printf("%4ld %6s %7d %5c
    ",p1->num,p1->name,p1->age,p1->sex);
                                break;
                            case 2:
                                system("cls");
                                printf("请输入修改后的姓名:");
                                gets(s);
                                //getchar();
                                strcpy(p1->name,s);
                                printf("修改成功!
    ");
                                printf("学号      姓名    年龄   性别
    ");
                                printf("%4ld %6s %7d %5c
    ",p1->num,p1->name,p1->age,p1->sex);
                                break;
                            case 3:
                                system("cls");
                                printf("请输入修改后的年龄:");
                                scanf("%ld",&age);
                                p1->age=age;
                                printf("修改成功!
    ");
                                printf("学号      姓名    年龄   性别
    ");
                                printf("%4ld %6s %7d %5c
    ",p1->num,p1->name,p1->age,p1->sex);
                                break;
                            case 4:
                                system("cls");
                                printf("请输入修改后的性别:");
                                scanf("%c",&sex);
                                p1->sex=sex;
                                printf("修改成功!
    ");
                                printf("学号      姓名    年龄   性别
    ");
                                printf("%4ld %6s %7d %5c
    ",p1->num,p1->name,p1->age,p1->sex);
                                break;
                            default:
                                system("cls");
                                printf("请重新输入!
    ");
                        }
                    }
                }
                else
                {
                    system("cls");
                    print(head);
                    printf("没有学号%ld,请重新输入!
    ",num);
                }
            }
            else if(choice1==2)
            {
                printf("要修改的学生的姓名:");
                scanf("%s",s);
                p1=head;
                while(strcmp(p1->name,s)!=0&&p1->next!=NULL)
                {
                    p1=p1->next;
                }
                if(strcmp(p1->name,s)==0)
                {
                    printf("当前信息:
    ");
                    printf("学号      姓名    年龄   性别
    ");
                    printf("%4ld %6s %7d %5c
    ",p1->num,p1->name,p1->age,p1->sex);
                    while(1)
                    {
                        modify_menu();
                        printf("请输入选择:");
                        scanf("%d",&choice);
                        getchar();
                        if(choice==0)
                        {
                            system("cls");
                            break;
                        }
                        switch(choice)
                        {
                            case 1:
                                system("cls");
                                a[num-190400]=0;//修改这个学号要置零
                                printf("请输入修改后的学号:");
                                scanf("%ld",&num);
                                while(a[num-190400])
                                {
                                    printf("输入的学号%ld已经存在,请重新输入 !
    ",num);
                                    printf("请输入修改后的学号:");
                                    scanf("%ld",&num);
                                }
                                a[num-190400]=1;//修改后要置1
                                p1->num=num;
                                printf("修改成功!
    ");
                                printf("学号      姓名    年龄   性别
    ");
                                printf("%4ld %6s %7d %5c
    ",p1->num,p1->name,p1->age,p1->sex);
                                break;
                            case 2:
                                system("cls");
                                printf("请输入修改后的姓名:");
                                gets(s);
                                //getchar();
                                strcpy(p1->name,s);
                                printf("修改成功!
    ");
                                printf("学号      姓名    年龄   性别
    ");
                                printf("%4ld %6s %7d %5c
    ",p1->num,p1->name,p1->age,p1->sex);
                                break;
                            case 3:
                                system("cls");
                                printf("请输入修改后的年龄:");
                                scanf("%ld",&age);
                                p1->age=age;
                                printf("修改成功!
    ");
                                printf("学号      姓名    年龄   性别
    ");
                                printf("%4ld %6s %7d %5c
    ",p1->num,p1->name,p1->age,p1->sex);
                                break;
                            case 4:
                                system("cls");
                                printf("请输入修改后的性别:");
                                scanf("%c",&sex);
                                p1->sex=sex;
                                printf("修改成功!
    ");
                                printf("学号      姓名    年龄   性别
    ");
                                printf("%4ld %6s %7d %5c
    ",p1->num,p1->name,p1->age,p1->sex);
                                break;
                            default:
                                system("cls");
                                printf("请重新输入!
    ");
                        }
                    }
                }
                else
                {
                    system("cls");
                    print(head);
                    printf("没有学生:%s,请重新输入!
    ",s);
                }
            }
            else
            {
                printf("请根据菜单重新输入!
    ");
            }
        }
        return head;
    }
    View Code
    •  Student *bubblesort(Student *head);//信息排序

    将学生信息按学号进行升序排序,暂时用的是冒泡排序,交换链表结点的值

    形参是链表的头指针,返回值是排序后的头指针

    调用了void swap(int *a,int *b);交换两个结点的值、void sort_menu();排序菜单、void print(Student *head);打印函数

    Student* bubblesort(Student* head)
    {
        int choice=1;
        while(choice)
        {
            sort_menu();
            printf("请输入选择:");
            scanf("%d",&choice);
            if(choice==0)
            {
                system("cls");
                break;
            }
            else if(choice==1)
            {
                if(head==NULL&&head->next==NULL)
                    return head;
                Student* p=NULL;
                int flag=1;
                while(p!=head->next&&flag)
                {
                    Student* q=head;
                    flag=0;
                    for(;q->next&&q->next!=p;q=q->next)
                    {
                        if(q->num>q->next->num)
                        {
                            swap(&q->num,&q->next->num);
                            flag=1;
                        }
                    }
                    p=q;
                }
                printf("排序后:
    ");
                print(head);
            }
            else
            {
                system("cls");
                printf("请根据菜单重新输入!
    ");
            }
        }
        return head;
    }
    View Code
    • int input(Student *p);//因为要输入的有很多,写一个输入函数,容错包含在这个函数里
    int input(Student *p)
    {
        p->num=190400;
        printf("请输入学生信息(以输入学号0为终止):
    ");
        printf("注:学号六位(190400-190449)、年龄0-30、性别(f\m)
    ");
        printf("学号:");
        scanf("%ld",&p->num);
        if(p->num==0)
            return 0;
        while(p->num<190400||p->num>190449)
        {
            printf("学号为6位(190400-190449),请重新输入!
    ");
            scanf("%ld",&p->num);
        }
        getchar();
        printf("姓名:");
        gets(p->name);
        printf("年龄:");
        scanf("%d",&p->age);
        while(p->age<0||p->age>30)
        {
            printf("年龄为0-30,请重新输入!
    ");
            scanf("%d",&p->age);
        }
        getchar();
        printf("性别:");
        scanf("%c",&p->sex);
        while(p->sex!='f'&&p->sex!='m')
        {
            getchar();
            printf("性别输入为f\m,请重新输入!
    ");
            scanf("%c",&p->sex);
        }
        return p->num;
    }
    View Code
    • void swap(int *a,int *b);//排序中用
    void swap(int *a,int *b)
    {
        int t;
        t=*a;
        *a=*b;
        *b=t;
    }
    View Code
    • void print(Student *head);//打印信息

    传入链表的头节点,将整个链表的信息打印

    •  void save(Student *head);//保存
    void save(Student *head)
    {
        Student *p=head;
        FILE *fp;
        if((fp=fopen("stud.dat","a"))==NULL)
        {
            printf("打开文件失败!
    ");
            exit(0);
        }
        for(int i=0;i<n;i++)
        {
            fwrite(p,LEN,1,fp);
            p=p->next;
        }
        fclose(fp);
        printf("保存成功!
    ");
    }
    View Code

    采用文件的处理方式

    • void menu();//主菜单

    为了让使用界面更加可观,采用了一点美化方式

    void menu()
    {
        printf("*********************菜单*************************
    ");
        printf("┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
    ");
        printf("┃____________0.退出______________________________┃
    ");
        printf("┃____________1.录入学生信息______________________┃
    ");
        printf("┃____________2.保存学生信息______________________┃
    ");
        printf("┃____________3.浏览学生信息______________________┃
    ");
        printf("┃____________4.查询学生信息______________________┃
    ");
        printf("┃____________5.修改学生信息______________________┃
    ");
        printf("┃____________6.删除学生信息______________________┃
    ");
        printf("┃____________7.将学生信息排序____________________┃
    ");
        printf("┃            8.帮助                              ┃
    ");
        printf("┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
    ");
    }
    View Code
    • 各种子菜单

    void search_menu();
    void del_menu();
    void modify_menu();
    void help_menu();
    void sort_menu();

    void search_menu()
    {
        printf("┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
    ");
        printf("┃____________0.退出______________________________┃
    ");
        printf("┃____________1.输入学号搜索______________________┃
    ");
        printf("┃____________2.输入姓名搜索______________________┃
    ");
        printf("┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
    ");
    }
    void del_menu()
    {
        printf("┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
    ");
        printf("┃____________0.退出______________________________┃
    ");
        printf("┃____________1.输入学号删除______________________┃
    ");
        printf("┃____________2.输入姓名删除______________________┃
    ");
        printf("┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
    ");
    }
    void modify_menu()
    {
        printf("┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
    ");
        printf("┃____________0.退出______________________________┃
    ");
        printf("┃____________1.修改学号__________________________┃
    ");
        printf("┃____________2.修改姓名__________________________┃
    ");
        printf("┃____________3.修改年龄__________________________┃
    ");
        printf("┃____________4.修改性别__________________________┃
    ");
        printf("┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
    ");
    
    }
    void help_menu()
    {
        printf("┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
    ");
        printf("┃____________友情提示____________________________┃
    ");
        printf("┃____________按照提示输入________________________┃
    ");
        printf("┃____________每输入一个信息按回车________________┃
    ");
        printf("┃____________学号范围[190400,190449]_____________┃
    ");
        printf("┃____________年龄范围[0,30]______________________┃
    ");
        printf("┃____________性别输入f/m_________________________┃
    ");
        printf("┃____________学号输入不能重复____________________┃
    ");
        printf("┃____________不要恶意输入________________________┃
    ");
        printf("┃            每结束一个操作就要保存              ┃
    ");
        printf("┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
    ");
    }
    void sort_menu()
    {
         printf("┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
    ");
         printf("┃____________0.退出______________________________┃
    ");
         printf("┃____________1.根据学号升序排序__________________┃
    ");
         printf("┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
    ");
    }
    View Code

    四、程序运行结果测试与分析

    运行过程中执行不同的函数会到不同的界面,也就是清屏

    • 开始界面

    •  录入学生信息

    输入0就终止输入

    输入的学号重复就重新输入

    •  浏览学生信息

    •  查询学生信息

     

     

    •  修改学生信息

     当输入的学号不存在

     

     修改学号时,如果重复了,就提示重新输入

    •  删除学生信息

    •  将学生信息排序

    五、结论与心得

    第一次自己一个人做完一个简陋的学生管理系统,学到了很多(还是太菜了)。

    首先就是不要害怕,看到一个大作业不要恐惧,多好的锻炼机会啊

    先自己想大体思路和方法,不要一开始就去参考别人的,后期可以看看别人有什么好的方法,并且去改进。

    做一个大的管理系统,需要的函数很多,要采用模块化设计,什么功能都要单独写一个函数,修改调试的时候会非常方便。

    如果想要做的人性化,方便用户使用,要考虑的全面,比如清屏又不能清空很多次,及时打印信息等,可以换位思考,从用户的角度出发。

    六、不足和改进之处

    •  首先就是调试版本学生的信息只有基础的几个,很少好调试,最后一定要补充完整。
    • 输入的容错还有很多缺漏,比如多输入了,或者按错了,有可能会进入死循环,需要想更好的解决办法
    •  写入文件的地方还没有考虑很多
    • 为了使学号输入不重复,开了一个数组,把学号范围定死了,学生数也是有限的,需要再改进
    • 对学生信息排序用的是冒泡排序,效率不高,可以改成快排。
    • 修改、删除、查找这几个函数中的代码有很多重复的部分,简单的复制粘贴太Low了,可以将重复很多次的部分再写成一个函数

    完整代码(有错误请指出)

    #include <stdio.h>
    #include <stdlib.h>
    #include<malloc.h>
    #include<string.h>
    #include<windows.h>//清屏功能
    #define LEN sizeof(struct Student)
    int n=0;
    int a[50]={0};//暂定50个学生,存放学号,可以修改,为了不重复
    typedef  struct Student{//参数还能增加很多
        long num;
        char name[20];
        int age;
        char sex;
        struct Student* next;
    };
    
    typedef struct Birthday{
        int year;
        int month;
        int day;
    };
    typedef  struct Student Student;
    Student *add(Student *head);//录入信息
    void search(Student *head);//查找信息
    Student *del(Student *head);//删除信息
    Student *modify(Student *head);//修改信息
    Student *bubblesort(Student *head);//信息排序,
    void swap(int *a,int *b);//排序中用
    
    void print(Student *head);//打印信息
    void save(Student *head);//保存
    
    void menu();//菜单
    void search_menu();
    void del_menu();
    void modify_menu();
    void help_menu();
    void sort_menu();
    
    int input(Student *p);//因为要输入的有很多,写一个输入函数,容错包含在这个函数里
    
    
    int main()
    {
        int choice;
        Student* head=NULL;
        printf("恭喜您被选入学生管理系统的内测!
    ");
        printf("想进入测评吗?1-愿意 0-退出
    ");
        scanf("%d",&choice);
        if(choice==0)
            exit(0);
        system("cls");
        printf("欢迎使用学生管理系统!
    ");
        printf("友情提示:请先看帮助菜单!
    ");
        while(1)
        {
            menu();
            printf("请输入选择:");
            scanf("%d",&choice);
            switch(choice)
            {
                case 0:
                    system("cls");
                    printf("感谢使用学生管理系统!记得给个好评yo~
    ");
                    printf("——————按任意键退出——————");
                    exit(0);
                case 1:
                    system("cls");
                    printf("——————————————输入学生信息————————————————
    ");
                    head=add(head);
                    break;
                case 2:
                    save(head);
                    break;
                case 3:
                    system("cls");
                    printf("——————————————浏览学生信息————————————————
    ");
                    print(head);
                    break;
                case 4:
                    system("cls");
                    printf("——————————————查找学生信息————————————————
    ");
                    search(head);
                    break;
                case 5:
                    system("cls");
                    printf("——————————————修改学生信息————————————————
    ");
                    head=modify(head);
                    break;
                case 6:
                    system("cls");
                    printf("——————————————删除学生信息————————————————
    ");
                    head=del(head);
                    break;
                case 7:
                    system("cls");
                    printf("——————————————将学生信息排序——————————————
    ");
                    head=bubblesort(head);
                    break;
                case 8:
                    system("cls");
                    help_menu();
                    break;
                default:
                    system("cls");
                    printf("请根据菜单重新输入!
    ");
            }
        }
    
        return 0;
    }
    
    Student* add(Student *head)
    {
        Student *p1;
        while((p1=(Student *)malloc(LEN))&&input(p1))
        {
    
            while(a[p1->num-190400])
            {
                system("cls");
                print(head);
                printf("输入的学号%ld重复,请重新输入!
    ",p1->num);
                input(p1);
            }
            a[p1->num-190400]=1;
            n++;
            if(head==NULL)
            {
                head=p1;
                p1->next=NULL;
            }
            else
            {
                p1->next=head;
                head=p1;
            }
            system("cls");
        }
        system("cls");
        free(p1);
        return head;
    }
    
    Student *del(Student *head)
    {
        Student *p1=head,*p2;
        int choice=1;
        long num;
        char s[20];
        while(choice)
        {
            del_menu();
            printf("请输入选择:");
            scanf("%d",&choice);
            if(choice==0)
            {
                system("cls");
                break;
            }
            else if(choice==1)
            {
                system("cls");
                if(head==NULL)
                {
                    printf("没有任何信息!
    ");
                    return head;
                }
                p1=head;
                printf("请输入要删除的学号:");
                scanf("%ld",&num);
                while(p1->num!=num&&p1->next!=NULL)
                {
                    p2=p1;
                    p1=p1->next;
                }
                if(p1->num==num)//找到了要删除的数字
                {
                    if(p1==head)//删除头结点
                        head=p1->next;
                    else//删除中间或尾结点
                    {
                        p2->next=p1->next;
                    }
                    a[p1->num-190400]=0;
                    p1=p1->next;
                    printf("删除成功!
    ");
                    n--;
                }
                else
                {
                    printf("没找到要删除的学号%ld!请重新输入
    ",num);
                }
            }
            else if(choice==2)
            {
                system("cls");
                if(head==NULL)
                {
                    printf("没有任何信息!
    ");
                    return head;
                }
                p1=head;
                printf("请输入要删除的姓名:");
                scanf("%s",s);
                while(strcmp(p1->name,s)!=0&&p1->next!=NULL)
                {
                    p2=p1;
                    p1=p1->next;
                }
                if(strcmp(p1->name,s)==0)//找到了要删除的数字
                {
                    if(p1==head)//删除头结点
                        head=p1->next;
                    else//删除中间或尾结点
                    {
                        p2->next=p1->next;
                    }
                    a[p1->num-190400]=0;
                    p1=p1->next;
                    printf("删除成功!
    ");
                    n--;
                }
                else
                {
                    printf("没找到要删除的姓名%s!请重新输入
    ",s);
                }
            }
            else
            {
                system("cls");
                printf("请根据菜单重新输入!
    ");
            }
        }
        return head;
    }
    
    Student *modify(Student *head)
    {
        Student *p1=head;
        int choice,choice1;
        long num;
        char s[20];
        int age;
        char sex;
        if(head==NULL)
        {
            printf("没有任何信息!
    ");
            return head;
        }
        while(1)
        {
            printf("┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
    ");
            printf("┃____________0.退出______________________________┃
    ");
            printf("┃____________1.输入要修改的学生的学号____________┃
    ");
            printf("┃____________2.输入要修改的学生的姓名____________┃
    ");
            printf("┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
    ");
            printf("请输入选择:");
            scanf("%d",&choice1);
            if(choice1==0)
            {
                system("cls");
                return head;
            }
            else if(choice1==1)
            {
                printf("要修改的学生的学号:");
                scanf("%ld",&num);
                p1=head;
                while(p1->num!=num&&p1->next!=NULL)
                {
                    p1=p1->next;
                }
                if(p1->num==num)
                {
                    printf("当前信息:
    ");
                    printf("学号      姓名    年龄   性别
    ");
                    printf("%4ld %6s %7d %5c
    ",p1->num,p1->name,p1->age,p1->sex);
                    while(1)
                    {
                        modify_menu();
                        printf("请输入选择:");
                        scanf("%d",&choice);
                        getchar();
                        if(choice==0)
                        {
                            system("cls");
                            break;
                        }
                        switch(choice)
                        {
                            case 1:
                                system("cls");
                                a[num-190400]=0;//修改这个学号要置零
                                printf("请输入修改后的学号:");
                                scanf("%ld",&num);
                                while(a[num-190400])
                                {
                                    printf("输入的学号%ld已经存在,请重新输入 !
    ",num);
                                    printf("请输入修改后的学号:");
                                    scanf("%ld",&num);
                                }
                                a[num-190400]=1;//修改后要置1
                                p1->num=num;
                                printf("修改成功!
    ");
                                printf("学号      姓名    年龄   性别
    ");
                                printf("%4ld %6s %7d %5c
    ",p1->num,p1->name,p1->age,p1->sex);
                                break;
                            case 2:
                                system("cls");
                                printf("请输入修改后的姓名:");
                                gets(s);
                                //getchar();
                                strcpy(p1->name,s);
                                printf("修改成功!
    ");
                                printf("学号      姓名    年龄   性别
    ");
                                printf("%4ld %6s %7d %5c
    ",p1->num,p1->name,p1->age,p1->sex);
                                break;
                            case 3:
                                system("cls");
                                printf("请输入修改后的年龄:");
                                scanf("%ld",&age);
                                p1->age=age;
                                printf("修改成功!
    ");
                                printf("学号      姓名    年龄   性别
    ");
                                printf("%4ld %6s %7d %5c
    ",p1->num,p1->name,p1->age,p1->sex);
                                break;
                            case 4:
                                system("cls");
                                printf("请输入修改后的性别:");
                                scanf("%c",&sex);
                                p1->sex=sex;
                                printf("修改成功!
    ");
                                printf("学号      姓名    年龄   性别
    ");
                                printf("%4ld %6s %7d %5c
    ",p1->num,p1->name,p1->age,p1->sex);
                                break;
                            default:
                                system("cls");
                                printf("请重新输入!
    ");
                        }
                    }
                }
                else
                {
                    system("cls");
                    print(head);
                    printf("没有学号%ld,请重新输入!
    ",num);
                }
            }
            else if(choice1==2)
            {
                printf("要修改的学生的姓名:");
                scanf("%s",s);
                p1=head;
                while(strcmp(p1->name,s)!=0&&p1->next!=NULL)
                {
                    p1=p1->next;
                }
                if(strcmp(p1->name,s)==0)
                {
                    printf("当前信息:
    ");
                    printf("学号      姓名    年龄   性别
    ");
                    printf("%4ld %6s %7d %5c
    ",p1->num,p1->name,p1->age,p1->sex);
                    while(1)
                    {
                        modify_menu();
                        printf("请输入选择:");
                        scanf("%d",&choice);
                        getchar();
                        if(choice==0)
                        {
                            system("cls");
                            break;
                        }
                        switch(choice)
                        {
                            case 1:
                                system("cls");
                                a[num-190400]=0;//修改这个学号要置零
                                printf("请输入修改后的学号:");
                                scanf("%ld",&num);
                                while(a[num-190400])
                                {
                                    printf("输入的学号%ld已经存在,请重新输入 !
    ",num);
                                    printf("请输入修改后的学号:");
                                    scanf("%ld",&num);
                                }
                                a[num-190400]=1;//修改后要置1
                                p1->num=num;
                                printf("修改成功!
    ");
                                printf("学号      姓名    年龄   性别
    ");
                                printf("%4ld %6s %7d %5c
    ",p1->num,p1->name,p1->age,p1->sex);
                                break;
                            case 2:
                                system("cls");
                                printf("请输入修改后的姓名:");
                                gets(s);
                                //getchar();
                                strcpy(p1->name,s);
                                printf("修改成功!
    ");
                                printf("学号      姓名    年龄   性别
    ");
                                printf("%4ld %6s %7d %5c
    ",p1->num,p1->name,p1->age,p1->sex);
                                break;
                            case 3:
                                system("cls");
                                printf("请输入修改后的年龄:");
                                scanf("%ld",&age);
                                p1->age=age;
                                printf("修改成功!
    ");
                                printf("学号      姓名    年龄   性别
    ");
                                printf("%4ld %6s %7d %5c
    ",p1->num,p1->name,p1->age,p1->sex);
                                break;
                            case 4:
                                system("cls");
                                printf("请输入修改后的性别:");
                                scanf("%c",&sex);
                                p1->sex=sex;
                                printf("修改成功!
    ");
                                printf("学号      姓名    年龄   性别
    ");
                                printf("%4ld %6s %7d %5c
    ",p1->num,p1->name,p1->age,p1->sex);
                                break;
                            default:
                                system("cls");
                                printf("请重新输入!
    ");
                        }
                    }
                }
                else
                {
                    system("cls");
                    print(head);
                    printf("没有学生:%s,请重新输入!
    ",s);
                }
            }
            else
            {
                printf("请根据菜单重新输入!
    ");
            }
        }
        return head;
    }
    
    void print(Student* head)//打印学生信息
    {
        Student* p=head;
        if(head!=NULL)
        {
            printf("当前有%d条学生信息:
    ",n);
            printf("学号      姓名    年龄   性别
    ");
            do
            {
                printf("%4ld %6s %7d %5c
    ",p->num,p->name,p->age,p->sex);
                p=p->next;
            }while(p!=NULL);
        }
        else
            printf("没有任何信息!
    ");
    }
    
    void search(Student *head)
    {
        Student *p1=head;
        int choice=1;
        long num;
        char s[20];
        while(choice)
        {
                search_menu();
                printf("请输入选择:");
                scanf("%d",&choice);
                if(choice==0)
                {
                    system("cls");
                    break;
                }
                else if(choice==1)
                {
                    system("cls");
                    p1=head;//如果不初始化,第二次查找会出错
                    printf("请输入学号查找 :");
                    scanf("%ld",&num);
                    while(p1->num!=num&&p1->next!=NULL)
                    {
                        p1=p1->next;
                    }
                    if(p1->num==num)
                    {
                        printf("找到了!
    ");
                        printf("学号      姓名    年龄   性别
    ");
                        printf("%4ld %6s %7d %5c
    ",p1->num,p1->name,p1->age,p1->sex);
                    }
                    else
                    {
                        system("cls");
                        print(head);
                        printf("学号:%ld不存在!
    ",num);
                    }
    
                }
                else if(choice==2)
                {
                    system("cls");
                    p1=head;
                    printf("请输入姓名查找 :");
                    scanf("%s",s);
                    while(strcmp(p1->name,s)!=0&&p1->next!=NULL)
                    {
                        p1=p1->next;
                    }
                    if(strcmp(p1->name,s)==0)
                    {
                        printf("找到了!
    ");
                        printf("学号      姓名    年龄   性别
    ");
                        printf("%4ld %6s %7d %5c
    ",p1->num,p1->name,p1->age,p1->sex);
                    }
                    else
                    {
                        system("cls");
                        print(head);
                        printf("学生:%s不存在!
    ",s);
                    }
    
                }
                else
                {
                    system("cls");
                    printf("请根据菜单重新输入!
    ");
                }
        }
    }
    
    void menu()
    {
        printf("*********************菜单*************************
    ");
        printf("┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
    ");
        printf("┃____________0.退出______________________________┃
    ");
        printf("┃____________1.录入学生信息______________________┃
    ");
        printf("┃____________2.保存学生信息______________________┃
    ");
        printf("┃____________3.浏览学生信息______________________┃
    ");
        printf("┃____________4.查询学生信息______________________┃
    ");
        printf("┃____________5.修改学生信息______________________┃
    ");
        printf("┃____________6.删除学生信息______________________┃
    ");
        printf("┃____________7.将学生信息排序____________________┃
    ");
        printf("┃            8.帮助                              ┃
    ");
        printf("┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
    ");
    }
    
    void search_menu()
    {
        printf("┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
    ");
        printf("┃____________0.退出______________________________┃
    ");
        printf("┃____________1.输入学号搜索______________________┃
    ");
        printf("┃____________2.输入姓名搜索______________________┃
    ");
        printf("┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
    ");
    }
    
    void del_menu()
    {
        printf("┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
    ");
        printf("┃____________0.退出______________________________┃
    ");
        printf("┃____________1.输入学号删除______________________┃
    ");
        printf("┃____________2.输入姓名删除______________________┃
    ");
        printf("┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
    ");
    }
    
    void modify_menu()
    {
        printf("┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
    ");
        printf("┃____________0.退出______________________________┃
    ");
        printf("┃____________1.修改学号__________________________┃
    ");
        printf("┃____________2.修改姓名__________________________┃
    ");
        printf("┃____________3.修改年龄__________________________┃
    ");
        printf("┃____________4.修改性别__________________________┃
    ");
        printf("┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
    ");
    
    }
    
    void save(Student *head)
    {
        Student *p=head;
        FILE *fp;
        if((fp=fopen("stud.dat","a"))==NULL)
        {
            printf("打开文件失败!
    ");
            exit(0);
        }
        for(int i=0;i<n;i++)
        {
            fwrite(p,LEN,1,fp);
            p=p->next;
        }
        fclose(fp);
        printf("保存成功!
    ");
    }
    
    
    int input(Student *p)
    {
        p->num=190400;
        printf("请输入学生信息(以输入学号0为终止):
    ");
        printf("注:学号六位(190400-190449)、年龄0-30、性别(f\m)
    ");
        printf("学号:");
        scanf("%ld",&p->num);
        if(p->num==0)
            return 0;
        while(p->num<190400||p->num>190449)
        {
            printf("学号为6位(190400-190449),请重新输入!
    ");
            scanf("%ld",&p->num);
        }
        getchar();
        printf("姓名:");
        gets(p->name);
        printf("年龄:");
        scanf("%d",&p->age);
        while(p->age<0||p->age>30)
        {
            printf("年龄为0-30,请重新输入!
    ");
            scanf("%d",&p->age);
        }
        getchar();
        printf("性别:");
        scanf("%c",&p->sex);
        while(p->sex!='f'&&p->sex!='m')
        {
            getchar();
            printf("性别输入为f\m,请重新输入!
    ");
            scanf("%c",&p->sex);
        }
        return p->num;
    }
    
    void sort_menu()
    {
         printf("┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
    ");
         printf("┃____________0.退出______________________________┃
    ");
         printf("┃____________1.根据学号升序排序__________________┃
    ");
         printf("┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
    ");
    }
    
    void help_menu()
    {
        printf("┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
    ");
        printf("┃____________友情提示____________________________┃
    ");
        printf("┃____________按照提示输入________________________┃
    ");
        printf("┃____________每输入一个信息按回车________________┃
    ");
        printf("┃____________学号范围[190400,190449]_____________┃
    ");
        printf("┃____________年龄范围[0,30]______________________┃
    ");
        printf("┃____________性别输入f/m_________________________┃
    ");
        printf("┃____________学号输入不能重复____________________┃
    ");
        printf("┃____________不要恶意输入________________________┃
    ");
        printf("┃            每结束一个操作就要保存              ┃
    ");
        printf("┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
    ");
    }
    
    void swap(int *a,int *b)
    {
        int t;
        t=*a;
        *a=*b;
        *b=t;
    }
    
    Student* bubblesort(Student* head)
    {
        int choice=1;
        while(choice)
        {
            sort_menu();
            printf("请输入选择:");
            scanf("%d",&choice);
            if(choice==0)
            {
                system("cls");
                break;
            }
            else if(choice==1)
            {
                if(head==NULL&&head->next==NULL)
                    return head;
                Student* p=NULL;
                int flag=1;
                while(p!=head->next&&flag)
                {
                    Student* q=head;
                    flag=0;
                    for(;q->next&&q->next!=p;q=q->next)
                    {
                        if(q->num>q->next->num)
                        {
                            swap(&q->num,&q->next->num);
                            flag=1;
                        }
                    }
                    p=q;
                }
                printf("排序后:
    ");
                print(head);
            }
            else
            {
                system("cls");
                printf("请根据菜单重新输入!
    ");
            }
        }
        return head;
    }
    View Code
  • 相关阅读:
    译码器(选择器)
    RAM搭建
    ALU运算
    Base64
    关于Singleton
    C# WinForm导出Excel
    windows phone 前台布局以及画刷Brush使用 北京
    点击控件出现下沉或者倾斜技巧。(是你的控件不在死板,) 北京
    Centos5.4+Nginx+mysql5+php5+Zend3.3.3详细安装教程(转载请注明formating编写)
    如何锻炼身体
  • 原文地址:https://www.cnblogs.com/Hfolsvh/p/14224381.html
Copyright © 2011-2022 走看看