经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C++ » 查看文章
学生选题信息管理系统
来源:cnblogs  作者:回忆酿的甜  时间:2018/11/19 9:24:50  对本文有异议
  1. //
  1. //本人大一写东忙西忙写的,感觉很一般,巩固基础麻,没有别的大神写的那么酷炫,但是还是基本任务完成了。
  1. //需先建立student.txt文件


    #include<fstream>
  2. #include<cstring>
  3. #include<stdio.h>
  4. #include<iostream>
  5. #include<stdlib.h>
  6. using namespace std;
  7. #define OK 1
  8. #define ERROR 0
  9. #define OVERFLOW -1
  10. #define List_INIT_SPACE 100 //存储空间初始分配量
  11. #define List_INC_SPACE 10 //存储空间分配增量
  12. int node = 1;
  13. typedef struct Student
  14. {
  15. int age; //年龄
  16. int student_id; //学号
  17. int course_id; //题目编号
  18. char student_name[40]; //姓名
  19. char sex[10]; //性别
  20. char Class[40]; //班级
  21. char specialty[40]; //专业
  22. char course_name[40]; //题目名称
  23. char keyword[40]; //关键词
  24. char technology[40];
  25. }Student;
  26. typedef struct STent_list
  27. {
  28. struct Student *stu; //存储空间基址
  29. int length; //当前长度
  30. int listsize; //当前分配的存储容量(以sizeof(Student)为单位
  31. }STent_list;
  32. int STent_listInit(STent_list &S)
  33. {
  34. //在内存中分配空间
  35. S.stu = (Student*)malloc(List_INC_SPACE*sizeof(Student));
  36. if(!S.stu) exit(OVERFLOW); //存储空间分配失败
  37. //构造一个空的线性表
  38. S.length = 0;
  39. S.listsize = List_INC_SPACE; //初始存储容量
  40. return OK;
  41. }//函数STent_listInit结束
  42. void write(STent_list &S, int n)
  43. {
  44. fstream myfile;
  45. myfile.open("student.txt",ios::out|ios::binary);
  46. //fstream myfile("student.txt",ios::out|ios::binary);
  47. if(! myfile)
  48. {
  49. cout<<"该文件不能打开!"<<endl;
  50. abort(); //异常终止函数
  51. }
  52. int count = n;
  53. myfile<<count<<endl<<endl;
  54. for(int i = 0; i <= count; i++)
  55. {
  56. myfile<<(S.stu[i]).student_id<<" "<<(S.stu[i]).student_name<<" "<<(S.stu[i]).sex<<" "<<(S.stu[i]).age<<" "<<(S.stu[i]).Class<<" "<<(S.stu[i]).specialty<<" "<<S.stu[i].course_id<<" "<<S.stu[i].course_name<<" "<<S.stu[i].keyword<<" "<<S.stu[i].technology<<" "<<endl;
  57. }
  58. myfile.close();
  59. }
  60. /**************************************/
  61. //函数名: read(STent_list &S)
  62. //参数: (传入) STent_list S顺序表
  63. //返回值: int型,返回1表示创建成功,0表示失败
  64. //功能: 读取顺序表中的内容
  65. /*************************************/
  66. int read(STent_list &S)
  67. {
  68. fstream myfile;
  69. myfile.open("student.txt",ios::in|ios::binary);
  70. //fstream myfile("student.txt",ios::out|ios::binary);
  71. if(! myfile)
  72. {
  73. cout<<"该文件不能打开"<<endl;
  74. abort();
  75. }
  76. int count;
  77. myfile.seekg(0);
  78. myfile>>count;
  79. for(int i = 0; i <= count; i++)
  80. {
  81. myfile>>S.stu[i].student_id>>S.stu[i].student_name>>S.stu[i].sex>>S.stu[i].age>>S.stu[i].Class>>S.stu[i].specialty>>S.stu[i].course_id>>S.stu[i].course_name>>S.stu[i].keyword>>S.stu[i].technology;
  82. }
  83. myfile.close();
  84. return count;
  85. }
  86. /*****************************************/
  87. //函数名:add(STent_list &S)
  88. //参数: (传入)STent_list S,顺序表
  89. //返回值: 空
  90. //功能: 添加学生信息
  91. /*****************************************/
  92. void add(STent_list &S)
  93. {
  94. int n = read(S);
  95. int i = 0;
  96. char sign = 'F';
  97. cout<<endl<<"请输入增加的学生的相关信息:"<<endl;
  98. while(sign != 'N')
  99. {
  100. loop:
  101. cout<<"学号:";
  102. cin>>S.stu[i].student_id;
  103. cout<<endl;
  104. int c = 0;
  105. while(c < i)
  106. {
  107. c++;
  108. if(S.stu[i].student_id == S.stu[i-c].student_id)
  109. {
  110. cout<<"你输入的学号已经存在!请重新输入" <<endl;
  111. goto loop;
  112. }
  113. }
  114. cout<<"姓名:";
  115. cin>>S.stu[i].student_name;
  116. cout<<endl;
  117. cout<<"性别:";
  118. cin>>S.stu[i].sex;
  119. cout<<endl;
  120. cout<<"年龄:";
  121. cin>>S.stu[i].age;
  122. cout<<endl;
  123. cout<<"班级:";
  124. cin>>S.stu[i].Class;
  125. cout<<endl;
  126. cout<<"专业:";
  127. cin>>S.stu[i].specialty;
  128. cout<<endl;
  129. cout<<"题目编号:";
  130. cin>>S.stu[i].course_id;
  131. cout<<endl;
  132. cout<<"题目名称:";
  133. cin>>S.stu[i].course_name;
  134. cout<<endl;
  135. cout<<"关键词:";
  136. cin>>S.stu[i].keyword;
  137. cout<<endl;
  138. cout<<"实现技术:";
  139. cin>>S.stu[i].technology;
  140. cout<<endl;
  141. cout<<"提示:是否继续写入学生信息?(Y/N):";
  142. cin>>sign;
  143. i++;
  144. }
  145. write(S, i);
  146. }
  147. /*****************************************************/
  148. //函数名: search_student_id(STent_list &S)
  149. //参数: (传入)STent_list S,顺序表
  150. //返回值: 空
  151. //功能: 根据学号查找学生信息
  152. /*****************************************************/
  153. void search_student_id(STent_list &S)
  154. {
  155. int n = read(S);
  156. int s;
  157. int i = 0;
  158. cout<<endl<<"查找学生信息:"<<endl;
  159. cout<<"请输入需要查找学生的学号:"<<endl;
  160. cin>>s;
  161. while((S.stu[i].student_id - s) != 0 && i < n) i++;
  162. if(i == n)
  163. {
  164. cout<<"提示: 对不起,无法找到该学生的信息! "<<endl;
  165. }
  166. else
  167. {
  168. cout<<"***************************"<<endl;
  169. cout<<"学号:"<<S.stu[i].student_id<<endl;
  170. cout<<"姓名:"<<S.stu[i].student_name<<endl;
  171. cout<<"性别: "<<S.stu[i].sex<<endl;
  172. cout<<"年龄:"<<S.stu[i].age<<endl;
  173. cout<<"班级:"<<S.stu[i].Class<<endl;
  174. cout<<"专业:"<<S.stu[i].specialty<<endl;
  175. cout<<"题目编号:"<<S.stu[i].course_id<<endl;
  176. cout<<"题目名称:"<<S.stu[i].course_name<<endl;
  177. cout<<"关键词:"<<S.stu[i].keyword<<endl;
  178. cout<<"实现技术:"<<S.stu[i].technology<<endl;
  179. }
  180. }
  181. /*******************************************************/
  182. //函数名: search_student_name(STent_list &S)
  183. //参数: (传入)STent_list S,顺序表
  184. //返回值: 空
  185. //功能: 根据学生姓名查找学生信息
  186. /*******************************************************/
  187. void search_student_name(STent_list &S)
  188. {
  189. int n = read(S);
  190. char a[100];
  191. cout<<"请输入需要查找的姓名:"<<endl;
  192. cin>>a;
  193. for(int i = 0; i < n; i++)
  194. if(strcmp(S.stu[i].student_name, a) == 0)
  195. {
  196. cout<<"****************************"<<endl;
  197. cout<<"学号:"<<S.stu[i].student_id<<endl;
  198. cout<<"姓名:"<<S.stu[i].student_name<<endl;
  199. cout<<"性别: "<<S.stu[i].sex<<endl;
  200. cout<<"年龄:"<<S.stu[i].age<<endl;
  201. cout<<"班级:"<<S.stu[i].Class<<endl;
  202. cout<<"专业:"<<S.stu[i].specialty<<endl;
  203. cout<<"题目编号:"<<S.stu[i].course_id<<endl;
  204. cout<<"题目名称:"<<S.stu[i].course_name<<endl;
  205. cout<<"关键词:"<<S.stu[i].keyword<<endl;
  206. cout<<"实现技术:"<<S.stu[i].technology<<endl;
  207. }
  208. }
  209. /*******************************************************/
  210. //函数名: search_course_id(STent_list &S)
  211. //参数: (传入)STent_list S,顺序表
  212. //返回值: 空
  213. //功能: 根据学生课程设计的编号查找
  214. /*******************************************************/
  215. void search_course_id(STent_list &S)
  216. {
  217. int n = read(S);
  218. int b;
  219. int i = 0;
  220. cout<<"请输入需要查找的题目编号:"<<endl;
  221. cin>>b;
  222. while((S.stu[i].course_id - b) != 0 && i < n) i++;
  223. if(i == n)
  224. {
  225. cout<<"提示:对不起,无法找到该信息!"<<endl;
  226. }
  227. else
  228. {
  229. for(i = 0; i < n; i++)
  230. if(S.stu[i].course_id - b == 0)
  231. {
  232. cout<<"******************************"<<endl;
  233. cout<<"学号:"<<S.stu[i].student_id<<endl;
  234. cout<<"姓名:"<<S.stu[i].student_name<<endl;
  235. cout<<"性别: "<<S.stu[i].sex<<endl;
  236. cout<<"年龄:"<<S.stu[i].age<<endl;
  237. cout<<"班级:"<<S.stu[i].Class<<endl;
  238. cout<<"专业:"<<S.stu[i].specialty<<endl;
  239. cout<<"题目编号:"<<S.stu[i].course_id<<endl;
  240. cout<<"题目名称:"<<S.stu[i].course_name<<endl;
  241. cout<<"关键词:"<<S.stu[i].keyword<<endl;
  242. cout<<"实现技术:"<<S.stu[i].technology<<endl;
  243. }
  244. }
  245. }
  246. /****************************************************/
  247. //函数名: search_course_name(STent_list &S)
  248. //参数: (传入)STent_list S,顺序表
  249. //返回值: 空
  250. //功能: 根据课程设计名称查找
  251. /***************************************************/
  252. void search_course_name(STent_list &S)
  253. {
  254. int n = read(S);
  255. char c[100];
  256. cout<<"请输入需要查找的题目名称:"<<endl;
  257. cin>>c;
  258. for(int i = 0; i < n; i++)
  259. if(strcmp(S.stu[i].course_name, c) == 0)
  260. {
  261. cout<<"******************************"<<endl;
  262. cout<<"学号:"<<S.stu[i].student_id<<endl;
  263. cout<<"姓名:"<<S.stu[i].student_name<<endl;
  264. cout<<"性别: "<<S.stu[i].sex<<endl;
  265. cout<<"年龄:"<<S.stu[i].age<<endl;
  266. cout<<"班级:"<<S.stu[i].Class<<endl;
  267. cout<<"专业:"<<S.stu[i].specialty<<endl;
  268. cout<<"题目编号:"<<S.stu[i].course_id<<endl;
  269. cout<<"题目名称:"<<S.stu[i].course_name<<endl;
  270. cout<<"关键词:"<<S.stu[i].keyword<<endl;
  271. cout<<"实现技术:"<<S.stu[i].technology<<endl;
  272. }
  273. }
  274. /******************************************************/
  275. //函数名: search(STent_list &S)
  276. //参数: (传入)STent_list S,顺序表
  277. //返回值: 空
  278. //功能: 选择查找关键词
  279. /******************************************************/
  280. void search(STent_list &S)
  281. {
  282. int n = read(S);
  283. cout<<"**(1)根据学号查询 **"<<endl;
  284. cout<<"**(2)根据姓名查询 **"<<endl;
  285. cout<<"**(3)根据编号查询 **"<<endl;
  286. cout<<"**(4)根据名称查询 **"<<endl;
  287. cout<<endl;
  288. int c;
  289. cout<<"请输入选择: ";
  290. cin>>c;
  291. switch(c)
  292. {
  293. case 1: search_student_id(S);break;
  294. case 2: search_student_name(S);break;
  295. case 3: search_course_id(S);break;
  296. case 4: search_course_name(S);break;
  297. default: cout<<"输入错误,请重新输入!"<<endl;
  298. }
  299. write(S, n);
  300. }
  301. /*******************************************/
  302. //函数名: student_alter(STent_list &S)
  303. //参数: (传入)STent_list S,顺序表
  304. //返回值: 空
  305. //功能: 修改学生信息
  306. /******************************************/
  307. void student_alter(STent_list &S)
  308. {
  309. int n = read(S);
  310. int s;
  311. int i = 0;
  312. cout<<endl<<"修改学生信息:"<<endl;
  313. cout<<"请输入需要修改学生的学号:"<<endl;
  314. cin>>s;
  315. while((S.stu[i].student_id - s) != 0 && i < n) i++;
  316. if(i == n)
  317. {
  318. cout<<"提示:对不起,无该学生的信息!!!"<<endl;
  319. }
  320. else
  321. {
  322. cout<<"该学生的信息:"<<endl;
  323. cout<<"学号:"<<S.stu[i].student_id<<endl;
  324. cout<<"姓名:"<<S.stu[i].student_name<<endl;
  325. cout<<"性别: "<<S.stu[i].sex<<endl;
  326. cout<<"年龄:"<<S.stu[i].age<<endl;
  327. cout<<"班级:"<<S.stu[i].Class<<endl;
  328. cout<<"专业:"<<S.stu[i].specialty<<endl;
  329. cout<<"题目编号:"<<S.stu[i].course_id<<endl;
  330. cout<<"题目名称:"<<S.stu[i].course_name<<endl;
  331. cout<<"关键词:"<<S.stu[i].keyword<<endl;
  332. cout<<"实现技术:"<<S.stu[i].technology<<endl;
  333. cout<<"请重新输入该学生的信息"<<endl;
  334. cout<<"学号:";
  335. cin>>S.stu[i].student_id;
  336. cout<<endl;
  337. cout<<"姓名:";
  338. cin>>S.stu[i].student_name;
  339. cout<<endl;
  340. cout<<"性别:";
  341. cin>>S.stu[i].sex;
  342. cout<<endl;
  343. cout<<"年龄:";
  344. cin>>S.stu[i].age;
  345. cout<<endl;
  346. cout<<"班级:";
  347. cin>>S.stu[i].Class;
  348. cout<<endl;
  349. cout<<"专业:";
  350. cin>>S.stu[i].specialty;
  351. cout<<endl;
  352. cout<<"题目编号:";
  353. cin>>S.stu[i].course_id;
  354. cout<<endl;
  355. cout<<"题目名称:";
  356. cin>>S.stu[i].course_name;
  357. cout<<endl;
  358. cout<<"关键词:";
  359. cin>>S.stu[i].keyword;
  360. cout<<endl;
  361. cout<<"实现技术:";
  362. cin>>S.stu[i].technology;
  363. cout<<endl;
  364. char c;
  365. cout<<"是否保存数据?(y/n)"<<endl;
  366. cin>>c;
  367. if(c == 'y')
  368. cout<<"修改成功!"<<endl;
  369. write(S, n);
  370. }
  371. }
  372. /**************************************/
  373. //函数名: student_delete(STent_list &S)
  374. //参数: (传入)STent_list S,顺序表
  375. //返回值: 空
  376. //功能: 删除学生信息
  377. /*************************************/
  378. void student_delete(STent_list &S)
  379. {
  380. int n = read(S);
  381. int s;
  382. int i = 0, j;
  383. cout<<endl<<"删除学生信息:"<<endl;
  384. cout<<"请输入需要删除学生的学号:"<<endl;
  385. cin>>s;
  386. while((S.stu[i].student_id - s) != 0 && i < n) i++;
  387. if(i == n)
  388. {
  389. cout<<"提示:记录为空!!!"<<endl;
  390. }
  391. else
  392. {
  393. cout<<"提示:已成功删除!"<<endl;
  394. cout<<"你要删除的信息如下:"<<endl;
  395. cout<<"学号:"<<S.stu[i].student_id<<endl;
  396. cout<<"姓名:"<<S.stu[i].student_name<<endl;
  397. cout<<"性别: "<<S.stu[i].sex<<endl;
  398. cout<<"年龄:"<<S.stu[i].age<<endl;
  399. cout<<"班级:"<<S.stu[i].Class<<endl;
  400. cout<<"专业:"<<S.stu[i].specialty<<endl;
  401. cout<<"题目编号:"<<S.stu[i].course_id<<endl;
  402. cout<<"题目名称:"<<S.stu[i].course_name<<endl;
  403. cout<<"关键词:"<<S.stu[i].keyword<<endl;
  404. cout<<"实现技术:"<<S.stu[i].technology<<endl;
  405. for(j = i; j < n-1; j++)
  406. {
  407. S.stu[j].student_id = S.stu[j+1].student_id;
  408. strcpy(S.stu[j].student_name,S.stu[j+1].student_name);
  409. strcpy(S.stu[j].sex,S.stu[j+1].sex);
  410. S.stu[j].age = S.stu[j+1].age;
  411. strcpy(S.stu[j].Class,S.stu[j+1].Class);
  412. strcpy(S.stu[j].specialty,S.stu[j+1].specialty);
  413. S.stu[j].course_id = S.stu[j+1].course_id;
  414. strcpy(S.stu[j].course_name,S.stu[j+1].course_name);
  415. strcpy(S.stu[j].keyword,S.stu[j+1].keyword);
  416. strcpy(S.stu[j].technology,S.stu[j+1].technology);
  417. }
  418. }
  419. write(S, n-1);
  420. }
  421. /******************************************/
  422. //函数名: total(STent_list &S)
  423. //参数: (传入)STent_list S,顺序表
  424. //返回值: 空
  425. //功能: 统计学生信息
  426. /*****************************************/
  427. void total(STent_list &S)
  428. {
  429. int n = read(S);
  430. char c[100];
  431. int ok = 0;
  432. cout<<"请输入需要查找的题目名称:"<<endl;
  433. cin>>c;
  434. for(int i = 0; i < n; i++)
  435. if(strcmp(S.stu[i].course_name, c) == 0)
  436. {
  437. cout<<"你要统计的信息如下:"<<endl;
  438. cout<<"学号:"<<S.stu[i].student_id<<endl;
  439. cout<<"姓名:"<<S.stu[i].student_name<<endl;
  440. cout<<"性别: "<<S.stu[i].sex<<endl;
  441. cout<<"年龄:"<<S.stu[i].age<<endl;
  442. cout<<"班级:"<<S.stu[i].Class<<endl;
  443. cout<<"专业:"<<S.stu[i].specialty<<endl;
  444. cout<<"题目编号:"<<S.stu[i].course_id<<endl;
  445. cout<<"题目名称:"<<S.stu[i].course_name<<endl;
  446. cout<<"关键词:"<<S.stu[i].keyword<<endl;
  447. cout<<"实现技术:"<<S.stu[i].technology<<endl;
  448. ok = 1;
  449. }
  450. if(ok == 0)
  451. {
  452. cout<<"没有此条记录!"<<endl;
  453. }
  454. }
  455. /********************************************/
  456. //函数名: display(STent_list &S)
  457. //参数: (传入)STent_list S,顺序表
  458. //返回值: 空
  459. //功能: 输出所有学生的全部信息
  460. /********************************************/
  461. void display(STent_list &S)
  462. {
  463. int n = read(S);
  464. cout<<endl<<"显示全部学生信息:"<<endl;
  465. if(! S.stu)
  466. {
  467. cout<<"没有记录"<<endl;
  468. }
  469. else
  470. {
  471. for(int i = 0; i < n; i++)
  472. {
  473. cout<<"学号:"<<S.stu[i].student_id<<endl;
  474. cout<<"姓名:"<<S.stu[i].student_name<<endl;
  475. cout<<"性别: "<<S.stu[i].sex<<endl;
  476. cout<<"年龄:"<<S.stu[i].age<<endl;
  477. cout<<"班级:"<<S.stu[i].Class<<endl;
  478. cout<<"专业:"<<S.stu[i].specialty<<endl;
  479. cout<<"题目编号:"<<S.stu[i].course_id<<endl;
  480. cout<<"题目名称:"<<S.stu[i].course_name<<endl;
  481. cout<<"关键词:"<<S.stu[i].keyword<<endl;
  482. cout<<"实现技术:"<<S.stu[i].technology<<endl;
  483. }
  484. }
  485. }
  486. int main()
  487. {
  488. char choice;
  489. cout<<endl<<endl<<"\t\t\t"<<" **欢迎使用课程设计选题管理系统**" <<endl<<endl;
  490. cout<<"\t\t\t"<<"1.*********添加新的记录**********"<<endl;
  491. cout<<"\t\t\t"<<"2.*********查询记录信息**********"<<endl;
  492. cout<<"\t\t\t"<<"3.*********修改学生信息**********"<<endl;
  493. cout<<"\t\t\t"<<"4.*********删除学生信息**********"<<endl;
  494. cout<<"\t\t\t"<<"5.*********统计所有记录**********"<<endl;
  495. cout<<"\t\t\t"<<"6.*********显示所有记录**********"<<endl;
  496. cout<<"\t\t\t"<<"0.********* 退出系统 **********"<<endl;
  497. STent_list S;
  498. STent_listInit(S);
  499. cout<<"\t\t\t"<<"请输入您的选择:";
  500. cin>>choice;
  501. if(choice == '0')
  502. {
  503. cout<<endl<<"\t"<<"\t"<<"\t"<<"谢谢使用本系统! "<<endl<<endl;
  504. exit(0);
  505. }
  506. else if(choice == '1')
  507. {
  508. add(S);
  509. system("pause");
  510. main();
  511. }
  512. else if(choice == '2')
  513. {
  514. search(S);
  515. system("pause");
  516. main();
  517. }
  518. else if(choice == '3')
  519. {
  520. student_alter(S);
  521. system("pause");
  522. main();
  523. }
  524. else if(choice == '4')
  525. {
  526. student_delete(S);
  527. system("pause");
  528. main();
  529. }
  530. else if(choice == '5')
  531. {
  532. total(S);
  533. system("pause");
  534. main();
  535. }
  536. else if(choice == '6')
  537. {
  538. display(S);
  539. system("pause");
  540. main();
  541. }
  542. else
  543. {
  544. cout<<"\t"<<"输入错误,请重新输入你的选择:";
  545. main();
  546. }
  547. return 0;
  548. }

  

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号