赞
踩
任务:为宿舍管理人员编写一个宿舍管理查询软件, 程序设计要求:
(1)采用交互工作方式
(2)可以增加、删除、修改信息
(3)建立数据文件 ,数据文件按关键字(姓名、学号、房号)进行排序(选择、快速排序、堆排序等任选一种)
(4)查询 : a.按姓名查询 ;b.按学号查询 ;c按房号查询
(5)打印任一查询结果(可以连续操作)
- #include <iostream>
- #include<string>
- #include<fstream>
- #include<ostream>
- #include<stdlib.h>
- using namespace std;
- #define Max 200
- typedef struct
- {
- int no;
- string name;
- int room;
- }student;
- student stu[Max];
- int n=0;
- void creat()
- {
- for(int i=0;i<n;i++)
- {
- cout<<"输入第"<<i+1<<"位同学学号:";
- cin>>stu[i].no;
- cout<<"输入第"<<i+1<<"位同学姓名:";
- cin>>stu[i].name;
- cout<<"输入第"<<i+1<<"位同学房号:";
- cin>>stu[i].room;
- cout<<"************************"<<endl;
- }
- cout<<"创建完毕!"<<endl;
- }
- void read()
- {
- ifstream ofile;
- int no1;
- string name1;
- int room1;
- ofile.open("student.txt",ios::in);
- if(ofile.is_open())
- {
- for(int i=0;i<n;i++)
- {
- ofile>>no1>>name1>>room1;
- stu[i].no=no1;
- stu[i].name=name1;
- stu[i].room=room1;
- }
- }
- ofile.close();
- }
- void write()
- {
- ofstream ifile("student.txt");
- for(int i=0;i<n;i++)
- {
- ifile<<stu[i].no<<" ";
- ifile<<stu[i].name<<" ";
- ifile<<stu[i].room<<endl;
- }
- }
- void add()
- {
- n++;
- student stu1;
- cout<<"输入新增同学学号:";
- cin>>stu1.no;
- cout<<"输入新增同学姓名:";
- cin>>stu1.name;
- cout<<"输入新增同学房号:";
- cin>>stu1.room;
- stu[n-1]=stu1;
- }
- void del()
- {
- int no1;
- cout<<"请输入所删除学生编号" ;
- cin>>no1;
- for(int i=0;i<n;i++)
- {
- if(no1==stu[i].no)
- {
- for(int k=i;k<n;k++)
- stu[k]=stu[k+1];
- break;
- }
- }
- n--;
- }
- void change()
- {
- int no1,i;
- cout<<"请输入所修改学生编号" ;
- cin>>no1;
- for( i=0;i<n;i++)
- {
- if(no1==stu[i].no)
- {
- break;
- }
- }
- int cho;
- string name1;
- int room1;
- cout<<"1.修改姓名"<<endl;
- cout<<"2.修改房号"<<endl;
- cout<<"输入你的选择:";
- cin>>cho;
- if(cho==1)
- {
- cout<<"请输入修改姓名:";
- cin>>name1;
- stu[i].name=name1;
- }
- if(cho==2)
- {
- cout<<"请输入修改姓名:";
- cin>>room1;
- stu[i].room=room1;
- }
- }
- void sort()
- {
- student t;
- int i,j,k;
- for( i=0;i<n-1;i++)
- {
- k=i;
- for(j=i+1;j<n;j++)
- if(stu[j].no<stu[k].no)
- k=j;
- if(k!=i)
- {
- t=stu[i];
- stu[i]=stu[k];
- stu[k]=t;
- }
- }
- }
- void search()
- {
- int cho;
- string name1;
- int no1;
- int room1;
- bool flag;
- cout<<"1.按学号查询"<<endl;
- cout<<"2.按姓名查询"<<endl;
- cout<<"3.按房号查询"<<endl;
- cout<<"输入你的选择:";
- cin>>cho;
- if(cho==1)
- {
- flag=false;
- sort();
- cout<<"输入查询学号:";
- cin>>no1;
- int low=0,high=n-1,mid;
- while(low<=high)
- {
- mid=(low+high)/2;
- if(no1==stu[mid].no)
- {
- cout<<"姓名:"<<stu[mid].name<<endl;
- cout<<"学号:"<<stu[mid].no<<endl;
- cout<<"房号:"<<stu[mid].room<<endl;
- flag=true;
- break;
- }
- else if(no1<stu[mid].no)
- high=mid-1;
- else
- low=mid+1;
- }
- if(!flag)
- {
- cout<<"查无此人"<<endl;
- }
- }
- if(cho==2)
- {
- flag=false;
- cout<<"输入查询姓名:";
- cin>>name1;
- for(int i=0;i<n;i++)
- {
- if(name1==stu[i].name)
- {
- cout<<"姓名:"<<stu[i].name<<endl;
- cout<<"学号:"<<stu[i].no<<endl;
- cout<<"房号:"<<stu[i].room<<endl;
- flag=true;
- break;
- }
- }
- if(!flag)
- {
- cout<<"查无此人"<<endl;
- }
- }
- if(cho==3)
- {
- flag=false;
- cout<<"输入查询房号:";
- cin>>room1;
- for(int i=0;i<n;i++)
- {
- if(room1==stu[i].room)
- {
- cout<<"姓名:"<<stu[i].name<<endl;
- cout<<"学号:"<<stu[i].no<<endl;
- cout<<"房号:"<<stu[i].room<<endl;
- flag=true;
- break;
- }
- }
- if(!flag)
- {
- cout<<"查无此人"<<endl;
- }
- }
- }
- void print()
- {
- cout<<"学号***姓名***房号"<<endl;
- for(int i=0;i<n;i++)
- cout<<stu[i].no<<" "<<stu[i].name<<" "<<stu[i].room<<endl;
- }
- void menu()
- {
- cout<<"*******欢迎使用宿舍管理系统*******"<<endl;
- cout<<"************1.读取文件************"<<endl;
- cout<<"************2.增加信息************"<<endl;
- cout<<"************3.删除信息************"<<endl;
- cout<<"************4.修改信息************"<<endl;
- cout<<"************5.排序信息************"<<endl;
- cout<<"************6.查询信息************"<<endl;
- cout<<"************7.保存文件************"<<endl;
- cout<<"************0.退出系统************"<<endl;
- }
- int main()
- {
- int len=0;
- string str;
- ifstream file("student.txt");
- while(file)
- {
- getline(file,str);
- if(str.length()>4)
- len++;
- }
- n=len;
- menu();
- int ch;
- do
- {
- cin>>ch;
- switch(ch)
- {
- case 1:read();print();system("pause");system("cls");break;
- case 2:add();print();system("pause");system("cls");break;
- case 3:del();print();system("pause");system("cls");break;
- case 4:change();print();system("pause");system("cls");break;
- case 5:sort();print();system("pause");system("cls");break;
- case 6:search();system("pause");system("cls");break;
- case 7:write();print();system("pause");system("cls");break;
- case 0:cout<<"退出系统,感谢使用!"<<endl;break;
- default:cout<<"输入错误!"<<endl;
- }
- menu();
- }while(ch!=0);
-
- return 0;
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。