当前位置:   article > 正文

C++:设计一个学生学籍管理系统,设计相关信息,并执行一些计算和文件操作_用c或者c++实现一个学籍管理系统 系统中需要实现的功能如下: 1 添加学生成员:向学

用c或者c++实现一个学籍管理系统 系统中需要实现的功能如下: 1 添加学生成员:向学

版本更新12.28修复了全部已知bug

1.修复了回显缺失的bug
2.修复了变量导致结果不正确的bug
3.修复了数据冲突的bug
4.修复了文件读写未关闭的bug 

题目:

设计一个学生学籍管理系统:

  1.   学生信息包括:姓名、学号、性别和英语、数学、程序设计、体育成绩。
  1.   数据录入支持键盘输入和文件导入;同时支持导入+输入,如自动列出“姓名、学号、性别”,而成绩部分由键盘输入。录入结果存入数据文件student.dat。
  1.   支持按学生姓名和学号查询,支持指定班级的两位序号范围查询,查询结果回显到屏幕上。
  1. l对所有学生,按照班级计算各科平均成绩。
  1. l支持按单科成绩排序和总分排序,排序结果写入文件并回显,文件名自拟。

需求分析:

由电脑程序执行的管理系统广泛用于个人信息,物品信息的储存上,而学生信息管理系统对于每个学校来说都是现代化管理不可或缺的一部分。

学生信息管理系统其开发主要包括后台数据库的建立和维护以及前端应用程序的开发两个方面,对于前者要求建立起数据库一致性和完整性、安全性好的数据库。而对于后者则要求应用程序功能完备,易使用的特点。

学生信息管理系统要实现的目标是为学校提供学生管理解决方案,具体目标如下:

  1. 提高学生信息管理效率,节约管理成本,增强学生管理的安全性。
  2. 满足学校学生管理的人员、老师和学生的不同层次和不同方面的需要。
  3. 为学校将来的信息化建设提供必要的支持。总之,通过该系统的建设来提高学校的学生信息管理效率,使得学校的发展能够适应当前的教育信息化建设的中体发展趋势。

究其要点,学生信息管理系统正在朝着更快速,更便捷,更安全的发展,并且将成为现在及未来很长一段时间学校对学生信息管理的重要工具。

设计思想和方案设计:

  1. 建立简洁明了的菜单,每个菜单都用函数包装好,便于代码的后序拼接和修改。
  2. 建立符合要求的学生类,学生重要的信息(姓名,学号等)应该作为私有成员被添加,并且提供函数便于外界访问或修改相关内容。
  3. 分数应该使用double类型方便求平均分,学号姓名等用string更方便。
  4. 一些常用的功能比如类的复制,带参构造,输出,访问,修改函数应该提前声明好方便后续运用。
  5. 重复多次的步骤尽量给其写一个通用函数,减少代码量和阅读压力。
  6. 所有的函数应该按顺序封装到一个头文件里,防止主函数和函数顺序混账不清的情况。
  7. 为了在读取文件到程序中时获得更大的储存空间,应该在主函数外声明大容量的数组。
  8. 进行文件读写时,应该确定以哪种方式,比如:添加应该以追加(ios::app),排序回显应该用清除再写(ios::trunc)。
  9. 在进行信息添加时应该注意雷同的情况,即:一个学号只能够对应一个人,而可以有多个同名的人。因此对于相同学号,应该给予更新,对于原先不存在的应该给予添加。
  10. 每实现一个功能应该立刻对其进行测试,否则全写完测试会导致错误难以判断甚至出现连环错误的情况,并且要检查当前实现的功能和之前实现的功能是否存在冲突,发现问题立刻修改,而不是搁置。
  11. 增加一些注释方便阅读,也方便长时间后再看进行回忆。

