帮我用C语言写一个学生选课及学籍管理系统
时间: 2023-11-13 21:04:23 浏览: 104
当然可以!以下是一个简单的示例代码,实现了基本的学生选课及学籍管理系统功能。请注意,这只是一个简单的示例,可能需要根据实际需求进行进一步的完善和扩展。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100
#define MAX_COURSES 10
#define MAX_NAME_LENGTH 50
typedef struct {
char name[MAX_NAME_LENGTH];
int studentId;
int courseId;
} Student;
typedef struct {
char name[MAX_NAME_LENGTH];
int courseId;
} Course;
Student students[MAX_STUDENTS];
Course courses[MAX_COURSES];
int numStudents = 0;
int numCourses = 0;
void addStudent() {
if (numStudents >= MAX_STUDENTS) {
printf("Max number of students reached.\n");
return;
}
Student newStudent;
printf("Enter student name: ");
scanf("%s", newStudent.name);
printf("Enter student ID: ");
scanf("%d", &newStudent.studentId);
students[numStudents++] = newStudent;
printf("Student added successfully.\n");
}
void addCourse() {
if (numCourses >= MAX_COURSES) {
printf("Max number of courses reached.\n");
return;
}
Course newCourse;
printf("Enter course name: ");
scanf("%s", newCourse.name);
printf("Enter course ID: ");
scanf("%d", &newCourse.courseId);
courses[numCourses++] = newCourse;
printf("Course added successfully.\n");
}
void enrollStudent() {
int studentId, courseId;
printf("Enter student ID: ");
scanf("%d", &studentId);
printf("Enter course ID: ");
scanf("%d", &courseId);
int studentIndex = -1;
for (int i = 0; i < numStudents; i++) {
if (students[i].studentId == studentId) {
studentIndex = i;
break;
}
}
if (studentIndex == -1) {
printf("Student not found.\n");
return;
}
int courseIndex = -1;
for (int i = 0; i < numCourses; i++) {
if (courses[i].courseId == courseId) {
courseIndex = i;
break;
}
}
if (courseIndex == -1) {
printf("Course not found.\n");
return;
}
students[studentIndex].courseId = courseId;
printf("Student enrolled successfully.\n");
}
void displayStudents() {
printf("Students:\n");
for (int i = 0; i < numStudents; i++) {
printf("Name: %s, ID: %d, Course ID: %d\n", students[i].name, students[i].studentId, students[i].courseId);
}
}
void displayCourses() {
printf("Courses:\n");
for (int i = 0; i < numCourses; i++) {
printf("Name: %s, ID: %d\n", courses[i].name, courses[i].courseId);
}
}
int main() {
int choice;
do {
printf("\nStudent Enrollment System\n");
printf("1. Add Student\n");
printf("2. Add Course\n");
printf("3. Enroll Student\n");
printf("4. Display Students\n");
printf("5. Display Courses\n");
printf("0. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
addStudent();
break;
case 2:
addCourse();
break;
case 3:
enrollStudent();
break;
case 4:
displayStudents();
break;
case 5:
displayCourses();
break;
case 0:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice.\n");
break;
}
} while (choice != 0);
return 0;
}
```
这个简单的学生选课及学籍管理系统允许你添加学生、添加课程、为学生选课、显示学生列表和课程列表。你可以根据实际需求对其进行扩展和改进。希望对你有所帮助!
阅读全文