服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C++实现学生管理系统示例解析

C++实现学生管理系统示例解析

2021-09-18 16:02我本傲骄 C/C++

这篇文章主要介绍了C++实现学生管理系统示例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

此项目为一个小型学生管理系统,仅供初学者学习交流使用,使用者可以在此项目中用已学的C++基础知识进行实战演练,加深对所学知识的了解。
可以从github直接克隆项目(项目地址)

1. 项目要点

1、在每次进入最初登陆界面时,由于要再次加载文件内容,因此需先将list underst 和 list ad 中的内容使用clear()函数清空后再读入。
2、在读取文件时,由于使用!infile.eof()函数会导致最后一行读取两次。因此,在读文件循环内加入infile.get(),目的是在读完一行后立即换行。

2. 功能介绍

C++实现学生管理系统示例解析

从上图可以看出,此项目主要有三个功能模块:开通管理员账户、管理员功能界面(管理员身份登录)、本科生功能界面(本科生省份登录)。第一次运行本项目时,首先要开通管理员账户才能进行其他的操作。

2.1 管理员功能界面

C++实现学生管理系统示例解析

从上图可以看到管理员共有五项功能。

  • 查看所有学生信息
  • 按姓名查看学生信息
  • 按学号查看学生信息录
  • 入学生信息按学号
  • 删除学生信息

2.2 学生功能界面

C++实现学生管理系统示例解析

从上图可以看到学生共有两项功能。

查看个人信息修改密码

需要注意的是,在登录学生账户之前首先要登进管理员系统创建学生信息之后才可以使用该账户登录学生界面。

