经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C 语言 » 查看文章
C语言实现简单班级成绩管理系统
来源:jb51  时间:2022/3/1 13:39:53  对本文有异议

前言:

有朋友最近在做c语言课设,要求写一个班级成绩管理系统,便写份简单的代码来玩。代码原创,未参考任何其他人的代码

程序要求

说明

  • 本程序主要采用结构体数组
  • 本文件采用多文件编写,由于程序规模小,故未采用编写头文件的方式
  • 使用 #pragma once 来防止头文件重复包含

代码

怎么使用本程序看看注释应该就知道了。run main.c 就行。其他各文件作用:

  • def.c 定义了一些常量和全局变量,结构体
  • myIO.c 实现了成绩录入和成绩打印输出
  • file.c 实现了将成绩保存为文件
  • menu.c 实现了菜单功能
  • function.c 包含其他一些要用的函数

main.c

  1. #include "menu.c"
  2.  
  3. int main()
  4. {
  5. ? ? select();
  6. ? ? return 0;
  7. }

def.c

  1. // 相同头文件只包含一次,后不赘述
  2. #pragma once
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #define OK 1
  7. #define ERROR 0
  8. #define TRUE 1
  9. #define FALSE 0
  10. #define Status int
  11.  
  12. // 课程
  13. typedef struct Course
  14. {
  15. ? ? char name[30];
  16. ? ? int score;
  17. } Course, *pCourse;
  18.  
  19. // 学生
  20. typedef struct Student
  21. {
  22. ? ? char number[30];
  23. ? ? char name[30];
  24. ? ? pCourse pC;
  25. } Student, *pStudent;
  26.  
  27. // n是学生数, m是课程数
  28. int n, m;
  29. char courseName[30], studentName[30], course[20][30];
  30. pStudent pS = NULL;

myIO.c

  1. #pragma once
  2. #include "def.c"
  3.  
  4. pStudent inputStudentInfo(void);
  5. void printStudentInfo(pStudent pS);
  6.  
  7. // 录入学生信息
  8. pStudent inputStudentInfo(void)
  9. {
  10. ? ? int i, j;
  11. ? ? printf("Please input the number of students and courses: ");
  12. ? ? scanf("%d %d", &n, &m);
  13. ? ? printf("Please input the name of courses: ");
  14. ? ? for (i = 0; i < m; i++)
  15. ? ? {
  16. ? ? ? ? scanf("%s", course[i]);
  17. ? ? }
  18. ? ? pStudent pS = (pStudent)malloc(sizeof(Student) * n);
  19. ? ? if (!pS)
  20. ? ? ? ? return NULL;
  21. ? ? printf("Please input the info: \n");
  22. ? ? for (i = 0; i < n; i++)
  23. ? ? {
  24. ? ? ? ? pS[i].pC = (pCourse)malloc(sizeof(Course) * m);
  25. ? ? ? ? if (!pS[i].pC)
  26. ? ? ? ? ? ? return NULL;
  27. ? ? ? ? scanf("%s %s", pS[i].name, pS[i].number);
  28. ? ? ? ? for (j = 0; j < m; j++)
  29. ? ? ? ? {
  30. ? ? ? ? ? ? strcpy(pS[i].pC[j].name, course[j]);
  31. ? ? ? ? ? ? scanf("%d", &pS[i].pC[j].score);
  32. ? ? ? ? }
  33. ? ? }
  34. ? ? return pS;
  35. }
  36.  
  37. // 打印所有学生信息
  38. void printStudentInfo(pStudent pS)
  39. {
  40. ? ? int i, j;
  41. ? ? // 打印标题
  42. ? ? printf("Name\tnumber\t");
  43. ? ? for (i = 0; i < m - 1; i++)
  44. ? ? ? ? printf("%s\t", course[i]);
  45. ? ? printf("%s\n", course[i]);
  46. ? ? // 显示信息
  47. ? ? for (i = 0; i < n; i++)
  48. ? ? {
  49. ? ? ? ? printf("%s\t%s\t", pS[i].name, pS[i].number);
  50. ? ? ? ? for (j = 0; j < m - 1; j++)
  51. ? ? ? ? ? ? printf("%d\t", pS[i].pC[j].score);
  52. ? ? ? ? printf("%d\n", pS[i].pC[j].score);
  53. ? ? }
  54. }

