特别基础版学生宿舍管理系统(C语言版)

特别基础版学生宿舍管理系统(C语言版) 本系统只有最基础功能输入错误指令易报错仅供小白批判性参考。1.核心问题我们的目的是什么目的是构建一个学生宿舍管理系统。由此想出我们需要进行管理的对象的组成是什么。管理对象分为两个整体学生和宿舍。学生会有很多基础信息此文仅以最基础的名字name和住宿状态state举例。*住宿状态便于使用在退宿和空床位查看上。宿舍要考虑的信息此文以楼栋building、房间号room、床位数量totalbeds、入住人数lived和入住学生student举例。2.如何表示这些数据信息使用结构体。int condition0表示未住1表示入住 students[6]表示每个宿舍最多6人3.如何引用和保存数据要让程序启动并读取数据可以使用文件来达成目的。如果文件不存在可以使用默认数据让程序能够运行。同时要考虑到系统中多个函数都会访问各个宿舍所以我们还需要设置全局变量。Dormitory dorms[MAX_DORM];int dorm_count 0;void loadFromFile(){ FILE *fp fopen(dorm.txt, r); if(fp NULL) { printf(Not find file. Loading local messages.\n); dorm_count 3; // 宿舍1A栋 2032人 strcpy(dorms[0].building, A); strcpy(dorms[0].room, 203); dorms[0].totalbeds 6; dorms[0].lived 6; dorms[0].students[0] (Student){ LiuXiangqi, 1}; dorms[0].students[1] (Student){ YangYu, 1}; // 宿舍2A 1041人 strcpy(dorms[1].building, HefengD); strcpy(dorms[1].room, 6058); dorms[1].totalbeds 6; dorms[1].lived 1; dorms[1].students[0] (Student){ LiYingjie, 1}; for(int i 1; i 6; i){ dorms[1].students[i].condition 0; } } else { // 文件存在从文件读取 fscanf(fp, %d\n, dorm_count); for(int i 0; i dorm_count; i) { fscanf(fp, %s%s%d%d\n, dorms[i].building, dorms[i].room, dorms[i].totalbeds, dorms[i].lived); for(int j 0; j dorms[i].totalbeds; j) { char name[20]; int condition; fscanf(fp, %s %d\n, name, condition); strcpy(dorms[i].students[j].name, name); dorms[i].students[j].condition condition; } } fclose(fp); printf(Loading Success! Dormitory have %d\n, dorm_count); } }4.关于添加宿舍先创建一个临时的new宿舍存放你需要录入的信息最后再提交进去。*关于while(getchar() ! \n)清空缓冲区的问题scanf读取后会在缓冲区留下换行符导致输入被跳过。此处其实不会遇到此问题但可以养成习惯。void AddDormitory(){ Dormitory new; printf(Please enter the building:\n); scanf(%s, new.building); while(getchar() ! \n); printf(Please enter the room:\n); scanf(%s, new.room); while(getchar() ! \n); printf(Please enter the beds:\n); scanf(%d, new.totalbeds); while(getchar() ! \n); for(int i 0; i new.totalbeds; i){ new.students[i].condition 0; } dorms[dorm_count] new; dorm_count; printf(Success!); }5.关于查询宿舍输入楼栋和房间号以查找到宿舍信息。其中定位到宿舍下标的逻辑会在多个函数应用到所有可以把这部分单拎出来写一个函数。int findDorm(char building[], char room[]) { for (int i 0; i dorm_count; i) { if (strcmp(dorms[i].building, building) 0 strcmp(dorms[i].room, room) 0) { return i; } } return -1; }然后是将这个函数应用到查询宿舍信息的函数void SearchDorm(){ char building[20]; char room[10]; printf(Please enter the building: ); scanf(%s, building); while(getchar() ! \n); printf(Please enter the room: ); scanf(%s, room); while(getchar() ! \n); int goal findDorm(building, room); if (goal -1) { printf(Not found!! \n); return; } printf(Dormitory %s, total beds %d, lived %d\n, dorms[goal].building, dorms[goal].room, dorms[goal].totalbeds, dorms[goal].lived); }6.关于学生入住学生入住需要我们先找到目标宿舍-找到空床位-录入学生-更新全局变量void checkin(){ char building[20]; char room[10]; printf(Please enter the building: ); scanf(%s, building); while(getchar() ! \n); printf(Please enter the room: ); scanf(%s, room); while(getchar() ! \n); int dormnum findDorm(building, room); if (dormnum -1) { printf(Not found!\n); return; } int bednum -1; for(int i 0; i dorms[dormnum].totalbeds; i) { if (dorms[dormnum].students[i].condition 0) { bednum i; break; } } if (bednum -1) { printf(full dormitory\n); return; } Student stu; printf(Please enter the name: ); scanf(%s, stu.name); while(getchar() ! \n); stu.condition 1; dorms[dormnum].students[bednum] stu; dorms[dormnum].lived; printf(Success! \n); }7.关于学生退宿退宿需要输入姓名查询到宿舍这里需要一个按姓名查找的功能。由于一个函数只能返回一个值所以用指针参数返回额外信息。由于要修改宿舍里学生的状态信息所以要传入指针的地址需要用到二级指针。int Studentfindnum(char* name, Dormitory** sdom, int* sbed){ for (int i 0; i dorm_count; i) { for (int j 0; j dorms[i].totalbeds; j) { if (dorms[i].students[j].condition 1 strcmp(dorms[i].students[j].name, name) 0) { *sdom dorms[i]; *sbed j; return 1; } } } return 0; }再将此应用到退宿中。void checkout() { char name[20]; printf(Please enter the name: ); scanf(%s, name); while(getchar() ! \n); Dormitory *sdorm; int sbed; if(Studentfindnum(name, sdorm, sbed) 0){ printf(Not found.\n); return; } sdorm-students[sbed].condition 0; sdorm-lived--; printf(Succeed!\n); }8.关于查询学生void StudentSearch(){ char name[20]; printf(Please enter the name:); scanf(%s, name); while(getchar() ! \n); Dormitory *sdorm; int sbed; if (Studentfindnum(name, sdorm, sbed)){ printf(%s %s \n, sdorm-building, sdorm-room); } else { printf(Not found. \n); } }9.关于空床统计可以考虑两种形式一个是总共有多少空床一个是每一栋有几个空床。void Emptybed(){ int empty 0; for(int i 0; i dorm_count; i){ empty (dorms[i].totalbeds - dorms[i].lived); } printf(Total empty beds: %d\n, empty); char building[10] ; for(int i 0; i dorm_count; i){ if(strcmp(building, dorms[i].building) ! 0){ if(i 0) printf(\n); else strcpy(building, dorms[i].building); printf(building%s:, building); } } printf(%s empty %d beds\n, dorms[i].rooms[i].totalbeds - dorms[i].lived); }10.关于入住率排序按入住率排序需要计算每个宿舍的入住率并排序。void Sort(){ Dormitory tmp[MAX_DORM]; for(int i 0; i dorm_count; i){ tmp[i] dorms[i]; } for(int i 0; i dorm_count-1; i){ for(int j 0; j dorm_count-1-i; j){ float r1 0.0f; if(tmp[j].totalbeds 0){ r1 (float)tmp[j].lived / tmp[j].totalbeds; } float r2 0.0f; if(tmp[j1].totalbeds 0){ r2 (float)tmp[j1].lived / tmp[j1].totalbeds; } if(r1 r2){ Dormitory tmp1 tmp[j]; tmp[j] tmp[j1]; tmp[j1] tmp1; } } } for(int i 0; i dorm_count; i){ float n 0.0f; if(tmp[i].totalbeds 0){ n (float)tmp[i].lived / tmp[i].totalbeds * 100; } printf(%s, %s, %d, %d, %.1f%%\n, tmp[i].building, tmp[i].room, tmp[i].totalbeds, tmp[i].lived, n); } }