编写一个简易的选课系统需要对C语言有一定的了解,包括数据结构、函数、文件操作等,下面是一个简单的选课系统的实现过程:
(图片来源网络,侵删)
1、我们需要定义课程的结构体,包括课程名称、课程编号、学分等信息,我们还需要定义学生的结构体,包括学生姓名、学号、已选课程等信息。
typedef struct Course { char name[50]; int id; int credits; } Course; typedef struct Student { char name[50]; int id; Course *courses[10]; int num_courses; } Student;
2、接下来,我们需要实现一些基本的功能,如添加课程、删除课程、添加学生、删除学生等,这些功能可以通过函数来实现。
void add_course(Course **courses, int *num_courses, char *name, int id, int credits) { Course *new_course = (Course *)malloc(sizeof(Course)); strcpy(new_course>name, name); new_course>id = id; new_course>credits = credits; (*courses)[*num_courses] = new_course; (*num_courses)++; } void delete_course(Course **courses, int *num_courses, int id) { for (int i = 0; i < *num_courses; i++) { if (courses[i]>id == id) { free(courses[i]); for (int j = i; j < *num_courses 1; j++) { courses[j] = courses[j + 1]; } (*num_courses); return; } } } void add_student(Student **students, int *num_students, char *name, int id) { Student *new_student = (Student *)malloc(sizeof(Student)); strcpy(new_student>name, name); new_student>id = id; new_student>num_courses = 0; (*students)[*num_students] = new_student; (*num_students)++; } void delete_student(Student **students, int *num_students, int id) { for (int i = 0; i < *num_students; i++) { if (students[i]>id == id) { for (int j = 0; j < students[i]>num_courses; j++) { free(students[i]>courses[j]); } free(students[i]); for (int j = i; j < *num_students 1; j++) { students[j] = students[j + 1]; } (*num_students); return; } } }
3、接下来,我们需要实现选课功能,学生可以通过输入课程编号来选课,如果课程不存在或者学生已经选过该课程,则需要提示错误信息,我们还需要在学生的结构体中添加一个已选课程的数组,用于存储学生已选的课程。
void enroll(Student *student, Course *course) { for (int i = 0; i < student>num_courses; i++) { if (student>courses[i]>id == course>id) { printf("Error: You have already enrolled in this course. "); return; } } if (student>num_courses >= 10) { printf("Error: You can only enroll in a maximum of 10 courses. "); return; } student>courses[student>num_courses++] = course; }
4、我们需要实现一个主函数,用于展示选课系统的界面,在主函数中,我们可以让用户选择操作(添加课程、删除课程、添加学生、删除学生、选课等),并根据用户的选择调用相应的函数,我们还需要在主函数中实现一个循环,让用户可以不断地进行操作,当用户选择退出时,程序结束。
int main() { Student students[100]; Course courses[100]; int num_students = 0; int num_courses = 0; int choice; int student_id, course_id; char student_name[50], course_name[50]; int credits; while (1) { printf("1. Add course "); printf("2. Delete course "); printf("3. Add student "); printf("4. Delete student "); printf("5. Enroll student in course "); printf("6. Quit "); printf("Enter your choice: "); scanf("%d", &choice); switch (choice) { case 1: // Add course printf("Enter course name: "); scanf("%s", course_name); getchar(); // Consume newline character left by scanf() function. printf("Enter course ID: "); scanf("%d", &course_id); getchar(); // Consume newline character left by scanf() function. printf("Enter course credits: "); scanf("%d", &credits); getchar(); // Consume newline character left by scanf() function.
原创文章,作者:酷盾叔,如若转载,请注明出处:https://www.kdun.com/ask/363300.html
本网站发布或转载的文章及图片均来自网络,其原创性以及文中表达的观点和判断不代表本网站。如有问题,请联系客服处理。
发表回复