C/C++教程

宿舍管理系统(应用C语言、数据结构与算法知识).链表。(文件自己创建,这是代码).

本文主要是介绍宿舍管理系统(应用C语言、数据结构与算法知识).链表。(文件自己创建,这是代码).,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
#include <iostream>
#include <windows.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 10
using namespace std;
struct Stu
{
    char seId[20];
    char seName[20];
    char cGender[10];
    char seCollege[40];
    char seMajor[40];
    char seClass[40];
    char seDrom[20];
};
typedef struct Student
{
    struct Stu date;
    struct Student *next;
} Student;
struct DORM
{
    char dorm[20];
    int stuNum;
};
typedef struct Dorm
{
    struct DORM date;
    struct Dorm *next;
    Student *next1;
} Dorm;
struct FlOOR
{
    char floor[20];
    int DormNum;
};
typedef struct Floor
{
    struct FlOOR date;
    struct Floor *next;
    struct Dorm *next1;
} Floor;

struct BUILD
{
    char build[20];
    int floorNum;
};
typedef struct Building
{
    struct Floor *next1;
    struct BUILD date;
} Building;

int SentenceFull(int n);//判满
void LegalInput(char *str,int n);//数字字符串合法输入
void stringInput(char *str,int n);//合法输入
void SexSelection(char *str);//性别选择

void print_Stu(struct Stu F);//输出学生详情
void print_Dorm(Dorm *F);//输出寝室详情
void print_Floor(Floor *F);//输出楼层详情
void print_Build(Building *F);//输出宿舍楼详情


Student *Tofind_stu(Student *F,char id[]);//按学号查找学生
Dorm *Tofind_dorm(Dorm *F,char id[],int n);//按寝室号查找寝室,n>4,嵌套查学生
Floor *Tofind_floor(Floor *F,char id,int n);//按楼层号查找楼层

void Find_Stu(Student *F);//查找 并输出学生详情
void Find_Dorm(Dorm *F);//查找并输出寝室详情
void Find_Floor(Floor *F);//查找并输出楼层详情

void Sort_stu(Student *L);//按学号将学生从小到大排序
void Sort_Dorm(Dorm *L);//按寝室号将寝室从小到大排序
void Sort_Floor(Floor *L);//按楼层号将楼层从小到大排序

void StudentInformation(struct Stu *F,char dorm[]);//录入学生信息
void CreatStudent(Student **F,char dorm[],int n);//创建n个学生
void CreatDorm(Dorm **F,int n);//创建n个寝室
void CreatFloor(Floor **F,int n);//创建n层楼层
void CreatBuild(Building **F,int n);//创建n栋大楼

void modify_stu(Student *F);//修改学生信息
void DeleteStudet(Student **F,char id[]);//按学号删除学生

void Delete_Stu(Student **D);//删除学生,寝室版
int Delete_Dorm(Dorm **L,char dorm[]);//删除寝室
int Delete_Floor(Floor **L,char floor[]);//删除楼层

int DestroyDorm(Student **L);//清空寝室内所有学生,需手动改变学生数量(D->date.stunum)
int DestroyFloor(Dorm **L);//清空楼层所有寝室,需手动改变寝室数量(D->date.dornNum)
int DestroyBuild(Floor **L);//清空大楼

int  OperationDorm(Dorm **F);//寝室操作
int OperationFloor(Floor **F);//楼层操作
int OperationBuild(Building **F);//大楼操作

