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

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

#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;
}

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

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

 

 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

需要什么私信我

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值