file.c

  1. #pragma once
  2. #include "def.c"
  3. Status saveStudentInfo(pStudent pS);
  4. Status saveStudentInfo(pStudent pS)
  5. {
  6. ? ? FILE *fp;
  7. ? ? int i, j;
  8. ? ? char filename[30], str[100] = "student number";
  9. ? ? printf("please input the filename: ");
  10. ? ? scanf("%s", filename);
  11. ? ? fp = fopen(filename, "w");
  12. ? ? if (!fp)
  13. ? ? ? ? return ERROR;
  14. ? ? for (i = 0; i < m; i++)
  15. ? ? {
  16. ? ? ? ? strcat(str, " ");
  17. ? ? ? ? strcat(str, course[i]);
  18. ? ? }
  19. ? ? strcat(str, "\n");
  20. ? ? for (i = 0; i < n; i++)
  21. ? ? {
  22. ? ? ? ? strcat(str, pS[i].name);
  23. ? ? ? ? strcat(str, " ");
  24. ? ? ? ? strcat(str, pS[i].number);
  25. ? ? ? ? for (j = 0; j < m; j++)
  26. ? ? ? ? {
  27. ? ? ? ? ? ? char score[30];
  28. ? ? ? ? ? ? itoa(pS[i].pC[j].score, score, 10);
  29. ? ? ? ? ? ? strcat(str, " ");
  30. ? ? ? ? ? ? strcat(str, score);
  31. ? ? ? ? }
  32. ? ? ? ? strcat(str, "\n");
  33. ? ? }
  34. ? ? fputs(str, fp);
  35. ? ? fclose(fp);
  36. ? ? return OK;
  37. }

menu.c

  1. #pragma once
  2. #include "def.c"
  3. #include "myIO.c"
  4. #include "file.c"
  5. #include "function.c"
  6. void menu();
  7. void select();
  8. // 菜单
  9. void menu()
  10. {
  11. ? ? printf("------------------------------------\n");
  12. ? ? printf("| ? ? ? ? ? ? ? ?Menu ? ? ? ? ? ? ?|\n");
  13. ? ? printf("| ? ? ? ? ? ? ?1. input ? ? ? ? ? ?|\n");
  14. ? ? printf("| ? ? ? ? ? ? ?2. show ? ? ? ? ? ? |\n");
  15. ? ? printf("| ? ? ? ? ? ? ?3. save ? ? ? ? ? ? |\n");
  16. ? ? printf("| ? ? ? ? ? ? ?4. sort ? ? ? ? ? ? |\n");
  17. ? ? printf("| ? ? ? ? ? ? ?5. modify ? ? ? ? ? |\n");
  18. ? ? printf("| ? ? ? ? ? ? ?6. count ? ? ? ? ? ?|\n");
  19. ? ? printf("| ? ? ? ? ? ? ?0. exit ? ? ? ? ? ? |\n");
  20. ? ? printf("------------------------------------\n");
  21. }
  22.  
  23. void select()
  24. {
  25. ? ? int branch;
  26. ? ? while (TRUE)
  27. ? ? {
  28. ? ? ? ? system("cls");
  29. ? ? ? ? menu();
  30. ? ? ? ? printf("[Input]: ");
  31. ? ? ? ? scanf("%d", &branch);
  32. ? ? ? ? if (!branch)
  33. ? ? ? ? ? ? break;
  34. ? ? ? ? switch (branch)
  35. ? ? ? ? {
  36. ? ? ? ? case 1:
  37. ? ? ? ? {
  38. ? ? ? ? ? ? pS = inputStudentInfo();
  39. ? ? ? ? ? ? if (pS == NULL)
  40. ? ? ? ? ? ? ? ? printf("input error! please input again\n");
  41. ? ? ? ? ? ? else
  42. ? ? ? ? ? ? ? ? printf("Input success!\n");
  43. ? ? ? ? ? ? system("pause");
  44. ? ? ? ? ? ? break;
  45. ? ? ? ? }
  46. ? ? ? ? case 2:
  47. ? ? ? ? {
  48. ? ? ? ? ? ? printStudentInfo(pS);
  49. ? ? ? ? ? ? system("pause");
  50. ? ? ? ? ? ? break;
  51. ? ? ? ? }
  52. ? ? ? ? case 3:
  53. ? ? ? ? {
  54. ? ? ? ? ? ? if (OK == saveStudentInfo(pS))
  55. ? ? ? ? ? ? ? ? printf("Save success!\n");
  56. ? ? ? ? ? ? else
  57. ? ? ? ? ? ? ? ? printf("Save fail!\n");
  58. ? ? ? ? ? ? system("pause");
  59. ? ? ? ? ? ? break;
  60. ? ? ? ? }
  61. ? ? ? ? case 4:
  62. ? ? ? ? {
  63. ? ? ? ? ? ? sort(pS);
  64. ? ? ? ? ? ? printf("sort success\n");
  65. ? ? ? ? ? ? system("pause");
  66. ? ? ? ? ? ? break;
  67. ? ? ? ? }
  68. ? ? ? ? case 5:
  69. ? ? ? ? {
  70. ? ? ? ? ? ? int res = modify(pS);
  71. ? ? ? ? ? ? if (res)
  72. ? ? ? ? ? ? {
  73. ? ? ? ? ? ? ? ? printf("change success!\n");
  74. ? ? ? ? ? ? }
  75. ? ? ? ? ? ? else
  76. ? ? ? ? ? ? {
  77. ? ? ? ? ? ? ? ? printf("change fail!\n");
  78. ? ? ? ? ? ? }
  79. ? ? ? ? ? ? system("pause");
  80. ? ? ? ? ? ? break;
  81. ? ? ? ? }
  82. ? ? ? ? case 6:
  83. ? ? ? ? {
  84. ? ? ? ? ? ? int choose;
  85. ? ? ? ? ? ? // 输入1 显示每门课程最高成绩信息
  86. ? ? ? ? ? ? // 输入2 显示每门课程平均成绩信息
  87. ? ? ? ? ? ? printf("choose 1 for the highest score: \n ");
  88. ? ? ? ? ? ? printf("choose 2 for the average score: \n");
  89. ? ? ? ? ? ? printf("[Input]: ");
  90. ? ? ? ? ? ? scanf("%d", &choose);
  91. ? ? ? ? ? ? if (choose == 1)
  92. ? ? ? ? ? ? {
  93. ? ? ? ? ? ? ? ? showMax(pS);
  94. ? ? ? ? ? ? }
  95. ? ? ? ? ? ? else if (choose == 2)
  96. ? ? ? ? ? ? {
  97. ? ? ? ? ? ? ? ? showAverage(pS);
  98. ? ? ? ? ? ? }
  99. ? ? ? ? ? ? else
  100. ? ? ? ? ? ? {
  101. ? ? ? ? ? ? ? ? // 输入非法提示信息
  102. ? ? ? ? ? ? ? ? printf("Input error!\n");
  103. ? ? ? ? ? ? }
  104. ? ? ? ? ? ? system("pause");
  105. ? ? ? ? ? ? break;
  106. ? ? ? ? }
  107. ? ? ? ? }
  108. ? ? }
  109. }

