飞道的博客

C语言学生成绩管理系统

350人阅读  评论(0)

 程序示例精选

C语言学生成绩管理系统

如需安装运行环境或远程调试,见文章底部微信名片,由专业技术人员远程协助!

前言

这篇博客针对<<C语言学生成绩管理系统>>编写代码,代码整洁,规则,易读。 学习与应用推荐首选。


文章目录

        一、所需工具软件

        二、使用步骤

                1. 引入库

                2. 创建结构体

                3. 创建元素操作函数

                4. 运行结果

         三在线协助


一、所需工具软件

          1. Visual Studio

          2. C/C++

二、使用步骤

1.引入库

代码如下(示例):


  
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<stdlib.h>
  4. #include<string.h>

2.创建结构体

代码如下(示例):


  
  1. struct Student { //每个学生对应一个结构体
  2. char ID[ 20]; //学号
  3. char Name[ 10]; //姓名
  4. float Mark1; //语文成绩
  5. float Mark2; //数学成绩
  6. float Mark3; //英语成绩
  7. float Mark4; //计算机成绩
  8. float All; //总分
  9. float Average; //平均成绩
  10. }students[ 1000];

该处使用的url网络请求的数据。

3.创建元素操作函数:

代码如下(示例):


  
  1. int num = 0; //计数器
  2. void Copy(struct Student* arr, int i, int j)
  3. {
  4. strcpy(arr[i].ID, arr[j].ID);
  5. strcpy(arr[i].Name, arr[j].Name);
  6. arr[i].Mark1 = arr[j].Mark1;
  7. arr[i].Mark2 = arr[j].Mark2;
  8. arr[i].Mark3 = arr[j].Mark3;
  9. arr[i].Mark4 = arr[j].Mark4;
  10. arr[i].All = arr[j].All;
  11. arr[i].Average = arr[j].Average;
  12. }
  13. int Student_SearchByName(char name[])//通过姓名来检索学生
  14. {
  15. int i;
  16. for (i = 0; i < num; i++)
  17. {
  18. if ( strcmp(students[i].Name, name) == 0) //通过strcmp函数来对比学生姓名,找到返回在数组的位置
  19. {
  20. return i;
  21. }
  22. }
  23. return -1; //未找到返回 -1
  24. }
  25. int Student_SearchByIndex(char id[])//通过学号来检索学生信息
  26. {
  27. int i;
  28. for (i = 0; i < num; i++)
  29. {
  30. if ( strcmp(students[i].ID, id) == 0) //通过strcmp函数来对比学生id,找到返回位置
  31. {
  32. return i;
  33. }
  34. }
  35. return -1; //未找到返回 -1
  36. }
  37. void Student_DisplaySingle(int index)//输出表头
  38. {
  39. printf( "%10s%10s%8s%8s%8s%10s\n", "学号", "姓名", "语文", "数学", "英语", "计算机", "总成绩", "平均成绩");
  40. printf( "-------------------------------------------------------------\n");
  41. printf( "%10s%10s%8.2f%8.2f%8.2f%8.2f%10.2f%10.2f\n", students[index].ID, students[index].Name,
  42. students[index].Mark1, students[index].Mark2, students[index].Mark3, students[index].Mark4, students[index].All, students[index].Average);
  43. }
  44. void inputt()//利用循环录入学生信息
  45. {
  46. while ( 1)
  47. {
  48. printf( "请输入学号:");
  49. scanf( "%s", &students[num].ID);
  50. getchar();
  51. printf( "请输入姓名:");
  52. scanf( "%s", &students[num].Name);
  53. getchar();
  54. printf( "请输入成绩:");
  55. scanf( "%f", &students[num].Mark1);
  56. getchar();
  57. printf( "请输入成绩:");
  58. scanf( "%f", &students[num].Mark2);
  59. getchar();
  60. printf( "请输入成绩:");
  61. scanf( "%f", &students[num].Mark3);
  62. getchar();
  63. printf( "请输入成绩:");
  64. scanf( "%f", &students[num].Mark4); //依次输入各项数据
  65. getchar();
  66. students[num].All = students[num].Mark1 + students[num].Mark2 + students[num].Mark3 + students[num].Mark4; //输完数据后自动计算总成绩与平均成绩
  67. students[num].Average = (students[num].Mark1 + students[num].Mark2 + students[num].Mark3 + students[num].Mark4) / 4;
  68. if ( Student_SearchByIndex(students[num].ID) == -1)
  69. {
  70. num++; //移向下一个位置
  71. }
  72. else
  73. {
  74. printf( "学号重复,输入数据无效 !!!\n");
  75. }
  76. printf( "是否继续?(y/n)");
  77. if ( getchar() == 'n')
  78. {
  79. break;
  80. }
  81. }
  82. }
  83. void modify()//修改成绩
  84. {
  85. while ( 1)
  86. {
  87. char id[ 20];
  88. int index;
  89. printf( "请输入要修改的学生的学号:");
  90. scanf( "%s", &id);
  91. getchar();
  92. index = Student_SearchByIndex(id); //调用搜查id函数,根据其返回值确定位置
  93. if (index == -1)
  94. {
  95. printf( "学生不存在!\n");
  96. }
  97. else
  98. {
  99. printf( "你要修改的学生信息为:\n");
  100. Student_DisplaySingle(index);
  101. printf( "-- 请输入新值--\n");
  102. printf( "请输入学号:");
  103. scanf( "%s", &students[index].ID);
  104. getchar();
  105. printf( "请输入姓名:");
  106. scanf( "%s", &students[index].Name);
  107. getchar();
  108. printf( "请输入语文成绩:");
  109. scanf( "%f", &students[index].Mark1);
  110. getchar();
  111. printf( "请输入数学成绩:");
  112. scanf( "%f", &students[index].Mark2);
  113. getchar();
  114. printf( "请输入英语成绩:");
  115. scanf( "%f", &students[index].Mark3);
  116. getchar();
  117. printf( "请输入计算机成绩:");
  118. scanf( "%f", &students[index].Mark4); //重新录入一套新的数据替代
  119. getchar();
  120. students[index].All = students[index].Mark1 + students[index].Mark2 + students[index].Mark3 + students[index].Mark4;
  121. students[index].Average = (students[index].Mark1 + students[index].Mark2 + students[index].Mark3 + students[index].Mark4) / 4;
  122. }
  123. printf( "是否继续?(y/n)");
  124. if ( getchar() == 'n')
  125. {
  126. break;
  127. }
  128. }
  129. }
  130. void deletee()//删除学生信息
  131. {
  132. int i;
  133. while ( 1)
  134. {
  135. char id[ 20];
  136. int index;
  137. printf( "请输入要删除的学生的学号:");
  138. scanf( "%s", &id);
  139. getchar();
  140. index = Student_SearchByIndex(id); //调用搜查id函数,根据其返回值确定位置
  141. if (index == -1)
  142. {
  143. printf( "学生不存在!\n");
  144. }
  145. else
  146. {
  147. printf( "你要删除的学生信息为:\n");
  148. Student_DisplaySingle(index);
  149. printf( "是否真的要删除?(y/n)");
  150. if ( getchar() == 'y')
  151. {
  152. for (i = index; i < num - 1; i++)
  153. {
  154. Copy(students, i, i + 1);
  155. //students[i]=students[i+1]; //把后边的对象都向前移动
  156. }
  157. num--;
  158. }
  159. getchar();
  160. }
  161. printf( "是否继续?(y/n)");
  162. if ( getchar() == 'n')
  163. {
  164. break;
  165. }
  166. }
  167. }
  168. void display()//打印已录入的学生信息
  169. {
  170. int a;
  171. printf( "%10s%10s%8s%8s%8s%8s%10s%10s\n", "学号", "姓名", "语文", "数学", "英语", "计算机", "总成绩", "平均成绩");
  172. printf( "-------------------------------------------------------------\n");
  173. for (a = 0; a < num; a++)
  174. {
  175. printf( "%10s%10s%8.2f%8.2f%8.2f%8.2f%10.2f%10.2f\n", students[a].ID, students[a].Name,
  176. students[a].Mark1, students[a].Mark2, students[a].Mark3, students[a].Mark4, students[a].All, students[a].Average);
  177. }
  178. }
  179. void insert()//指定位置插入学生信息
  180. {
  181. int a, b, c;
  182. printf( "请输入你要插入的位置");
  183. scanf( "%d", &a);
  184. if (a > num) {
  185. printf( "输入的位置有误,请重新输入,当前共%d条数据\n", num);
  186. scanf( "%d", &a);
  187. }
  188. b = num - 1;
  189. for (; b >= a - 1; b--)
  190. {
  191. //strcpy(students[b+1].ID,students[b].ID);
  192. //strcpy(students[b+1].Name,students[b].Name);
  193. //students[b+1].Mark1=students[b].Mark1;
  194. //students[b+1].Mark2=students[b].Mark2;
  195. //students[b+1].Mark3=students[b].Mark3;
  196. //students[b+1].Mark4=students[b].Mark4;
  197. //students[b+1].All=students[b].All;
  198. //students[b+1].Average=students[b].Average;
  199. Copy(students, b + 1, b); //根据其输入的位置,将其及以后的数据向后移动一个位置
  200. }
  201. num++;
  202. printf( "请输入学号:");
  203. scanf( "%s", &students[a - 1].ID);
  204. getchar();
  205. printf( "请输入姓名:");
  206. scanf( "%s", &students[a - 1].Name);
  207. getchar();
  208. printf( "请输入语文成绩:");
  209. scanf( "%f", &students[a - 1].Mark1);
  210. getchar();
  211. printf( "请输入数学成绩:");
  212. scanf( "%f", &students[a - 1].Mark2);
  213. getchar();
  214. printf( "请输入英语成绩:");
  215. scanf( "%f", &students[a - 1].Mark3);
  216. getchar();
  217. printf( "请输入计算机成绩:");
  218. scanf( "%f", &students[a - 1].Mark4); //输入新数据
  219. getchar();
  220. students[a - 1].All = students[a - 1].Mark1 + students[a - 1].Mark2 + students[a - 1].Mark3 + students[a - 1].Mark4;
  221. students[a - 1].Average = (students[a - 1].Mark1 + students[a - 1].Mark2 + students[a - 1].Mark3 + students[a - 1].Mark4) / 4;
  222. }
  223. void search()//查询学生信息
  224. {
  225. while ( 1)
  226. {
  227. char name[ 20];
  228. int index;
  229. printf( "请输入要查询的学生的姓名:");
  230. scanf( "%s", &name);
  231. getchar();
  232. index = Student_SearchByName(name); //调用搜查name函数,根据其返回值确定位置
  233. if (index == -1)
  234. {
  235. printf( "学生不存在!\n");
  236. }
  237. else
  238. {
  239. printf( "你要查询的学生信息为:\n");
  240. Student_DisplaySingle(index);
  241. }
  242. printf( "是否继续?(y/n)");
  243. if ( getchar() == 'n')
  244. {
  245. break;
  246. }
  247. }
  248. }
  249. void sort()//根据平均分排序
  250. {
  251. int i, j;
  252. //struct students tmp;
  253. for (i = 0; i < num; i++)
  254. {
  255. students[i].Average = (students[i].Mark1 + students[i].Mark2 + students[i].Mark3 + students[i].Mark4) / 4;
  256. }
  257. for (i = 0; i < num; i++)
  258. {
  259. for (j = 1; j < num - i; j++)
  260. {
  261. if (students[j - 1].Average < students[j].Average)
  262. {
  263. Copy(students, num, j - 1);
  264. Copy(students, j - 1, j);
  265. Copy(students, j, num);
  266. //tmp=students[j-1];
  267. //students[j-1]=students[j];
  268. //students[j]=tmp; //冒泡排序
  269. }
  270. }
  271. }
  272. int a;
  273. printf( "%10s%10s%8s%8s%8s%10s\n", "学号", "姓名", "语文", "数学", "英语", "计算机", "总成绩", "平均成绩");
  274. printf( "-------------------------------------------------------------\n");
  275. for (a = 0; a < num; a++)
  276. {
  277. printf( "%10s%10s%8.2f%8.2f%8.2f%8.2f%10.2f%10.2f\n", students[a].ID, students[a].Name,
  278. students[a].Mark1, students[a].Mark2, students[a].Mark3, students[a].Mark4, students[a].All, students[a].Average);
  279. }
  280. }
  281. void SearchLow()//搜索不及格的并输出
  282. {
  283. int a;
  284. printf( " 语文不及格的有%10s%10s%8s\n", "学号", "姓名", "语文");
  285. for (a = 0; a < num; a++)
  286. {
  287. if (students[a].Mark1 < 60)
  288. printf( "%10s%10s%8.2f\n", students[a].Name, students[a].ID, students[a].Mark1); //从头搜索到尾,若小于60就输出
  289. }
  290. printf( " 数学不及格的有%10s%10s%8s\n", "学号", "姓名", "数学");
  291. for (a = 0; a < num; a++)
  292. {
  293. if (students[a].Mark2 < 60)
  294. printf( "%10s%10s%8.2f\n", students[a].Name, students[a].ID, students[a].Mark2);
  295. }
  296. printf( " 英语不及格的有%10s%10s%8s\n", "学号", "姓名", "英语");
  297. for (a = 0; a < num; a++)
  298. {
  299. if (students[a].Mark3 < 60)
  300. printf( "%10s%10s%8.2f\n", students[a].Name, students[a].ID, students[a].Mark3);
  301. }
  302. printf( " 计算机不及格的有%10s%10s%8s\n", "学号", "姓名", "计算机");
  303. for (a = 0; a < num; a++)
  304. {
  305. if (students[a].Mark4 < 60)
  306. printf( "%10s%10s%8.2f\n", students[a].Name, students[a].ID, students[a].Mark4);
  307. }
  308. system( "pause");
  309. }
  310. void SearchHigh()//搜索成绩最高者输出
  311. {
  312. int a;
  313. int max;
  314. printf( " 语文最高分为%10s%10s%8s\n", "学号", "姓名", "语文");
  315. max = students[ 0].Mark1;
  316. for (a = 1; a < num; a++)
  317. {
  318. if (students[a].Mark1 > max)
  319. max = students[a].Mark1;
  320. }
  321. for (a = 0; a < num; a++)
  322. {
  323. if (max == students[a].Mark1)
  324. printf( "%10s%10s%8.2f\n", students[a].Name, students[a].ID, students[a].Mark1);
  325. }
  326. printf( " 数学最高分为%10s%10s%8s\n", "学号", "姓名", "数学");
  327. max = students[ 0].Mark2;
  328. for (a = 1; a < num; a++)
  329. {
  330. if (students[a].Mark2 > max)
  331. max = students[a].Mark2;
  332. }
  333. for (a = 0; a < num; a++)
  334. {
  335. if (max == students[a].Mark2)
  336. printf( "%10s%10s%8.2f\n", students[a].Name, students[a].ID, students[a].Mark2);
  337. }
  338. printf( " 英语最高分为%10s%10s%8s\n", "学号", "姓名", "英语");
  339. max = students[ 0].Mark3;
  340. for (a = 1; a < num; a++)
  341. {
  342. if (students[a].Mark3 > max)
  343. max = students[a].Mark3;
  344. }
  345. for (a = 0; a < num; a++)
  346. {
  347. if (max == students[a].Mark3)
  348. printf( " %10s%10s%8.2f\n", students[a].Name, students[a].ID, students[a].Mark3);
  349. }
  350. printf( " 计算机最高分为%10s%10s%8s\n", "学号", "姓名", "计算机");
  351. max = students[ 0].Mark4;
  352. for (a = 1; a < num; a++)
  353. {
  354. if (students[a].Mark4 > max)
  355. max = students[a].Mark4;
  356. }
  357. for (a = 0; a < num; a++)
  358. {
  359. if (max == students[a].Mark4)
  360. printf( " %10s%10s%8.2f\n", students[a].Name, students[a].ID, students[a].Mark4);
  361. }
  362. system( "pause");
  363. }
  364. void Save()
  365. {
  366. FILE* fp = fopen( "temp.txt", "w+");
  367. fprintf(fp, "%d\n", num);
  368. for ( int i = 0; i < num; i++)
  369. {
  370. fprintf(fp, "%s %s %f %f %f %f %f %f\n", students[i].ID, students[i].Name, students[i].Mark1, students[i].Mark2, students[i].Mark3, students[i].Mark4, students[i].All, students[i].Average);
  371. }
  372. fclose(fp);
  373. }
  374. void Load()
  375. {
  376. FILE* fp = fopen( "temp.txt", "r");
  377. fscanf(fp, "%d", &num);
  378. for ( int i = 0; i < num; i++)
  379. {
  380. fscanf(fp, "%s %s %f %f %f %f %f %f\n", students[i].ID, students[i].Name, &students[i].Mark1, &students[i].Mark2, &students[i].Mark3, &students[i].Mark4, &students[i].All, &students[i].Average);
  381. }
  382. fclose(fp);
  383. }
  384. int main() {
  385. int i;
  386. while ( 1) {
  387. Load();
  388. printf( "\t\t\t\t\t-------- 学生成绩管理系统-------\n\n\n\n"); //菜单
  389. printf( "\t\t\t\t\t1. 增加学生记录\n\n");
  390. printf( "\t\t\t\t\t2. 修改学生记录\n\n");
  391. printf( "\t\t\t\t\t3. 删除学生记录\n\n");
  392. printf( "\t\t\t\t\t4. 插入学生记录\n\n");
  393. printf( "\t\t\t\t\t5. 显示所有记录\n\n");
  394. printf( "\t\t\t\t\t6. 查询学生记录\n\n");
  395. printf( "\t\t\t\t\t7. 按平均成绩排序\n\n");
  396. printf( "\t\t\t\t\t8. 输出各科目不及格学生\n\n");
  397. printf( "\t\t\t\t\t9. 输出各科目最高分\n\n");
  398. printf( "\t\t\t\t\t0. 退出\n\n\n");
  399. printf( "请选择(0-9):");
  400. scanf( "%d", &i);
  401. switch (i) {
  402. case 1: inputt(); break;
  403. case 2: modify(); break;
  404. case 3: deletee(); break;
  405. case 4: insert(); break;
  406. case 5: display(); break;
  407. case 6: search(); break;
  408. case 7: sort(); break;
  409. case 8: SearchLow(); break;
  410. case 9: SearchHigh(); break;
  411. case 0: exit( 0);
  412. default:;
  413. }
  414. Save();
  415. }
  416. return 0;
  417. }

4.运行结果如下: 

 

三、在线协助: 

如需安装运行环境或远程调试,见文章底部微信名片,由专业技术人员远程协助!


转载:https://blog.csdn.net/alicema1111/article/details/128562565
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场