核心代码:

  1. /*总控制函数*/
  2. void long_main() {
  3. ifstream ifs;
  4. while (true) {
  5. switch (Mainmenu())
  6. {
  7. case 1: {
  8. switch (findstumenu()) {
  9. case 1: {
  10. bool p = false; /*是否查到*/
  11. string num;
  12. cout << "请输入学生学号:";
  13. cin >> num;
  14. ifs.open("student.dat", ios::in);
  15. string buf; //文件读取
  16. while (getline(ifs, buf))
  17. {
  18. stu* stu1 = new stu();
  19. stringtostudent(buf, stu1);
  20. if (stu1->getnum() == num) {
  21. stu1->studisplay();
  22. p = true;
  23. }
  24. }
  25. if (!p)cout << "无此学生信息" << endl;
  26. ifs.close();
  27. break;
  28. }
  29. case 2: {
  30. string name;
  31. int len = 0;
  32. cout << "请输入学生姓名:";
  33. cin >> name;
  34. ifs.open("student.dat", ios::in);
  35. string buf; //文件读取
  36. while (getline(ifs, buf))
  37. {
  38. stu* stu1 = new stu();
  39. stringtostudent(buf, stu1);
  40. if (stu1->getname() == name) {
  41. st[len].stucpy(*stu1);
  42. len++;
  43. }
  44. }
  45. if (!len)cout << "无此学生信息" << endl;
  46. else {
  47. for (int i = 0; i < len; i++) { /*输出排序*/
  48. for (int j = 0; j < len - 1; j++) {
  49. if (st[j].getnum() > st[j + 1].getnum()) {
  50. stu s;
  51. s.stucpy(st[j]);
  52. st[j].stucpy(st[j + 1]);
  53. st[j + 1].stucpy(s);
  54. }
  55. }
  56. }
  57. cout << "查询到" << len << "条信息:" << endl;
  58. for (int i = 0; i < len; i++) {
  59. cout << i + 1 << ": " << st[i].getnum() << " " << st[i].getname() << endl; /*不确定具体信息时只展示姓名和学号*/
  60. }
  61. cout << "选择查询学生序号:";
  62. int k; /*选择具体信息编号*/
  63. do {
  64. cin >> k;
  65. if (k > len || k < 1)cout << "无效的指令,请重新输入" << endl;
  66. else break;
  67. } while (true);
  68. st[k - 1].studisplay();
  69. }
  70. ifs.close();
  71. break;
  72. }
  73. case 3: {
  74. string clas;
  75. int len = 0;
  76. cout << "请输入班号:";
  77. cin >> clas;
  78. ifs.open("student.dat", ios::in);
  79. string buf; //文件读取
  80. while (getline(ifs, buf))
  81. {
  82. stu* stu1 = new stu();
  83. stringtostudent(buf, stu1);
  84. if (strsplit(stu1->getnum(), 0, 7) == clas) {
  85. st[len].stucpy(*stu1);
  86. len++;
  87. }
  88. }
  89. if (!len)cout << "无此班级" << endl;
  90. else {
  91. cout << "查询到" << len << "条信息:" << endl;
  92. for (int i = 0; i < len; i++) {
  93. cout << i + 1 << ": " << st[i].getnum() << " " << st[i].getname() << endl; /*不确定具体信息时只展示姓名和学号*/
  94. }
  95. cout << "选择查询学生序号:";
  96. int k;
  97. do {
  98. cin >> k;
  99. if (k > len || k < 1)cout << "无效的指令,请重新输入" << endl;
  100. else break;
  101. } while (true);
  102. st[k - 1].studisplay();
  103. }
  104. ifs.close();
  105. break;
  106. }
  107. case 4:break;
  108. }break;
  109. case 2: {
  110. switch (addstumenu())
  111. {
  112. case 1: {
  113. cout << "输入学号(十位),姓名,性别(m/w),英语成绩,数学成绩,程序设计成绩,体育成绩" << endl;
  114. stu stu1;
  115. string nu, na, se;
  116. cin >> nu >> na >> se >> stu1.English >> stu1.Math >> stu1.Program_design >> stu1.Sports; /*手动输入(空格为间隔)*/
  117. bool p = false; /*相同学号是否已存在*/
  118. int loc = 0;
  119. ifs.open("student.dat", ios::in);
  120. string buf; //文件读取
  121. while (getline(ifs, buf))
  122. {
  123. stu* stu2 = new stu();
  124. stringtostudent(buf, stu2);
  125. if (stu2->getnum() == nu) {
  126. p = true;
  127. break;
  128. }
  129. loc++;
  130. }
  131. ifs.close();
  132. if (!p) { /*不存在添加*/
  133. int len = 0;
  134. ifs.open("student.dat", ios::in);
  135. string buf; //文件读取
  136. while (getline(ifs, buf))
  137. {
  138. stu* stu2 = new stu();
  139. stringtostudent(buf, stu2);
  140. st[len].stucpy(*stu2);
  141. len++;
  142. }
  143. ofstream ofs;
  144. ofs.open("student.dat", ios::out);
  145. for (int i = 0; i < len; i++) {
  146. ofs << st[i].getnum() << " " << st[i].getname() << " "<<st[i].getsex()<<" " << st[i].English << " " << st[i].Math << " " << st[i].Program_design << " " << st[i].Sports << " " << "1" << endl;
  147. }
  148. ofs << nu << " " << na << " " << se << " " << stu1.English << " " << stu1.Math << " " << stu1.Program_design << " " << stu1.Sports << " " << "1" << endl;
  149. ofs.close();
  150. num++;
  151. }
  152. else { /*存在则更新*/
  153. int len = 0;
  154. ifs.open("student.dat", ios::in);
  155. string buf; //文件读取
  156. while (getline(ifs, buf))
  157. {
  158. stu* stu2 = new stu();
  159. stringtostudent(buf, stu2);
  160. st[len].stucpy(*stu2);
  161. len++;
  162. }
  163. ifs.close();
  164. st[loc].stucpy(stu1);
  165. st[loc].changename(na);
  166. st[loc].changenum(nu);
  167. st[loc].changesex(se);
  168. ofstream ofs;
  169. ofs.open("student.dat", ios::out);
  170. for (int i = 0; i < len; i++) {
  171. ofs << st[i].getnum() << " " << st[i].getname() << " "<<st[i].getsex()<<" " << st[i].English << " " << st[i].Math << " " << st[i].Program_design << " " << st[i].Sports << " " << "1" << endl;
  172. }
  173. ofs.close();
  174. }
  175. break;
  176. }
  177. case 2: {
  178. cout << "输入文件路径" << endl;
  179. string ss;
  180. cin >> ss;
  181. int len = 0;
  182. int len1 = 0;
  183. stu st1[maxsize];
  184. ifs.open("student.dat", ios::in);
  185. string buf; //文件读取
  186. while (getline(ifs, buf))
  187. {
  188. stu* stu2 = new stu();
  189. stringtostudent(buf, stu2);
  190. st[len].stucpy(*stu2);
  191. len++;
  192. }
  193. ifs.close();
  194. ifs.open(ss, ios::in);
  195. if (!ifs.is_open()) { /*打不开证明没有这个文件,或者权限不够*/
  196. cout << "文件路径无效!" << endl;
  197. break;
  198. }
  199. while (getline(ifs, buf))
  200. {
  201. stu* stu2 = new stu();
  202. stringtostudent(buf, stu2);
  203. st1[len1].stucpy(*stu2);
  204. len1++;
  205. }
  206. ifs.close();
  207. ofstream ofs;
  208. ofs.open("student.dat", ios::app);
  209. for (int i = 0; i < len1; i++) {
  210. bool t = true; /*是否已存在*/
  211. for (int j = 0; j < len; j++) {
  212. if (st1[i].getnum() == st[j].getnum())t = false;
  213. }
  214. if (t)ofs << st1[i].getnum() << " " << st1[i].getname() << " " << st1[i].getsex() << " " << st1[i].English << " " << st1[i].Math << " " << st1[i].Program_design << " " << st1[i].Sports << " " << "1" << endl; /*未存在添加,已存在不添加*/
  215. }
  216. ofs.close();
  217. check();
  218. break;
  219. }
  220. case 3:break;
  221. }
  222. break;
  223. }break;
  224. case 3: {
  225. int len = 0;
  226. ifs.open("student.dat", ios::in);
  227. string buf; //文件读取
  228. while (getline(ifs, buf))
  229. {
  230. bool b = true; /*未找到班级*/
  231. stu* stu1 = new stu();
  232. stringtostudent(buf, stu1);
  233. for (int i = 0; i < len; i++) {
  234. if (st[i].getnum() == strsplit(stu1->getnum(), 0, 7)) { /*班级名和学生学号前八位相等*/
  235. b = false;
  236. st[i].English += stu1->English; /*此段代码中st数组作为班级数组*/
  237. st[i].Math += stu1->Math;
  238. st[i].Program_design += stu1->Program_design;
  239. st[i].Sports += stu1->Sports;
  240. st[i].score++; /*记录人数*/
  241. }
  242. }
  243. if (b) { /*没有班级则新建班级*/
  244. st[len].changenum(strsplit(stu1->getnum(), 0, 7));
  245. st[len].Math = st[len].Program_design = st[len].Sports = st[len].English =st[len].score=0;
  246. st[len].English += stu1->English;
  247. st[len].Math += stu1->Math;
  248. st[len].Program_design += stu1->Program_design;
  249. st[len].Sports += stu1->Sports;
  250. st[len].score++; /*记录人数*/
  251. len++;
  252. }
  253. }
  254. for (int i = 0; i < len; i++) { /*输出排序*/
  255. for (int j = 0; j < len - 1; j++) {
  256. if (st[j].getnum() > st[j + 1].getnum()) {
  257. stu s;
  258. s.stucpy(st[j]);
  259. st[j].stucpy(st[j + 1]);
  260. st[j + 1].stucpy(s);
  261. }
  262. }
  263. }
  264. for (int i = 0; i < len; i++) {
  265. cout << "班号:" << st[i].getnum() << " " << "英语平均分:" << st[i].English / st[i].score << " 数学平均分:" << st[i].Math / st[i].score << " 程序设计平均分" << st[i].Program_design / st[i].score << " 体育平均分" << st[i].Sports / st[i].score << endl;
  266. }
  267. ifs.close();
  268. }break;
  269. case 4: {
  270. ifstream ifs;
  271. int len = 0;
  272. ifs.open("student.dat", ios::in);
  273. string buf; //文件读取
  274. while (getline(ifs, buf))
  275. {
  276. stu* stu2 = new stu();
  277. stringtostudent(buf, stu2);
  278. st[len].stucpy(*stu2);
  279. len++;
  280. }
  281. ifs.close();
  282. switch (scoresortmenu()) {
  283. case 1: {
  284. for (int i = 0; i < len; i++) {
  285. for (int j = 0; j < len - 1; j++) { /*成绩排序*/
  286. if (st[j].total < st[j + 1].total) {
  287. stu stu1;
  288. stu1.stucpy(st[j]);
  289. st[j].stucpy(st[j + 1]);
  290. st[j+1].stucpy(stu1);
  291. }
  292. }
  293. }
  294. ofstream ofs;
  295. ofs.open("Total_stu.dat", ios::trunc); /*重写文件*/
  296. for (int i = 0; i < len; i++) {
  297. ofs << st[i].getnum() << " " << st[i].getname() << " ";
  298. if (st[i].getsex() == "m")ofs << "男";
  299. else ofs << "女";
  300. ofs<<" "<<st[i].total << endl;
  301. cout << st[i].getnum() << " " << st[i].getname() << " ";
  302. if (st[i].getsex() == "m")cout << "男";
  303. else cout << "女";
  304. cout << " " << st[i].total<< endl;
  305. }
  306. ofs.close();
  307. break;
  308. }
  309. case 2: {
  310. for (int i = 0; i < len; i++) {
  311. for (int j = 0; j < len - 1; j++) {
  312. if (st[j].English < st[j + 1].English) {
  313. stu stu1;
  314. stu1.stucpy(st[j]);
  315. st[j].stucpy(st[j + 1]);
  316. st[j + 1].stucpy(stu1);
  317. }
  318. }
  319. }
  320. ofstream ofs;
  321. ofs.open("English_stu.dat", ios::trunc);
  322. for (int i = 0; i < len; i++) {
  323. ofs << st[i].getnum() << " " << st[i].getname() << " ";
  324. if (st[i].getsex() == "m")ofs << "男";
  325. else ofs << "女";
  326. ofs << " " << st[i].English << endl;
  327. ofs.close();
  328. cout << st[i].getnum() << " " << st[i].getname() << " ";
  329. if (st[i].getsex() == "m")cout << "男";
  330. else cout << "女";
  331. cout << " " << st[i].English << endl;
  332. }
  333. break;
  334. }
  335. case 3: {
  336. for (int i = 0; i < len; i++) {
  337. for (int j = 0; j < len - 1; j++) {
  338. if (st[j].Math < st[j + 1].Math) {
  339. stu stu1;
  340. stu1.stucpy(st[j]);
  341. st[j].stucpy(st[j + 1]);
  342. st[j + 1].stucpy(stu1);
  343. }
  344. }
  345. }
  346. ofstream ofs;
  347. ofs.open("Math_stu.dat", ios::trunc);
  348. for (int i = 0; i < len; i++) {
  349. ofs << st[i].getnum() << " " << st[i].getname() << " ";
  350. if (st[i].getsex() == "m")ofs << "男";
  351. else ofs << "女";
  352. ofs << " " << st[i].Math<< endl;
  353. cout << st[i].getnum() << " " << st[i].getname() << " ";
  354. if (st[i].getsex() == "m")cout << "男";
  355. else cout << "女";
  356. cout << " " << st[i].Math<< endl;
  357. }
  358. ofs.close();
  359. break;
  360. }
  361. case 4: {
  362. for (int i = 0; i < len; i++) {
  363. for (int j = 0; j < len - 1; j++) {
  364. if (st[j].Program_design < st[j + 1].Program_design) {
  365. stu stu1;
  366. stu1.stucpy(st[j]);
  367. st[j].stucpy(st[j + 1]);
  368. st[j + 1].stucpy(stu1);
  369. }
  370. }
  371. }
  372. ofstream ofs;
  373. ofs.open("Program_design_stu.dat", ios::trunc);
  374. for (int i = 0; i < len; i++) {
  375. ofs << st[i].getnum() << " " << st[i].getname() << " ";
  376. if (st[i].getsex() == "m")ofs << "男";
  377. else ofs << "女";
  378. ofs << " " << st[i].Program_design << endl;
  379. cout << st[i].getnum() << " " << st[i].getname() << " ";
  380. if (st[i].getsex() == "m")cout << "男";
  381. else cout << "女";
  382. cout << " " << st[i].Program_design << endl;
  383. }
  384. ofs.close();
  385. break;
  386. }
  387. case 5: {
  388. for (int i = 0; i < len; i++) {
  389. for (int j = 0; j < len - 1; j++) {
  390. if (st[j].Sports < st[j + 1].Sports) {
  391. stu stu1;
  392. stu1.stucpy(st[j]);
  393. st[j].stucpy(st[j + 1]);
  394. st[j + 1].stucpy(stu1);
  395. }
  396. }
  397. }
  398. ofstream ofs;
  399. ofs.open("Sports_stu.dat", ios::trunc);
  400. for (int i = 0; i < len; i++) {
  401. ofs << st[i].getnum() << " " << st[i].getname() << " ";
  402. if (st[i].getsex() == "m")ofs << "男";
  403. else ofs << "女";
  404. ofs << " "<< st[i].Sports << endl;
  405. cout << st[i].getnum() << " " << st[i].getname() << " ";
  406. if (st[i].getsex() == "m")cout << "男";
  407. else cout<< "女";
  408. cout << " "<< st[i].Sports << endl;
  409. }
  410. ofs.close();
  411. break;
  412. }
  413. case 6:break;
  414. }
  415. }break;
  416. default:return;
  417. }
  418. }
  419. }
  420. }

