C语言设计学生选课系统各功能代码

这是一个简单的C语言学生选课系统,包含以下几个功能:

  1. 显示课程列表
  2. 显示学生列表
  3. 学生选课
  4. 学生退课
  5. 显示学生选课情况

这里给出每个功能的代码实现:

  1. 显示课程列表
void displayCourses(Course *courses, int numCourses) {
    printf("课程列表:\n");
    for (int i = 0; i < numCourses; i++) {
        printf("%d. %s\n", i + 1, courses[i].name);
    }
}
  1. 显示学生列表
void displayStudents(Student *students, int numStudents) {
    printf("学生列表:\n");
    for (int i = 0; i < numStudents; i++) {
        printf("%d. %s\n", i + 1, students[i].name);
    }
}
  1. 学生选课
void addCourse(Student *student, Course *course) {
    if (student->numCourses >= MAX_COURSES_PER_STUDENT) {
        printf("该学生选课已满!\n");
        return;
    }
    for (int i = 0; i < student->numCourses; i++) {
        if (student->courses[i] == course) {
            printf("该学生已经选过该门课程!\n");
            return;
        }
    }
    student->courses[student->numCourses++] = course;
    printf("选课成功!\n");
}
  1. 学生退课
void dropCourse(Student *student, Course *course) {
    int found = 0;
    for (int i = 0; i < student->numCourses; i++) {
        if (student->courses[i] == course) {
            found = 1;
            for (int j = i; j < student->numCourses - 1; j++) {
                student->courses[j] = student->courses[j + 1];
            }
            student->numCourses--;
            break;
        }
    }
    if (found) {
        printf("退课成功!\n");
    } else {
        printf("该学生未选过该门课程!\n");
    }
}
  1. 显示学生选课情况
void displayStudentCourses(Student *student) {
    printf("学生 %s 的选课情况:\n", student->name);
    if (student->numCourses == 0) {
        printf("该学生未选课!\n");
    } else {
        for (int i = 0; i < student->numCourses; i++) {
            printf("%d. %s\n", i + 1, student->courses[i]->name);
        }
    }
}

完整代码如下:

#include <stdio.h>
#include <string.h>

#define MAX_COURSES 100
#define MAX_STUDENTS 100
#define MAX_COURSES_PER_STUDENT 5

typedef struct {
    char name[50];
} Course;

typedef struct {
    char name[50];
    Course *courses[MAX_COURSES_PER_STUDENT];
    int numCourses;
} Student;

void displayCourses(Course *courses, int numCourses) {
    printf("课程列表:\n");
    for (int i = 0; i < numCourses; i++) {
        printf("%d. %s\n", i + 1, courses[i].name);
    }
}

void displayStudents(Student *students, int numStudents) {
    printf("学生列表:\n");
    for (int i = 0; i < numStudents; i++) {
        printf("%d. %s\n", i + 1, students[i].name);
    }
}

void addCourse(Student *student, Course *course) {
    if (student->numCourses >= MAX_COURSES_PER_STUDENT) {
        printf("该学生选课已满!\n");
        return;
    }
    for (int i = 0; i < student->numCourses; i++) {
        if (student->courses[i] == course) {
            printf("该学生已经选过该门课程!\n");
            return;
        }
    }
    student->courses[student->numCourses++] = course;
    printf("选课成功!\n");
}

void dropCourse(Student *student, Course *course) {
    int found = 0;
    for (int i = 0; i < student->numCourses; i++) {
        if (student->courses[i] == course) {
            found = 1;
            for (int j = i; j < student->numCourses - 1; j++) {
                student->courses[j] = student->courses[j + 1];
            }
            student->numCourses--;
            break;
        }
    }
    if (found) {
        printf("退课成功!\n");
    } else {
        printf("该学生未选过该门课程!\n");
    }
}

void displayStudentCourses(Student *student) {
    printf("学生 %s 的选课情况:\n", student->name);
    if (student->numCourses == 0) {
        printf("该学生未选课!\n");
    } else {
        for (int i = 0; i < student->numCourses; i++) {
            printf("%d. %s\n", i + 1, student->courses[i]->name);
        }
    }
}

int main() {
    Course courses[MAX_COURSES];
    Student students[MAX_STUDENTS];
    int numCourses = 0;
    int numStudents = 0;

    while (1) {
        printf("\n");
        printf("1. 显示课程列表\n");
        printf("2. 显示学生列表\n");
        printf("3. 学生选课\n");
        printf("4. 学生退课\n");
        printf("5. 显示学生选课情况\n");
        printf("6. 退出\n");
        printf("请选择操作:");

        int choice;
        scanf("%d", &choice);

        if (choice == 1) {
            displayCourses(courses, numCourses);
        } else if (choice == 2) {
            displayStudents(students, numStudents);
} else if (choice == 3) {
char studentName[50];
printf("请输入学生姓名:");
scanf("%s", studentName);
        Student *student = NULL;
        for (int i = 0; i < numStudents; i++) {
            if (strcmp(students[i].name, studentName) == 0) {
                student = &students[i];
                break;
            }
        }
        if (student == NULL) {
            if (numStudents >= MAX_STUDENTS) {
                printf("学生人数已达上限,无法添加新学生!\n");
                continue;
            }
            strcpy(students[numStudents].name, studentName);
            students[numStudents].numCourses = 0;
            student = &students[numStudents++];
        }

        displayCourses(courses, numCourses);
        int courseChoice;
        printf("请选择课程编号:");
        scanf("%d", &courseChoice);
        if (courseChoice < 1 || courseChoice > numCourses) {
            printf("输入无效!\n");
            continue;
        }
        addCourse(student, &courses[courseChoice - 1]);
    } else if (choice == 4) {
        char studentName[50];
        printf("请输入学生姓名:");
        scanf("%s", studentName);

        Student *student = NULL;
        for (int i = 0; i < numStudents; i++) {
            if (strcmp(students[i].name, studentName) == 0) {
                student = &students[i];
                break;
            }
        }
        if (student == NULL) {
            printf("该学生不存在!\n");
            continue;
        }

        displayStudentCourses(student);
        int courseChoice;
        printf("请选择课程编号:");
        scanf("%d", &courseChoice);
        if (courseChoice < 1 || courseChoice > student->numCourses) {
            printf("输入无效!\n");
            continue;
        }
        dropCourse(student, student->courses[courseChoice - 1]);
    } else if (choice == 5) {
        char studentName[50];
        printf("请输入学生姓名:");
        scanf("%s", studentName);

        Student *student = NULL;
        for (int i = 0; i < numStudents; i++) {
            if (strcmp(students[i].name, studentName) == 0) {
                student = &students[i];
                break;
            }
        }
        if (student == NULL) {
            printf("该学生不存在!\n");
            continue;
        }

        displayStudentCourses(student);
    } else if (choice == 6) {
        break;
    } else {
        printf("输入无效!\n");
    }
}

return 0;
}


以上就是一个简单的学生选课系统的代码实现,包括添加学生、添加课程、学生选课、学生退课和查询学生选课情况等功能。可以根据实际需求进行修改和扩展。

 
  • 学生选课系统