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

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef struct student
    {
        int id;
        char name[20];
        int age;
        char sex;
        char birthday[20];
        char address[20];
        char phone[15];
        char email[30];
        struct student *next;
    }student;
    
    student *head = NULL;
    int length;
    void create()
    {
        student *p1,*p2;
        length = 0;
        p1 = (student *)malloc(sizeof(student));
        p1->id = -1;
        if(head == NULL)
        {
            head = p1;
        }
        printf("请输入学生的学号、姓名、年龄、性别、出生年月、地址、电话、电子邮箱:\n");
        while(1)
        {
            p2 = (student *)malloc(sizeof(student));
            scanf("%d %s %d %c %s %s %s %s",&p2->id,p2->name,&p2->age,&p2->sex,&p2->birthday,&p2->address,p2->phone,p2->email);
            if(p2->id == 0)
            {
                printf("链表创建完成!\n");
                break;
            }
            length ++;
            p1->next = p2;
            p2->next = NULL;
            p1 = p1->next;
        }
        return ;
    }
    
    void LoadStudentInFromFile()
    {
        student *p,*q;
        int c;
        FILE* f;
        f = fopen("input.txt","rb");
        if(f == NULL)
        {
            return ;
        }
        fseek(f,0,SEEK_SET);
        p = (student *)malloc(sizeof(student));
        p->next = NULL;
        head = p;
        while(!feof(f))
        {
            c = fgetc(f);
            if(c != -1)
            {
                fseek(f,-1,SEEK_CUR);
            }
            else
            {
                return ;
            }
            q = (student *)malloc(sizeof(student));
            fscanf(f,"%d",&q->id);
            fscanf(f,"%s",q->name);
            fscanf(f,"%d",&q->age);
            fscanf(f,"%c",&q->sex);
            fscanf(f,"%s",q->birthday);
            fscanf(f,"%s",q->address);
            fscanf(f,"%s",q->phone);
            fscanf(f,"%s",q->email);
            q->next = NULL;
            p->next = q;
            p = p->next;
            length ++;//链表长度
        }
    }
    
    void ModifyStudentInfo()
    {
        student *p = head->next;
        int num;
        printf("请输入要修改的学生的学号:");
        scanf("%d",&num);
        while(p != NULL)
        {
            if(p->id == num)
            {
                printf("修改前,学号为%d的学生信息如下:\n",num);
                printf("%d %s %d %c %s %s %s %s",p->id,p->name,p->age,p->sex,p->birthday,p->address,p->phone,p->email);
                printf("请输入学生的新电话:");
                getchar();
                gets(p->phone);
                printf("请输入学生的新地址:");
                gets(p->address);
                printf("修改后,学号为%d的学生信息如下:\n",num);
                printf("%d %s %d %c %s %s %s %s",&p->id,p->name,&p->age,p->sex,p->birthday,p->address,p->phone,p->email);
                return ;
            }
            p = p->next;
        }
        if(p == NULL)
        {
            printf("该学号不存在!\n");
            return ;
        }
    }
    void display()
    {
        student *p = head->next;
        printf("链表中所有的学生信息如下:\n");
        while(p != NULL)
        {
            printf("%d %s %d %c %s %s %s %s",p->id,p->name,p->age,p->sex,p->birthday,p->address,p->phone,p->email);
            printf("\n");
            p = p->next;
        }
        return ;
    }
    
    void search()
    {
        int num,x;
        char name[20];
        student *p = head->next;
        printf("请选择查询方式:\n");
        printf("1、按学号查询\t2、按姓名查询\n");
        scanf("%d",&x);
        if(x == 1)
        {
            printf("需要查找的学生学号为:");
            scanf("%d",num);
            while(p != NULL)
            {
                if(p->id == num)
                {
                    printf("学号为%d的学生信息如下:\n",num);
                    printf("%d %s %d %c %s %s %s %s",p->id,p->name,p->age,p->sex,p->birthday,p->address,p->phone,p->email);
                    return ;
                }
                p = p->next;
            }
            if(p == NULL)
            {
                printf("无此记录!\n");
            }
        }
        else if(x == 2)
        {
            printf("需要查找的学生姓名为:");
            getchar();
            gets(name);
            p = head->next;
            while(p != NULL)
            {
                if(strcmp(p->name,name) == 0)
                {
                    printf("学生姓名为%s的学生信息如下:\n",name);
                    printf("%d %s %d %c %s %s %s %s",p->id,p->name,p->age,p->sex,p->birthday,p->address,p->phone,p->email);
                    return ;
                }
                p = p->next;
            }
            if(p == NULL)
            {
                printf("无此记录!\n");
            }
        }
        return ;
    }
    
    void insert()
    {
        int num,i;
        student *p,*q;
        p = head;
    
        printf("请输入你要插入的位置:");
        scanf("%d",&num);
        if(num > length)
        {
            printf("找不到插入的位置\n");
            return ;
        }
        else
        {
            printf("请输入你要插入的学生的信息:\n");
            q = (student *)malloc(sizeof(student));
            scanf("%d %s %d %c %s %s %s %s",&q->id,q->name,&q->age,&q->sex,q->birthday,q->address,q->phone,q->email);
            while(p != NULL)
            {
                if(p->id == q->id)
                {
                    printf("该学号已经存在,无法插入!\n");
                    return ;
                }
                p = p->next;
            }
            p = head;
            for(i=0; i<num; ++i)
            {
                p = p->next;
            }
            q->next = p->next;
            p->next = q;
            length ++;
            printf("插入成功!\n");
            return ;
        }
    }
    
    void Delete()
    {
        int num;
        student *p,*q;
        q = head;
        p = head->next;
        printf("请输入要删除的学生的学号:\n");
        scanf("%d",&num);
    
        while(p != NULL)
        {
            if(p->id == num)
            {
                q->next = p->next;
                free(p);
                length --;
                printf("删除成功!\n");
                return ;
            }
            p = p->next;
            q = q->next;
        }
        if(p == NULL)
        {
            printf("找不到要删除的编号!\n");
            return ;
        }
    }
    
    void menu()
    {
        printf("___________________________________________________\n");
        printf("|        学生信息管理系统          |\n");
        printf("|        0、退出系统              |\n");
        printf("|        1、录入学生信息              |\n");
        printf("|        2、建立链表              |\n");
        printf("|        3、显示链表              |\n");
        printf("|        4、查找链表中的某个元素          |\n");
        printf("|        5、删除链表中指定学号的结点      |\n");
        printf("|        6、指定位置上插入一个新结点      |\n");
        printf("|        7、修改学生信息              |\n");
        printf("__________________________________________________\n");
        return ;
    }
    
    int main(void)
    {
        int a;
        menu();
        while(1)
        {
            printf("请输入相应的功能:");
            scanf("%d",&a);
            switch(a)
            {
            case 0:
                return 0;
            case 1:
                LoadStudentInFromFile();
                menu();
                break;
            case 2:
                create();
                menu();
                break;
            case 3:
                if(head)
                {
                    display();
                    menu();
                }
                else
                {
                    printf("链表为空,请先建立链表!\n");
                    menu();
                }
                break;
            case 4:
                if(head)
                {
                    search();
                    menu();
                }
                else
                {
                    printf("链表为空,请先建立链表!\n");
                    menu();
                }
                break;
            case 5:
                if(head)
                {
                    Delete();
                    menu();
                }
                else
                {
                    printf("链表为空,请先建立链表!\n");
                    menu();
                }
                break;
            case 6:
                if(head)
                {
                    insert();
                    menu();
                }
                else
                {
                    printf("链表为空,请先建立链表!\n");
                    menu();
                }
                break;
            case 7:
                if(head)
                {
                    ModifyStudentInfo();
                    menu();
                }
                else
                {
                    printf("链表为空,请先建立链表!\n");
                    menu();
                }
                break;
            default:
                break;
            }
        }
        system("pause");
        return 0;
    }
  • 相关阅读:
    Maven部署构件至远程仓库
    Maven远程仓库的认证
    Maven远程仓库的配置
    Maven实战系列文章
    使用Maven私服的好处
    使用Mavne生成可以执行的jar文件
    Visual Studio for Mac 简介
    HTTP 2.0与HTTP 1.1区别
    使用Microsoft的IoC框架:Unity来对.NET应用进行解耦
    围绕央行系统升级所产生的常见问题
  • 原文地址:https://www.cnblogs.com/tslDream/p/4454391.html
Copyright © 2011-2022 走看看