C语言学生信息管理系统(链表实现)

偏执的太偏执、 2024-04-17 06:28 193阅读 0赞

基础功能:

  • 使用链表
  • 使用结构体
  • 对学生信息的增删改查
  • 按姓名或者学号进行学生成绩的排序

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. #define M 2
  5. #define N 4
  6. typedef struct score//定义成绩结构体
  7. {
  8. int math;//数学
  9. int c_language;//c语言
  10. int english;//英语
  11. double ave;//平均分
  12. int total_sum;//总分
  13. }SCORE;
  14. typedef struct stu
  15. {
  16. char id[20];//学号
  17. char name [10];//姓名
  18. char sex[20];//性别
  19. char pro[20];//专业
  20. int year;//入学年份
  21. SCORE stu_score;//学生成绩
  22. char stu_lesson[N][50];//学生选课
  23. }STU;
  24. typedef struct list
  25. {
  26. STU data;
  27. struct list *next;
  28. }LIST;//创建链表结构体
  29. LIST *Creat_List(STU s[]);//链表的创建
  30. int Stu_Entry(STU stu[]);//学生信息的录入
  31. int Score_Entry(STU score[]);//学生成绩的录入
  32. void S_score(LIST*head);//利用链表进行成绩的输出
  33. void infor_show(LIST*head);//利用链表对学生信息进行输出
  34. void S_sort(STU s[],int flag);//利用结构体进行排序
  35. SCORE Score_search(char *s,LIST *p,int flag);//成绩的查询
  36. LIST *Delet_Score(char*s,LIST*tr,int flag);//成绩的删除
  37. LIST *Delet_infor(char*st,LIST *ht,int flag);//学生记录的删除
  38. LIST *Re_score(char *sr,SCORE R_score,LIST *h,int flag);//成绩修改
  39. void Show_infor(char *S_s,LIST*h,int flag);//学生信息的查询
  40. int Elect_les(STU lesson[]);//学生选课
  41. LIST* Insert_infor(LIST *in_infor );//学生信息的添加
  42. void main()
  43. {
  44. printf("\t\t\t欢迎进入学生信息管理系统\n");
  45. system("color 9");
  46. int choice;//输入你的选择
  47. char user[23];//输入用户名
  48. char key[20];//输入密码
  49. STU stu[M];//结构体数组
  50. LIST *head;//链表的头结点
  51. head=Creat_List(stu);
  52. printf("请输入用户名:\n");
  53. scanf("%s",user);
  54. printf("请输入密码:\n");
  55. scanf("%s",key);
  56. printf("*******************************\n");
  57. printf("* 1---信息录入 *\n");
  58. printf("* 2---成绩录入 *\n");
  59. printf("* 3---成绩输出 *\n");
  60. printf("* 4---成绩排序 *\n");
  61. printf("* 5---成绩查询 *\n");
  62. printf("* 6---成绩修改 *\n");
  63. printf("* 7---信息添加 *\n");
  64. printf("* 8---信息删除 *\n");
  65. printf("* 9---成绩删除 *\n");
  66. printf("* 10--信息查询 *\n");
  67. printf("* 11--信息输出 *\n");
  68. printf("* 12--学生选课 *\n");
  69. printf("* 13--退出程序 *\n");
  70. printf("*******************************\n");
  71. if(strcmp(user,"admin")==0&&strcmp(key,"123456")==0)
  72. {
  73. printf("请输入你的选择:\n");
  74. while(scanf("%d",&choice)!=EOF)
  75. {
  76. system("cls");
  77. printf("*******************************\n");
  78. printf("* 1---信息录入 *\n");
  79. printf("* 2---成绩录入 *\n");
  80. printf("* 3---成绩输出 *\n");
  81. printf("* 4---成绩排序 *\n");
  82. printf("* 5---成绩查询 *\n");
  83. printf("* 6---成绩删除 *\n");
  84. printf("* 7---信息添加 *\n");
  85. printf("* 8---信息删除 *\n");
  86. printf("* 9---成绩修改 *\n");
  87. printf("* 10--信息查询 *\n");
  88. printf("* 11--信息输出 *\n");
  89. printf("* 12--学生选课 *\n");
  90. printf("* 13--退出程序 *\n");
  91. printf("*******************************\n");
  92. switch(choice)
  93. {
  94. case 1:{
  95. printf("请录入学生的信息:\n");
  96. if(Stu_Entry(stu))
  97. printf("信息录入完成\n");
  98. else
  99. printf("信息录入失败\n");
  100. system("pause");break;
  101. }
  102. case 2:{
  103. printf("请录入每位学生对应的成绩:\n");
  104. if(Score_Entry(stu))
  105. printf("学生成绩录入完成\n");
  106. else
  107. printf("学生成绩录入失败\n");
  108. system("pause");break;
  109. }
  110. case 3:{
  111. printf("成绩的输出为:\n");
  112. S_score(Creat_List(stu));
  113. system("pause");break;
  114. }
  115. case 4:{
  116. int choice;
  117. printf("请输入排序的方式(1-平均分排序 2-总分排序)\n");
  118. scanf("%d",&choice);
  119. printf("成绩排序的结果为:\n");
  120. S_sort(stu,choice);
  121. S_score(Creat_List(stu));
  122. system("pause");break;
  123. system("pause");break;
  124. }
  125. case 5:{
  126. SCORE result;
  127. char name_search[20];
  128. char id_search[20];
  129. int choice;
  130. printf("请输入查询方式(1-学号查询 2-姓名查询):\n");
  131. scanf("%d",&choice);
  132. switch(choice)
  133. {
  134. case 1:
  135. { printf("请输入需要查询的学生的学号:\n");
  136. scanf("%s",id_search);
  137. printf("学号为%s学生的数学 c语言 英语成绩 总分以及平均分为:\n",id_search);
  138. result= Score_search(id_search,Creat_List(stu),1);
  139. printf("查询的结果为:\n");
  140. printf("%d %d %d %d %.1lf\n",result.math,
  141. result.c_language,result.english,result.total_sum,result.ave);
  142. break;
  143. }
  144. case 2:{
  145. printf("请输入需要查询的学生的姓名:\n");
  146. scanf("%s",name_search);
  147. printf("学生%s的数学 c语言 英语成绩 总分以及平均分为:\n",name_search);
  148. result= Score_search(id_search,Creat_List(stu),2);
  149. printf("查询的结果为:\n");
  150. printf("%d %d %d %d %.1lf\n",result.math,
  151. result.c_language,result.english,result.total_sum,result.ave);
  152. break;
  153. }
  154. }
  155. system("pause");break;
  156. }
  157. case 6: {
  158. char id[20];//需要查询学号
  159. char name[20];//需要查询的姓名
  160. int choice;//输入你的选择
  161. printf("请输入查询方式(1-学号查询 2-姓名查询):\n");
  162. scanf("%d",&choice);
  163. switch(choice)
  164. {
  165. case 1:
  166. {
  167. printf("请输入要删除成绩学生的学号:\n");
  168. scanf("%s",id);
  169. if(Delet_Score(id,Creat_List(stu),1)!=NULL)
  170. {
  171. printf("成绩删除成功!\n");
  172. S_score(Delet_Score(id,Creat_List(stu),1));
  173. }
  174. else
  175. printf("成绩删除失败!\n");
  176. break;
  177. }
  178. case 2:{
  179. printf("请输入要删除信息学生的姓名:\n");
  180. scanf("%s",name);
  181. printf("信息删除后的结果为:\n");
  182. if(Delet_Score(name,Creat_List(stu),2)!=NULL)
  183. {
  184. printf("成绩删除成功!\n");
  185. S_score(Delet_Score(name,Creat_List(stu),2));
  186. }
  187. else
  188. printf("成绩删除失败!\n");
  189. break;
  190. }
  191. }
  192. system("pause");break;
  193. }
  194. case 7:{
  195. printf("插入后的结果为:\n");
  196. S_score(Insert_infor(Creat_List(stu)));
  197. system("pause");break;
  198. }
  199. case 8:{
  200. printf("学生信息的删除:\n");
  201. LIST *q;
  202. char id[20];
  203. char name[20];
  204. int choice;
  205. printf("请输入查询方式(1-学号查询 2-姓名查询):\n");
  206. scanf("%d",&choice);
  207. switch(choice)
  208. {
  209. case 1:
  210. {
  211. printf("请输入要删除信息学生的学号:\n");
  212. scanf("%s",id);
  213. q=Delet_infor(id,Creat_List(stu),1);
  214. if(q==NULL)
  215. printf("信息删除失败\n");
  216. else
  217. {
  218. printf("信息删除后的结果为:\n");
  219. infor_show(q);
  220. }
  221. break;
  222. }
  223. case 2:{
  224. printf("请输入要删除信息学生的姓名:\n");
  225. scanf("%s",name);
  226. printf("信息删除后的结果为:\n");
  227. if(q==NULL)
  228. printf("信息删除失败!\n");
  229. else
  230. {
  231. printf("信息删除后的结果为:\n");
  232. q=Delet_infor(name,Creat_List(stu),2);
  233. }
  234. infor_show(q);
  235. break;
  236. }
  237. }
  238. system("pause");break;
  239. }
  240. case 9:{
  241. int choice;
  242. SCORE re_score;
  243. int sum=0;
  244. char id[20];
  245. char name[20];
  246. printf("请输入要修改的英语 c语言 数学:\n");
  247. scanf("%d %d %d",&re_score.english,&re_score.c_language,
  248. &re_score.math);
  249. sum=re_score.english+re_score.c_language+re_score.math;
  250. re_score.ave=sum/3.0;
  251. printf("请输入查询方式(1-学号查询 2-姓名查询):\n");
  252. scanf("%d",&choice);
  253. switch(choice)
  254. {
  255. case 1:
  256. { printf("请输入要修改学生的学号:\n");
  257. scanf("%s",id);
  258. printf("成绩修改后的结果为:\n");
  259. S_score(Re_score(id,re_score,Creat_List(stu),choice));
  260. break;
  261. }
  262. case 2:{
  263. printf("请输入要修改学生的姓名:\n");
  264. scanf("%s",name);
  265. printf("成绩修改后的结果为:\n");
  266. S_score(Re_score(name,re_score,Creat_List(stu),choice));
  267. break;
  268. }
  269. }
  270. system("pause");break;
  271. }
  272. case 10:{
  273. char id[20];
  274. char name[20];
  275. int choice;
  276. printf("请输入查询方式(1-学号查询 2-姓名查询):\n");
  277. scanf("%d",&choice);
  278. switch(choice)
  279. {
  280. case 1:
  281. { printf("请输入要查询学生的学号:\n");
  282. scanf("%s",id);
  283. Show_infor(id,Creat_List(stu),choice);
  284. break;
  285. }
  286. case 2:{
  287. printf("请输入要查询学生的姓名:\n");
  288. scanf("%s",name);
  289. Show_infor(id,Creat_List(stu),choice);
  290. break;
  291. }
  292. }
  293. system("pause");break;
  294. }
  295. case 11: {
  296. printf("学生信息的输出结果为:\n");
  297. infor_show(Creat_List(stu));
  298. system("pause");break;
  299. }
  300. case 12:{ system("cls");
  301. //system("pause");
  302. printf("请输入选修课程中的四门课程:\n");
  303. if( Elect_les(stu))
  304. printf("学生选课成功!\n");
  305. else
  306. printf("学生选课失败!\n");
  307. break;
  308. }
  309. case 13: {
  310. printf("系统已经安全退出!!!");
  311. exit(0);
  312. system("pause");break;
  313. }
  314. default:{
  315. printf("输入的选择有误,请重新输入你的选择:\n");
  316. system("pause");break;
  317. }
  318. }
  319. printf("可以选择继续录入也可以按ctrl+z退出\n");
  320. }
  321. }
  322. else
  323. {
  324. system("cls");
  325. printf("输入用户名或者密码错误,请退出后重新输入!\n");
  326. }
  327. }
  328. LIST *Creat_List(STU *s)//PS:链表的建立
  329. {
  330. //尾插法进行建立链表
  331. LIST *h,*p;
  332. LIST*tail;
  333. int i;
  334. h=(LIST*)(malloc)(sizeof(LIST));
  335. if(h==NULL)
  336. return h;
  337. else
  338. tail=h;
  339. for(i=0;i<M;i++)
  340. {
  341. p=(LIST*)malloc(sizeof(LIST));
  342. if(p==NULL)
  343. return NULL;
  344. else
  345. {
  346. p->data=s[i];
  347. tail->next=p;
  348. tail=p;
  349. tail->next=NULL;
  350. }
  351. }
  352. //头插法进行创建链表
  353. /* LIST*h,*p;
  354. int i;
  355. h=(LIST*)malloc(sizeof(LIST));
  356. if(h==NULL)
  357. return NULL;
  358. else
  359. h->next=NULL;
  360. for(i=0;i<M;i++)
  361. {
  362. p=(LIST*)malloc(sizeof(LIST));
  363. if(p==NULL)
  364. return NULL;
  365. else
  366. {
  367. p->data=s[i];
  368. p->next=h->next;
  369. h->next=p;
  370. }
  371. }*/
  372. return h;
  373. }
  374. int Stu_Entry(STU stu[M])//PS:学生信息录入函数的封装
  375. {
  376. int i;
  377. for(i=0;i<M;i++)
  378. {
  379. printf("输入第%d个学生学号 姓名 性别 专业 入学年份: \n", i + 1);
  380. scanf("%s %s %s %s %d", stu[i].id, stu[i].name, stu[i].sex,
  381. &stu[i].pro, &stu[i].year);
  382. stu[i].stu_score.math=0;
  383. stu[i].stu_score.c_language=0;
  384. stu[i].stu_score.english=0;
  385. }
  386. if(i==M)
  387. return 1;
  388. else
  389. return 0;
  390. }
  391. int Score_Entry(STU score[])//PS:成绩的输入的函数的封装
  392. {
  393. int i;
  394. int s=0;
  395. STU *p=score;
  396. for(i=0;i<M;i++)
  397. {
  398. printf("请输入%d位学生的数学 c语言 英语:\n",i+1);
  399. scanf("%d %d %d",&score[i].stu_score.math,&score[i].stu_score.c_language,
  400. &score[i].stu_score.english);
  401. getchar();
  402. s=score[i].stu_score.math+score[i].stu_score.c_language+score[i].stu_score.english;
  403. score[i].stu_score.total_sum=s;
  404. score[i].stu_score.ave=s/3.0;
  405. }
  406. if(i==M)
  407. return 1;
  408. else
  409. return 0;
  410. }
  411. void S_score(LIST*head)//PS:成绩输出函数的封装
  412. {
  413. LIST *p;
  414. int count=1;
  415. p=head->next;
  416. if(p==NULL)
  417. {
  418. printf("链表发生错误!!!\n");
  419. return;
  420. }
  421. else
  422. {
  423. printf("HEAD->\n");
  424. while(p!=NULL)
  425. {
  426. printf("%d-学号:%s学生的数学 c语言 英语成绩 总分以及平均分为:\n",count,p->data.id);
  427. printf("%d %d %d %d %.1lf\n",p->data.stu_score.math,
  428. p->data.stu_score.c_language,
  429. p->data.stu_score.english,p->data.stu_score.total_sum,p->data.stu_score.ave);
  430. p=p->next;
  431. count++;
  432. }
  433. printf("->END\n");
  434. }
  435. }
  436. void S_sort(STU s[],int flag)//PS:成绩的排序函数的封装 利用结构体
  437. {
  438. int i,j;
  439. int k;
  440. STU t;
  441. if(flag==1)
  442. {
  443. for(i=0;i<M-1;i++)//选择排序
  444. { k=i;
  445. for(j=i;j<M;j++)
  446. if(s[k].stu_score.ave<s[j].stu_score.ave)
  447. k=j;
  448. if(k!=i)
  449. {
  450. t=s[k];
  451. s[k]=s[i];
  452. s[i]=t;
  453. }
  454. }
  455. }
  456. if(flag==2)
  457. {
  458. for(i=0;i<M-1;i++)//选择排序
  459. { k=i;
  460. for(j=i;j<M;j++)
  461. if(s[k].stu_score.total_sum<s[j].stu_score.total_sum)
  462. k=j;
  463. if(k!=i)
  464. {
  465. t=s[k];
  466. s[k]=s[i];
  467. s[i]=t;
  468. }
  469. }
  470. }
  471. }
  472. SCORE Score_search(char *s,LIST *h,int flag)//PS:成绩的查询函数的封装
  473. {
  474. LIST *p;
  475. p=h;
  476. if(p==NULL)
  477. {
  478. printf("链表有误!\n");
  479. }
  480. else
  481. {
  482. /*while(p!=NULL)
  483. {
  484. if(strcmp(p->data.id,s)==0)
  485. {
  486. break;
  487. }
  488. p=p->next;
  489. }
  490. return (p->data.stu_score);*/
  491. if(strcmp(p->data.id,s)==0&&flag==1)
  492. return (p->data.stu_score);
  493. else if(strcmp(p->data.name,s)==0&&flag==2)
  494. {
  495. return (p->data.stu_score);
  496. }
  497. else
  498. return (Score_search(s,p->next,flag));//递归
  499. }
  500. }
  501. LIST* Delet_Score(char* s,LIST *tr,int flag)//PS:删除成绩函数的封装
  502. {
  503. LIST *p;
  504. p=tr->next;
  505. if(p==NULL)
  506. {
  507. printf("链表有误,成绩删除失败!\n");
  508. return 0;
  509. }
  510. else
  511. { if(flag==1)
  512. {
  513. while(p!=NULL)
  514. {
  515. if(strcmp(p->data.id,s)==0)
  516. {
  517. p->data.stu_score.c_language=0;
  518. p->data.stu_score.english=0;
  519. p->data.stu_score.math=0;
  520. p->data.stu_score.total_sum=0;
  521. p->data.stu_score.ave=0.0;
  522. break;
  523. }
  524. p=p->next;
  525. }
  526. }
  527. if(flag==2)
  528. {
  529. while(p!=NULL)
  530. {
  531. if(strcmp(p->data.id,s)==0)
  532. {
  533. p->data.stu_score.c_language=0;
  534. p->data.stu_score.english=0;
  535. p->data.stu_score.math=0;
  536. p->data.stu_score.ave=0.0;
  537. break;
  538. }
  539. p=p->next;
  540. }
  541. }
  542. if(p==NULL)
  543. return NULL;
  544. else
  545. return tr;
  546. }
  547. }
  548. LIST* Delet_infor(char *st,LIST *ht,int flag)//PS:学生信息删除函数的封装
  549. {
  550. LIST*p;
  551. LIST*tem;
  552. p=ht->next;
  553. if(p==NULL)
  554. {
  555. printf("链表错误!\n");
  556. return NULL;
  557. }
  558. else
  559. {
  560. while(p!=NULL)
  561. {
  562. if(strcmp(p->data.id,st)==0&&flag==1)
  563. {
  564. tem=p;
  565. break;
  566. }
  567. if(strcmp(p->data.name,st)==0&&flag==2)
  568. {
  569. tem=p;
  570. break;
  571. }
  572. p=p->next;
  573. }
  574. while(strcmp(p->data.id,st)!=0&& p->next!=NULL)//PS:循环查找要删除的节点
  575. {
  576. tem=p;
  577. p=p->next;
  578. }
  579. if( strcmp(p->data.id,st)==0)//找到了一个节点的num和num相等
  580. {
  581. if(p == ht->next)//找到的节点是头节点后面的一个节点
  582. {
  583. ht->next= p->next;
  584. }
  585. else
  586. {
  587. tem->next=p->next;
  588. }
  589. return ht;
  590. }
  591. else
  592. return ht=NULL;
  593. }
  594. }
  595. LIST *Re_score(char *sr,SCORE R_score,LIST *h,int flag)//PS:成绩修改函数的封装
  596. {
  597. //LIST *q;
  598. //q=h;
  599. LIST *p;
  600. p=h->next;
  601. if(p==NULL)
  602. {
  603. printf("链表发生错误,成绩修改失败!\n");
  604. return p;
  605. }
  606. else
  607. {
  608. while(p!=NULL)
  609. {
  610. if(strcmp(p->data.id,sr)==0&&flag==1)
  611. {
  612. break;
  613. }
  614. if(strcmp(p->data.id,sr)==0&&flag==2)
  615. {
  616. break;
  617. }
  618. p=p->next;
  619. }
  620. p->data.stu_score= R_score;
  621. }
  622. return h;
  623. }
  624. void infor_show(LIST*h)//PS:学生信息全部输出函数的封装
  625. {
  626. LIST *p;
  627. int i;
  628. p=h->next;
  629. if(p==NULL)
  630. printf("链表发生错误!!!\n");
  631. else
  632. {
  633. while(p!=NULL)
  634. { printf("学号%s的学生信息:\n",p->data.id);
  635. printf("姓名:%s 性别:%s 专业:%s 入学年份:%d\n",p->data.name,p->data.sex,p->data.pro,p->data.year);
  636. printf("该学生的选择的选修课为:\n");
  637. for(i=0;i<N;i++)
  638. printf("%s\t",p->data.stu_lesson[i]);
  639. p=p->next;
  640. printf("\n");
  641. }
  642. }
  643. }
  644. void Show_infor(char *S_s,LIST*h,int flag)//PS:信息查询输出函数的封装
  645. {
  646. LIST*p;
  647. p=h->next;
  648. int i;
  649. if(p==NULL)
  650. printf("链表出现错误!!!\n");
  651. else
  652. {
  653. while(p!=NULL)
  654. {
  655. if(strcmp(p->data.id,S_s)==0&&flag==1)
  656. break;
  657. if(strcmp(p->data.id,S_s)==0&&flag==2)
  658. break;
  659. p=p->next;
  660. }
  661. printf("学生的姓名 性别 专业 入学年份\n");
  662. printf("%s %s %s %d\n",p->data.name,p->data.sex,p->data.pro,p->data.year);
  663. printf("该学生的选择的选修课为:\n");
  664. for(i=0;i<N;i++)
  665. printf("%s\t",p->data.stu_lesson[i]);
  666. }
  667. }
  668. int Elect_les(STU lesson[])//PS:选修课的录入
  669. {
  670. int i,j;
  671. printf("*******************************\n");
  672. printf("* 1---语言文学 *\n");
  673. printf("* 2---人文地理 *\n");
  674. printf("* 3---思想政治 *\n");
  675. printf("* 4---口才艺术 *\n");
  676. printf("* 5---经济管理 *\n");
  677. printf("* 6---国际贸易 *\n");
  678. printf("* 7---职场礼仪 *\n");
  679. printf("*******************************\n");
  680. for(i=0;i<M;i++)
  681. {
  682. printf("请输入第%d位学生的选修的课程:\n",i+1);
  683. for(j=0;j<N;j++)
  684. {
  685. scanf("%s",lesson[i].stu_lesson[j]);
  686. getchar();
  687. }
  688. }
  689. if(i==M)
  690. return 1;
  691. else
  692. return 0;
  693. }
  694. LIST* Insert_infor(LIST *in_infor)//PS:学生信息添加函数的封装
  695. {
  696. LIST *p;
  697. LIST *st;
  698. LIST *q;
  699. int s=0;
  700. double ave=0;
  701. st=in_infor->next;
  702. p=(LIST*)malloc(sizeof (LIST));
  703. printf("请输入要填加的学生的学号 姓名 性别 专业 入学年份:\n");
  704. scanf("%s %s %s %s %d", p->data.id, p->data.name, p->data.sex,
  705. p->data.pro,&p->data.year);
  706. printf("请输入要填加的学生的成绩:\n");
  707. scanf("%d %d %d",&p->data.stu_score.math,&p->data.stu_score.english,
  708. &p->data.stu_score.c_language);
  709. s=p->data.stu_score.c_language+p->data.stu_score.english+p->data.stu_score.math;
  710. p->data.stu_score.ave=s/3.0;
  711. //st=in_infor->next;
  712. if(st==NULL)
  713. {
  714. printf("链表发生错误!\n");
  715. return NULL;
  716. }
  717. else
  718. {
  719. /*in_infor->next=p;
  720. p->next=st;
  721. return in_infor;*/
  722. while(st!=NULL)
  723. {
  724. if(st->data.stu_score.ave<p->data.stu_score.ave)
  725. break;
  726. st=st->next;
  727. }
  728. q=in_infor;
  729. while(q->next!=st)
  730. q=q->next;
  731. q->next=p;
  732. p->next=st;
  733. return in_infor;
  734. }
  735. }

发表评论

表情:
评论列表 (有 0 条评论,193人围观)

还没有评论,来说两句吧...

相关阅读