测试用例:

本代码涉及大量文件操作:

运行要素:

  1. 在源代码所在文件夹中应该创建一个student.dat文件
  2. 在进行以文件形式添加人员信息时应该指明添加信息所在的路径

运行截图:

查询操作:

 

 

添加操作(stuadd.dat为同文件夹内的文件): 

平均成绩(按学号中的班级计算):

排名回显:

总结:

  1. 在此次实验中,我相对于以前对“面对对象”开发和“如何使用好封装类”有了更好的认识。并且对于C++相关文件的操作有了更加透彻的理解,方便日后进一步的使用。
  2. 对结构化,标准化的程序设计有了更深的理解,较以前更能够将自己的一段代码标准化和兼容化,不仅方便对代码的解读,也在代码的写作上更加省时省力。
  3. 程序的不足之处在于功能很少,有更多完善空间,面对一些问题还是没有足够的解决方法,比如:转专业,分流等导致学号和班级不符,一些操作没有纠错处理等。
  4. 在程序设计过程中,发现了自己在文件操作上有很多不足,经常错误的使用读取或者写入方法导致实验数据丢失,功亏一篑的情况。
  5. 在此次实验后,我更加意识到我在C++方面的很多不足,尤其是知识掌握程度,面对未来的学习,我应该不断查缺补漏,并学习更多新知识提升自己的程序设计能力和与程序设计相关的逻辑思维能力。

