Student Course Selection System in C Language with Source Code

“To become a master, it is not achieved overnight, unless one is a natural talent in martial arts, but such people… are one in ten thousand.”

—— LandladyThis principle also applies to learning C language. There are indeed few people with extraordinary talent in programming; most of us need to go through a long process of learning to progress from a novice in C language to an expert.So how do we learn?Of course, by practicing a C language problem every day!!Student Course Selection System in C Language with Source Code

Author

Yan Xiaolin

Working during the day, dreaming at night. I have stories, do you have wine?

Example 52: C Language Implementation Student Course Selection System

Source Code Demonstration:

/* * Student Course Selection System - Complete Implementation * Features: Course Management, Student Management, Course Selection and Drop, Conflict Detection * Author: DeepSeek Chat * Date: 2023-11-15 */
#include <stdio.h>    // Standard Input Output
#include <stdlib.h>   // Standard Library Functions
#include <string.h>   // String Handling
#include <stdbool.h> // Boolean Type Support
/* System Constants Definition */
#define MAX_COURSES 100     // Maximum Number of Courses
#define MAX_STUDENTS 100    // Maximum Number of Students
#define MAX_NAME_LEN 50     // Maximum Length of Name
#define MAX_ID_LEN 20       // Maximum Length of ID
#define MAX_SELECTED 10     // Maximum Number of Courses Selected by Each Student
/* Course Structure Definition */
typedef struct {
    char id[MAX_ID_LEN];      // Course ID
    char name[MAX_NAME_LEN];  // Course Name
    int credit;               // Credits
    int time_slot;            // Time Slot (1-8)
    int capacity;             // Course Capacity
    int enrolled;             // Number of Enrolled Students
} Course;
/* Student Structure Definition */
typedef struct {
    char id[MAX_ID_LEN];      // Student ID
    char name[MAX_NAME_LEN];  // Name
    char selected[MAX_SELECTED][MAX_ID_LEN]; // Array of Selected Course IDs
    int selected_count;       // Number of Selected Courses
    int total_credit;         // Total Credits
} Student;
/* Global Variables */
Course courses[MAX_COURSES];  // Array of Courses
Student students[MAX_STUDENTS]; // Array of Students
int course_count = 0;        // Current Number of Courses
int student_count = 0;       // Current Number of Students
/* Function Declarations */
void add_course();           // Add Course
void show_all_courses();     // Show All Courses
int find_course(const char* id); // Find Course
void add_student();          // Add Student
void show_all_students();    // Show All Students
int find_student(const char* id); // Find Student
bool has_time_conflict(int student_idx, int course_idx); // Check Time Conflict
void select_course();        // Course Selection Function
void drop_course();          // Course Drop Function
void show_student_courses(); // Show Student Course Selection Status
void show_menu();            // Show Main Menu
void course_management_menu(); // Course Management Menu
void student_management_menu(); // Student Management Menu
/* Main Function */
int main() {
    int choice; // User Choice
    // Main Loop
    do {
        show_menu();
        scanf("%d", &choice);
        // Execute Corresponding Function Based on User Choice
        switch (choice) {
            case 1: course_management_menu(); break; // Course Management
            case 2: student_management_menu(); break; // Student Management
            case 3: select_course(); break;         // Course Selection
            case 4: drop_course(); break;           // Course Drop
            case 5: show_student_courses(); break;  // Query Course Selection
            case 6: show_all_courses(); break;      // Show Courses
            case 7: show_all_students(); break;    // Show Students
            case 0: printf("Thank you for using!\n"); break;    // Exit
            default: printf("Invalid Choice!\n");          // Error Handling
        }
    } while (choice != 0);
    return 0;
}
/* Course Management Submenu */
void course_management_menu() {
    int choice;
    do {
        printf("\nCourse Management\n");
        printf("1.Add Course\n");
        printf("2.Show All Courses\n");
        printf("0.Return\nPlease choose: ");
        scanf("%d", &choice);
        switch (choice) {
            case 1: add_course(); break;     // Add Course
            case 2: show_all_courses(); break; // Show Courses
            case 0: break;                   // Return
            default: printf("Invalid Choice!\n");   // Error Handling
        }
    } while (choice != 0);
}
/* Student Management Submenu */
void student_management_menu() {
    int choice;
    do {
        printf("\nStudent Management\n");
        printf("1.Add Student\n");
        printf("2.Show All Students\n");
        printf("0.Return\nPlease choose: ");
        scanf("%d", &choice);
        switch (choice) {
            case 1: add_student(); break;    // Add Student
            case 2: show_all_students(); break; // Show Students
            case 0: break;                  // Return
            default: printf("Invalid Choice!\n");  // Error Handling
        }
    } while (choice != 0);
}
/* Add New Course */
void add_course() {
    // Check if Course Count has Reached Limit
    if (course_count >= MAX_COURSES) {
        printf("Course count has reached the limit!\n");
        return;
    }
    Course c; // New Course Variable
    printf("Please enter Course ID: "); scanf("%s", c.id);
    printf("Please enter Course Name: "); scanf("%s", c.name);
    printf("Please enter Credits: "); scanf("%d", &c.credit);
    printf("Please enter Time Slot (1-8): "); scanf("%d", &c.time_slot);
    printf("Please enter Capacity: "); scanf("%d", &c.capacity);
    c.enrolled = 0; // Initialize Number of Enrolled Students to 0
    // Add New Course to Array
    courses[course_count++] = c;
    printf("Course added successfully!\n");
}
/* Show All Course Information */
void show_all_courses() {
    // Print Header
    printf("\n%-10s %-20s %-6s %-8s %-8s %-8s\n",            "ID", "Name", "Credits", "Time Slot", "Capacity", "Enrolled");
    // Iterate and Print All Courses
    for (int i = 0; i < course_count; i++) {
        printf("%-10s %-20s %-6d %-8d %-8d %-8d\n",                courses[i].id, courses[i].name, courses[i].credit,               courses[i].time_slot, courses[i].capacity, courses[i].enrolled);
    }
}
/* Find Course Index by Course ID */
int find_course(const char* id) {
    // Linear Search Course Array
    for (int i = 0; i < course_count; i++) {
        if (strcmp(courses[i].id, id) == 0) {
            return i; // Found, return index
        }
    }
    return -1; // Not Found, return -1
}
/* Add New Student */
void add_student() {
    // Check if Student Count has Reached Limit
    if (student_count >= MAX_STUDENTS) {
        printf("Student count has reached the limit!\n");
        return;
    }
    Student s; // New Student Variable
    printf("Please enter Student ID: "); scanf("%s", s.id);
    printf("Please enter Name: "); scanf("%s", s.name);
    s.selected_count = 0;   // Initialize Number of Selected Courses
    s.total_credit = 0;     // Initialize Total Credits
    // Add New Student to Array
    students[student_count++] = s;
    printf("Student added successfully!\n");
}
/* Show All Student Information */
void show_all_students() {
    // Print Header
    printf("\n%-10s %-20s %-8s %-8s\n", "Student ID", "Name", "Selected Count", "Total Credits");
    // Iterate and Print All Students
    for (int i = 0; i < student_count; i++) {
        printf("%-10s %-20s %-8d %-8d\n",                students[i].id, students[i].name,                students[i].selected_count, students[i].total_credit);
    }
}
/* Find Student Index by Student ID */
int find_student(const char* id) {
    // Linear Search Student Array
    for (int i = 0; i < student_count; i++) {
        if (strcmp(students[i].id, id) == 0) {
            return i; // Found, return index
        }
    }
    return -1; // Not Found, return -1
}
/* Check Time Conflict */
bool has_time_conflict(int student_idx, int course_idx) {
    Student* s = &students[student_idx];   // Get Student Pointer
    Course* new_c = &courses[course_idx];  // Get Course Pointer
    // Check Time Slot of Selected Courses
    for (int i = 0; i < s->selected_count; i++) {
        int idx = find_course(s->selected[i]);
        if (idx != -1 && courses[idx].time_slot == new_c->time_slot) {
            return true; // Conflict Found
        }
    }
    return false; // No Conflict
}
/* Student Course Selection Function */
void select_course() {
    char s_id[MAX_ID_LEN], c_id[MAX_ID_LEN];
    printf("Please enter Student ID: "); scanf("%s", s_id);
    printf("Please enter Course ID: "); scanf("%s", c_id);
    // Find Student and Course
    int s_idx = find_student(s_id);
    int c_idx = find_course(c_id);
    // Check Existence
    if (s_idx == -1) { printf("Student does not exist!\n"); return; }
    if (c_idx == -1) { printf("Course does not exist!\n"); return; }
    Student* s = &students[s_idx];
    Course* c = &courses[c_idx];
    // Check if Course Already Selected
    for (int i = 0; i < s->selected_count; i++) {
        if (strcmp(s->selected[i], c_id) == 0) {
            printf("This course has already been selected!\n"); return;
        }
    }
    // Check Course Capacity
    if (c->enrolled >= c->capacity) { printf("Course is full!\n"); return; }
    // Check Time Conflict
    if (has_time_conflict(s_idx, c_idx)) { printf("Time conflict!\n"); return; }
    // Execute Course Selection
    strcpy(s->selected[s->selected_count++], c_id); // Add Course ID
    s->total_credit += c->credit;                  // Increase Credits
    c->enrolled++;                                 // Increase Enrolled Count
    printf("Course selection successful!\n");
}
/* Student Course Drop Function */
void drop_course() {
    char s_id[MAX_ID_LEN], c_id[MAX_ID_LEN];
    printf("Please enter Student ID: "); scanf("%s", s_id);
    printf("Please enter Course ID: "); scanf("%s", c_id);
    // Find Student and Course
    int s_idx = find_student(s_id);
    int c_idx = find_course(c_id);
    // Check Existence
    if (s_idx == -1) { printf("Student does not exist!\n"); return; }
    if (c_idx == -1) { printf("Course does not exist!\n"); return; }
    Student* s = &students[s_idx];
    Course* c = &courses[c_idx];
    // Find and Remove Course
    for (int i = 0; i < s->selected_count; i++) {
        if (strcmp(s->selected[i], c_id) == 0) {
            // Shift Remaining Courses Forward
            for (int j = i; j < s->selected_count-1; j++) {
                strcpy(s->selected[j], s->selected[j+1]);
            }
            s->selected_count--;       // Decrease Number of Selected Courses
            s->total_credit -= c->credit; // Decrease Credits
            c->enrolled--;            // Decrease Enrolled Count
            printf("Course drop successful!\n");
            return;
        }
    }
    printf("This student has not selected this course!\n");
}
/* Show Student Course Selection Status */
void show_student_courses() {
    char s_id[MAX_ID_LEN];
    printf("Please enter Student ID: "); scanf("%s", s_id);
    // Find Student
    int s_idx = find_student(s_id);
    if (s_idx == -1) { printf("Student does not exist!\n"); return; }
    Student* s = &students[s_idx];
    // Print Basic Student Information
    printf("\nStudent %s (%s) Course Selection Status:\nNumber of Selected Courses: %d, Total Credits: %d\n",           s->name, s->id, s->selected_count, s->total_credit);
    // Print Course Selection Header
    printf("\n%-10s %-20s %-6s %-8s\n", "ID", "Name", "Credits", "Time Slot");
    // Print Each Course Information
    for (int i = 0; i < s->selected_count; i++) {
        int c_idx = find_course(s->selected[i]);
        if (c_idx != -1) {
            printf("%-10s %-20s %-6d %-8d\n",                    courses[c_idx].id, courses[c_idx].name,                    courses[c_idx].credit, courses[c_idx].time_slot);
        }
    }
}
/* Show Main Menu */
void show_menu() {
    printf("\nStudent Course Selection System\n");
    printf("1.Course Management\n2.Student Management\n3.Course Selection\n4.Course Drop\n");
    printf("5.Query Course Selection\n6.Show Courses\n7.Show Students\n0.Exit\nPlease choose: ");
}

