android-nuc / 18-C-Train

18级Android实验室(人工智能+移动互联) C语言培训
16 stars 10 forks source link

陈抒菡 宿舍管理系统改 #52

Closed notwiioe closed 5 years ago

notwiioe commented 5 years ago

宿舍管理系统

#include"stdio.h"
#include"stdlib.h"
#include"string.h"
#include"windows.h"
typedef struct Student{
    char name[20];
    int id;
    int age;
    char gender[4];
    int dorm;
    int bed;
    struct Student *next;
}Stu;
int n=0;
Stu *c,*r,*head;
Stu *p;
FILE *fp;
void typein()
{
    for(int i = 0;i < n;i ++)
    {
        printf("Please input student's information:\n name:\n id:\n age:\n gender:\n dorm:\n bed:\n");
        c = (Stu *)malloc(sizeof(Stu));
        scanf("%s%d%d%s%d%d",&c->name,&c->id,&c->age,&c->gender,&c->dorm,&c->bed);
        c->next = NULL;
        if(!head)
            head = c;
        else
            r->next = c;
        r = c;
    }
}
void input(FILE *fp)
{
    c = (Stu *)malloc(sizeof(Stu));
    fread(c,sizeof(Stu),1,fp);
    r = head = c;
    while(c->next)
    {
        c = c->next;
        c = (Stu *)malloc(sizeof(Stu));
        fread(c,sizeof(Stu),1,fp);
        r->next = c;
        r = c;
    }
    r->next = NULL;
    fclose(fp);
}
//输出
void output()
{
    p = head;
    printf("student information:\n");
    while(p)
    {
        printf("   name:%s     id:%d    age:%d gender:%s   dorm:%d    bed:%d\n\n",p->name,p->id,p->age,p->gender,p->dorm,p->bed);
        p = p->next;
    }
}
//查询
void query()
{
    int stu_id;
    if(!p)
        printf("null\n");
    else
        {
            printf("please enter the id you want:\n");
            scanf("%d",&stu_id);
            while(p)
            {
                if(p->id == stu_id)
                {
                     printf(" name:%s\n id:%d\n age:%d\n gender:%s\n dorm:%d\n bed:%d\n",p->name,p->id,p->age,p->gender,p->dorm,p->bed);
                     break;
                }
                else
                    p = p->next;
            }
       }
}
//插入
void insert()
{
    Stu *new_infor;
    int in;
    new_infor = (Stu *)malloc(sizeof(Stu));
    printf("Please input the new student information:\n name:\n id:\n age:\n gender:\n dorm:\n bed:\n");
    scanf("%s%d%d%s%d%d",&new_infor->name,&new_infor->id,&new_infor->age,&new_infor->gender,&new_infor->dorm,&new_infor->bed);
    printf("Please enter the location you want to insert:");
    scanf("%d",&in);
    if(in == 1)
    {
        new_infor->next = head;
        head = new_infor;
    }
    else
    {
        for(int j = 1;j < in - 1 && p;j ++)
            p = p->next;
        new_infor->next = p->next;
        p->next = new_infor;
    }
    n++;
}
//删除
void Delete()
{
    Stu *ne;
    int del;
    printf("Please enter the location you want to delete:");
    scanf("%d",&del);
    if(del == 1)
            head = head->next;
    else
    {
        for(int j = 1;j < del -1 && p;j ++)
            p = p->next;
        ne = p->next;
        p->next = ne->next;
    }
    free(ne);
    n--;
}
//存储
void storage()
{
    fp=fopen("D:\\data.dat","w");
    if(!fp)
    {
        printf("failed to open file");
        exit(0);
    }
    fwrite(p,sizeof(Stu),n,fp);
    fclose(fp);
    printf("done\n");
}

int main()
{
    int sw;
    fp=fopen("D:\\data.dat","rb");
    head = NULL;
    if(fp == NULL)
    {
        printf("please enter the number you want to input\n");
        scanf("%d",&n);
        typein();
    }
    else
        input(fp);
    output();
    while(1)
    {
        p = head;
        printf("3查询4插入5删除6存储与退出");
        scanf("%d",&sw);
        switch(sw)
        {
        case 3:
            query();
            break;
        case 4:
            system("cls");
            insert();
            output();
            break;
        case 5:
            system("cls");
            Delete();
            output();
            break;
        case 6:
            system("cls");
            storage();
            printf("seeya\n\n");
            exit(0);
        }
    }
    return 0;
}