function.c

  1. #include "def.c"
  2.  
  3. void sort(pStudent pS);
  4. void Swap(pStudent s1, pStudent s2);
  5. void showAverage(pStudent pS);
  6. void showMax(pStudent pS);
  7. Status modify(pStudent pS);
  8.  
  9. // 按课程成绩排序
  10. void sort(pStudent pS)
  11. {
  12. ? ? int courseNumber, i, j, k;
  13. ? ? char courseName[30];
  14. ? ? printf("please input the course name which you want to sort: ");
  15. ? ? scanf("%s", courseName);
  16. ? ? for (courseNumber = 0; courseNumber < m; courseNumber++)
  17. ? ? ? ? if (strcmp(course[courseNumber], courseName) == 0)
  18. ? ? ? ? ? ? break;
  19. ? ? // 如果找不到课程,则认为是按总分排序
  20. ? ? if (courseNumber == m)
  21. ? ? {
  22. ? ? ? ? printf("Sort as total score: \n");
  23. ? ? ? ? // 选择排序
  24. ? ? ? ? for (i = 0; i < n - 1; i++)
  25. ? ? ? ? {
  26. ? ? ? ? ? ? int flag = i;
  27. ? ? ? ? ? ? for (j = i + 1; j < n; j++)
  28. ? ? ? ? ? ? {
  29. ? ? ? ? ? ? ? ? int totalScore_1 = 0, totalScore_2 = 0;
  30. ? ? ? ? ? ? ? ? for (k = 0; k < m; k++)
  31. ? ? ? ? ? ? ? ? {
  32. ? ? ? ? ? ? ? ? ? ? totalScore_1 += pS[j].pC[k].score;
  33. ? ? ? ? ? ? ? ? ? ? totalScore_2 += pS[flag].pC[k].score;
  34. ? ? ? ? ? ? ? ? }
  35. ? ? ? ? ? ? ? ? if (totalScore_1 > totalScore_2)
  36. ? ? ? ? ? ? ? ? {
  37. ? ? ? ? ? ? ? ? ? ? flag = j;
  38. ? ? ? ? ? ? ? ? }
  39. ? ? ? ? ? ? }
  40. ? ? ? ? ? ? Swap(&pS[i], &pS[flag]);
  41. ? ? ? ? }
  42. ? ? }
  43. ? ? else
  44. ? ? {
  45. ? ? ? ? // 选择排序
  46. ? ? ? ? for (i = 0; i < n - 1; i++)
  47. ? ? ? ? {
  48. ? ? ? ? ? ? int flag = i;
  49. ? ? ? ? ? ? for (j = i + 1; j < n; j++)
  50. ? ? ? ? ? ? {
  51. ? ? ? ? ? ? ? ? if (pS[j].pC[courseNumber].score > pS[flag].pC[courseNumber].score)
  52. ? ? ? ? ? ? ? ? {
  53. ? ? ? ? ? ? ? ? ? ? flag = j;
  54. ? ? ? ? ? ? ? ? }
  55. ? ? ? ? ? ? }
  56. ? ? ? ? ? ? Swap(&pS[i], &pS[flag]);
  57. ? ? ? ? }
  58. ? ? }
  59. }
  60.  
  61. // 修改学生信息
  62. Status modify(pStudent pS)
  63. {
  64. ? ? // 密码是1314
  65. ? ? char password[30] = "1314", psd[30];
  66. ? ? char number[30];
  67. ? ? int score, i, j;
  68. ? ? printf("please input password: ");
  69. ? ? scanf("%s", psd);
  70. ? ? // 密码正确才继续,否则返回ERROR
  71. ? ? if (strcmp(password, psd) == 0)
  72. ? ? {
  73. ? ? ? ? printf("please input the student's number: ");
  74. ? ? ? ? scanf("%s", number);
  75. ? ? ? ? for (i = 0; i < n; i++)
  76. ? ? ? ? {
  77. ? ? ? ? ? ? // 找到学生则继续,否则返回ERROR
  78. ? ? ? ? ? ? if (strcmp(pS[i].number, number) == 0)
  79. ? ? ? ? ? ? {
  80. ? ? ? ? ? ? ? ? printf("please input the course and score one by one: \n");
  81. ? ? ? ? ? ? ? ? scanf("%s %d", courseName, &score);
  82. ? ? ? ? ? ? ? ? for (j = 0; j < m; j++)
  83. ? ? ? ? ? ? ? ? {
  84. ? ? ? ? ? ? ? ? ? ? // 找到课程才继续,否则返回ERROR
  85. ? ? ? ? ? ? ? ? ? ? if (strcmp(pS[i].pC[j].name, courseName) == 0)
  86. ? ? ? ? ? ? ? ? ? ? {
  87. ? ? ? ? ? ? ? ? ? ? ? ? // 修改课程成绩
  88. ? ? ? ? ? ? ? ? ? ? ? ? pS[i].pC[j].score = score;
  89. ? ? ? ? ? ? ? ? ? ? ? ? return OK;
  90. ? ? ? ? ? ? ? ? ? ? }
  91. ? ? ? ? ? ? ? ? }
  92. ? ? ? ? ? ? ? ? return ERROR;
  93. ? ? ? ? ? ? }
  94. ? ? ? ? }
  95. ? ? ? ? return ERROR;
  96. ? ? }
  97. ? ? else
  98. ? ? ? ? return ERROR;
  99. }
  100.  
  101. // 输出各课程最高分的学生
  102. void showMax(pStudent pS)
  103. {
  104. ? ? int i, j, max;
  105. ? ? for (i = 0; i < m; i++)
  106. ? ? {
  107. ? ? ? ? max = 0;
  108. ? ? ? ? for (j = 0; j < n; j++)
  109. ? ? ? ? {
  110. ? ? ? ? ? ? if (pS[j].pC[i].score > pS[max].pC[i].score)
  111. ? ? ? ? ? ? ? ? max = j;
  112. ? ? ? ? }
  113. ? ? ? ? printf("%s\t%s\t%s\t%d\n", course[i], pS[max].name, pS[max].number, pS[max].pC[i].score);
  114. ? ? }
  115. }
  116.  
  117. // 显示各课程的平均成绩
  118. void showAverage(pStudent pS)
  119. {
  120. ? ? int i, j;
  121. ? ? double ave;
  122. ? ? for (i = 0; i < m; i++)
  123. ? ? {
  124. ? ? ? ? ave = 0;
  125. ? ? ? ? for (j = 0; j < n; j++)
  126. ? ? ? ? {
  127. ? ? ? ? ? ? ave += pS[j].pC[i].score;
  128. ? ? ? ? }
  129. ? ? ? ? printf("%s\t%.2lf\n", course[i], ave / n);
  130. ? ? }
  131. }
  132.  
  133. void Swap(pStudent s1, pStudent s2)
  134. {
  135. ? ? int i;
  136. ? ? char studentName[30], number[30];
  137. ? ? // 交换姓名
  138. ? ? strcpy(studentName, s1->name);
  139. ? ? strcpy(s1->name, s2->name);
  140. ? ? strcpy(s2->name, studentName);
  141. ? ? // 交换学号
  142. ? ? strcpy(number, s1->number);
  143. ? ? strcpy(s1->number, s2->number);
  144. ? ? strcpy(s2->number, number);
  145. ? ? // 交换成绩
  146. ? ? for (i = 0; i < m; i++)
  147. ? ? {
  148. ? ? ? ? int temp = s1->pC[i].score;
  149. ? ? ? ? s1->pC[i].score = s2->pC[i].score;
  150. ? ? ? ? s2->pC[i].score = temp;
  151. ? ? }
  152. }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号