学生宿舍管理系统.docx
- 文档编号:7037100
- 上传时间:2023-01-16
- 格式:DOCX
- 页数:17
- 大小:16.46KB
学生宿舍管理系统.docx
《学生宿舍管理系统.docx》由会员分享,可在线阅读,更多相关《学生宿舍管理系统.docx(17页珍藏版)》请在冰豆网上搜索。
学生宿舍管理系统
#include"stdio.h"
#include"iostream.h"
#include"string.h"
#defineERROR0
#defineOK1
#defineMAXSIZE2500
#defineEQ(a,b)((a)==(b))
typedefintStatus;
intN=4;//全局变量来储存所输入的个数;
structstudent
{
//数据结构类型;
charname[10];
intnum;
introom;
};
/*--------------------------------------------------------------------------*/
StatusInput()
{
//数据输入;
inti=0;
intflag=1;
charch;
FILE*fpdata;
studentstu[MAXSIZE];
if((fpdata=fopen("studata.txt","wb"))==NULL)
{
printf("cannotopenstudata.cfile!
");
returnERROR;
}
while(flag)
{
printf("是否继续输入(Y/N):
");
cin>>ch;
if(ch=='y'||ch=='Y')
{
scanf("%s%d%d",stu[i].name,&stu[i].num,&stu[i].room);
fwrite(&stu[i],sizeof(structstudent),1,fpdata);
i++;
}
elseflag=0;
}
N=i;
fclose(fpdata);
printf("数据输入完成!
\n");
returnOK;
}
StatusPrint(char*filename)
{
//输出数据;
FILE*fpdata;
studentst;
inti=0;
if((fpdata=fopen(filename,"rb"))==NULL)
{
printf("cannotopenstudata.cfile!
");
returnERROR;
}
while(i { fread(&st,sizeof(structstudent),1,fpdata); printf("%s%d%d\n",st.name,st.num,st.room); i++; } fclose(fpdata); returnOK; } /***************************************************************/ /***************************************************************/ StatusNameSort() { //姓名排序; inti,j; FILE*fpdata; FILE*fpnamesort; studentstu[MAXSIZE]; if((fpdata=fopen("studata.txt","rb"))==NULL) { printf("cannotopendata.cfile! "); returnERROR; } for(i=1;i<=N;i++) { fread(&stu[i],sizeof(structstudent),1,fpdata); } fclose(fpdata); if((fpnamesort=fopen("namesort.txt","wb"))==NULL) { printf("cannotopennsort.c"); returnERROR; } for(i=2;i<=N;i++) { if(strcmp(stu[i].name,stu[i-1].name)<0) { strcpy(stu[0].name,stu[i].name); stu[0].num=stu[i].num; stu[0].room=stu[i].room; strcpy(stu[i].name,stu[i-1].name); stu[i].num=stu[i-1].num; stu[i].room=stu[i-1].room; for(j=i-2;(strcmp(stu[0].name,stu[j].name))<0;j--) { strcpy(stu[j+1].name,stu[j].name); stu[j+1].num=stu[j].num; stu[j+1].room=stu[j].room; } strcpy(stu[j+1].name,stu[0].name); stu[j+1].num=stu[0].num; stu[j+1].room=stu[0].room; } } for(i=1;i<=N;i++) { fwrite(&stu[i],sizeof(structstudent),1,fpnamesort); } fclose(fpnamesort); returnOK; } /***************************************************************/ /***************************************************************/ StatusNumSort() { //学号排序; inti,j; FILE*fpdata; FILE*fpnumsort; studentstu[MAXSIZE]; if((fpdata=fopen("studata.txt","rb"))==NULL) { printf("cannotopendata.cfile! "); returnERROR; } for(i=1;i<=N;i++) { fread(&stu[i],sizeof(structstudent),1,fpdata); } fclose(fpdata); if((fpnumsort=fopen("numsort.txt","wb"))==NULL) { printf("cannotopennsort.c"); returnERROR; } for(i=2;i<=N;i++) { if(stu[i].num { strcpy(stu[0].name,stu[i].name); stu[0].num=stu[i].num; stu[0].room=stu[i].room; strcpy(stu[i].name,stu[i-1].name); stu[i].num=stu[i-1].num; stu[i].room=stu[i-1].room; for(j=i-2;stu[0].num { strcpy(stu[j+1].name,stu[j].name); stu[j+1].num=stu[j].num; stu[j+1].room=stu[j].room; } strcpy(stu[j+1].name,stu[0].name); stu[j+1].num=stu[0].num; stu[j+1].room=stu[0].room; } } for(i=1;i<=N;i++) { fwrite(&stu[i],sizeof(structstudent),1,fpnumsort); } fclose(fpnumsort); returnOK; } /***************************************************************/ /***************************************************************/ StatusRoomSort() { //房间号排序; inti,j; FILE*fpdata; FILE*fproomsort; studentstu[MAXSIZE]; if((fpdata=fopen("studata.txt","rb"))==NULL) { printf("cannotopendata.cfile! "); returnERROR; } for(i=1;i<=N;i++) { fread(&stu[i],sizeof(structstudent),1,fpdata); } fclose(fpdata); if((fproomsort=fopen("roomsort.txt","wb"))==NULL) { printf("cannotopennsort.c"); returnERROR; } for(i=2;i<=N;i++) { if(stu[i].room { strcpy(stu[0].name,stu[i].name); stu[0].num=stu[i].num; stu[0].room=stu[i].room; strcpy(stu[i].name,stu[i-1].name); stu[i].num=stu[i-1].num; stu[i].room=stu[i-1].room; for(j=i-2;stu[0].room { strcpy(stu[j+1].name,stu[j].name); stu[j+1].num=stu[j].num; stu[j+1].room=stu[j].room; } strcpy(stu[j+1].name,stu[0].name); stu[j+1].num=stu[0].num; stu[j+1].room=stu[0].room; } } for(i=1;i<=N;i++) { fwrite(&stu[i],sizeof(structstudent),1,fproomsort); } fclose(fproomsort); returnOK; } /***************************************************************/ /***************************************************************/ StatusNameSearch(char*namekey) { //按名字查询; //顺序查找 inti; FILE*f; studentstu[MAXSIZE]; if((f=fopen("namesort.txt","rb"))==NULL) { printf("cannotopennamesort.txtfile! "); returnERROR; } for(i=1;i<=N;i++) { fread(&stu[i],sizeof(structstudent),1,f); } intlow=1; inthigh=N; //intmid; intflag=0; intm=N; //charnamekey; //intt; strcpy(stu[0].name,namekey); for(m=N;m>0;--m) { if(! strcmp(stu[m].name,namekey)) printf("%s%d%d\n",stu[m].name,stu[m].num,stu[m].room); } returnm; fclose(f); if(flag>0) { printf("%s%d%d\n",stu[flag].name,stu[flag].num,stu[flag].room); } elseprintf("对不起,没有找到相匹配的记录\n"); returnOK; } /***************************************************************/ /***************************************************************/ StatusNumSearch(intkey) { //按学号查询; inti; FILE*f; studentstu[MAXSIZE]; if((f=fopen("numsort.txt","rb"))==NULL) { printf("cannotopennamesort.txtfile! "); returnERROR; } for(i=1;i<=N;i++) { fread(&stu[i],sizeof(structstudent),1,f); } intlow=1; inthigh=N; intmid; intflag=0; while(low<=high) { mid=(low+high)/2; if(stu[mid].num==key) { flag=mid; break; } else { if(stu[mid].num>key) high=mid-1; else low=mid+1; } } fclose(f); if(flag>0) { printf("%s%d%d\n",stu[flag].name,stu[flag].num,stu[flag].room); } elseprintf("对不起,没有找到相匹配的记录\n"); returnOK; } /***************************************************************/ /***************************************************************/ StatusRoomSearch(intkey) { //按房间号查询; inti; FILE*f; studentstu[MAXSIZE]; if((f=fopen("roomsort.txt","rb"))==NULL) { printf("cannotopenroomsort.txtfile! "); returnERROR; } for(i=1;i<=N;i++) { fread(&stu[i],sizeof(structstudent),1,f); } intlow=1; inthigh=N; //intmid; intflag=0; intt; stu[0].room=key; for(t=N;t>0;--t) {if(stu[t].room==key) printf("%s%d%d\n",stu[t].name,stu[t].num,stu[t].room); } returnt; fclose(f); if(flag>0) { printf("%s%d%d\n",stu[flag].name,stu[flag].num,stu[flag].room); } elseprintf("对不起,没有找到相匹配的记录\n"); returnOK; } /***************************************************************/ /**************************main()****************************/ /***************************************************************/ voidmain() { intnum; introom; intflag,flag1,flag2; charcha; charname[10]; cout<<"********学生宿舍管理系统********"< printf("------PoweredbyGUOXIAOJIE\n"); printf("------------------------------------------------------\n"); cout<<"是否需要录入入住学生信息(Y/N)"< charcha1; cin>>cha1; if(cha1=='Y'||cha1=='y') {printf("------------------------------------------------------\n"); printf("请将住宿学生信息输入(姓名学号房间号)\n"); Input(); NameSort(); NumSort(); RoomSort(); } while(flag) {printf("------------------------------------------------------\n"); cout<<"****************学生宿舍管理系统****************"< printf("------------------------------------------------------\n"); printf("请选择操作: \n"); printf("A.按姓名查询\n"); printf("B.按学号查询\n"); printf("C.按房号查询\n"); printf("D.退出\n"); cout<<"注意: 字母请输入大写……"< cin>>cha; switch(cha) { case'A': flag1=1; while(flag1) {printf("------------------------------------------------------\n"); printf("A.输入要查询的名字\n"); printf("B.按姓名全部输出\n"); printf("C.返回\n"); printf("注意: 字母请输入大写……\n"); cin>>cha; switch(cha) { case'A': scanf("%s",name); printf("查询结果为: \n"); printf("------------------------------------------------------\n"); NameSearch(name); printf("------------------------------------------------------\n"); break; case'B': Print("namesort.txt"); break; case'C': flag1=0; break; default: printf("------------------------------------------------------\n"); printf("! ! ! ! ! ! ! ! 你输入的命令无效。 注意: 命令字母请输入大写! ! ! ! ! ! ! ! ! ! ! \n"); } } break; case'B': while(flag2) {printf("------------------------------------------------------\n"); printf("A.输入要查询的学号\n"); printf("B.按学号全部输出\n"); printf("C.返回\n"); printf("注意: 字母请输入大写……\n"); cin>>cha; switch(cha) { case'A': scanf("%d",&num); printf("查询结果为: \n"); printf("------------------------------------------------------\n"); NumSearch(num); break; case'B': Print("numsort.txt"); break; case'C': flag2=0; break; default: printf("------------------------------------------------------\n"); printf("! ! ! ! ! ! ! ! 你输入的命令无效。 注意: 命令字母请输入大写! ! ! ! ! ! ! ! ! ! ! \n"); } } break; case'C': flag1=1; while(flag1) {printf("------------------------------------------------------\n"); printf("A.输入要查询的宿舍号\n"); printf("B.按宿舍号全部输出\n"); printf("C.返回\n"); printf("注意: 字母请输入大写……\n"); cin>>cha; switch(cha) { case'A': scanf("%d",&room); printf("查询结果为: \n"); printf("-----------------------------------------------
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 学生宿舍 管理 系统