学生宿舍管理系统测试文档
推荐回答
我这边有两个!希望可以帮到你! 第一个VC #include "stdafx.h" #include "stdio.h" #include "iostream.h" #include "string.h" #define ERROR 0 #define OK 1 #define MAXSIZE 2500 #define EQ(a,b) ((a)==(b)) typedef int Status; int N=4;//全局变量来储存所输入的个数; struct student { //数据结构类型; char name[10]; int num; int room; }; /*--------------------------------------------------------------------------*/ Status Input() { //数据输入; int i=0; int flag=1; char ch; FILE * fpdata; student stu[MAXSIZE]; if((fpdata=fopen("studata.txt","wb"))==NULL) { printf("cannot open studata.c file!"); return ERROR; } 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(struct student),1,fpdata); i++; } else flag=0; } N=i; fclose(fpdata); printf("数据输入完成!\n"); return OK; } Status Print(char *filename) { //输出数据; FILE * fpdata; student st; int i=0; if((fpdata=fopen(filename,"rb"))==NULL) { printf("cannot open studata.c file!"); return ERROR; } while(i<N) { fread(&st,sizeof(struct student),1,fpdata); printf("%s %d %d\n",st.name,st.num,st.room); i++; } fclose(fpdata); return OK; } /* ******** ******** ********* ********* ********* ********* ********* */ /* ******** ******** ********* ********* ********* ********* ********* */ Status NameSort() { //姓名排序; int i,j; FILE * fpdata; FILE * fpnamesort; student stu[MAXSIZE]; if((fpdata=fopen("studata.txt","rb"))==NULL) { printf("cannot open data.c file!"); return ERROR; } for(i=1;i<=N;i++) { fread(&stu[i],sizeof(struct student),1,fpdata); } fclose(fpdata); if((fpnamesort=fopen("namesort.txt","wb"))==NULL) { printf("cannot open nsort.c"); return ERROR; } 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(struct student),1,fpnamesort); } fclose(fpnamesort); return OK; } /* ******** ******** ********* ********* ********* ********* ********* */ /* ******** ******** ********* ********* ********* ********* ********* */ Status NumSort() { //学号排序; int i,j; FILE * fpdata; FILE * fpnumsort; student stu[MAXSIZE]; if((fpdata=fopen("studata.txt","rb"))==NULL) { printf("cannot open data.c file!"); return ERROR; } for(i=1;i<=N;i++) { fread(&stu[i],sizeof(struct student),1,fpdata); } fclose(fpdata); if((fpnumsort=fopen("numsort.txt","wb"))==NULL) { printf("cannot open nsort.c"); return ERROR; } for(i=2;i<=N;i++) { if(stu[i].num<stu[i-1].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<stu[j].num;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(struct student),1,fpnumsort); } fclose(fpnumsort); return OK; } /* ******** ******** ********* ********* ********* ********* ********* */ /* ******** ******** ********* ********* ********* ********* ********* */ Status RoomSort() { //房间号排序; int i,j; FILE * fpdata; FILE * fproomsort; student stu[MAXSIZE]; if((fpdata=fopen("studata.txt","rb"))==NULL) { printf("cannot open data.c file!"); return ERROR; } for(i=1;i<=N;i++) { fread(&stu[i],sizeof(struct student),1,fpdata); } fclose(fpdata); if((fproomsort=fopen("roomsort.txt","wb"))==NULL) { printf("cannot open nsort.c"); return ERROR; } for(i=2;i<=N;i++) { if(stu[i].room<stu[i-1].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<stu[j].room;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(struct student),1,fproomsort); } fclose(fproomsort); return OK; } /* ******** ******** ********* ********* ********* ********* ********* */ /* ******** ******** ********* ********* ********* ********* ********* */ Status NameSearch(char * namekey) { //按名字查询; //顺序查找 int i; FILE * f; student stu[MAXSIZE]; if((f=fopen("namesort.txt","rb"))==NULL) { printf("cannot open namesort.txt file!"); return ERROR; } for(i=1;i<=N;i++) { fread(&stu[i],sizeof(struct student),1,f); } int low=1; int high=N; //int mid; int flag=0; int m=N; //char namekey; //int t; 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); } return m; fclose(f); if(flag>0) { printf("%s %d %d\n",stu[flag].name,stu[flag].num,stu[flag].room); } else printf("对不起,没有找到相匹配的记录\n"); return OK; } /* ******** ******** ********* ********* ********* ********* ********* */ /* ******** ******** ********* ********* ********* ********* ********* */ Status NumSearch(int key) { //按学号查询; int i; FILE * f; student stu[MAXSIZE]; if((f=fopen("numsort.txt","rb"))==NULL) { printf("cannot open namesort.txt file!"); return ERROR; } for(i=1;i<=N;i++) { fread(&stu[i],sizeof(struct student),1,f); } int low=1; int high=N; int mid; int flag=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); } else printf("对不起,没有找到相匹配的记录\n"); return OK; } /* ******** ******** ********* ********* ********* ********* ********* */ /* ******** ******** ********* ********* ********* ********* ********* */ Status RoomSearch(int key) { //按房间号查询; int i; FILE * f; student stu[MAXSIZE]; if((f=fopen("roomsort.txt","rb"))==NULL) { printf("cannot open roomsort.txt file!"); return ERROR; } for(i=1;i<=N;i++) { fread(&stu[i],sizeof(struct student),1,f); } int low=1; int high=N; //int mid; int flag=0; int t; 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); } return t; fclose(f); if(flag>0) { printf("%s %d %d\n",stu[flag].name,stu[flag].num,stu[flag].room); } else printf("对不起,没有找到相匹配的记录\n"); return OK; } /* ******** ******** ********* ********* ********* ********* ********* */ /* ******** ******** ********* main() ********* ********* ********* */ /* ******** ******** ********* ********* ********* ********* ********* */ void main() { int num; int room; int flag,flag1,flag2; char cha; char name[10]; cout<<" ******** 学生宿舍管理系统 ********"<<endl; printf(" ------Powered by GUOXIAOJIE\n"); printf("------------------------------------------------------\n"); cout<<"是否需要录入入住学生信息(Y/N)"<<endl; char cha1; cin>>cha1; if(cha1=='Y'||cha1=='y') {printf("------------------------------------------------------\n"); printf("请将住宿学生信息输入(姓名 学号 房间号)\n"); Input(); NameSort(); NumSort(); RoomSort(); } while(flag) {printf("------------------------------------------------------\n"); cout<<"**************** 学生宿舍管理系统 ****************"<<endl; printf("------------------------------------------------------\n"); printf("请选择操作:\n"); printf("A.按姓名查询\n"); printf("B.按学号查询\n"); printf("C.按房号查询\n"); printf("D.退出\n"); cout<<"注意:字母请输入大写……"<<endl; 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("------------------------------------------------------\n"); RoomSearch(room); printf("------------------------------------------------------\n"); break; case 'B': Print("roomsort.txt"); break; case 'C': flag1=0; break; default: printf("------------------------------------------------------\n"); printf("!!!!!!!!你输入的命令无效。注意:命令字母请输入大写!!!!!!!!!!!\n"); } } break; case 'D' : flag=0; break; default: printf("------------------------------------------------------\n"); printf("!!!!!!!!你输入的命令无效。注意:命令字母请输入大写!!!!!!!!!!!\n"); } } } 第二个C++ #include<iostream.h> #include<string.h> class student//学生类 { public: char student_name[10]; int room_number; int student_number; int student_sc; }; student student_info[16]; int number=0; /////////////////////////////////////////////////////////////////////// void input()//输入函数 { cout<<"输入学生人数:"; cin>>number; if(number<=16) { for(int i=0;i<number;i++) { cout<<"输入学生学号:"; cin>>student_info[i].student_number; cout<<"输入学生姓名:"; cin>>student_info[i].student_name; student_info[i].student_sc=i+1; cout<<"输入房间号:"; cin>>student_info[i].room_number; } cout<<"输入完毕。"<<endl; } else{cout<<"人数超过容纳上限。"<<endl;} } ///////////////////////////////////////////////////////////////////////////// void show()//显示函数 { cout<<"姓名"<<" "<<"学号"<<" "<<"序号"<<" "<<"房间"<<endl; for(int i=0;i<number;i++) { cout<<student_info[i].student_name<<" "; cout<<student_info[i].student_number<<" "; cout<<student_info[i].student_sc<<" "; cout<<student_info[i].room_number<<endl; } } ////////////////////////////////////////////////////////////////////////// void del()//删除函数 { cout<<"输入要删除的学生号:"<<endl; int temp=0; cin>>temp; for(int i=0;i<number;i++) { if(student_info[i].student_number==temp) { for(int j=i;(j+1)<number;j++) { student_info[j].student_number=student_info[j+1].student_number; student_info[j].student_sc=student_info[j+1].student_sc; student_info[j].room_number=student_info[j+1].room_number; strcpy(student_info[j].student_name,student_info[j+1].student_name); } student_info[number].student_number=0; student_info[number].student_sc=0; student_info[number].room_number=0; strcpy(student_info[number].student_name,""); } } } //////////////////////////////////////////////////////////////////////////// void change()//交换函数 { cout<<"输入要交换的学生号:"; char temp_name[10]; int temp_number=0; int temp_sc=0; int temp_room=0; int i,j; cin>>i>>j; i--; j--; temp_number=student_info[j].student_number; temp_sc=student_info[j].student_sc; temp_room=student_info[j].room_number; strcpy(temp_name,student_info[j].student_name); student_info[j].student_number=student_info[i].student_number; student_info[j].student_sc=student_info[i].student_sc; student_info[j].room_number=student_info[i].room_number; strcpy(student_info[j].student_name,student_info[i].student_name); student_info[i].student_number= temp_number; student_info[i].student_sc=temp_sc; student_info[i].room_number=temp_room; strcpy(student_info[i].student_name,temp_name); } ////////////////////////////////////////////////////////////////////////// void search_sc()//查找序号 { int temp=0; cout<<"输入要查询的序号:"; cin>>temp; int i=0; while(student_info[i].student_sc!=(temp)) i++; cout<<student_info[i].student_name<<" "; cout<<student_info[i].student_number<<" "; cout<<student_info[i].student_sc<<" "; cout<<student_info[i].room_number<<endl; } ////////////////////////////////////////////////////////////////////////// void search_name()//查找姓名 { char temp[10]; cout<<"输入要查询的姓名:"; cin>>temp; int i=0; while(strcmp(student_info[i].student_name,temp)) i++; cout<<student_info[i].student_name<<" "; cout<<student_info[i].student_number<<" "; cout<<student_info[i].student_sc<<" "; cout<<student_info[i].room_number<<endl; } ///////////////////////////////////////////////////////////////////////// void search_number()//查找学号 { int temp=0; cout<<"输入要查询的学号:"; cin>>temp; int i=0; while(student_info[i].student_number!=(temp)) i++; cout<<student_info[i].student_name<<" "; cout<<student_info[i].student_number<<" "; cout<<student_info[i].student_sc<<" "; cout<<student_info[i].room_number<<endl; } ////////////////////////////////////////////////////////////////////////// void win()//界面函数 { while(1) { cout<<endl<<"系统菜单:" <<endl; cout<<"1、修改子菜单"<<endl; cout<<"2、查询子菜单" <<endl; int server=0; cout<<"输入子菜单类型:" ; cin>>server; if(server==1) { cout<<"1、输入:"<<endl; cout<<"2、删除"<<endl; cout<<"3、交换"<<endl; cout<<"4、显示所有"<<endl; cout<<"5、结束"<<endl; cout<<"选择子菜单:"; int temp=0; cin>>temp; switch(temp) { case 1: input();break; case 2: del();break; case 3: change();break; case 4: show();break; case 5: exit(0); } } else if(server==2) { cout<<"1、按姓名查询"<<endl; cout<<"2、按学号查询"<<endl; cout<<"3、按序号查询"<<endl; cout<<"选择子菜单:"; int temp; cin>>temp; switch(temp) { case 1: search_name();break; case 2: search_number();break; case 3: search_sc();break; } } } } ///////////////////////////////////////////////////////////////////////// void main()//主函数 { win(); }
以上问题属网友观点,不代表本站立场,仅供参考!