当前位置:首页 > 通信资讯 > 正文

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

1. 项目要点

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

2. 功能介绍

学生管理系统设计与实现(C++实现)(用c++设计一个学生管理系统)

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

2.1 管理员功能界面

学生管理系统设计与实现(C++实现)(用c++设计一个学生管理系统)

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

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

2.2 学生功能界面

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

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

如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。