int FileStore(Building *B);//从文件读取
int FileRead(Building **D);//存储链表到文件
//判断选择
int SentenceFull(int n)
{
    int x;
    scanf("%d",&x);
    if(n+x<=MAX&&x>=0)
    {
        return x;
    }
    else
    {
        printf("输入错误,请重新输入\n");
        SentenceFull(n);
    }
}
void stringInput(char *str,int n)//合法输入
{
    scanf("%s",str);
    getchar();
    while(strlen(str)>n)
    {
        printf("长度超过 %d, 重新输入\n",n);
        scanf("%s",str);
        getchar(); 
    }
}
void LegalInput(char *str,int n)//数字字符串合法输入
{
    scanf("%s",str);
    getchar();
    int i=0;
    if(strlen(str)<n)
    {
        for(int i=0; i<strlen(str); i++)
        {
            if(str[i]>'9'||str[i]<'0')
            {
                printf("输入的字符非数字,请重新输入\n");
                LegalInput(str,n);
            }
        }
    }
    if(strlen(str)>n)
    {
        printf("长度超过 %d, 重新输入\n",n);
        LegalInput(str,n);
    }
}
void SexSelection(char *str)//性别选择
{
    printf("男0,女1  ");
    int n=0;
    scanf("%d",&n);
    if(n==0)
        strcpy(str,"男");
    if(n==1)
        strcpy(str,"女");

}
//输出
void print_Stu(struct Stu F)
{
	system("cls");
    printf("----------------------------\n");
    printf("|                          |\n");
    printf("|学号:     %s       \n",F.seId);
    printf("|名字:     %s      \n",F.seName);
    printf("|性别:     %s     \n",F.cGender);
    printf("|寝室:     %s      \n",F.seDrom);
    printf("|学院:     %s   \n",F.seCollege);
    printf("|专业:     %s     \n",F.seMajor);
    printf("|班级:     %s     \n",F.seClass);
    printf("|                          |\n");
    printf("----------------------------\n");
}
void print_Dorm(Dorm *F)
{
    printf("      寝室号:  %s\n",F->date.dorm);
    Student *p1;
    p1=F->next1;
    if(F->date.stuNum==0)
        printf("寝室为空\n");
    while(p1!=NULL)
    {
        printf("------------------------\n");
        printf("学号:%s   \n",p1->date.seId);
        printf("名字:%s \n",p1->date.seName);
        printf("------------------------\n");
        p1=p1->next;
    }
}
void print_Floor(Floor *F)
{
    printf("       楼层名:%s  \n",F->date.floor);
    Dorm *p1;
    p1=F->next1;
    if(F->date.DormNum==0)
        printf("楼层为空\n");
    while(p1!=NULL)
    {
        printf("---------------------------\n");
        printf("寝室号:%s    \n",p1->date.dorm);
        printf("学生数量:%d\n",p1->date.stuNum);
        printf("---------------------------\n");
        p1=p1->next;
    }
}
void print_Build(Building *F)
{
    printf("        大楼:  %s\n",F->date.build);
    Floor *p1;
    p1=F->next1;
    if(F->date.floorNum==0)
        printf("大楼为空\n");
    while(p1!=NULL)
    {
        printf("----------------------------\n");
        printf("楼层名:%s    \n",p1->date.floor);
        printf("寝室数量:%d\n",p1->date.DormNum);
        printf("----------------------------\n");
        p1=p1->next;
    }
}
//查找
Student *Tofind_stu(Student *F,char id[])//查学生
{
    Student *p1,*p2=NULL;
    p1=F;
    while(p1!=NULL)
    {
        if(strcmp(p1->date.seId,id)==0)
        {
            p2=p1;

            break;
        }
        if(strcmp(p1->date.seName,id)==0)
        {
            p2=p1;
            break;
        }
        else p1=p1->next;
    }
    if(p1==NULL)
        return p1;
    else
    {
        return p2;
    }
}
Dorm *Tofind_dorm(Dorm *F,char id[],int n)
{
    Dorm *p1,*p2=NULL;
    p1=F;
    int flag=0;
    while(p1!=NULL)
    {
        if(n>4)
        {
            Tofind_stu(p1->next1,id);
        }
        if(strcmp(p1->date.dorm,id)==0)
        {
            p2=p1;
            break;
        }
        else p1=p1->next;
    }
    if(p1==NULL)
        return p1;
    else
    {
        return p2;
    }
}
Floor *Tofind_floor(Floor *F,char id[],int n)
{
    Floor *p1,*p2=NULL;
    p1=F;
    int flag=0;
    while(p1!=NULL)
    {
        if(n>3)
        {
            Tofind_dorm(p1->next1,id,n);
        }
        if(strcmp(p1->date.floor,id)==0)
        {
            p2=p1;
            break;
        }
        else p1=p1->next;
    }
    if(p1==NULL)
        return p1;
    else
    {
        return p2;
    }
}
//查找加输出
void Find_Stu(Student *F)
{
    Student *p1=NULL;
    char id[20];
    printf("查询学生学号:    ");
    LegalInput(id,20);
    p1=Tofind_stu(F,id);
    if(p1==NULL)
    {
        printf("该学生不存在\n");
        printf("----------------------------\n\n");
    }
    if(p1)
    {
        printf("----------------------------\n");
        printf("|                          |\n");
        print_Stu(p1->date);
        printf("|   修改信息 1     返回   0   |\n");
        printf("|                          |\n");
        printf("----------------------------\n\n");
        int n;
        scanf("%d",&n);
        if(n==1)
        {
            modify_stu(p1);
        }
    }
}
void Find_Dorm(Dorm *F)
{
	int n;
    Dorm *p1=NULL;
    char id[20];
    printf("查询寝室:   ");
    scanf("%s",id);
    p1=Tofind_dorm(F,id,4);
    if(p1==NULL)
    {
        printf("该寝室不存在\n");
        printf("----------------------------\n\n");
    }
    if(p1)
    {
        printf("----------------------------\n");
        printf("|                          |\n");
        print_Dorm(p1);
        printf("|    操作   1      返回    |\n");
        printf("|                          |\n");
        printf("----------------------------\n\n");
      
        scanf("%d",&n);
        if(n==1)
        {
            OperationDorm(&p1);
        }
    }
}
void Find_Floor(Floor *F)
{
    Floor *p1=NULL;
    char id[20];
    printf("查询楼层:   ");
    scanf("%s",id);
    p1=Tofind_floor(F,id,3);

    if(p1==NULL)
    {
        printf("该楼层不存在\n");
        printf("----------------------------\n\n");
    }
    if(p1)
    {
        printf("----------------------------\n");
        printf("|                          |\n");
        print_Floor(p1);
        printf("|    操作 1   返回         |\n");
        printf("|                          |\n");
        printf("----------------------------\n\n");
        int n;
        scanf("%d",&n);
        if(n==1)
        {
            OperationFloor(&p1);
        }
    }
}
//排序
void Sort_stu(Student *L)
{
    Student *p,*q;
    Stu temp;
    for (p=L; p!=NULL; p=p->next)
    {
        for(q=p->next; q!=NULL; q=q->next)
        {
            if(strcmp(p->date.seId,q->date.seId)<0)
            {
                temp=q->date;
                q->date=p->date;
                p->date=temp;
            }
        }
    }
}
void Sort_Dorm(Dorm *L)
{
    Dorm *p,*q;
    Dorm temp;
    for (p=L; p!=NULL; p=p->next)
    {
        for(q=p->next; q!=NULL; q=q->next)
        {
            if(strcmp(p->date.dorm,q->date.dorm)<0)
            {
                temp.date=q->date;
                temp.next1=q->next1;
                q->date=p->date;
                q->next1=p->next1;
                p->date=temp.date;
                p->next1=temp.next1;
            }
        }
    }
}
void Sort_Floor(Floor *L)
{
    Floor *p,*q;
    Floor  temp;
    for (p=L; p!=NULL; p=p->next)
    {
        for(q=p->next; q!=NULL; q=q->next)
        {
            if(strcmp(p->date.floor,q->date.floor)<0)
            {
                temp.date=q->date;
                temp.next1=q->next1;
                q->date=p->date;
                q->next1=p->next1;
                p->date=temp.date;
                p->next1=temp.next1;
            }
        }
    }
}
//创建
void StudentInformation(struct Stu *F,char dorm[])//录入学生信息
{
    char id[20];
    printf("-------------------------------------\n");
    printf("学号:  ");
    LegalInput(id,20);
    if(id=="000")
        F=NULL;
    else
    {
        strcpy((*F).seId,id);
        printf("名字:  ");
        stringInput( (*F).seName,20);
        printf("性别:  ");
        SexSelection((*F).cGender);
        printf("学院:  ");
        stringInput((*F).seCollege,40);
        printf("专业:   ");
        stringInput((*F).seMajor,40);
        printf("班级:   ");
        stringInput( (*F).seClass,40);
        strcpy((*F).seDrom,dorm);
        printf("\n学生%s 信息录入成功\n",(*F).seName);
        printf("-------------------------------------\n\n");
    }
}
void CreatStudent(Student **F,char dorm[],int n)//插入n个学生
{
    if(n==0)
        (*F)=NULL;
    else
    {
        for(int i=0; i<n; i++)
        {
            Student *p1=new Student;
            StudentInformation(&(p1->date),dorm);
            p1->next = NULL;
            if(*F)
            {
                p1->next=(*F)->next;
                (*F)->next=p1;
            }
            else
            {
                *F = p1;
            }
        }
    }
}