全部代码:

stu.cpp:

  1. #include<iostream>
  2. #include<algorithm>
  3. #include<string>
  4. #include<fstream>
  5. #define spaces 7
  6. #define maxsize 1000
  7. using namespace std;
  8. static int num = 0;
  9. class stu {
  10. private: /*学生信息属于私密信息*/
  11. string name; /*姓名*/
  12. string num; /*学号*/
  13. string sex; /*性别*/
  14. public:
  15. double English; /*各科成绩*/
  16. double Math;
  17. double Program_design;
  18. double Sports;
  19. double total; /*总成绩*/
  20. int score; /*判断此条信息是否完善*/
  21. stu() { /*默认构造*/
  22. name = " "; num = " "; sex = " "; English = 0; Math = 0; Program_design = 0; Sports = 0; score = 0;
  23. settotal();
  24. }
  25. stu(string na, string nu, string se, double en, double ma, double c, double s) { /*全参数构造*/
  26. name = na; num = nu; English = en; Math = ma; Program_design = c; Sports = s; score = 1;
  27. if (se == "男" || se == "m")sex = 'm';
  28. else sex = 'w';
  29. settotal();
  30. }
  31. stu(string na, string nu, string se) { /*不含成绩的构造*/
  32. name = na, num = nu;
  33. English = 0; Math = 0; Program_design = 0; Sports = 0; score = 0;
  34. if (se == "男" || se == "m")sex = 'm';
  35. else sex = 'w';
  36. settotal();
  37. }
  38. void stucpy(stu st) { /*值复制函数*/
  39. name = st.getname();
  40. num = st.getnum();
  41. sex = st.getsex();
  42. English = st.English;
  43. Math = st.Math;
  44. Program_design = st.Program_design;
  45. Sports = st.Sports;
  46. score = st.score;
  47. settotal();
  48. }
  49. void studisplay() { /*输出函数*/
  50. cout << num << " " << name << " " << English << " " << Math << " " << Program_design << " " << Sports << endl;
  51. }
  52. void settotal() { /*计和函数*/
  53. total = English + Math + Program_design + Sports;
  54. }
  55. string getname() { /*私有成员访问*/
  56. return name;
  57. }
  58. string getnum() {
  59. return num;
  60. }
  61. string getsex() {
  62. return sex;
  63. }
  64. void changename(string na) { /*私有成员修改*/
  65. name = na;
  66. }
  67. void changenum(string nu) {
  68. num = nu;
  69. }
  70. void changesex(string se) {
  71. sex = se;
  72. }
  73. };
  74. stu st[maxsize];
  75. #include"stu_deal.h" /*函数文件*/
  76. int main() {
  77. check(); /*查阅人数*/
  78. long_main();
  79. return 0;
  80. }

