找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1790|回复: 1
打印 上一主题 下一主题
收起左侧

C语言学生成绩管理系统

[复制链接]
跳转到指定楼层
楼主
ID:949201 发表于 2021-7-5 08:57 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4. #include <string.h>
  5. struct student
  6. {
  7. int num;
  8. char name[20];
  9. float score1;
  10. float score2;
  11. float score3;
  12. float average;
  13. };
  14. struct student stu[50];
  15. struct student avg;
  16. int count=0,passwd=1;
  17. int k=0;
  18. void input();
  19. void display();
  20. void sort();
  21. void sort_name();
  22. void sort_score1();
  23. void sort_score2();
  24. void sort_score3();
  25. void sort_average();
  26. void query();
  27. void query_num();
  28. void query_name();
  29. void update();
  30. void update_num();
  31. void update_name();
  32. void re();
  33. void re_num();
  34. void re_name();
  35. void renew();
  36. void renew_num();
  37. void renew_name();
  38. void pa();
  39. void list();
  40. void main()
  41. {
  42. int p;
  43. system("cls");
  44. printf("\n\n\n\n\n\n\n\t\t\t请输入1启动: ");
  45. scanf("%d",&p);
  46. if (p==passwd)
  47. list();
  48. else
  49. {
  50. printf("\n\n\n\n输入错误!任意键重新输入!");
  51. getch();
  52. main();        

  53. }

  54. }
  55. void list()
  56. {
  57. int a;
  58. system("cls");

  59. printf("----------------学生成绩管理系统----------------\n\n\n");
  60. printf("1.录入\n2.显示\n3.排序\n4.查询\n5.修改\n6.删除\n7.恢复\n8.退出\n\n");
  61. printf("请选择: ");
  62. scanf("%d",&a);
  63. switch(a)
  64.          {
  65.           case 1:
  66.           input();
  67.           break;
  68.           case 2:
  69.           display();
  70.           break;
  71.           case 3:
  72.           sort();
  73.           break;
  74.           case 4:
  75.           query();
  76.           break;
  77.           case 5:
  78.           update();
  79.           break;
  80.           case 6:
  81.           re();
  82.           break;
  83.           case 7:
  84.           renew();
  85.           break;
  86.           case 8:
  87.           break;
  88.           default:
  89.           printf("选择错误!");
  90.           break;
  91.          }
  92. }
  93. void input()
  94. {
  95. char select;
  96. system("cls");
  97. printf("学生成绩录入\n\n");
  98. do
  99.          {
  100.           printf("\n请输入第%d个学生信息\n\n",count+1);
  101. printf("学号: ");
  102. scanf("%d",&stu[count].num);
  103. printf("姓名: ");  
  104. scanf("%s",stu[count].name);
  105. printf("成绩一:");
  106. scanf("%f",&stu[count].score1);
  107. printf("成绩二:");
  108. scanf("%f",&stu[count].score2);
  109. printf("成绩三:");
  110. scanf("%f",&stu[count].score3);
  111. stu[count].average=(stu[count].score1+stu[count].score2+stu[count].score3)/3;
  112. printf("是否继续输入(y/n): ");
  113. fflush(stdin);
  114. select=getchar();
  115. count++;
  116. }while(select=='y'||select=='Y');
  117. if (select=='n'||select=='N')
  118. {  
  119. list();
  120. }
  121. }
  122. void display()
  123. {
  124. int i;
  125. system("cls");
  126. printf("学生成绩显示:\n\n");
  127. printf("%-8s%-12s%-12s%-12s%-12s%s\n","学号","姓 名","成绩一","成绩二","成绩三","平均成绩");
  128. printf("%-8s%-12s%-12s%-12s%-12s%s\n","====","========","=======","=======","========","========");
  129. for (i=0;i<count;i++)
  130. {
  131. printf("%-8d%-12s%-12.2f%-12.2f%-12.2f%.2f\n",stu[i].num,stu[i].name,stu[i].score1,stu[i].score2,stu[i].score3,stu[i].average);
  132. }
  133. printf("任意键返回主菜单......");
  134. getch();
  135. list();
  136. }
  137. void sort()
  138. {
  139. int select;
  140. system("cls");
  141. printf("学生成绩排序:\n\n\n");
  142. printf("\n\t\t\t1.按姓名排序\n\t\t\t2.按成绩一排序\n\t\t\t3.按成绩二排序\n\t\t\t4.按成绩三排序\n\t\t\t5.按平均分排序\n\t\t\t6.返回\n\n");
  143. printf("请选择: ");
  144. scanf("%d",&select);
  145. switch(select)
  146. {
  147. case 1:
  148. sort_name();
  149. break;
  150. case 2:
  151. sort_score1();
  152. break;
  153. case 3:
  154. sort_score2();
  155. break;
  156. case 4:
  157. sort_score3();
  158. break;
  159. case 5:
  160. sort_average();
  161. break;
  162. case 6:
  163. list();
  164. break;
  165. default:
  166. printf("输入错误!任意键返回主菜单!");
  167. getch();
  168. list();
  169. break;
  170. }
  171. }
  172. void sort_name()
  173. {
  174. int i;
  175. struct student temp;
  176. system("cls");
  177. printf("按姓名排序: \n\n");
  178. printf("%-8s%-12s%-12s%-12s%-12s%s\n","学号","姓 名","成绩一","成绩二","成绩三","平均成绩");
  179. printf("%-8s%-12s%-12s%-12s%-12s%s\n","====","========","=======","=======","========","========");
  180. for (i=0;i<count;i++)
  181. {
  182. if (strlen(stu[i].name)<strlen(stu[i+1].name))
  183. {
  184. temp=stu[i];
  185. stu[i]=stu[i+1];
  186. stu[i+1]=temp;
  187. }
  188. }
  189. for (i=0;i<count;i++)
  190. {
  191. printf("%-8d%-12s%-12.2f%-12.2f%-12.2f%.2f\n",stu[i].num,stu[i].name,stu[i].score1,stu[i].score2,stu[i].score3,stu[i].average);
  192. }
  193. printf("任意键返回......");
  194. getch();
  195. sort();
  196. }
  197. void sort_score1()
  198. {
  199. int i;
  200. struct student temp;
  201. system("cls");
  202. printf("按成绩一排序: \n\n");
  203. printf("%-8s%-12s%-12s%-12s%-12s%s\n","学号","姓 名","成绩一","成绩二","成绩三","平均成绩");
  204. printf("%-8s%-12s%-12s%-12s%-12s%s\n","====","========","=======","=======","========","========");
  205. for (i=0;i<count;i++)
  206. {
  207. if (stu[i].score1<stu[i+1].score1)
  208. {
  209. temp=stu[i];
  210. stu[i]=stu[i+1];
  211. stu[i+1]=temp;
  212. }
  213. }
  214. for (i=0;i<count;i++)
  215. {
  216. printf("%-8d%-12s%-12.2f%-12.2f%-12.2f%.2f\n",stu[i].num,stu[i].name,stu[i].score1,stu[i].score2,stu[i].score3,stu[i].average);
  217. }
  218. printf("任意键返回......");
  219. getch();
  220. sort();
  221. }
  222. void sort_score2()
  223. {
  224. int i;
  225. struct student temp;
  226. system("cls");
  227. printf("按成绩一排序: \n\n");
  228. printf("%-8s%-12s%-12s%-12s%-12s%s\n","学号","姓 名","成绩一","成绩二","成绩三","平均成绩");
  229. printf("%-8s%-12s%-12s%-12s%-12s%s\n","====","========","=======","=======","========","========");
  230. for (i=0;i<count;i++)
  231. {
  232. if (stu[i].score2<stu[i+1].score2)
  233. {
  234. temp=stu[i];
  235. stu[i]=stu[i+1];
  236. stu[i+1]=temp;
  237. }
  238. }
  239. for (i=0;i<count;i++)
  240. {
  241. printf("%-8d%-12s%-12.2f%-12.2f%-12.2f%.2f\n",stu[i].num,stu[i].name,stu[i].score1,stu[i].score2,stu[i].score3,stu[i].average);
  242. }
  243. printf("任意键返回......");
  244. getch();
  245. sort();
  246. }
  247. void sort_score3()
  248. {
  249. int i;
  250. struct student temp;
  251. system("cls");
  252. printf("按成绩一排序: \n\n");
  253. printf("%-8s%-12s%-12s%-12s%-12s%s\n","学号","姓 名","成绩一","成绩二","成绩三","平均成绩");
  254. printf("%-8s%-12s%-12s%-12s%-12s%s\n","====","========","=======","=======","========","========");
  255. for (i=0;i<count;i++)
  256. {
  257. if (stu[i].score2<stu[i+1].score3)
  258. {
  259. temp=stu[i];
  260. stu[i]=stu[i+1];
  261. stu[i+1]=temp;
  262. }
  263. }
  264. for (i=0;i<count;i++)
  265. {
  266. printf("%-8d%-12s%-12.2f%-12.2f%-12.2f%.2f\n",stu[i].num,stu[i].name,stu[i].score1,stu[i].score2,stu[i].score3,stu[i].average);
  267. }
  268. printf("任意键返回......");
  269. getch();
  270. sort();
  271. }
  272. void sort_average()
  273. {
  274. int i;
  275. struct student temp;
  276. system("cls");
  277. printf("按平均分排序: \n\n");
  278. printf("%-8s%-12s%-12s%-12s%-12s%s\n","学号","姓 名","成绩一","成绩二","成绩三","平均成绩");
  279. printf("%-8s%-12s%-12s%-12s%-12s%s\n","====","========","=======","=======","========","========");
  280. for (i=0;i<count;i++)
  281. {
  282. if (stu[i].average<stu[i+1].average)
  283. {
  284. temp=stu[i];
  285. stu[i]=stu[i+1];
  286. stu[i+1]=temp;
  287. }
  288. }
  289. for (i=0;i<count;i++)
  290. {
  291. printf("%-8d%-12s%-12.2f%-12.2f%-12.2f%.2f\n",stu[i].num,stu[i].name,stu[i].score1,stu[i].score2,stu[i].score3,stu[i].average);
  292. }
  293. printf("任意键返回......");
  294. getch();
  295. sort();
  296. }
  297. void query()
  298. {
  299. int select;
  300. system("cls");
  301. printf("学生成绩查询:");
  302. printf("\n\n\n\t\t\t1.按学号查询\n\n\t\t\t2.按姓名查询\n\n\t\t\t3.返回");
  303. printf("\n\n请选择: ");
  304. scanf("%d",&select);
  305. switch(select)
  306. {
  307. case 1:
  308. query_num();
  309. break;
  310. case 2:
  311. query_name();
  312. break;
  313. case 3:
  314. list();
  315. break;
  316. default:
  317. printf("输入错误!任意键返回主菜单!");
  318. getch();
  319. list();
  320. }
  321. }
  322. void query_num()
  323. {
  324. int q,i;
  325. system("cls");
  326. printf("按学生学号查询:\n\n");
  327. printf("请输入要查询学生的学号:");
  328. scanf("%d",&q);
  329. for (i=0;i<count;i++)
  330. {
  331. if (stu[i].num==q)
  332. {
  333. printf("\n%-8s%-12s%-12s%-12s%-12s%s\n","学号","姓 名","成绩一","成绩二","成绩三","平均成绩");
  334. printf("%-8s%-12s%-12s%-12s%-12s%s\n","====","========","=======","========","========","========");
  335. printf("%-8d%-12s%-12.2f%-12.2f%-12.2f%.2f\n",stu[i].num,stu[i].name,stu[i].score1,stu[i].score2,stu[i].score3,stu[i].average);
  336. printf("查询完毕,任意键返回!");
  337. getch();
  338. query();
  339. break;
  340. }
  341. else  
  342. continue;
  343. }
  344. printf("对不起,您所查询的学生不存在!任意键返回!");
  345. getch();
  346. query();
  347. }
  348. void query_name()
  349. {
  350. int i,j;
  351. char find[20];
  352. system("cls");
  353. printf("按学生姓名查询\n\n\n");
  354. printf("输入要查询的学生的姓名: ");
  355. scanf("%s",find);
  356. for        (i=0;i<count;i++)
  357. {
  358. j=strcmp(find,stu[i].name);  
  359. if (j==0)
  360. {
  361. printf("\n%-8s%-12s%-12s%-12s%-12s%s\n","学号","姓 名","成绩一","成绩二","成绩三","平均成绩");
  362. printf("%-8s%-12s%-12s%-12s%-12s%s\n","====","========","=======","=======","========","========");
  363. printf("%-8d%-12s%-12.2f%-12.2f%-12.2f%.2f\n",stu[i].num,stu[i].name,stu[i].score1,stu[i].score2,stu[i].score3,stu[i].average);
  364. printf("\n\n查询完毕,任意键返回!");
  365. getch();
  366. query();
  367. break;
  368. }

  369. else  
  370. continue;
  371. }
  372. printf("对不起,您所查询的学生不存在!任意键返回!");
  373. getch();
  374. query();
  375. }
  376. void update()
  377. {
  378. int select;
  379. system("cls");
  380. printf("修改学生信息:\n\n\n");
  381. printf("\t\t\t1.按学号修改\n\n\t\t\t2.按姓名修改\n\n\t\t\t3.返回");
  382. printf("\n\n请选择:");
  383. scanf("%d",&select);
  384. switch(select)
  385. {
  386. case 1:
  387. update_num();
  388. break;
  389. case 2:
  390. update_name();
  391. break;
  392. case 3:
  393. list();
  394. break;
  395. default:
  396. printf("输入错误!任意键返回!");
  397. getch();
  398. list();
  399. break;
  400. }
  401. }
  402. void update_num()
  403. {
  404. int i,find;
  405. system("cls");
  406. printf("按学生学号修改: \n\n");
  407. printf("请输入要修改的学生的学号: ");
  408. scanf("%d",&find);
  409. for (i=0;i<count;i++)
  410. {
  411. if (stu[i].num==find)
  412. {
  413. printf("\n\n学号: ");
  414. scanf("%d",&stu[i].num);
  415. printf("姓名: ");
  416. scanf("%s",stu[i].name);
  417. printf("成绩一: ");
  418. scanf("%f",&stu[i].score1);
  419. printf("成绩二: ");
  420. scanf("%f",&stu[i].score2);
  421. printf("成绩三: ");
  422. scanf("%f",&stu[i].score3);
  423. printf("修改成功!任意键返回主菜单!");
  424. getch();
  425. list();
  426. break;
  427. }
  428. else
  429. continue;
  430. }

  431. printf("您所要修改的学生不存在,任意键返回主菜单!");
  432. getch();
  433. list();
  434. }
  435. void update_name()
  436. {
  437. int i,j=0;
  438. char find[20];
  439. system("cls");
  440. printf("按学生姓名修改: \n\n");
  441. printf("请输入要修改的学生的姓名: ");
  442. scanf("%s",find);
  443. for (i=0;i<count;i++)
  444. {
  445. j=strcmp(find,stu[i].name);  
  446. if (j==0)
  447. {
  448. printf("\n\n学号: ");
  449. scanf("%d",&stu[i].num);
  450. printf("姓名: ");
  451. scanf("%s",stu[i].name);
  452. printf("成绩一: ");
  453. scanf("%f",&stu[i].score1);
  454. printf("成绩二: ");
  455. scanf("%f",&stu[i].score2);
  456. printf("成绩三: ");
  457. scanf("%f",&stu[i].score3);
  458. printf("修改成功!任意键返回主菜单!");
  459. getch();
  460. list();
  461. break;
  462. }

  463. else
  464. continue;
  465. }

  466. printf("您所要修改的学生不存在,任意键返回主菜单!");
  467. getch();
  468. list();
  469. }
  470. void re()
  471. {
  472. int select;
  473. system("cls");
  474. printf("删除学生信息: \n\n\n");
  475. printf("\t\t\t1.按学号删除\n\n\t\t\t2.按姓名删除\n\n\t\t\t3.返回\n\n");
  476. printf("请选择: ");
  477. scanf("%d",&select);
  478. switch(select)
  479. {
  480. case 1:
  481. re_num();
  482. break;
  483. case 2:
  484. re_name();
  485. break;
  486. case 3:
  487. list();
  488. break;
  489. default:
  490. printf("输入错误!任意键返回!");
  491. getch();
  492. list();
  493. }
  494. }
  495. void re_num()
  496. {
  497. int i,find,j;
  498. system("cls");
  499. printf("按学号删除学生信息: \n\n");
  500. printf("请输入要删除的学生的学号: ");
  501. scanf("%d",&find);
  502. for (i=0;i<count;i++)
  503. {
  504. if (stu[i].num==find)
  505. {
  506. j=1;
  507. k=i;
  508. avg=stu[i];
  509. break;
  510. }
  511. else  
  512. continue;

  513. }

  514. if (j!=1)
  515. {
  516. printf("您所删除的学生不存在!任意键返回主菜单!");
  517. getch();
  518. list();
  519. }
  520. else
  521. for (i=k;i<count-1;i++)        
  522. {
  523. stu[i]=stu[i+1];

  524. }
  525. printf("删除成功!任意键返回主菜单!");        
  526. count--;
  527. getch();
  528. list();
  529. }
  530. void re_name()
  531. {
  532. int i,j,m;
  533. char find[20];
  534. system("cls");
  535. printf("按姓名删除学生信息: \n\n");
  536. printf("请输入要删除的学生的姓名: ");
  537. scanf("%s",find);
  538. for (i=0;i<count;i++)
  539. {
  540. m=strcmp(find,stu[i].name);         
  541. if (m==0)
  542. {
  543. j=1;
  544. k=i;
  545. avg=stu[i];
  546. break;
  547. }
  548. else  
  549. continue;

  550. }

  551. if (j!=1)
  552. {
  553. printf("您所删除的学生不存在!任意键返回主菜单!");
  554. getch();
  555. list();
  556. }
  557. else
  558. for (i=k;i<count-1;i++)        
  559. {
  560. stu[i]=stu[i+1];

  561. }
  562. printf("删除成功!任意键返回主菜单!");        
  563. count--;
  564. getch();
  565. s();
  566. }
  567. void renew()
  568. {
  569. int select;
  570. system("cls");
  571. printf("恢复学生信息: \n\n\n");
  572. printf("\t\t\t1.按学号恢复\n\n\t\t\t2.按姓名恢复\n\n\t\t\t3.返回\n\n");
  573. printf("请选择: ");
  574. scanf("%d",&select);
  575. switch(select)
  576. {
  577. case 1:
  578. renew_num();
  579. break;
  580. case 2:
  581. renew_name();
  582. break;
  583. case 3:
  584. list();
  585. break;
  586. default:
  587. printf("输入错误!任意键返回!");
  588. getch();
  589. list();
  590. }
  591. }
  592. void renew_num()
  593. {
  594. int i,del;
  595. system("cls");
  596. printf("请输入要恢复的学生的学号: ");
  597. scanf("%d",&del);
  598. if (del==avg.num)
  599. {
  600. for (i=k;i<count;i++)
  601. {
  602. stu[i+1]=stu[i];
  603. stu[i]=avg;
  604. break;
  605. }
  606. }
  607. else  

  608. {
  609. printf("您要恢复的学生不存在,任意键返回主菜单!");
  610. getch();
  611. list();
  612. }
  613. count++;
  614. printf("恢复成功!");
  615. getch();
  616. list();
  617. }
  618. void renew_name()
  619. {
  620. int i,j;
  621. char del[20];
  622. system("cls");
  623. printf("请输入要恢复的学生的姓名: ");
  624. scanf("%s",del);
  625. j=strcmp(del,avg.name);         
  626. if (j==0)
  627. {
  628. for (i=k;i<count;i++)
  629. {
  630. stu[i+1]=stu[i];
  631. stu[i]=avg;
  632. break;
  633. }
  634. }

  635. else  

  636. {
  637. printf("您要恢复的学生不存在,任意键返回主菜单!");
  638. getch();
  639. list();
  640. }
  641. count++;
  642. printf("恢复成功!");
  643. getch();
  644. list();
  645. }

复制代码

评分

参与人数 1黑币 +30 收起 理由
admin + 30 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:958828 发表于 2021-8-5 09:54 | 只看该作者
问题1:退出没有写入文件,数据会丢:
问题2:修改后,平均成绩不会更新
问题3:排序算法错误,冒泡只写了一层循环
问题4:报错信息不及时更新,新的错误提示旧的错误信息(偶尔出现)
问题5:578行list() 写成s();
建议main函数最好还是写成int main() 加 return 0;
整体写的不错
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表