3. code(以下代码均在VS2017上编译运行通过)

  1. /*Administer.h 此文件为Administer类的头文件*/
  2. #pragma once
  3. #include<iostream>
  4. #include<string>
  5. using namespace std;
  6. class Administer
  7. {
  8. private:
  9. string name;
  10. string ID;
  11. string password;
  12. public:
  13. Administer() {}
  14. Administer(string na, string id, string passw);
  15. string get_id(){return ID;}
  16. string get_name(){return name;}
  17. string get_password(){ return password; }
  18. void display();
  19. };
  1. /*Administer.cpp 此文件为Administer类的实现*/
  2. #include"Administer.h"
  3. Administer::Administer(string na, string id, string passw) :name(na), ID(id), password(passw)
  4. {}
  5. void Administer::display()
  6. {
  7. cout << endl << "******************" << endl;
  8. cout << endl << "* 姓名:" << name;
  9. cout << endl << "* 账号:" << ID;
  10. cout << endl << "******************" << endl;
  11. }
  1. /*UnderStudent.h 此文件为UnderStuent类的头文件*/
  2. #pragma once
  3. #include<iostream>
  4. #include<string>
  5. using namespace std;
  6. class Understudent
  7. {
  8. private:
  9. string name;
  10. string ID;
  11. string password;
  12. float grade;
  13. string sex;
  14. public:
  15. Understudent() {}
  16. Understudent(string na, string id, string passw, float gra, string s);
  17. string get_name(){return name;}
  18. string get_id(){return ID;}
  19. string get_password(){return password;}
  20. float get_grade() { return grade; }
  21. string get_sex() { return sex; }
  22. void display();
  23. bool operator == (const Understudent &u)const //注意此处参数必须为const类型,才能与STL中list的内置函数匹配
  24. {
  25. return ID == u.ID;
  26. }
  27. void set_password(string passw)
  28. {
  29. password = passw;
  30. }
  31. };
  1. /*UnderStudent.cpp 此文件为UnderStudent类的实现*/
  2. #include"UnderStudent.h"
  3. Understudent::Understudent(string na, string id, string passw, float gra, string s):name(na),ID(id),password(passw),grade(gra), sex(s)
  4. {}
  5. void Understudent::display()
  6. {
  7. cout << "******************"<< endl;
  8. cout << "* 姓名:" << name << endl;
  9. cout << "* 学号:" << ID <<endl ;
  10. cout << "* 性别:" << sex <<endl ;
  11. cout << "* 绩点:" << grade << endl;
  12. cout << "******************"<< endl;
  13. }
  1. /*System.h 此文件为System的头文件*/
  2. #pragma once
  3. #include<list>
  4. #include<fstream>
  5. #include"UnderStudent.h"
  6. #include"Administer.h"
  7. class System
  8. {
  9. private:
  10. list<Understudent> underst;
  11. list<Administer> ad;
  12. static int underst_count;
  13. static int ad_count;
  14. public:
  15. virtual void load_interface(); //登陆界面
  16. void exit_system(); //退出系统
  17. void understudent_functionshow(); //学生用户功能界面
  18. void administer_functionshow(); //管理员功能界面
  19. void set_ad_account(); //设置管理员账户
  20. void enter_ad_account(); //管理员身份登陆
  21. void enter_underst_account(); //本科生身份登陆
  22. void save_undst(); //保存本科生数据
  23. void save_ad(); //保存管理员数据
  24. void load_undst(); //读取本科生数据
  25. void load_ad(); //读取管理员数据
  26. /*管理员功能*/
  27. void input_underst_info(); //录入本科生信息
  28. void look_all_underst(); //查看所有本科生信息
  29. void look_underst_by_name(string name); //根据姓名查看本科生信息
  30. void look_underst_by_id(string id); //根据ID查看本科生信息
  31. void delete_underst_by_id(string id); //根据ID删除本科生信息
  32. /*本科生功能*/
  33. void change_password(string id); //修改密码
  34. };
  1. /*System.cpp 此文件为System类的实现*/
  2. #include"System.h"
  3.  
  4. int System::underst_count = 0;
  5. int System::ad_count = 0;
  6.  
  7. //登陆界面
  8. void System::load_interface()
  9. {
  10. int i;
  11. do
  12. {
  13. system("cls");
  14. load_ad();
  15. load_undst();
  16. cout << "********************" << endl;
  17. cout << "1)开通管理员账户!" << endl;
  18. cout << "2)管理员身份登陆!" << endl;
  19. cout << "3)本科生身份登陆!" << endl;
  20. cout << "4)退出系统!" << endl;
  21. cout << "********************" << endl;
  22. cout << "请输入操作:";
  23. cin >> i;
  24. while (i < 1 || i>4)
  25. {
  26. cout << "请输入正确的序号!" << endl;
  27. cout << "请重新输入:";
  28. cin >> i;
  29. }
  30. switch (i)
  31. {
  32. case 1:
  33. set_ad_account();
  34. break;
  35. case 2:
  36. enter_ad_account();
  37. break;
  38. case 3:
  39. enter_underst_account();
  40. break;
  41. case 4:
  42. exit_system();
  43. break;
  44. default:
  45. break;
  46. }
  47. //cin.get();
  48. } while (true);
  49. }
  50.  
  51. //退出系统
  52. void System::exit_system()
  53. {
  54. cout << "****************感谢使用!******************" << endl;
  55. exit(0);
  56. }
  57.  
  58. //本科生功能界面
  59. void System::understudent_functionshow()
  60. {
  61. cout << "***************************" << endl;
  62. cout << "1)查看个人信息" << endl;
  63. cout << "2)修改密码" << endl;
  64. cout << "3)返回上一级菜单!" << endl;
  65. cout << "*****************************" << endl;
  66. cout << "请选择你要进行的操作:";
  67. }
  68.  
  69. //管理员功能界面
  70. void System::administer_functionshow()
  71. {
  72. cout << "***************************" << endl;
  73. cout << "1)查看所有学生信息!" << endl;
  74. cout << "2)按姓名查找学生信息!" << endl;
  75. cout << "3)按学号查找学生信息!" << endl;
  76. cout << "4)录入学生信息" << endl;
  77. cout << "5)按学号删除学生信息" << endl;
  78. cout << "6)返回上一级菜单!" << endl;
  79. cout << "*****************************" << endl;
  80. cout << "请选择你要进行的操作:";
  81. }
  82.  
  83. //设置管理员账户
  84. void System::set_ad_account()
  85. {
  86. string name;
  87. string id;
  88. string password;
  89. string password2;
  90. cout << endl<<"请输入姓名:";
  91. cin >> name;
  92. cout << endl << "请输入ID:";
  93. cin >> id;
  94. cout << endl << "请输入密码:";
  95. cin >> password;
  96. cout << endl << "请再次输入密码:";
  97. cin >> password2;
  98. while (password != password2)
  99. {
  100. cout << "两次密码不一致,请再次确认:";
  101. cin >> password2;
  102. }
  103. Administer adm(name, id, password);
  104. ad.push_back(adm);
  105. cout << "开户成功!" << endl;
  106. cin.get();
  107. ad_count++;
  108. save_ad();
  109. }
  110.  
  111. //管理员身份登陆
  112. void System::enter_ad_account()
  113. {
  114. string udst_name; //要查询的学生的名字
  115. string udst_id; //要查询学生的ID
  116. string id;
  117. string passw;
  118. list<Administer>::iterator iter;
  119. cout << "请输入账户:";
  120. cin >> id;
  121. int flag = 1;
  122. for (iter = ad.begin(); iter != ad.end(); iter++)
  123. {
  124. if (id == iter->get_id())
  125. {
  126. flag = 0;
  127. break;
  128. }
  129. }
  130. if (flag)
  131. {
  132. cout << endl<<"账户不存在!" << endl;
  133. return;
  134. }
  135. cout << endl << "请输入密码:";
  136. cin >> passw;
  137. while (passw != iter->get_password())
  138. {
  139. cout << endl<<"密码错误,请重新输入:";
  140. cin >> passw;
  141. }
  142. cin.get();
  143. int n;
  144. do
  145. {
  146. system("cls");
  147. administer_functionshow();
  148. cin >> n;
  149. while (n < 1 || n>6)
  150. {
  151. cout << "请输入正确的选项:";
  152. cin >> n;
  153. }
  154. switch (n)
  155. {
  156. case 1:
  157. look_all_underst();
  158. break;
  159. case 2:
  160. cout << "请输入要查询学生的名字:";
  161. cin >> udst_name;
  162. look_underst_by_name(udst_name);
  163. break;
  164. case 3:
  165. cout << "请输入要查询学生的ID:";
  166. cin >> udst_id;
  167. look_underst_by_id(udst_id);
  168. break;
  169. case 4:
  170. input_underst_info();
  171. break;
  172. case 5:
  173. cout << "请输入要删除学生的ID:";
  174. cin >> udst_id;
  175. delete_underst_by_id(udst_id);
  176. break;
  177. case 6:
  178. return;
  179. break;
  180. default:
  181. break;
  182. }
  183. } while (1);
  184. }
  185.  
  186. //本科生身份登陆
  187. void System::enter_underst_account()
  188. {
  189. list<Understudent>::iterator iter;
  190. string id;
  191. string passw;
  192. cout << "请输入账户:";
  193. cin >> id;
  194. int flag = 1;
  195. for (iter = underst.begin(); iter != underst.end(); iter++)
  196. {
  197. if (id == iter->get_id())
  198. {
  199. flag = 0;
  200. break;
  201. }
  202. }
  203. if (flag)
  204. {
  205. cout << endl << "账户不存在!" << endl;
  206. return;
  207. }
  208. cout << endl << "请输入密码:";
  209. cin >> passw;
  210. while (passw != iter->get_password())
  211. {
  212. cout << endl << "密码错误,请重新输入:";
  213. cin >> passw;
  214. }
  215. int n;
  216. do
  217. {
  218. system("cls");
  219. understudent_functionshow();
  220. cin >> n;
  221. while (n < 1 || n>3)
  222. {
  223. cout << endl << "请输入正确的操作:";
  224. cin >> n;
  225. }
  226. system("cls");
  227. switch (n)
  228. {
  229. case 1:
  230. iter->display();
  231. break;
  232. case 2:
  233.  
  234. change_password(id);
  235. break;
  236. case 3:
  237. return;
  238. break;
  239. default:
  240. break;
  241. }
  242. system("pause");
  243.  
  244. } while (true);
  245. }
  246.  
  247. //保存管理员数据
  248. void System::save_ad()
  249. {
  250. ofstream outfile("administer.dat",ios::out);
  251. list<Administer>::iterator iter;
  252. outfile << ad_count << endl;
  253. for (iter = ad.begin(); iter != ad.end(); iter++)
  254. {
  255. outfile << iter->get_name() << "\t" << iter->get_id() << "\t" << iter->get_password() << endl;
  256. }
  257. outfile.close();
  258. }
  259.  
  260. //保存本科生数据
  261. void System::save_undst()
  262. {
  263. ofstream outfile("understudent.dat",ios::out);
  264. list<Understudent>::iterator iter;
  265. outfile << underst_count << endl;
  266. for (iter = underst.begin(); iter != underst.end(); iter++)
  267. {
  268. outfile << iter->get_name() << "\t" << iter->get_id() << "\t" << iter->get_password() << "\t" << iter->get_grade() << "\t"
  269. << iter->get_sex() << endl;
  270. }
  271. outfile.close();
  272. }
  273.  
  274. //读取本科生数据
  275. void System::load_undst()
  276. {
  277. ifstream infile("understudent.dat");
  278. if (!infile)
  279. {
  280. cout << "无本科生资料!" << endl;
  281. return;
  282. }
  283. string name;
  284. string ID;
  285. string password;
  286. float grade;
  287. string sex;
  288. infile >> underst_count;//读取本科生总人数
  289. infile.get();
  290. if (!underst.empty())
  291. underst.clear();
  292. while (!infile.eof() && infile.peek() != EOF)
  293. {
  294. infile >> name >> ID >> password >> grade >> sex;
  295. Understudent undst(name, ID, password, grade, sex);
  296. underst.push_back(undst);
  297. infile.get();
  298. }
  299. infile.close();
  300. cout << "读取本科生资料正常。" << endl;
  301.  
  302. }
  303.  
  304. //读取管理员数据
  305. void System::load_ad()
  306. {
  307. ifstream infile("administer.dat");
  308. if (!infile)
  309. {
  310. cout << "无管理员资料!" << endl;
  311. return;
  312. }
  313. string name;
  314. string ID;
  315. string password;
  316. infile >> ad_count;//读取管理员总人数
  317. infile.get();
  318. if (!ad.empty())
  319. ad.clear();
  320. while (!infile.eof()||infile.peek()!=EOF)
  321. {
  322. infile >> name >> ID >> password;
  323. Administer adm(name, ID, password);
  324. ad.push_back(adm);
  325. infile.get();
  326. }
  327. infile.close();
  328. cout << "读取管理员资料正常。" << endl;
  329. }
  330.  
  331. /*
  332. 管理员权限:
  333. */
  334.  
  335. //1)查看所有本科生信息
  336. void System::look_all_underst()
  337. {
  338. system("cls");
  339. if (underst.empty())
  340. {
  341. cout << "无本科生数据!" << endl;
  342. system("pause");
  343. return;
  344. }
  345. list<Understudent>::iterator iter;
  346. cout << "姓名" << "\t" << "ID" << "\t" << "\t" <<"性别" << "\t" << "绩点" << endl;
  347. for (iter = underst.begin(); iter != underst.end(); iter++)
  348. cout << iter->get_name() << "\t" << iter->get_id() << "\t" << iter->get_sex() << "\t" << iter->get_grade() << endl;
  349. cout << endl << "学生总人数:" << underst_count << endl;
  350. system("pause");
  351. }
  352.  
  353. //2)按姓名查看本科生数据
  354. void System::look_underst_by_name(string udst_name)
  355. {
  356. system("cls");
  357. if (underst.empty())
  358. {
  359. cout << "无本科生数据!" << endl;
  360. system("pause");
  361. return;
  362. }
  363. list<Understudent>::iterator iter;
  364. for (iter = underst.begin(); iter != underst.end(); iter++)
  365. {
  366. if (iter->get_name() == udst_name)
  367. {
  368. cout << "姓名" << "\t" << "ID" << "\t" << "\t" << "性别" << "\t" << "绩点" << endl;
  369. cout << iter->get_name() << "\t" << iter->get_id() << "\t" << iter->get_sex() << "\t" << iter->get_grade() << endl;
  370. //姓名可以重复,因此遍历所有
  371. }
  372. if (iter == --underst.end())
  373. {
  374. system("pause");
  375. return;
  376. }
  377. }
  378. cout << "无该生数据!" << endl;
  379. system("pause");
  380. return;
  381. }
  382.  
  383. //3)按ID查看本科生数据
  384. void System::look_underst_by_id(string udst_id)
  385. {
  386. system("cls");
  387. if (underst.empty())
  388. {
  389. cout << "无本科生数据!" << endl;
  390. system("pause");
  391. return;
  392. }
  393. list<Understudent>::iterator iter;
  394. for (iter = underst.begin(); iter != underst.end(); iter++)
  395. {
  396. if (iter->get_id()==udst_id)
  397. {
  398. cout << "姓名" << "\t" << "ID" << "\t" << "\t" << "性别" << "\t" << "绩点" << endl;
  399. cout << iter->get_name() << "\t" << iter->get_id() << "\t" << iter->get_sex() << "\t" << iter->get_grade() << endl;
  400. system("pause");
  401. return; //ID不能有重复
  402. }
  403. }
  404. cout << "无该生数据!" << endl;
  405. system("pause");
  406. return;
  407. }
  408.  
  409. //4)录入本科生信息
  410. void System::input_underst_info()
  411. {
  412.  
  413. string name;
  414. string ID;
  415. string password;
  416. float grade;
  417. string sex;
  418. char s; //是否继续录入flag
  419. do
  420. {
  421. system("cls");
  422. cout << endl << "请输入学生姓名:";
  423. cin >> name;
  424. cout << endl << "请输入学生ID:";
  425. cin >> ID;
  426. cout << endl << "请输入学生初始密码:";
  427. cin >> password;
  428. cout << endl << "请输入学生绩点:";
  429. cin >> grade;
  430. cout <<endl<< "请输入学生性别:";
  431. cin >> sex;
  432. Understudent undst(name, ID, password, grade, sex);
  433. underst.push_back(undst);
  434. underst_count++;
  435. cout << endl << "是否继续录入?(Y/N)";
  436. cin >> s;
  437. while (s != 'Y'&&s != 'N'&&s != 'y'&&s != 'n')
  438. {
  439. cout << endl << "请输入正确操作(Y/N):";
  440. cin >> s;
  441. }
  442. } while (s == 'Y'||s=='y');
  443. save_undst();
  444. }
  445.  
  446. //5)按ID删除学生信息
  447. void System::delete_underst_by_id(string udst_id)
  448. {
  449. system("cls");
  450. if (underst.empty())
  451. {
  452. cout << "无本科生数据!" << endl;
  453. system("pause");
  454. return;
  455. }
  456. list<Understudent>::iterator iter;
  457. string name;
  458. string ID;
  459. string password;
  460. float grade;
  461. string sex;
  462. for (iter = underst.begin(); iter != underst.end(); iter++)
  463. {
  464. if (iter->get_id() == udst_id)
  465. {
  466. name = iter->get_name();
  467. ID = iter->get_id();
  468. password = iter->get_password();
  469. grade = iter->get_grade();
  470. sex = iter->get_sex();
  471. Understudent undst(name, ID, password, grade, sex);
  472. underst.remove(undst);
  473. underst_count--;
  474. cout << "删除成功!" << endl;
  475. system("pause");
  476. save_undst();
  477. return; //ID不能有重复
  478. }
  479. }
  480. cout << "无该生数据!" << endl;
  481. system("pause");
  482. return;
  483. }
  484.  
  485. /*
  486. 本科生权限
  487. */
  488. //2)修改密码
  489. void System::change_password(string id)
  490. {
  491. string password, passw;
  492. cout << "请输入新密码:";
  493. cin >> password;
  494. cout <<endl<<"请再次输入:";
  495. cin >> passw;
  496. while (password != passw)
  497. {
  498. cout << endl<<"两次密码不一致,请重新输入:";
  499. cin >> passw;
  500. }
  501. list<Understudent>::iterator iter;
  502. for (iter = underst.begin(); iter != underst.end(); iter++)
  503. {
  504. if (iter->get_id() == id)
  505. break;
  506. }
  507. iter->set_password(password);
  508. cout << "修改密码成功!" << endl;
  509. save_undst();
  510. }
  1. /*mai.cpp 此文件为主函数所在文件*/
  2. #include"System.h"
  3. int main()
  4. {
  5. System s;
  6. s.load_interface();
  7. system("pause");
  8. return 0;
  9. }

代码目录结构

C++实现学生管理系统示例解析

到此这篇关于C++实现学生管理系统示例解析的文章就介绍到这了,更多相关C++学生管理系统内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/qq_42780025/article/details/94453068

延伸 · 阅读

精彩推荐