Four Core Functions

1. Course Management

Add New Course (including Course ID, Name, Credits, Time Slot, and Capacity)

Show All Course Information

2. Student Management

Add New Student (including Student ID and Name)

Show All Student Information

3. Course Selection Function

Student Course Selection (check Course Capacity and Time Conflicts)

Student Course Drop

Query Student Course Selection Status

4. Conflict Detection

Check if Selected Courses have Time Conflicts

Check if Course is Full

Compilation and Execution Results are as follows:

Student Course Selection System
1.Course Management
2.Student Management
3.Course Selection
4.Course Drop
5.Query Course Selection
6.Show Courses
7.Show Students
0.Exit
Please choose: 1
Course Management
1.Add Course
2.Show All Courses
0.Return
Please choose: 1
Please enter Course ID: 001
Please enter Course Name: Chinese
Please enter Credits: 100
Please enter Time Slot (1-8): 3
Please enter Capacity: 8
Course added successfully!
Course Management
1.Add Course
2.Show All Courses
0.Return
Please choose: 2
ID       Name                 Credits   Time Slot   Capacity     Enrolled
001        Chinese                 100    3        8        0
Course Management
1.Add Course
2.Show All Courses
0.Return
Please choose:

Follow the public account below, reply 1, and you will be added to a programming community of 20,000 people for free, plus 100 C language source code examples~Learn C/C++ without getting lost

Leave a Comment