当前位置:   article > 正文

宿舍管理查询软件_宿舍管理查询软件 【要求】(1)按照指定值(如学号、 姓名、 房号等)进行排序(冒泡

宿舍管理查询软件 【要求】(1)按照指定值(如学号、 姓名、 房号等)进行排序(冒泡

任务:为宿舍管理人员编写一个宿舍管理查询软件, 程序设计要求:

(1)采用交互工作方式

(2)可以增加、删除、修改信息

(3)建立数据文件 ,数据文件按关键字(姓名、学号、房号)进行排序(选择、快速排序、堆排序等任选一种)

(4)查询  : a.按姓名查询 ;b.按学号查询 ;c按房号查询

(5)打印任一查询结果(可以连续操作)

  1. #include <iostream>
  2. #include<string>
  3. #include<fstream>
  4. #include<ostream>
  5. #include<stdlib.h>
  6. using namespace std;
  7. #define Max 200
  8. typedef struct
  9. {
  10. int no;
  11. string name;
  12. int room;
  13. }student;
  14. student stu[Max];
  15. int n=0;
  16. void creat()
  17. {
  18. for(int i=0;i<n;i++)
  19. {
  20. cout<<"输入第"<<i+1<<"位同学学号:";
  21. cin>>stu[i].no;
  22. cout<<"输入第"<<i+1<<"位同学姓名:";
  23. cin>>stu[i].name;
  24. cout<<"输入第"<<i+1<<"位同学房号:";
  25. cin>>stu[i].room;
  26. cout<<"************************"<<endl;
  27. }
  28. cout<<"创建完毕!"<<endl;
  29. }
  30. void read()
  31. {
  32. ifstream ofile;
  33. int no1;
  34. string name1;
  35. int room1;
  36. ofile.open("student.txt",ios::in);
  37. if(ofile.is_open())
  38. {
  39. for(int i=0;i<n;i++)
  40. {
  41. ofile>>no1>>name1>>room1;
  42. stu[i].no=no1;
  43. stu[i].name=name1;
  44. stu[i].room=room1;
  45. }
  46. }
  47. ofile.close();
  48. }
  49. void write()
  50. {
  51. ofstream ifile("student.txt");
  52. for(int i=0;i<n;i++)
  53. {
  54. ifile<<stu[i].no<<" ";
  55. ifile<<stu[i].name<<" ";
  56. ifile<<stu[i].room<<endl;
  57. }
  58. }
  59. void add()
  60. {
  61. n++;
  62. student stu1;
  63. cout<<"输入新增同学学号:";
  64. cin>>stu1.no;
  65. cout<<"输入新增同学姓名:";
  66. cin>>stu1.name;
  67. cout<<"输入新增同学房号:";
  68. cin>>stu1.room;
  69. stu[n-1]=stu1;
  70. }
  71. void del()
  72. {
  73. int no1;
  74. cout<<"请输入所删除学生编号" ;
  75. cin>>no1;
  76. for(int i=0;i<n;i++)
  77. {
  78. if(no1==stu[i].no)
  79. {
  80. for(int k=i;k<n;k++)
  81. stu[k]=stu[k+1];
  82. break;
  83. }
  84. }
  85. n--;
  86. }
  87. void change()
  88. {
  89. int no1,i;
  90. cout<<"请输入所修改学生编号" ;
  91. cin>>no1;
  92. for( i=0;i<n;i++)
  93. {
  94. if(no1==stu[i].no)
  95. {
  96. break;
  97. }
  98. }
  99. int cho;
  100. string name1;
  101. int room1;
  102. cout<<"1.修改姓名"<<endl;
  103. cout<<"2.修改房号"<<endl;
  104. cout<<"输入你的选择:";
  105. cin>>cho;
  106. if(cho==1)
  107. {
  108. cout<<"请输入修改姓名:";
  109. cin>>name1;
  110. stu[i].name=name1;
  111. }
  112. if(cho==2)
  113. {
  114. cout<<"请输入修改姓名:";
  115. cin>>room1;
  116. stu[i].room=room1;
  117. }
  118. }
  119. void sort()
  120. {
  121. student t;
  122. int i,j,k;
  123. for( i=0;i<n-1;i++)
  124. {
  125. k=i;
  126. for(j=i+1;j<n;j++)
  127. if(stu[j].no<stu[k].no)
  128. k=j;
  129. if(k!=i)
  130. {
  131. t=stu[i];
  132. stu[i]=stu[k];
  133. stu[k]=t;
  134. }
  135. }
  136. }
  137. void search()
  138. {
  139. int cho;
  140. string name1;
  141. int no1;
  142. int room1;
  143. bool flag;
  144. cout<<"1.按学号查询"<<endl;
  145. cout<<"2.按姓名查询"<<endl;
  146. cout<<"3.按房号查询"<<endl;
  147. cout<<"输入你的选择:";
  148. cin>>cho;
  149. if(cho==1)
  150. {
  151. flag=false;
  152. sort();
  153. cout<<"输入查询学号:";
  154. cin>>no1;
  155. int low=0,high=n-1,mid;
  156. while(low<=high)
  157. {
  158. mid=(low+high)/2;
  159. if(no1==stu[mid].no)
  160. {
  161. cout<<"姓名:"<<stu[mid].name<<endl;
  162. cout<<"学号:"<<stu[mid].no<<endl;
  163. cout<<"房号:"<<stu[mid].room<<endl;
  164. flag=true;
  165. break;
  166. }
  167. else if(no1<stu[mid].no)
  168. high=mid-1;
  169. else
  170. low=mid+1;
  171. }
  172. if(!flag)
  173. {
  174. cout<<"查无此人"<<endl;
  175. }
  176. }
  177. if(cho==2)
  178. {
  179. flag=false;
  180. cout<<"输入查询姓名:";
  181. cin>>name1;
  182. for(int i=0;i<n;i++)
  183. {
  184. if(name1==stu[i].name)
  185. {
  186. cout<<"姓名:"<<stu[i].name<<endl;
  187. cout<<"学号:"<<stu[i].no<<endl;
  188. cout<<"房号:"<<stu[i].room<<endl;
  189. flag=true;
  190. break;
  191. }
  192. }
  193. if(!flag)
  194. {
  195. cout<<"查无此人"<<endl;
  196. }
  197. }
  198. if(cho==3)
  199. {
  200. flag=false;
  201. cout<<"输入查询房号:";
  202. cin>>room1;
  203. for(int i=0;i<n;i++)
  204. {
  205. if(room1==stu[i].room)
  206. {
  207. cout<<"姓名:"<<stu[i].name<<endl;
  208. cout<<"学号:"<<stu[i].no<<endl;
  209. cout<<"房号:"<<stu[i].room<<endl;
  210. flag=true;
  211. break;
  212. }
  213. }
  214. if(!flag)
  215. {
  216. cout<<"查无此人"<<endl;
  217. }
  218. }
  219. }
  220. void print()
  221. {
  222. cout<<"学号***姓名***房号"<<endl;
  223. for(int i=0;i<n;i++)
  224. cout<<stu[i].no<<" "<<stu[i].name<<" "<<stu[i].room<<endl;
  225. }
  226. void menu()
  227. {
  228. cout<<"*******欢迎使用宿舍管理系统*******"<<endl;
  229. cout<<"************1.读取文件************"<<endl;
  230. cout<<"************2.增加信息************"<<endl;
  231. cout<<"************3.删除信息************"<<endl;
  232. cout<<"************4.修改信息************"<<endl;
  233. cout<<"************5.排序信息************"<<endl;
  234. cout<<"************6.查询信息************"<<endl;
  235. cout<<"************7.保存文件************"<<endl;
  236. cout<<"************0.退出系统************"<<endl;
  237. }
  238. int main()
  239. {
  240. int len=0;
  241. string str;
  242. ifstream file("student.txt");
  243. while(file)
  244. {
  245. getline(file,str);
  246. if(str.length()>4)
  247. len++;
  248. }
  249. n=len;
  250. menu();
  251. int ch;
  252. do
  253. {
  254. cin>>ch;
  255. switch(ch)
  256. {
  257. case 1:read();print();system("pause");system("cls");break;
  258. case 2:add();print();system("pause");system("cls");break;
  259. case 3:del();print();system("pause");system("cls");break;
  260. case 4:change();print();system("pause");system("cls");break;
  261. case 5:sort();print();system("pause");system("cls");break;
  262. case 6:search();system("pause");system("cls");break;
  263. case 7:write();print();system("pause");system("cls");break;
  264. case 0:cout<<"退出系统,感谢使用!"<<endl;break;
  265. default:cout<<"输入错误!"<<endl;
  266. }
  267. menu();
  268. }while(ch!=0);
  269. return 0;
  270. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/在线问答5/article/detail/788946
推荐阅读
相关标签
  

闽ICP备14008679号