void CreatDorm(Dorm **F,int n)//创建寝室
{
    if(n==0)
        (*F)=NULL;
    else
    {
        for(int i=0; i<n; i++)
        {
            Dorm *p1;
            int num;
            printf("\n-------------------------------------\n");
            printf("寝室号:   ");
            char id[20];
            scanf("%s",id);
            printf("寝室学生人数(<=10):    ");//输入寝室人数
            scanf("%d",&num);
            if(num<0)
                p1=NULL;
            else
            {
                p1=new Dorm;
                p1->date.stuNum=num;
                strcpy(p1->date.dorm,id);
                Student * D = NULL;
                CreatStudent(&D,id,num);
                p1->next1=D;
            }
            p1->next=NULL;
            if(*F)
            {
                p1->next=(*F)->next;
                (*F)->next=p1;
            }
            else
            {
                *F = p1;
            }
        }
        printf("寝室%s创建成功\n",(*F)->date.dorm);
        printf("-------------------------------------\n\n");
    }
}
void CreatFloor(Floor **F,int n)//创建楼层
{
    if(n==0)
        (*F)=NULL;
    else
    {
        for(int i=0; i<n; i++)
        {
            Floor *p1;
            int num;
            printf("-------------------------------------\n\n");
            printf("楼层名:");
            char floor[20];
            scanf("%s",floor);
            printf("楼层寝室数量:(<=10)");//输入寝室数量
            scanf("%d",&num);
            if(num<0)
                p1=NULL;
            else
            {
                p1=new Floor;
                p1->date.DormNum=num;
                strcpy(p1->date.floor,floor);

                printf("请输入寝室信息\n");
                Dorm * D = NULL;
                CreatDorm(&D,p1->date.DormNum);
                p1->next1=D;
            }
            p1->next=NULL;
            if(*F!=NULL)
            {
                p1->next=(*F)->next;
                (*F)->next=p1;

            }
            else
            {
                *F=p1;
            }
        }
        printf("楼层%s创建成功\n\n",(*F)->date.floor);
        printf("-------------------------------------\n\n");
    }
}
void CreatBuild(Building **F)
{
    Building *p1;
    printf("            创建宿舍楼\n");
    printf("-------------------------------------\n");
    printf("宿舍楼名称:  ");
    char build[20];
    scanf("%s",&build);
    int num;
    printf("楼层数量:(<=10)  ");
    num=SentenceFull(0);
    if(num<0)
        p1=NULL;
    else
    {
        p1=new  Building;
        p1->date.floorNum=num;
        strcpy(p1->date.build,build);
        printf("           请输入楼层信息\n");
        printf("-------------------------------------\n\n");
        Floor * D = NULL;
        CreatFloor(&D,p1->date.floorNum);
        p1->next1=D;
    }
    *F = p1;

}
//修改
void modify_stu(Student *F)//学生信息修改
{
    printf("_____________________________\n");
    printf("|修改名字 1      ");
    printf("修改学号 2      \n");
    printf("|修改性别 3      |");
    printf("修改班级 4      \n");
    printf("|修改学院 5      ");
    printf("修改专业 6      |\n");
    printf("_____________________________\n");
    printf("请输入选择:");
    int n;
    scanf("%d",&n);
    if(F!=NULL)
    {
        if(n==1)
        {
            printf("输入修改后学生名字.");
            char name[20];
            stringInput(name,20);
            memset(F->date.seName,'\0',20*sizeof(char));
            strcpy(F->date.seName,name);
        }
        if(n==2)
        {
            printf("输入修改后学生学号 ");
            char ID[20];
            scanf("%s",ID);
            memset(F->date.seId,'\0',20*sizeof(char));
            strcpy(F->date.seId,ID);
        }
        if(n==3)
        {
            printf("输入学生性别");
            char Gender[10];
            SexSelection(Gender);
            memset(F->date.cGender,'\0',10*sizeof(char));
            strcpy(F->date.cGender,Gender);
        }
        if(n==4)
        {
            printf("输入修改后学生所在班级");
            char College[40];
            stringInput( College,40);
            memset(F->date.seCollege,'\0',40*sizeof(char));
            strcpy(F->date.seCollege,College);
        }
        if(n==5)
        {
            printf("输入修改后学生所在学院");
            char Major[40];
            stringInput(Major,40);
            memset(F->date.seMajor,'\0',40*sizeof(char));
            strcpy(F->date.seMajor,Major);
        }
        if(n==6)
        {
            printf("输入修改后学生所在专业 ");
            char Class[40];
            stringInput( Class,40);
            memset(F->date.seClass,'\0',40*sizeof(char));
            strcpy(F->date.seClass,Class);
        }
    }
}
//删除
void  DeleteStudet(Student **F,char id[])//删除学生
{
    Student  *p=*F;
    Student  *q=NULL;
    if(strcmp(p->date.seId,id)==0)
    {
        *F=p->next;
        free(p);
    }
    else
    {
        while(p!=NULL)
        {
            q=p;
            p=p->next;
            if (p == NULL)//遍历完也没找到
            {
                printf("没有找到要删除的学生\n");
            }
            else if(strcmp(p->date.seId,id)==0)
            {
                q->next = p->next;
                delete(p);
                break;
            }
            else
            {
                printf("没有找到要删除的学生\n");
            }
        }
    }
}

