#include<iostream>
usingnamespacestd;
constintROOM_NUM=4;
constintSTU_NUM_EACH_ROOM=4;

enumOP...{RESIDEIN=1,OUT,QUERY,QUIT};
enumQUER...{BYNO=1,BYNAME,BYROOMNO,ALL,BACK};//查询方式

typedefstructRoom...{
introomNo;
intsNum;//该房间现住人数
intsNo[STU_NUM_EACH_ROOM];//房间里住的学生的学号
structRoom*next;
}Room,*PRoom;

typedefstructStudent...{
intsNo;
charsName[20];
introomNo;
structStudent*next;
}Student,*PStudent;
voidprintMessage(constchar*mess)
...{
cout<<mess;
}
voidmakeRooms(PRoom&prs)
...{
prs=newRoom[ROOM_NUM];
if(prs)
...{
for(inti=0;i<ROOM_NUM;++i)
...{
prs[i].roomNo=i+1;
prs[i].sNum=0;
memset(prs[i].sNo,0,sizeof(prs[i].sNo));
prs[i].next=NULL;
}
}
else
...{
printMessage("宿舍没盖成功,可能是资金的问题! ");
exit(1);
}
}
intgetRoomNo(PRoom&prs)
...{
for(inti=0;i<ROOM_NUM;++i)
...{
if(prs[i].sNum<STU_NUM_EACH_ROOM)
returnprs[i].roomNo;
}
return0;
}
PStudentcheckSno(PStudent&pslh,intsno)
...{
for(PStudentps=pslh->next;ps;ps=ps->next)
...{
if(ps->sNo==sno)
returnps;
}
returnNULL;
}
voidresideIn(PStudent&pslh,PRoom&prs)
...{
cin.clear();
introomNo=getRoomNo(prs);
if(roomNo!=0)
...{
Student*psNew=newStudent;
printMessage("输入学号,姓名:");
if(cin>>psNew->sNo>>psNew->sName)
...{
if(!checkSno(pslh,psNew->sNo))
...{
psNew->roomNo=roomNo;
psNew->next=NULL;
PStudentpst=pslh;
while(pst->next)
pst=pst->next;
pst->next=psNew;
for(inti=0;i<STU_NUM_EACH_ROOM;++i)
if(prs[roomNo-1].sNo[i]==0)
...{
prs[roomNo-1].sNo[i]=psNew->sNo;
break;
}
prs[roomNo-1].sNum++;
pslh->sNo++;//保存总人数
cout<<"入住手续办完,旅途愉快,您的房间是:"<<roomNo<<endl;
}
else
...{
printMessage("该生学号不正确! ");
deletepsNew;
}
}
else
...{
cin.clear();
deletepsNew;
}
}
else
...{
printMessage("对不起,客满 ");
}
}
voidresideOut(PStudent&pslh,PRoom&prs)
...{
printMessage("输入退房者学号:");
intsno;
cin>>sno;
PStudentpst=checkSno(pslh,sno);
if(pst)
...{
PStudentps=pslh;
while(ps->next!=pst)
ps=ps->next;
ps->next=pst->next;
introomNo=pst->roomNo;
for(inti=0;i<STU_NUM_EACH_ROOM;++i)
if(prs[roomNo-1].sNo[i]==pst->sNo)
...{
prs[roomNo-1].sNo[i]=0;
break;
}
prs[roomNo-1].sNum--;
pslh->sNo--;
deletepst;
printMessage("退房成功! ");
}
else
...{
printMessage("该生不存在! ");
}
}
voidqueryByNo(PStudent&pslh,PRoom&prs)
...{
printMessage("输入学号:");
intsno;
cin>>sno;
PStudentpst=checkSno(pslh,sno);
cout<<"查询结果:"<<endl;
if(pst)
...{
cout<<"学号:"<<sno<<endl;
cout<<"姓名:"<<pst->sName<<endl;
cout<<"房间:"<<pst->roomNo<<endl;
cout<<"==============="<<endl;
}
else
...{
printMessage("该生不存在! ");
}
}
voidqueryByName(PStudent&pslh,PRoom&prs)
...{
printMessage("输入姓名:");
charsname[20];
cin>>sname;
boolfound=false;
cout<<"查询结果:"<<endl;
for(PStudentps=pslh->next;ps;ps=ps->next)
...{
if(strcmp(ps->sName,sname)==0)
...{
cout<<"学号:"<<ps->sNo<<endl;
cout<<"姓名:"<<ps->sName<<endl;
cout<<"房间:"<<ps->roomNo<<endl;
cout<<"==============="<<endl;
found=true;
}
}
if(!found)
printMessage("该生不存在! ");
}
voidqueryByRoomNo(PStudent&pslh,PRoom&prs)
...{
printMessage("输入房间号:");
introomNo;
cin>>roomNo;
cout<<"查询结果:"<<endl;
if(roomNo>=ROOM_NUM)
...{
printMessage("无此房间! ");
}
else
...{
cout<<"房间号:"<<roomNo<<endl;
cout<<"学生数:"<<prs[roomNo-1].sNum<<endl;
for(inti=0;i<STU_NUM_EACH_ROOM;++i)
if(prs[roomNo-1].sNo[i]!=0)
...{
cout<<"学号:"<<prs[roomNo-1].sNo[i]<<endl;
}
}
}
voidqueryAll(PStudent&pslh,PRoom&prs)
...{
boolfound=false;
cout<<"查询结果:"<<endl;
for(PStudentps=pslh->next;ps;ps=ps->next)
...{
cout<<"学号:"<<ps->sNo<<endl;
cout<<"姓名:"<<ps->sName<<endl;
cout<<"房间:"<<ps->roomNo<<endl;
cout<<"==============="<<endl;
found=true;
}
if(!found)
printMessage("无记录 ");
}
voidquery(PStudent&pslh,PRoom&prs)
...{
intqueryBy;
printMessage("选择查询方式:");
cin.clear();
while(cin>>queryBy)
...{
if(queryBy==BACK)
break;
switch(queryBy)
...{
caseBYNO:
queryByNo(pslh,prs);
break;
caseBYNAME:
queryByName(pslh,prs);
break;
caseBYROOMNO:
queryByRoomNo(pslh,prs);
break;
caseALL:
queryAll(pslh,prs);
break;
default:
printMessage("选择方式错误,请重新选择! ");
break;
}
printMessage("选择查询方式:");
}
cin.clear();
}
intmain()
...{
PRoompRooms=NULL;
makeRooms(pRooms);
PStudentpStuLinkHeader=newStudent;
pStuLinkHeader->sNo=0;
pStuLinkHeader->next=NULL;
intop;
printMessage("1.入住 ");
printMessage("2.退房 ");
printMessage("3.查询 ");
printMessage("4.退出程序 ");
printMessage("选择服务:");
while(cin>>op)
...{
if(op==QUIT)
break;
switch(op)
...{
caseRESIDEIN:
resideIn(pStuLinkHeader,pRooms);
break;
caseOUT:
resideOut(pStuLinkHeader,pRooms);
break;
caseQUERY:
printMessage("1.按学号 ");
printMessage("2.按名字 ");
printMessage("3.按房间号 ");
printMessage("4.列出所有 ");
printMessage("5.返回上层 ");
query(pStuLinkHeader,pRooms);
break;
default:
printMessage("选择错误,请重新选择! ");
break;
}
printMessage("选择服务:");
cin.clear();
}
return0;
}
本文介绍了一个简单的宿舍管理系统的设计与实现。系统采用C++语言,利用结构体和枚举等基本数据类型来管理学生住宿信息,包括入住、退房及查询等功能。
1407

被折叠的 条评论
为什么被折叠?