stu_deal.h:

  1. #pragma once
  2. void check() { /*查阅函数,统计文件中学生个数*/
  3. num = 0;
  4. ifstream ifs;
  5. ifs.open("student.dat", ios::in);
  6. if (!ifs.is_open())
  7. {
  8. cout << "Erro Read" << endl;
  9. }
  10. string buf; //文件读取
  11. while (getline(ifs, buf))
  12. {
  13. num++;
  14. }
  15. ifs.close();
  16. }
  17. void findspace(string s, int* a) { /*找字符串中空格方便进行字符串分割*/
  18. int len = 0;
  19. for (int i = 0; i < s.length(); i++) {
  20. if (s[i] == ' ') {
  21. a[len] = i;
  22. len++;
  23. }
  24. }
  25. }
  26. string strsplit(string s, int begin, int end) { /*取begin到end的所有字符*/
  27. string ss;
  28. for (int i = begin; i <= end; i++) {
  29. ss.push_back(s[i]);
  30. }
  31. return ss;
  32. }
  33. double stringtodouble(string s) { /*从文件中提取分数为string类型,将其转化为double类型(有精度损失)*/
  34. double ss = 0, sss = 0;
  35. int loc = s.length();
  36. for (int i = 0; i < s.length(); i++) {
  37. if (s[i] == '.')loc = i;
  38. }
  39. for (int i = 0; i < loc; i++) { /*整数位*/
  40. ss *= 10;
  41. ss += (int)s[i] - 48;
  42. }
  43. for (int i = s.length() - 1; i >= loc; i--) { /*小数位*/
  44. sss /= 10;
  45. sss += (int)s[i] - 48;
  46. }
  47. return sss + ss;
  48. }
  49. void stringtostudent(string s, stu* student) { /*从文件提取的信息为string类型,将其转化为student(class)类型*/
  50. string na, nu, se, en, ma, c, sp;
  51. int* a;
  52. a = new int[spaces + 1];
  53. findspace(s, a);
  54. nu = strsplit(s, 0, a[0] - 1); /*从长字符串中提取各项信息*/
  55. na = strsplit(s, a[0] + 1, a[1] - 1);
  56. se = strsplit(s, a[1] + 1, a[2] - 1);
  57. en = strsplit(s, a[2] + 1, a[3] - 1);
  58. ma = strsplit(s, a[3] + 1, a[4] - 1);
  59. c = strsplit(s, a[4] + 1, a[5] - 1);
  60. sp = strsplit(s, a[5] + 1, a[6] - 1);
  61. student->changename(na);
  62. student->changenum(nu);
  63. student->changesex(se);
  64. student->English = stringtodouble(en);
  65. student->Math = stringtodouble(ma);
  66. student->Program_design = stringtodouble(c);
  67. student->Sports = stringtodouble(sp);
  68. student->score = 1;
  69. }
  70. int Mainmenu() { /*主菜单*/
  71. cout << endl << "档案中共有学生" << num << "人" << endl;
  72. cout << "请选择操作:" << endl;
  73. cout << "1:查询学生" << endl;
  74. cout << "2:添加学生" << endl;
  75. cout << "3:平均成绩" << endl;
  76. cout << "4:分数排名" << endl;
  77. cout << "5:退出" << endl;
  78. int chose;
  79. do {
  80. cin >> chose;
  81. if (chose < 1 || chose>5)cout << "无效指令,重新输入!" << endl; /*指令异常处理*/
  82. else break;
  83. } while (true);
  84. return chose;
  85. }
  86. int findstumenu() { /*查询信息菜单*/
  87. cout << endl << "选择查询方式:" << endl;
  88. cout << "1.学号查询" << endl;
  89. cout << "2.姓名查询" << endl;
  90. cout << "3.班级序号查询" << endl;
  91. cout << "4.后退" << endl;
  92. int chose;
  93. do {
  94. cin >> chose;
  95. if (chose < 1 || chose>4)cout << "无效指令,重新输入!" << endl;
  96. else break;
  97. } while (true);
  98. return chose;
  99. }
  100. int addstumenu() { /*添加信息菜单*/
  101. cout << endl << "选择添加方式:" << endl;
  102. cout << "1.手动输入" << endl;
  103. cout << "2.文件导入" << endl;
  104. cout << "3.后退" << endl;
  105. int chose;
  106. do {
  107. cin >> chose;
  108. if (chose < 1 || chose>3)cout << "无效指令,重新输入!" << endl;
  109. else break;
  110. } while (true);
  111. return chose;
  112. }
  113. int scoresortmenu() { /*分数排名菜单*/
  114. cout << endl<<"选择排序所参照的成绩" << endl;
  115. cout << "1.总分" << endl;
  116. cout << "2.英语" << endl;
  117. cout << "3.数学" << endl;
  118. cout << "4.程序设计" << endl;
  119. cout << "5.体育" << endl;
  120. cout << "6.后退" << endl;
  121. int chose;
  122. do {
  123. cin >> chose;
  124. if (chose < 1 || chose>5)cout << "无效指令,重新输入!" << endl;
  125. else break;
  126. } while (true);
  127. return chose;
  128. }
  129. void long_main() {
  130. ifstream ifs;
  131. while (true) {
  132. switch (Mainmenu())
  133. {
  134. case 1: {
  135. switch (findstumenu()) {
  136. case 1: {
  137. bool p = false; /*是否查到*/
  138. string num;
  139. cout << "请输入学生学号:";
  140. cin >> num;
  141. ifs.open("student.dat", ios::in);
  142. string buf; //文件读取
  143. while (getline(ifs, buf))
  144. {
  145. stu* stu1 = new stu();
  146. stringtostudent(buf, stu1);
  147. if (stu1->getnum() == num) {
  148. stu1->studisplay();
  149. p = true;
  150. }
  151. }
  152. if (!p)cout << "无此学生信息" << endl;
  153. ifs.close();
  154. break;
  155. }
  156. case 2: {
  157. string name;
  158. int len = 0;
  159. cout << "请输入学生姓名:";
  160. cin >> name;
  161. ifs.open("student.dat", ios::in);
  162. string buf; //文件读取
  163. while (getline(ifs, buf))
  164. {
  165. stu* stu1 = new stu();
  166. stringtostudent(buf, stu1);
  167. if (stu1->getname() == name) {
  168. st[len].stucpy(*stu1);
  169. len++;
  170. }
  171. }
  172. if (!len)cout << "无此学生信息" << endl;
  173. else {
  174. for (int i = 0; i < len; i++) { /*输出排序*/
  175. for (int j = 0; j < len - 1; j++) {
  176. if (st[j].getnum() > st[j + 1].getnum()) {
  177. stu s;
  178. s.stucpy(st[j]);
  179. st[j].stucpy(st[j + 1]);
  180. st[j + 1].stucpy(s);
  181. }
  182. }
  183. }
  184. cout << "查询到" << len << "条信息:" << endl;
  185. for (int i = 0; i < len; i++) {
  186. cout << i + 1 << ": " << st[i].getnum() << " " << st[i].getname() << endl; /*不确定具体信息时只展示姓名和学号*/
  187. }
  188. cout << "选择查询学生序号:";
  189. int k; /*选择具体信息编号*/
  190. do {
  191. cin >> k;
  192. if (k > len || k < 1)cout << "无效的指令,请重新输入" << endl;
  193. else break;
  194. } while (true);
  195. st[k - 1].studisplay();
  196. }
  197. ifs.close();
  198. break;
  199. }
  200. case 3: {
  201. string clas;
  202. int len = 0;
  203. cout << "请输入班号:";
  204. cin >> clas;
  205. ifs.open("student.dat", ios::in);
  206. string buf; //文件读取
  207. while (getline(ifs, buf))
  208. {
  209. stu* stu1 = new stu();
  210. stringtostudent(buf, stu1);
  211. if (strsplit(stu1->getnum(), 0, 7) == clas) {
  212. st[len].stucpy(*stu1);
  213. len++;
  214. }
  215. }
  216. if (!len)cout << "无此班级" << endl;
  217. else {
  218. cout << "查询到" << len << "条信息:" << endl;
  219. for (int i = 0; i < len; i++) {
  220. cout << i + 1 << ": " << st[i].getnum() << " " << st[i].getname() << endl; /*不确定具体信息时只展示姓名和学号*/
  221. }
  222. cout << "选择查询学生序号:";
  223. int k;
  224. do {
  225. cin >> k;
  226. if (k > len || k < 1)cout << "无效的指令,请重新输入" << endl;
  227. else break;
  228. } while (true);
  229. st[k - 1].studisplay();
  230. }
  231. ifs.close();
  232. break;
  233. }
  234. case 4:break;
  235. }break;
  236. case 2: {
  237. switch (addstumenu())
  238. {
  239. case 1: {
  240. cout << "输入学号(十位),姓名,性别(m/w),英语成绩,数学成绩,程序设计成绩,体育成绩" << endl;
  241. stu stu1;
  242. string nu, na, se;
  243. cin >> nu >> na >> se >> stu1.English >> stu1.Math >> stu1.Program_design >> stu1.Sports; /*手动输入(空格为间隔)*/
  244. bool p = false; /*相同学号是否已存在*/
  245. int loc = 0;
  246. ifs.open("student.dat", ios::in);
  247. string buf; //文件读取
  248. while (getline(ifs, buf))
  249. {
  250. stu* stu2 = new stu();
  251. stringtostudent(buf, stu2);
  252. if (stu2->getnum() == nu) {
  253. p = true;
  254. break;
  255. }
  256. loc++;
  257. }
  258. ifs.close();
  259. if (!p) { /*不存在添加*/
  260. int len = 0;
  261. ifs.open("student.dat", ios::in);
  262. string buf; //文件读取
  263. while (getline(ifs, buf))
  264. {
  265. stu* stu2 = new stu();
  266. stringtostudent(buf, stu2);
  267. st[len].stucpy(*stu2);
  268. len++;
  269. }
  270. ofstream ofs;
  271. ofs.open("student.dat", ios::out);
  272. for (int i = 0; i < len; i++) {
  273. ofs << st[i].getnum() << " " << st[i].getname() << " "<<st[i].getsex()<<" " << st[i].English << " " << st[i].Math << " " << st[i].Program_design << " " << st[i].Sports << " " << "1" << endl;
  274. }
  275. ofs << nu << " " << na << " " << se << " " << stu1.English << " " << stu1.Math << " " << stu1.Program_design << " " << stu1.Sports << " " << "1" << endl;
  276. ofs.close();
  277. num++;
  278. }
  279. else { /*存在则更新*/
  280. int len = 0;
  281. ifs.open("student.dat", ios::in);
  282. string buf; //文件读取
  283. while (getline(ifs, buf))
  284. {
  285. stu* stu2 = new stu();
  286. stringtostudent(buf, stu2);
  287. st[len].stucpy(*stu2);
  288. len++;
  289. }
  290. ifs.close();
  291. st[loc].stucpy(stu1);
  292. st[loc].changename(na);
  293. st[loc].changenum(nu);
  294. st[loc].changesex(se);
  295. ofstream ofs;
  296. ofs.open("student.dat", ios::out);
  297. for (int i = 0; i < len; i++) {
  298. ofs << st[i].getnum() << " " << st[i].getname() << " "<<st[i].getsex()<<" " << st[i].English << " " << st[i].Math << " " << st[i].Program_design << " " << st[i].Sports << " " << "1" << endl;
  299. }
  300. ofs.close();
  301. }
  302. break;
  303. }
  304. case 2: {
  305. cout << "输入文件路径" << endl;
  306. string ss;
  307. cin >> ss;
  308. int len = 0;
  309. int len1 = 0;
  310. stu st1[maxsize];
  311. ifs.open("student.dat", ios::in);
  312. string buf; //文件读取
  313. while (getline(ifs, buf))
  314. {
  315. stu* stu2 = new stu();
  316. stringtostudent(buf, stu2);
  317. st[len].stucpy(*stu2);
  318. len++;
  319. }
  320. ifs.close();
  321. ifs.open(ss, ios::in);
  322. if (!ifs.is_open()) { /*打不开证明没有这个文件,或者权限不够*/
  323. cout << "文件路径无效!" << endl;
  324. break;
  325. }
  326. while (getline(ifs, buf))
  327. {
  328. stu* stu2 = new stu();
  329. stringtostudent(buf, stu2);
  330. st1[len1].stucpy(*stu2);
  331. len1++;
  332. }
  333. ifs.close();
  334. ofstream ofs;
  335. ofs.open("student.dat", ios::app);
  336. for (int i = 0; i < len1; i++) {
  337. bool t = true; /*是否已存在*/
  338. for (int j = 0; j < len; j++) {
  339. if (st1[i].getnum() == st[j].getnum())t = false;
  340. }
  341. if (t)ofs << st1[i].getnum() << " " << st1[i].getname() << " " << st1[i].getsex() << " " << st1[i].English << " " << st1[i].Math << " " << st1[i].Program_design << " " << st1[i].Sports << " " << "1" << endl; /*未存在添加,已存在不添加*/
  342. }
  343. ofs.close();
  344. check();
  345. break;
  346. }
  347. case 3:break;
  348. }
  349. break;
  350. }break;
  351. case 3: {
  352. int len = 0;
  353. ifs.open("student.dat", ios::in);
  354. string buf; //文件读取
  355. while (getline(ifs, buf))
  356. {
  357. bool b = true; /*未找到班级*/
  358. stu* stu1 = new stu();
  359. stringtostudent(buf, stu1);
  360. for (int i = 0; i < len; i++) {
  361. if (st[i].getnum() == strsplit(stu1->getnum(), 0, 7)) { /*班级名和学生学号前八位相等*/
  362. b = false;
  363. st[i].English += stu1->English; /*此段代码中st数组作为班级数组*/
  364. st[i].Math += stu1->Math;
  365. st[i].Program_design += stu1->Program_design;
  366. st[i].Sports += stu1->Sports;
  367. st[i].score++; /*记录人数*/
  368. }
  369. }
  370. if (b) { /*没有班级则新建班级*/
  371. st[len].changenum(strsplit(stu1->getnum(), 0, 7));
  372. st[len].Math = st[len].Program_design = st[len].Sports = st[len].English =st[len].score=0;
  373. st[len].English += stu1->English;
  374. st[len].Math += stu1->Math;
  375. st[len].Program_design += stu1->Program_design;
  376. st[len].Sports += stu1->Sports;
  377. st[len].score++; /*记录人数*/
  378. len++;
  379. }
  380. }
  381. for (int i = 0; i < len; i++) { /*输出排序*/
  382. for (int j = 0; j < len - 1; j++) {
  383. if (st[j].getnum() > st[j + 1].getnum()) {
  384. stu s;
  385. s.stucpy(st[j]);
  386. st[j].stucpy(st[j + 1]);
  387. st[j + 1].stucpy(s);
  388. }
  389. }
  390. }
  391. for (int i = 0; i < len; i++) {
  392. cout << "班号:" << st[i].getnum() << " " << "英语平均分:" << st[i].English / st[i].score << " 数学平均分:" << st[i].Math / st[i].score << " 程序设计平均分" << st[i].Program_design / st[i].score << " 体育平均分" << st[i].Sports / st[i].score << endl;
  393. }
  394. ifs.close();
  395. }break;
  396. case 4: {
  397. ifstream ifs;
  398. int len = 0;
  399. ifs.open("student.dat", ios::in);
  400. string buf; //文件读取
  401. while (getline(ifs, buf))
  402. {
  403. stu* stu2 = new stu();
  404. stringtostudent(buf, stu2);
  405. st[len].stucpy(*stu2);
  406. len++;
  407. }
  408. ifs.close();
  409. switch (scoresortmenu()) {
  410. case 1: {
  411. for (int i = 0; i < len; i++) {
  412. for (int j = 0; j < len - 1; j++) { /*成绩排序*/
  413. if (st[j].total < st[j + 1].total) {
  414. stu stu1;
  415. stu1.stucpy(st[j]);
  416. st[j].stucpy(st[j + 1]);
  417. st[j+1].stucpy(stu1);
  418. }
  419. }
  420. }
  421. ofstream ofs;
  422. ofs.open("Total_stu.dat", ios::trunc); /*重写文件*/
  423. for (int i = 0; i < len; i++) {
  424. ofs << st[i].getnum() << " " << st[i].getname() << " ";
  425. if (st[i].getsex() == "m")ofs << "男";
  426. else ofs << "女";
  427. ofs<<" "<<st[i].total << endl;
  428. cout << st[i].getnum() << " " << st[i].getname() << " ";
  429. if (st[i].getsex() == "m")cout << "男";
  430. else cout << "女";
  431. cout << " " << st[i].total<< endl;
  432. }
  433. ofs.close();
  434. break;
  435. }
  436. case 2: {
  437. for (int i = 0; i < len; i++) {
  438. for (int j = 0; j < len - 1; j++) {
  439. if (st[j].English < st[j + 1].English) {
  440. stu stu1;
  441. stu1.stucpy(st[j]);
  442. st[j].stucpy(st[j + 1]);
  443. st[j + 1].stucpy(stu1);
  444. }
  445. }
  446. }
  447. ofstream ofs;
  448. ofs.open("English_stu.dat", ios::trunc);
  449. for (int i = 0; i < len; i++) {
  450. ofs << st[i].getnum() << " " << st[i].getname() << " ";
  451. if (st[i].getsex() == "m")ofs << "男";
  452. else ofs << "女";
  453. ofs << " " << st[i].English << endl;
  454. ofs.close();
  455. cout << st[i].getnum() << " " << st[i].getname() << " ";
  456. if (st[i].getsex() == "m")cout << "男";
  457. else cout << "女";
  458. cout << " " << st[i].English << endl;
  459. }
  460. break;
  461. }
  462. case 3: {
  463. for (int i = 0; i < len; i++) {
  464. for (int j = 0; j < len - 1; j++) {
  465. if (st[j].Math < st[j + 1].Math) {
  466. stu stu1;
  467. stu1.stucpy(st[j]);
  468. st[j].stucpy(st[j + 1]);
  469. st[j + 1].stucpy(stu1);
  470. }
  471. }
  472. }
  473. ofstream ofs;
  474. ofs.open("Math_stu.dat", ios::trunc);
  475. for (int i = 0; i < len; i++) {
  476. ofs << st[i].getnum() << " " << st[i].getname() << " ";
  477. if (st[i].getsex() == "m")ofs << "男";
  478. else ofs << "女";
  479. ofs << " " << st[i].Math<< endl;
  480. cout << st[i].getnum() << " " << st[i].getname() << " ";
  481. if (st[i].getsex() == "m")cout << "男";
  482. else cout << "女";
  483. cout << " " << st[i].Math<< endl;
  484. }
  485. ofs.close();
  486. break;
  487. }
  488. case 4: {
  489. for (int i = 0; i < len; i++) {
  490. for (int j = 0; j < len - 1; j++) {
  491. if (st[j].Program_design < st[j + 1].Program_design) {
  492. stu stu1;
  493. stu1.stucpy(st[j]);
  494. st[j].stucpy(st[j + 1]);
  495. st[j + 1].stucpy(stu1);
  496. }
  497. }
  498. }
  499. ofstream ofs;
  500. ofs.open("Program_design_stu.dat", ios::trunc);
  501. for (int i = 0; i < len; i++) {
  502. ofs << st[i].getnum() << " " << st[i].getname() << " ";
  503. if (st[i].getsex() == "m")ofs << "男";
  504. else ofs << "女";
  505. ofs << " " << st[i].Program_design << endl;
  506. cout << st[i].getnum() << " " << st[i].getname() << " ";
  507. if (st[i].getsex() == "m")cout << "男";
  508. else cout << "女";
  509. cout << " " << st[i].Program_design << endl;
  510. }
  511. ofs.close();
  512. break;
  513. }
  514. case 5: {
  515. for (int i = 0; i < len; i++) {
  516. for (int j = 0; j < len - 1; j++) {
  517. if (st[j].Sports < st[j + 1].Sports) {
  518. stu stu1;
  519. stu1.stucpy(st[j]);
  520. st[j].stucpy(st[j + 1]);
  521. st[j + 1].stucpy(stu1);
  522. }
  523. }
  524. }
  525. ofstream ofs;
  526. ofs.open("Sports_stu.dat", ios::trunc);
  527. for (int i = 0; i < len; i++) {
  528. ofs << st[i].getnum() << " " << st[i].getname() << " ";
  529. if (st[i].getsex() == "m")ofs << "男";
  530. else ofs << "女";
  531. ofs << " "<< st[i].Sports << endl;
  532. cout << st[i].getnum() << " " << st[i].getname() << " ";
  533. if (st[i].getsex() == "m")cout << "男";
  534. else cout<< "女";
  535. cout << " "<< st[i].Sports << endl;
  536. }
  537. ofs.close();
  538. break;
  539. }
  540. case 6:break;
  541. }
  542. }break;
  543. default:return;
  544. }
  545. }
  546. }
  547. }

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

闽ICP备14008679号