学生选课管理系统c语言源代码
时间: 2024-12-26 13:02:09 浏览: 20
以下是一个简单的学生选课管理系统的C语言源代码示例。这个系统可以实现基本的学生和课程管理功能,包括添加学生、添加课程、选课和查看选课情况。
```c
#include <stdio.h>
#include <string.h>
#define MAX_STUDENTS 100
#define MAX_COURSES 100
#define NAME_LENGTH 50
struct Student {
int id;
char name[NAME_LENGTH];
};
struct Course {
int id;
char name[NAME_LENGTH];
};
struct Enrollment {
int student_id;
int course_id;
};
struct Student students[MAX_STUDENTS];
int student_count = 0;
struct Course courses[MAX_COURSES];
int course_count = 0;
struct Enrollment enrollments[MAX_STUDENTS * MAX_COURSES];
int enrollment_count = 0;
void add_student() {
if (student_count >= MAX_STUDENTS) {
printf("学生数量已达上限。\n");
return;
}
struct Student s;
printf("请输入学生ID: ");
scanf("%d", &s.id);
printf("请输入学生姓名: ");
scanf("%s", s.name);
students[student_count++] = s;
printf("学生添加成功。\n");
}
void add_course() {
if (course_count >= MAX_COURSES) {
printf("课程数量已达上限。\n");
return;
}
struct Course c;
printf("请输入课程ID: ");
scanf("%d", &c.id);
printf("请输入课程名称: ");
scanf("%s", c.name);
courses[course_count++] = c;
printf("课程添加成功。\n");
}
void enroll_course() {
int student_id, course_id;
printf("请输入学生ID: ");
scanf("%d", &student_id);
printf("请输入课程ID: ");
scanf("%d", &course_id);
for (int i = 0; i < student_count; i++) {
if (students[i].id == student_id) {
for (int j = 0; j < course_count; j++) {
if (courses[j].id == course_id) {
enrollments[enrollment_count].student_id = student_id;
enrollments[enrollment_count].course_id = course_id;
enrollment_count++;
printf("选课成功。\n");
return;
}
}
printf("课程不存在。\n");
return;
}
}
printf("学生不存在。\n");
}
void view_enrollments() {
printf("选课情况:\n");
for (int i = 0; i < enrollment_count; i++) {
int student_id = enrollments[i].student_id;
int course_id = enrollments[i].course_id;
char student_name[NAME_LENGTH], course_name[NAME_LENGTH];
for (int j = 0; j < student_count; j++) {
if (students[j].id == student_id) {
strcpy(student_name, students[j].name);
break;
}
}
for (int j = 0; j < course_count; j++) {
if (courses[j].id == course_id) {
strcpy(course_name, courses[j].name);
break;
}
}
printf("学生: %s, 课程: %s\n", student_name, course_name);
}
}
int main() {
int choice;
while (1) {
printf("\n学生选课管理系统\n");
printf("1. 添加学生\n");
printf("2. 添加课程\n");
printf("3. 选课\n");
printf("4. 查看选课情况\n");
printf("5. 退出\n");
printf("请选择操作: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_student();
break;
case 2:
add_course();
break;
case 3:
enroll_course();
break;
case 4:
view_enrollments();
break;
case 5:
printf("退出系统。\n");
return 0;
default:
printf("无效选择,请重新选择。\n");
}
}
}
```
这个程序实现了以下功能:
1. 添加学生
2. 添加课程
3. 学生选课
4. 查看选课情况
阅读全文