以下是一个简单的学生选课系统的示例:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100
#define MAX_COURSES 50
typedef struct {
char name[30];
int student_id;
char major[30];
int course_ids[MAX_COURSES];
int num_courses;
} Student;
typedef struct {
char title[30];
int course_id;
char instructor[30];
int max_students;
int num_students;
int student_ids[MAX_STUDENTS];
} Course;
Student students[MAX_STUDENTS];
Course courses[MAX_COURSES];
int num_students = 0;
int num_courses = 0;
int add_student(char *name, int student_id, char *major) {
if (num_students < MAX_STUDENTS) {
Student *student = &students[num_students];
strcpy(student->name, name);
student->student_id = student_id;
strcpy(student->major, major);
student->num_courses = 0;
num_students++;
return 1;
}
return 0;
}
int add_course(char *title, int course_id, char *instructor, int max_students) {
if (num_courses < MAX_COURSES) {
Course *course = &courses[num_courses];
strcpy(course->title, title);
course->course_id = course_id;
strcpy(course->instructor, instructor);
course->max_students = max_students;
course->num_students = 0;
num_courses++;
return 1;
}
return 0;
}
int enroll_student(int student_id, int course_id) {
if (student_id >= 0 && student_id < num_students && course_id >= 0 && course_id < num_courses) {
Student *student = &students[student_id];
Course *course = &courses[course_id];
if (student->num_courses < MAX_COURSES && course->num_students < course->max_students) {
student->course_ids[student->num_courses] = course_id;
student->num_courses++;
course->student_ids[course->num_students] = student_id;
course->num_students++;
return 1;
}
}
return 0;
}
int main() {
add_student("Alice", 0, "Computer Science");
add_student("Bob", 1, "Mathematics");
add_student("Charlie", 2, "Physics");
add_course("Introduction to Computer Science", 0, "Dr. Smith", 30);
add_course("Calculus I", 1, "Dr. Johnson", 25);
add_course("Physics I", 2, "Dr. Lee", 20);
enroll_student(0, 0);
enroll_student(1, 1);
enroll_student(2, 2);
printf("Students enrolled in Introduction to Computer Science:\n");
for (int i = 0; i < courses[0].num_students; i++) {
printf("- %s\n", students[courses[0].student_ids[i]].name);
}
printf("Courses enrolled by Alice:\n");
for (int i = 0; i < students[0].num_courses; i++) {
printf("- %s\n", courses[students[0].course_ids[i]].title);
}
return 0;
}
请注意,此代码仅用于演示学生选课系统的基本概念。实际上,一个真正的学生选课系统需要更复杂的数据模型、用户界面、数据库存储和其他功能等。
希望这个代码示例对你有帮助,如果你需要更多帮助,请随时问我。