首页 > 编程 > C++ > 正文

C++实现简单的学生管理系统

2020-05-23 14:19:03
字体:大 中 小
来源:转载
供稿:网友

本文给大家分享的是使用C++实现的简单的学生管理系统的代码,主要是通过链表来实现,非常简洁,有需要的小伙伴可以参考下。

C++实现简单的学生管理系统

//Student.cpp

 

 
  1. #include<iostream> 
  2.  
  3. using namespace std; 
  4.  
  5. struct Stu 
  6. { 
  7. char no[10]; 
  8. char name[16]; 
  9. int math; 
  10. int chi; 
  11. double ave; 
  12. }; 
  13.  
  14. class Student 
  15. { 
  16. public: 
  17. Stu st; 
  18. Student * next; 
  19. public: 
  20. Student(){} 
  21. Student(Stu s) 
  22. { 
  23. st=s; 
  24. next=NULL; 
  25. st.ave=(st.math+st.chi)/2.0; 
  26. } 
  27. void setst(Stu s) 
  28. { 
  29. st=s; 
  30. st.ave=(st.math+st.chi)/2.0; 
  31. } 
  32. Stu getst() 
  33. { 
  34. return st; 
  35. } 
  36. void show() 
  37. { 
  38. cout<<"----------------------------"<<endl; 
  39. cout<<"学号:"<<st.no<<endl; 
  40. cout<<"姓名:"<<st.name<<endl; 
  41. cout<<"数学成绩:"<<st.math<<endl; 
  42. cout<<"语文成绩:"<<st.chi<<endl; 
  43. cout<<"平均成绩:"<<st.ave<<endl; 
  44. cout<<"----------------------------"<<endl; 
  45. } 
  46. }; 