void Delete_Stu(Student **D)
{
    char id[20];
    printf("-------------------------------------\n");
    printf("请输入要删除学生的学号\n");
    LegalInput(id,20);
    DeleteStudet(D,id);
    printf("学生%s已删除\n",id);
    printf("-------------------------------------\n\n");
}
int Delete_Dorm(Dorm **L,char dorm[])//删除寝室
{
    Dorm *p=*L;
    Dorm  *q=NULL;
    if(strcmp(p->date.dorm,dorm)==0)
    {
        *L=p->next;
        free(p);
    }
    else
    {
        while(p!=NULL)
        {
            q=p;
            p=p->next;
            if (p == NULL)
            {
                printf("没有找到要删除的寝室\n");
            }
            else if(strcmp(p->date.dorm,dorm)==0)
            {
                DestroyDorm(&q->next1);
                q->next = p->next;
                delete(p);
                break;
            }
            else
            {
                printf("没有找到要删除的寝室\n");
            }
        }
    }
}
int Delete_Floor(Floor **L,char floor[])
{
    Floor *p=*L;
    Floor  *q=NULL;
    if(strcmp(p->date.floor,floor)==0)
    {
        *L=p->next;
        free(p);
    }
    else
    {
        while(p!=NULL)
        {
            q=p;
            p=p->next;
            if (p == NULL)//遍历完也没找到
            {
                printf("没有找到要停用的楼层\n");
            }
            else if(strcmp(p->date.floor,floor)==0)
            {
                DestroyFloor(&q->next1);
                q->next = p->next;
                delete(p);
                break;
            }
            else
            {
                printf("没有找到要停用的楼层\n");
            }
        }
    }
}
int DestroyDorm(Student **L)//清空寝室
{
    Student *q;
    while(*L)
    {
        q=(*L)->next;
        free(*L);
        *L=q;
    }
    return 1;
}
int DestroyFloor(Dorm **L)//清空楼层
{
    Dorm *q;
    while(*L)
    {
        q=(*L)->next;
        Dorm * D =*L;
        DestroyDorm(&D->next1);
        free(*L);
        *L=q;
    }
    return 1;
}
int DestroyBuild(Floor **L)//清空大楼
{
    Floor *q;
    while(*L)
    {
        q=(*L)->next;
        Floor * D =*L;
        DestroyFloor(&D->next1);
        free(*L);
        *L=q;
    }
    return 1;
}
//操作
int  OperationDorm(Dorm **F)
{
    Dorm * D = NULL;
    D=*F;
    printf("------------------------------------\n");
    printf("|                                  |\n");
    printf("|    寝室详情 0      清空寝室 1    |\n");
    printf("|    添加学生 2      查看学生 3    |\n");
    printf("|    修改学生 4      删除学生 5    |\n");
    printf("|               退出               |\n");
    printf("|                                  |\n");
    printf("------------------------------------\n");
    int n;
    scanf("%d",&n);
    if(n==0)//寝室详情
    {
        print_Dorm(D);
        OperationDorm(&D);
        system("cls");
    }
    if(n==1)//清空寝室
    {
    	
        if(D->date.stuNum>0)
        {
            DestroyDorm(&D->next1);
            D->date.stuNum=0;
            printf("寝室已清空\n");
        }
        else printf("寝室为空\n");
        OperationDorm(&D);
        system("cls");
    }
    if(n==2)//添加学生
    {
        if( D->date.stuNum<MAX)
        {
            int x;
            printf("请输入添加学生数量<=%d\n",MAX-D->date.stuNum);
            x=SentenceFull(D->date.stuNum);
            CreatStudent(&D->next1,*&(D->date.dorm),x);
            D->date.stuNum=D->date.stuNum+x;
            Sort_stu(D->next1);
        }
        else printf("寝室已满");
        OperationDorm(&D);
        system("cls");
    }
    if(n==3)//查看学生
    {
        if(D->date.stuNum>0)
        {
            Find_Stu(D->next1);
            Sort_stu(D->next1);
        }
        else printf("寝室为空\n");
        OperationDorm(&D);
		system("cls"); 
    }
    if(n==4)
    {
        if(D->date.stuNum>0)
        {
            Student *p1=NULL;
            char id[20];
            printf("请输入要修改学生的学号\n");
            LegalInput(id,20);
            p1=Tofind_stu(D->next1,id);
            modify_stu(p1);
            print_Stu(p1->date);
            Sort_stu(D->next1);
        }
        OperationDorm(&D);
        system("cls");
    }
    if(n==5)//删除学生
    {
        if(D->date.stuNum>0)
        {
            Delete_Stu(&D->next1);
            print_Dorm(D);
        }
        OperationDorm(&D);
        system("cls");
    }
    if(n==6)
    {
        return 0;
    }
}

