当前位置:   article > 正文

用c语言写一个详细的学生选课系统附带代码_c语言学生选课系统代码

c语言学生选课系统代码

以下是一个简单的学生选课系统的示例:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define MAX_STUDENTS 100
  5. #define MAX_COURSES 50
  6. typedef struct {
  7. char name[30];
  8. int student_id;
  9. char major[30];
  10. int course_ids[MAX_COURSES];
  11. int num_courses;
  12. } Student;
  13. typedef struct {
  14. char title[30];
  15. int course_id;
  16. char instructor[30];
  17. int max_students;
  18. int num_students;
  19. int student_ids[MAX_STUDENTS];
  20. } Course;
  21. Student students[MAX_STUDENTS];
  22. Course courses[MAX_COURSES];
  23. int num_students = 0;
  24. int num_courses = 0;
  25. int add_student(char *name, int student_id, char *major) {
  26. if (num_students < MAX_STUDENTS) {
  27. Student *student = &students[num_students];
  28. strcpy(student->name, name);
  29. student->student_id = student_id;
  30. strcpy(student->major, major);
  31. student->num_courses = 0;
  32. num_students++;
  33. return 1;
  34. }
  35. return 0;
  36. }
  37. int add_course(char *title, int course_id, char *instructor, int max_students) {
  38. if (num_courses < MAX_COURSES) {
  39. Course *course = &courses[num_courses];
  40. strcpy(course->title, title);
  41. course->course_id = course_id;
  42. strcpy(course->instructor, instructor);
  43. course->max_students = max_students;
  44. course->num_students = 0;
  45. num_courses++;
  46. return 1;
  47. }
  48. return 0;
  49. }
  50. int enroll_student(int student_id, int course_id) {
  51. if (student_id >= 0 && student_id < num_students && course_id >= 0 && course_id < num_courses) {
  52. Student *student = &students[student_id];
  53. Course *course = &courses[course_id];
  54. if (student->num_courses < MAX_COURSES && course->num_students < course->max_students) {
  55. student->course_ids[student->num_courses] = course_id;
  56. student->num_courses++;
  57. course->student_ids[course->num_students] = student_id;
  58. course->num_students++;
  59. return 1;
  60. }
  61. }
  62. return 0;
  63. }
  64. int main() {
  65. add_student("Alice", 0, "Computer Science");
  66. add_student("Bob", 1, "Mathematics");
  67. add_student("Charlie", 2, "Physics");
  68. add_course("Introduction to Computer Science", 0, "Dr. Smith", 30);
  69. add_course("Calculus I", 1, "Dr. Johnson", 25);
  70. add_course("Physics I", 2, "Dr. Lee", 20);
  71. enroll_student(0, 0);
  72. enroll_student(1, 1);
  73. enroll_student(2, 2);
  74. printf("Students enrolled in Introduction to Computer Science:\n");
  75. for (int i = 0; i < courses[0].num_students; i++) {
  76. printf("- %s\n", students[courses[0].student_ids[i]].name);
  77. }
  78. printf("Courses enrolled by Alice:\n");
  79. for (int i = 0; i < students[0].num_courses; i++) {
  80. printf("- %s\n", courses[students[0].course_ids[i]].title);
  81. }
  82. return 0;
  83. }

请注意,此代码仅用于演示学生选课系统的基本概念。实际上,一个真正的学生选课系统需要更复杂的数据模型、用户界面、数据库存储和其他功能等。

希望这个代码示例对你有帮助,如果你需要更多帮助,请随时问我。

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/615770
推荐阅读
相关标签
  

闽ICP备14008679号