//main.cpp

 

 
  1. #include<fstream> 
  2. #include"Student.cpp" 
  3.  
  4. using namespace std; 
  5.  
  6. Student * create_List(); 
  7. void traverse_List(Student * pHead); 
  8. bool is_empty(Student * pHead); 
  9. int length_List(Student * pHead); 
  10. bool insert_List(Student * pHead,int position,Stu st); 
  11. bool delete_List(Student * pHead,int position,Stu * st); 
  12. void sort_List(Student * pHead);  
  13. void menu_select(); 
  14. void handle_menu(int s); 
  15. void outFile(); 
  16. Student * inFile(); 
  17. void delFile(); 
  18.  
  19. Student * pHead; 
  20.  
  21.  
  22. void main() 
  23. { 
  24. menu_select(); 
  25. } 
  26.  
  27. void menu_select() 
  28. { 
  29. int s; 
  30. cout<<"请输入您要操作的选项:"<<endl; 
  31. cout<<"1.增加原始记录"<<endl; 
  32. cout<<"2.按平均分排序显示所有记录"<<endl; 
  33. cout<<"3.保存原始文件"<<endl; 
  34. cout<<"4.读取原始文件"<<endl; 
  35. cout<<"5.删除原始文件"<<endl; 
  36. cout<<"6.插入单条记录"<<endl; 
  37. cout<<"7.删除单条记录"<<endl; 
  38. cout<<"8.显示记录总条数"<<endl; 
  39. cout<<"9.结束程序运行"<<endl<<endl; 
  40. cout<<"左边数字对应功能选择,请选择1-9:"; 
  41. cin>>s; 
  42. handle_menu(s); 
  43. } 
  44.  
  45. void handle_menu(int s) 
  46. { 
  47. switch (s) 
  48. { 
  49. case 1: 
  50. { 
  51. system("cls"); 
  52. pHead=create_List(); 
  53. system("cls"); 
  54. menu_select(); 
  55. break; 
  56. } 
  57. case 2: 
  58. { 
  59. if(NULL==pHead) 
  60. { 
  61. cout<<"记录总条数为零"<<endl; 
  62. getchar(); 
  63. getchar(); 
  64. system("cls"); 
  65. menu_select(); 
  66. } 
  67. system("cls"); 
  68. sort_List(pHead); 
  69. traverse_List(pHead); 
  70. getchar(); 
  71. getchar(); 
  72. system("cls"); 
  73. menu_select(); 
  74. break; 
  75. } 
  76. case 3: 
  77. { 
  78. if(pHead!=NULL) 
  79. { 
  80. system("cls"); 
  81. outFile(); 
  82. system("cls"); 
  83. menu_select(); 
  84. } 
  85. system("cls"); 
  86. menu_select(); 
  87. break; 
  88. } 
  89. case 4: 
  90. { 
  91. system("cls"); 
  92. pHead=inFile(); 
  93. system("cls"); 
  94. menu_select(); 
  95. break; 
  96. } 
  97. case 5: 
  98. { 
  99. system("cls"); 
  100. delFile(); 
  101. system("cls"); 
  102. menu_select(); 
  103. break; 
  104. } 
  105. case 6: 
  106. { 
  107. if(NULL==pHead) 
  108. { 
  109. cout<<"记录总条数为零"<<endl; 
  110. getchar(); 
  111. getchar(); 
  112. system("cls"); 
  113. menu_select(); 
  114. } 
  115. system("cls"); 
  116. int num; 
  117. Stu st; 
  118. traverse_List(pHead); 
  119. cout<<"您想在哪条记录后插入,请输入序号:"; 
  120. cin>>num; 
  121. cout<<"编辑要插入的记录:"<<endl; 
  122. cout<<"学号:"; 
  123. cin>>st.no; 
  124. cout<<"姓名:"; 
  125. cin>>st.name; 
  126. cout<<"数学成绩:"; 
  127. cin>>st.math; 
  128. cout<<"语文成绩:"; 
  129. cin>>st.chi; 
  130. if(insert_List(pHead,num-1,st)) 
  131. { 
  132. cout<<"插入成功!"<<endl; 
  133. } 
  134. else 
  135. { 
  136. cout<<"插入失败!"<<endl; 
  137. } 
  138. getchar(); 
  139. getchar(); 
  140. system("cls"); 
  141. menu_select(); 
  142. break; 
  143. } 
  144. case 7: 
  145. { 
  146. if(NULL==pHead) 
  147. { 
  148. cout<<"记录总条数为零"<<endl; 
  149. getchar(); 
  150. getchar(); 
  151. system("cls"); 
  152. menu_select(); 
  153. } 
  154. int num; 
  155. Stu * st=(Stu *)malloc(sizeof(Stu)); 
  156. traverse_List(pHead); 
  157. cout<<endl<<"请输入您要删除的记录的序号:"; 
  158. cin>>num; 
  159. if(delete_List(pHead,num,st)) 
  160. { 
  161. cout<<endl<<"成功删除的记录如下:"<<endl; 
  162. cout<<"学号:"<<st->no<<endl<<"姓名:"<<st->name<<endl; 
  163. } 
  164. else 
  165. { 
  166. cout<<"删除失败!"<<endl; 
  167. } 
  168. getchar(); 
  169. getchar(); 
  170. system("cls"); 
  171. menu_select(); 
  172. break; 
  173. } 
  174. case 8: 
  175. { 
  176. if(NULL!=pHead) 
  177. { 
  178. system("cls"); 
  179. cout<<"记录总条数:"<<length_List(pHead)<<"条"<<endl; 
  180. getchar(); 
  181. getchar(); 
  182. system("cls"); 
  183. menu_select(); 
  184. } 
  185. else 
  186. { 
  187. cout<<"记录总条数为零"<<endl; 
  188. getchar(); 
  189. getchar(); 
  190. system("cls"); 
  191. menu_select(); 
  192. } 
  193. break; 
  194. } 
  195. case 9: 
  196. { 
  197. system("cls"); 
  198. cout<<"成功退出!"<<endl; 
  199. exit(0); 
  200. break; 
  201. } 
  202. } 
  203. } 
  204.  
  205.  
  206. void delFile() 
  207. { 
  208. ofstream fileout; 
  209. fileout.open("c://kcsj.txt",ios_base::out); 
  210. fileout<<""; 
  211. fileout.close(); 
  212. } 
  213.  
  214.  
  215. Student * inFile() 
  216. { 
  217. Student * pHead=(Student *)malloc(sizeof(Student)); 
  218. if(NULL==pHead) 
  219. { 
  220. cout<<"分配失败,程序终止!"<<endl; 
  221. exit(0); 
  222. } 
  223. Student * pTail=pHead; 
  224. pTail->next=NULL; 
  225. ifstream in("c://kcsj.txt"); 
  226. if (!in.is_open()) 
  227. { 
  228. cout << "Error opening file"<<endl;  
  229. exit(0); 
  230. } 
  231. while (!in.eof()) 
  232. { 
  233. Stu st; 
  234. in.read(reinterpret_cast<char *>(&st), sizeof(st)); 
  235. if (in.fail())  
  236. { 
  237. break; 
  238. } 
  239. Student * pNew=new Student(); 
  240. if(NULL==pNew) 
  241. { 
  242. printf("分配失败,程序终止/n"); 
  243. exit(0); 
  244. } 
  245. pNew->setst(st); 
  246. pTail->next=pNew; 
  247. pNew->next=NULL; 
  248. pTail=pNew; 
  249. } 
  250. in.close(); 
  251. return pHead; 
  252. } 
  253.  
  254.  
  255. void outFile() 
  256. { 
  257. ofstream out; 
  258. out.open("c://kcsj.txt",ios_base::out|ios_base::app|ios::binary); 
  259. if(!out) 
  260. { 
  261. cout<<"文件不存在,退出时别忘记保存文件!"<<endl; 
  262. out.close(); 
  263. out.open("stu.dat",ios_base::out|ios::binary); 
  264. } 
  265. else 
  266. {  
  267. out.close(); 
  268. out.open("c://kcsj.txt",ios_base::out|ios_base::app|ios::binary); 
  269. } 
  270. Student * temp=pHead->next; 
  271. while(temp!=NULL) 
  272. { 
  273. Stu st=temp->getst(); 
  274. out.write(reinterpret_cast<char *>(&st), sizeof(st)); 
  275. temp=temp->next; 
  276. }  
  277. out.close(); 
  278. } 
  279.  
  280. Student * create_List() 
  281. { 
  282. int len; 
  283. Student * pHead=(Student *)malloc(sizeof(Student)); 
  284. if(NULL==pHead) 
  285. { 
  286. cout<<"分配失败,程序终止!"<<endl; 
  287. exit(0); 
  288. } 
  289. Student * pTail=pHead; 
  290. pTail->next=NULL; 
  291. cout<<"请输入要存储的学生人数:"; 
  292. cin>>len; 
  293. for(int i=0;i<len;i++) 
  294. { 
  295. Stu st; 
  296. cout<<"请输入第"<<i+1<<"个学生的学号:"; 
  297. cin>>st.no; 
  298. cout<<"请输入第"<<i+1<<"个学生的姓名:"; 
  299. cin>>st.name; 
  300. cout<<"请输入第"<<i+1<<"个学生的数学成绩:"; 
  301. cin>>st.math; 
  302. cout<<"请输入第"<<i+1<<"个学生的语文成绩:"; 
  303. cin>>st.chi; 
  304. Student * pNew=new Student(); 
  305. if(NULL==pNew) 
  306. { 
  307. printf("分配失败,程序终止/n"); 
  308. exit(0); 
  309. } 
  310. pNew->setst(st); 
  311. pTail->next=pNew; 
  312. pNew->next=NULL; 
  313. pTail=pNew; 
  314. } 
  315. return pHead; 
  316. } 
  317.  
  318. void traverse_List(Student * pHead) 
  319. { 
  320. int i=1; 
  321. Student * temp=pHead->next; 
  322. while(temp!=NULL) 
  323. { 
  324. cout<<endl<<"序号:"<<i<<endl; 
  325. temp->show(); 
  326. temp=temp->next; 
  327. i++; 
  328. } 
  329. }  
  330.  
  331. bool is_empty(Student * pHead) 
  332. { 
  333. if(NULL==pHead->next) 
  334. { 
  335. return true; 
  336. } 
  337. else 
  338. { 
  339. return false; 
  340. } 
  341. } 
  342.  
  343. int length_List(Student * pHead) 
  344. { 
  345. int len=0; 
  346. Student * temp=pHead->next; 
  347. while(temp) 
  348. { 
  349. len++; 
  350. temp=temp->next; 
  351. } 
  352. return len; 
  353. } 
  354.  
  355. bool insert_List(Student * pHead,int position,Stu st) 
  356. { 
  357. int i=0; 
  358. Student * p=pHead; 
  359.  
  360. while(NULL!=p&&i<position-1) 
  361. { 
  362. p=p->next; 
  363. i++; 
  364. } 
  365. if(i>position-i||NULL==p) 
  366. { 
  367. return false; 
  368. } 
  369. Student * pNew=(Student *)malloc(sizeof(Student)); 
  370. if(NULL==pNew) 
  371. { 
  372. cout<<"分配失败,程序终止"<<endl; 
  373. exit(0); 
  374. } 
  375. pNew->setst(st); 
  376. pNew->next=p->next; 
  377. p->next=pNew; 
  378. return true; 
  379. } 
  380.  
  381. bool delete_List(Student * pHead,int position,Stu * st) 
  382. { 
  383. int i=0; 
  384. Student * p=pHead; 
  385.  
  386. while(NULL!=p->next&&i<position-1) 
  387. { 
  388. p=p->next; 
  389. i++; 
  390. } 
  391. Student * q=p->next; 
  392. *st=q->getst(); 
  393. p->next=p->next->next; 
  394. free(q); 
  395. q=NULL; 
  396. return true; 
  397. } 
  398.  
  399. void sort_List(Student * pHead) 
  400. { 
  401. Student * p,* q; 
  402. Stu temp; 
  403. int i,j; 
  404. int len=length_List(pHead); 
  405. for(i=0,p=pHead->next;i<len-1;i++,p=p->next) 
  406. { 
  407. for(j=i+1,q=p->next;j<len;j++,q=q->next) 
  408. { 
  409. if(q->st.ave>p->st.ave) 
  410. { 
  411. temp=q->st; 
  412. q->st=p->st; 
  413. p->st=temp; 
  414. } 
  415. } 
  416. }  
  417. } 

以上所述就是本文的全部内容了,希望大家能够喜欢。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表