int OperationFloor(Floor **F)
{
    Floor* D = NULL;
    D=*F;
    printf("------------------------------------\n");
    printf("|                                  |\n");
    printf("|    楼层详情 0      清空楼层 1    |\n");
    printf("|    添加寝室 2      查看寝室 3    |\n");
    printf("|    寝室操作 4      删除寝室 5    |\n");
    printf("|               退出               |\n");
    printf("|                                  |\n");
    printf("------------------------------------\n");
    int n;
    scanf("%d",&n);
    if(n==0)//楼层详情
    {
    	system("cls");
        print_Floor(D);
        OperationFloor(&D);
    }
    if(n==1)//清空楼层
    {
    	system("cls");
        if(D->date.DormNum>0)
        {
            DestroyFloor(&D->next1);
            D->date.DormNum=0;
        }
        else printf("寝室为空\n");
        OperationFloor(&D);
    }
    if(n==2)//添加寝室
    {
    	system("cls");
        if( D->date.DormNum<MAX)
        {
            int x;
            printf("请输入添加寝室数量<=%d\n",MAX-D->date.DormNum);
            x=SentenceFull(D->date.DormNum);
            CreatDorm(&D->next1,x);
            D->date.DormNum=D->date.DormNum+x;
            Sort_Dorm(D->next1);
        }
        else printf("楼层已满");
        OperationFloor(&D);
    }
    if(n==3)//查看寝室
    {
    	system("cls");
        if(D->date.DormNum>0)
        {
            Find_Dorm(D->next1);
        }
        else printf("楼层为空\n");
        OperationFloor(&D);
    }
    if(n==4)
    {
    	system("cls");
        if(D->date.DormNum>0)
        {
            printf("请输入要进行操作的寝室号");
            Dorm *p1=NULL;
            char id[20];
            stringInput(id,20);
            p1=Tofind_dorm(D->next1,id,4);
            OperationDorm(&p1);
        }
    }
    if(n==5)//删除寝室
    {
    	system("cls");
        if(D->date.DormNum>0)
        {
            printf("请输入要删除的寝室号");
            char id[20];
            LegalInput(id,20);
            Delete_Dorm(&D->next1,id);
            print_Floor(D);
        }
        OperationFloor(&D);
    } 
}
int OperationBuild(Building **F)
{
    Building* D = NULL;
    D=*F;
    printf("------------------------------------\n");
    printf("|                                  |\n");
    printf("|    大楼详情 0      清空大楼 1    |\n");
    printf("|    启用楼层 2      查看楼层 3    |\n");
    printf("|    楼层操作 4      停用楼层 5    |\n");
    printf("|               退出               |\n");
    printf("|                                  |\n");
    printf("------------------------------------\n");
    int n;
    printf("请输入选择:  ");
    scanf("%d",&n);
    if(n==0)//大楼详情
    {
        print_Build(D);
        OperationBuild(&D);
    }
    if(n==1)
    {
        if(D->date.floorNum>0)
        {
            DestroyBuild(&D->next1);

            D->date.floorNum=0;
        }
        else printf("大楼为空\n");
        OperationBuild(&D);
    }
    if(n==2)//添加寝室
    {
        if(D->date.floorNum<MAX)
        {
            int x;
            printf("请输入启用楼层数量<=%d\n",MAX-D->date.floorNum);
            x=SentenceFull(D->date.floorNum);
            CreatFloor(&D->next1,x);
            D->date.floorNum=D->date.floorNum+x;
            Sort_Floor(D->next1);
        }
        else printf("无空闲宿舍");
        OperationBuild(&D);
    }
    if(n==3)//查看楼层
    {
        if(D->date.floorNum>0)
        {
            Find_Floor(D->next1);
        }
        else printf("大楼为空\n");
        OperationBuild(&D);
    }
    if(n==4)
    {
        if(D->date.floorNum>0)
        {
            printf("请输入要进行操作的楼层名");
            Floor *p1=NULL;
            char id[100];
            stringInput(id,100);
            p1=Tofind_floor(D->next1,id,4);
            OperationFloor(&p1);
        }
        OperationBuild(&D);
    }
    if(n==5)//删除大楼
    {
        if(D->date.floorNum>0)
        {
            printf("请输入要停用的楼层名");
            char id[20];
            stringInput(id,20);
            Delete_Floor(&D->next1,id);
            D->date.floorNum--;
            print_Build(D);
        }
        OperationBuild(&D);
    }
    return 0;
}
int   FileRead(Building **D)//读文件
{
    FILE *fp,*floor,*dorm,*stu;
    fp=fopen("Build.txt","rb");
    floor=fopen("Floor.txt","rb");
    dorm =fopen("dorm.txt","rb");
    stu=fopen("student.txt","rb");
    if(fp==NULL||floor==NULL||dorm==NULL ||stu==NULL)
    {
        printf("打开文件失败!");
        return 0;
    }
    Building *H=new Building;
    fread(&H->date,sizeof(BUILD),1,fp);
    Floor *p;
    Floor *q;
    FlOOR t;
    Floor* head=new Floor;
    p=q=head;
    while( fread(&t,sizeof(FlOOR),1,floor)==1)
    {
        q=new Floor;
        q->date=t;
        Dorm *a;
        Dorm *b;
        DORM x;
        Dorm *z=new Dorm;
        a=z;
        b=z;
        for(int r=0; r<t.DormNum; r++)
        {
            fread(&x,sizeof(DORM),1,dorm);
            b=new Dorm;
            b->date=x;
            Student *m;
            Student *n=NULL;
            Stu y;
            Student* h=new Student;
            m=n=h;
            int k;
            for(k=0; k<x.stuNum; k++)
            {
                fread(&y,sizeof(Stu),1,stu);
                n=new Student;
                n->date=y;
                m->next=n;
                m=n;
            }
            m->next=NULL;
            b->next1=h->next;
            a->next=b;
            a=b;
        }
        a->next=NULL;
        q->next1=z->next;
        p->next=q;
        p=q;
    }
    p->next=NULL;
    H->next1=head->next;
    fclose(floor);
    fclose(dorm);
    fclose(stu);
    fclose(fp);
    *D=H;
}
int FileStore(Building *B)//存文件
{
    FILE *fp,*floor,*dorm,*stu;
    fp=fopen("Build.txt","wb");
    floor=fopen("Floor.txt","wb");
    dorm =fopen("dorm.txt","wb");
    stu=fopen("student.txt","wb");
    if(fp==NULL||floor==NULL||dorm==NULL ||stu==NULL)
    {
        printf("打开文件失败!");
        return 0;
    }
    fwrite(&B->date,sizeof(BUILD),1,fp);
    Floor *F=NULL;
    F=B->next1;
    for(int i=0; i<B->date.floorNum; i++)
    {
        fwrite(&F->date,sizeof(FlOOR),1,floor);
        Dorm *D=NULL;
        D=F->next1;
        for(int y=0; y<F->date.DormNum; y++)
        {
            fwrite(&D->date,sizeof(DORM),1,dorm);

            Student  *S = NULL;
            S = D->next1;
            for(int k = 0; k <D-> date.stuNum; k ++)
            {
			
        		fwrite(&S->date,sizeof(Stu),1,stu);
                S = S->next;
            }
            D = D-> next;
        }
        F = F-> next;
    }
    fclose(floor);
    fclose(dorm);
    fclose(stu);
    fclose(fp);
}
int main(){

    Building  *F = NULL,*D = NULL;
    printf("欢迎进入宿舍管理系统\n");
    FILE *fp;
    char ch;
    if((fp=fopen("Build.txt","r"))== NULL)
        {
        printf("文件没打开!\n");

        }
    ch = fgetc(fp);
    if(ch == EOF)
    {
        printf("没有相应信息,请创建\n");
    	Sleep(1000);
        CreatBuild(&F);
        FileStore(F);
        FileRead(&D);
        OperationBuild(&D);
        FileRead(&D);
    }
    else
    {
        FileRead(&D);
        OperationBuild(&D);
    }
    FileStore(F);
}

这篇关于宿舍管理系统(应用C语言、数据结构与算法知识).链表。(文件自己创建,这是代码).的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!