同组的写了一个宿舍管理系统,发现和我的出入题目有很多,所以这里做个笔记记录一下
这里贴出题目
之前写的一个人事管理系统,我自己添加了一个新的版块是系统,功能为可以创建多个系统,分别管理不同系统里面的员工。这样的结构其实是嵌套的,这样就可以在主菜单中声明结构体数组,这样就可以了。但是宿舍管理系统不一样,仔细想想,每个学生对应一个床位,其实不是嵌套的关系了,这个时候不能再结构体中声明对应的楼层结构体,不然的意思就是每个学生都拥有一个结构楼,所以应该是在学生结构体外对应的声明。
替同学写的,还给送了我一个王者皮肤,哈哈哈
下面贴出代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 |
#include #include #include #include #include #define maxsize 1005//学生的最大总人数 #define vdorm 5//定义宿舍楼的最大数量 #define vroom 20//定义一层宿舍楼的最大房间数量 #define vbeed 5//定义一个房间的最大床位数量 //比较难点:1.需要考虑插入宿舍楼考虑住满情况不大,插入房间时需要考虑是否房间已经住满 using namespace std; //房间结构体 typedef struct{ int beed[vbeed]={0};//一个房间有四张床位,因为房间后面没有后缀信息,那么这个数组不能浪费,当为0时,表示该床位未住人 int people=0;//房间住的人数 bool full=0;//判断有没有住满变量 }roomx; //宿舍楼结构体 typedef struct{ roomx room[vroom]; int people;//宿舍楼所住的人数 int proom=0;//住满的房间数量 bool full=0;//判断有没有住满变量 }dormx; //学生信息结构体 typedef struct{ string name;//姓名 string sex;//性别 string studentid;//学号 string classid;//所属班级 string place;//所属院系 int dorm1;//宿舍楼号 int dorm2;//房间号 int dorm3;//床位号 }elemtype; dormx dorm[vdorm];//五栋楼,例如小张住dorm[1].room[1].beed[3] int nber=0;//学生的总人数,初始为0 bool bl;//判断变量 elemtype number[maxsize]; //水平线函数 void hr(){ cout<<"**************************************"<<endl; } //欢迎进入的函数 void hello(){ system("color 3E"); cout<<"*******************************"<<endl; cout<<"** ╭╮ ╭╮ ***"<<endl; cout<<"** ││欢迎进入宿舍系统││ ***"<<endl; cout<<"**╭┴┴———————┴┴╮***"<<endl; cout<<"**│ ● ● │***"<<endl; cout<<"**│○ ╰┬┬┬╯ ○│***"<<endl; cout<<"**│ ╰—╯ │***"<<endl; cout<<"**╰——┬O———O┬——╯***"<<endl; cout<<"** ╭╮ ╭╮ ***"<<endl; cout<<"** ╰┴————┴╯ ***"<<endl; cout<<"这是一个学生宿舍管理系统,如果你想使用本系统,我需要先向你介绍一下"<<endl; cout<<"本学生宿舍管理系统由宿舍楼号,房间号,床位号定位学生的位置,一共有五个宿舍楼,"<<endl; cout<<"每个宿舍楼有20个房间,每个房间有4张床位。宿舍楼分别为a1,a2,a3,b1,b2五栋,a为男生楼,b为女生楼"<<endl; system("pause");//按任意键继续 system("cls"); } //主菜单 void menu(){ system("cls"); hr(); cout<<"\t1.录入指定人数的学生信息"<<endl; cout<<"\t2.插入指定位置的学生"<<endl; cout<<"\t3.删除指定位置的学生"<<endl; cout<<"\t4.交换两个学生的信息"<<endl; cout<<"\t5.查询指定房间号的所有学生"<<endl; cout<<"\t6.统计指定宿舍没住满情况"<<endl; cout<<"\t7.显示所有学生的信息"<<endl; cout<<"\t8.退出系统"<<endl; cout<<"-------------------------------------"<<endl; cout<<"请输入你要选择功能前的序号"<<endl; hr(); } //判断学号是否正确函数 bool student(string studentid){ int id=10; if(studentid.length()==10) return true; else return false; } //判断性别是否正确的函数 bool issex(string sex){ if(sex=="男"||sex=="男生"||sex=="女"||sex=="女生") return true; else return false; } //判断是否是数字函数,带入参数类型string,如果是返回1 bool isNum(string str){ stringstream sin(str); double d; char c; if(!(sin>>d)) return false; if(sin>>c) return false; return true; } //判断宿舍楼是否正确函数 bool isdorm(string sex,string dorm1){ if(sex=="男"||sex=="男生"){ if(dorm1=="a1"||dorm1=="a2"||dorm1=="a3") return true; return false; }else{ if(dorm1=="b1"||dorm1=="b2") return true; else return false; } } //宿舍楼符号转换函数 int dormxx(string dormx){ if(dormx=="a1") return 0; else if(dormx=="a2") return 1; else if(dormx=="a3") return 2; else if(dormx=="b1") return 3; else if(dormx=="b2") return 4; else { cout<<"输出有误!请重新输入。"<<endl; return 5; } } //判断房间人满函数 void dormx2(){ } //判断床位重复函数 void dormx3(){ } //是否清屏函数 void clear(){ system("cls");//清屏 } //录入函数 void moreinsert(){ clear(); string name,sex,studentid,classid,place,dorm1,dorm2,dorm3; int ndorm2,ndorm3; cout<<"请输入你需要录入学生的人数"<<endl; int choose;cin>>choose; bool bl;//判断的学号的值 while(choose--){ nber++;//每录入一个学生,学生最大人数便需要加一 hr(); cout<<"这是第"<<nber<<"个学生"<<endl; cout<<"请输入该学生的姓名"<<endl; cin>>name; number[nber].name=name; cout<<"请输入该学生的性别"<<endl; while(cin>>sex){ if(issex(sex)){ number[nber].sex=sex; break; }else{ cout<<"输入的性别有误,请重新输入"<<endl; continue; }} cout<<"请输入该学生的学号"<<endl; while(cin>>studentid){ if(!isNum(studentid)){ cout<<"请输入正确的学号,由十位数字组成"<<endl; continue; } bl=student(studentid); if(bl){ number[nber].studentid=studentid; break; }else{ cout<<"学号为十位数,你输入的有误,请重新输入"<<endl; continue; }} cout<<"请输入该学生的所属班级"<<endl; cin>>classid; number[nber].classid=classid; cout<<"请输入该学生的所属院系"<<endl; cin>>place; number[nber].place=place; cout<<"请输入该学生的宿舍楼号"<<endl; while(cin>>dorm1){ bl=isdorm(sex,dorm1); if(bl){ number[nber].dorm1=dormxx(dorm1); dorm[dormxx(dorm1)].people++; break; }else{ cout<<"你输入的宿舍有误,男生宿舍为a1,a2,a3;女生宿舍为b1,b2"<<endl; continue; } } cout<<"请输入该学生的房间号"<<endl; while(cin>>dorm2){ if(!isNum(dorm2)){ cout<<"请正确输入房间号,1到20(请输入整数)"<<endl; continue; } else ndorm2=atoi(dorm2.c_str()); if(ndorm2>20||ndorm2<0){ cout<<"输入的房间有误,重新输入"<<endl; continue; } if(!dorm[dormxx(dorm1)].room[ndorm2].full){ number[nber].dorm2=ndorm2; //房间人数加一 dorm[dormxx(dorm1)].room[ndorm2].people++; //判断该房间是否住满,住满标记full变量 if(dorm[dormxx(dorm1)].room[ndorm2].people==4) dorm[dormxx(dorm1)].room[ndorm2].full=1; break; }else{ cout<<"本房间已经住满四人,请换房间输入"<<endl; continue; }} cout<<"请输入该学生的床位号"<<endl; while(cin>>dorm3){ if(!isNum(dorm3)){ cout<<"请正确输入床位号,1到4(请输入整数)"<<endl; continue; }else ndorm3=atoi(dorm3.c_str()); if(ndorm3>4||ndorm3<1){ cout<<"输入的床位有误,重新输入"<<endl; continue; } if(dorm[dormxx(dorm1)].room[ndorm2].beed[ndorm3]==0){ dorm[dormxx(dorm1)].room[ndorm2].beed[ndorm3]=1; number[nber].dorm3=ndorm3;break; }else{ cout<<"本床位已经住了人,请换床位输入"<<endl; continue; }} cout<<"该学生信息插入成功!"<<endl; } cout<<"录入信息完毕!"<<endl; system("pause"); hr(); } //插入函数 void insert(){ nber++; clear(); bool bl; string name,sex,studentid,classid,place,dorm1,dorm2,dorm3; int ndorm2,ndorm3; cout<<"请输入要插入的学生的姓名"<<endl; cin>>name; number[nber].name=name; cout<<"请输入要插入的学生的性别"<<endl; while(cin>>sex){ if(issex(sex)){ number[nber].sex=sex; break; }else{ cout<<"输入的性别有误,请重新输入"<<endl; continue; }} cout<<"请输入要插入的学生的学号"<<endl; while(cin>>studentid){ if(!isNum(studentid)){ cout<<"请输入正确的学号,由十位数字组成"<<endl; continue; } bl=student(studentid); if(bl){ number[nber].studentid=studentid; break; }else{ cout<<"学号为十位数,你输入的有误,请重新输入"<<endl; continue; }} cout<<"请输入要插入的学生的所属班级"<<endl; cin>>classid; number[nber].classid=classid; cout<<"请输入要插入学生的所属院系"<<endl; cin>>place; number[nber].place=place; cout<<"请输入要插入学生的宿舍楼号"<<endl; while(cin>>dorm1){ bl=isdorm(sex,dorm1); if(bl){ number[nber].dorm1=dormxx(dorm1); dorm[dormxx(dorm1)].people++; break; }else{ cout<<"你输入的宿舍有误,男生宿舍为a1,a2,a3;女生宿舍为b1,b2"<<endl; continue; } } cout<<"请输入要插入的学生的房间号"<<endl; while(cin>>dorm2){ if(!isNum(dorm2)){ cout<<"请正确输入房间号,1到20(请输入整数)"<<endl; continue; } else ndorm2=atoi(dorm2.c_str()); if(ndorm2>20||ndorm2<0){ cout<<"输入的房间有误,重新输入"<<endl; continue; } if(!dorm[dormxx(dorm1)].room[ndorm2].full){ number[nber].dorm2=ndorm2; //房间人数加一 dorm[dormxx(dorm1)].room[ndorm2].people++; //判断该房间是否住满,住满标记full变量 if(dorm[dormxx(dorm1)].room[ndorm2].people==4) dorm[dormxx(dorm1)].room[ndorm2].full=1; break; }else{ cout<<"本房间已经住满四人,请换房间输入"<<endl; continue; }} cout<<"请输入要插入学生的床位号"<<endl; while(cin>>dorm3){ if(!isNum(dorm3)){ cout<<"请正确输入床位号,1到4(请输入整数)"<<endl; continue; }else ndorm3=atoi(dorm3.c_str()); if(ndorm3>4||ndorm3<1){ cout<<"输入的床位有误,重新输入"<<endl; continue; } if(dorm[dormxx(dorm1)].room[ndorm2].beed[ndorm3]==0){ dorm[dormxx(dorm1)].room[ndorm2].beed[ndorm3]=1; number[nber].dorm3=ndorm3;break; }else{ cout<<"本床位已经住了人,请换床位输入"<<endl; continue; }} cout<<"该学生信息插入成功!"<<endl; system("pause"); } //删除函数 bool deleteber(){ clear(); cout<<"请输入你想要删除的学生的姓名"<<endl; string name; cin>>name; bool bl=0; int i; for( i=0;i<=nber;i++){ if(number[i].name==name){ bl=1; break; } } if(bl){ for(int j=i;j<nber;j++) number[j]=number[j+1]; nber--; dorm[number[i].dorm1].room[number[i].dorm2].people--; dorm[number[i].dorm1].people--; dorm[number[i].dorm1].room[number[i].dorm2].beed[number[i].dorm3]=0; cout<<"删除成功!"<<endl; system("pause"); return true; } else{ cout<<"未找到该学生"<<endl; system("pause"); hr(); return false; } } //交换函数 bool exchange(){ clear(); int i,j; bool bl=0; string name1,name2; int dorm1,dorm2,dorm3; cout<<"请输入需要交换的学生1的姓名"<<endl; cin>>name1; for(i=1;i<=nber;i++){ if(number[i].name==name1){ bl=1; break;} } if(bl){ cout<<"该学生信息已找到"<<endl; cout<<"\t姓名:"<<number[i].name<<"\t性别:"<<number[i].sex<<"\t学号;"<<number[i].studentid<<endl; cout<<"\t所属院系:"<<number[i].place<<"\t所属班级:"<<number[i].classid<<endl; } else{ cout<<"未找到该学生"<<endl; return false; } bl=0; cout<<"请输入需要交换的学生2的姓名"<<endl; cin>>name2; for(j=1;j<=nber;j++){ if(number[j].name==name2){ bl=1; break; }} if(bl){ cout<<"该学生信息已找到"<<endl; cout<<"\t姓名:"<<number[j].name<<"\t性别:"<<number[j].sex<<"\t学号;"<<number[j].studentid<<endl; cout<<"\t所属院系:"<<number[j].place<<"\t所属班级:"<<number[j].classid<<endl; } else{ cout<<"未找到该学生"<<endl; return false; } dorm1=number[i].dorm1;dorm2=number[i].dorm2;dorm3=number[i].dorm3; number[i].dorm1=number[j].dorm1;number[i].dorm2=number[j].dorm2;number[i].dorm3=number[j].dorm3; number[j].dorm1=dorm1;number[j].dorm2=dorm2;number[j].dorm3=dorm3; return true; system("pause"); } //查询指定房间所有学生函数 void roompeople(){ clear(); string dorm1;int dorm2; cout<<"请输入你想要查找的宿舍楼"<<endl; cin>>dorm1; cout<<"请输入你想要查找的房间号"<<endl; cin>>dorm2; system("cls"); int a=dormxx(dorm1); for(int i=1;i<=nber;i++){ if(number[i].dorm1==a&&number[i].dorm2==dorm2){ cout<<"\t姓名:"<<number[i].name<<"\t性别:"<<number[i].sex<<"\t学号;"<<number[i].studentid<<endl; cout<<"\t所属院系:"<<number[i].place<<"\t所属班级:"<<number[i].classid<<endl; } hr(); } system("pause"); } //统计宿舍没住满情况 void sumdorm(){ clear(); string dorm1; int a=0;//统计未住满的房间数量 int b=0;//统计住满的房间数量 cout<<"请输入需要统计的宿舍楼"<<endl; cout<<"宿舍楼分别为a1,a2,a3(男生楼);b1,b2(女生楼)"<<endl; do { cin>>dorm1; }while(dormxx(dorm1)==5); hr(); cout<<"该宿舍楼住有人数为: "<<dorm[dormxx(dorm1)].people<<"人"<<endl; for(int i=1;i<=20;i++){ cout<<"第"<<i<<"号房间所住的人数为: "<<dorm[dormxx(dorm1)].room[i].people<<endl; if(dorm[dormxx(dorm1)].room[i].people==0){ a++;//如果未住满a(未住满的房间数量)+1 } if(dorm[dormxx(dorm1)].room[i].full==1)//如果住满人b(住满房间数量)+1 b++; } cout<<"空房间数量: "<<a<<"个"<<endl; cout<<"满房间数量: "<<b<<"个"<<endl; system("pause"); } //显示所有学生人数的信息 void dispstudent(){ clear(); for(int i=1;i<=nber;i++){ hr(); cout<<"学生"<<i<<"的信息"<<endl; cout<<"\t姓名: "<<number[i].name<<endl; cout<<"\t性别: "<<number[i].sex<<endl; cout<<"\t学号: "<<number[i].studentid<<endl; cout<<"\t所属班级: "<<number[i].classid<<endl; cout<<"\t所属院系: " <<number[i].place<<endl; cout<<"\t宿舍楼号: "<<number[i].dorm1<<endl; cout<<"\t房间号: "<<number[i].dorm2<<endl; cout<<"\t床位号: "<<number[i].dorm3<<endl; hr(); } cout<<"所有学生显示完毕"<<endl; system("pause"); } int main(){ system("cls");//清屏 hello(); menu(); int choose;//定义用户的选择变量 cin>>choose; while(1){ if(choose==1){ moreinsert(); menu(); cin>>choose; continue; } else if(choose==2){ insert(); menu(); cin>>choose; continue; } else if(choose==3){ bl=deleteber(); if(bl) cout<<"删除成功"<<endl; else cout<<"删除失败"<<endl; hr(); menu(); cin>>choose; continue; } else if(choose==4){ bl=exchange(); if(bl) cout<<"交换成功"<<endl; else cout<<"交换失败"<<endl; menu(); cin>>choose; continue; } else if(choose==5){ roompeople(); menu(); cin>>choose; continue; } else if(choose==6){ sumdorm(); menu(); cin>>choose; continue; } else if(choose==7){ dispstudent(); menu(); cin>>choose; continue; } else if(choose==8){ break; }else { cout<<"输入错误请重新输入"<<endl; cin>>choose; } } cout<<"欢迎下次进入本学生宿舍管理系统"<<endl; } |