选课系统课程设计c语言不用链表
时间: 2024-03-30 12:31:05 浏览: 134
选课系统课程设计是一个常见的C语言项目,它可以帮助学生进行课程的选取和管理。在设计选课系统时,可以不使用链表,而是使用数组和结构体来实现。
首先,你可以定义一个结构体来表示每门课程的信息,包括课程名称、课程编号、授课教师等等。然后,你可以创建一个数组来存储这些课程信息。
接下来,你可以设计一些函数来实现选课系统的功能,比如添加课程、删除课程、查询课程等等。这些函数可以通过遍历数组来实现对课程信息的操作。
例如,你可以设计一个函数来添加课程,该函数会向数组中添加一个新的课程信息。你可以使用一个变量来记录当前数组中已有的课程数量,每次添加课程时,将新的课程信息存储在数组中,并将已有课程数量加一。
类似地,你可以设计其他函数来实现删除课程、查询课程等功能。通过遍历数组并根据条件进行操作,你可以实现选课系统的各种功能。
相关问题
用c语言链表设计一个选课系统
好的,以下是用 C 语言链表设计一个选课系统的代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 20
typedef struct course {
char name[MAX_NAME_LEN];
int id;
int capacity;
int enrolled;
struct course *next;
} Course;
Course *head = NULL;
void add_course(char *name, int id, int capacity) {
Course *new_course = (Course *)malloc(sizeof(Course));
strncpy(new_course->name, name, MAX_NAME_LEN);
new_course->id = id;
new_course->capacity = capacity;
new_course->enrolled = 0;
new_course->next = NULL;
if (head == NULL) {
head = new_course;
} else {
Course *cur = head;
while (cur->next != NULL) {
cur = cur->next;
}
cur->next = new_course;
}
}
void enroll(int id) {
Course *cur = head;
while (cur != NULL) {
if (cur->id == id) {
if (cur->enrolled < cur->capacity) {
cur->enrolled++;
printf("Enrolled in %s\n", cur->name);
} else {
printf("Course %s is full\n", cur->name);
}
return;
}
cur = cur->next;
}
printf("Course not found\n");
}
void print_courses() {
Course *cur = head;
while (cur != NULL) {
printf("%d: %s (%d/%d)\n", cur->id, cur->name, cur->enrolled, cur->capacity);
cur = cur->next;
}
}
int main() {
add_course("Math", 1, 30);
add_course("English", 2, 20);
add_course("History", 3, 25);
enroll(1);
enroll(1);
enroll(1);
enroll(1);
enroll(1);
enroll(1);
enroll(1);
enroll(1);
enroll(1);
enroll(1);
enroll(1);
enroll(1);
enroll(1);
enroll(1);
enroll(1);
enroll(1);
enroll(2);
enroll(2);
enroll(2);
enroll(2);
enroll(2);
enroll(2);
enroll(2);
enroll(2);
enroll(2);
enroll(2);
enroll(2);
enroll(3);
enroll(3);
enroll(3);
enroll(3);
enroll(3);
enroll(3);
enroll(3);
enroll(3);
enroll(3);
enroll(3);
enroll(3);
enroll(3);
enroll(3);
enroll(3);
print_courses();
return 0;
}
```
这个选课系统使用链表来存储课程信息,每个课程包括名称、编号、容量和已选人数等信息。可以通过 `add_course` 函数添加课程,通过 `enroll` 函数选课,通过 `print_courses` 函数打印所有课程的信息。
用c语言链表设计一个学生选课系统
好的,以下是一个用C语言链表设计的学生选课系统的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct course {
char name[20];
int id;
int capacity;
int enrolled;
struct course *next;
} Course;
typedef struct student {
char name[20];
int id;
Course *courses;
struct student *next;
} Student;
Course *create_course(char name[], int id, int capacity) {
Course *new_course = (Course*) malloc(sizeof(Course));
strcpy(new_course->name, name);
new_course->id = id;
new_course->capacity = capacity;
new_course->enrolled = 0;
new_course->next = NULL;
return new_course;
}
Student *create_student(char name[], int id) {
Student *new_student = (Student*) malloc(sizeof(Student));
strcpy(new_student->name, name);
new_student->id = id;
new_student->courses = NULL;
new_student->next = NULL;
return new_student;
}
void add_course(Student *student, Course *course) {
if (course->enrolled < course->capacity) {
Course *temp = student->courses;
while (temp != NULL) {
if (temp->id == course->id) {
printf("Student %s has already enrolled in course %s.\n", student->name, course->name);
return;
}
temp = temp->next;
}
course->enrolled++;
Course *new_course = create_course(course->name, course->id, course->capacity);
new_course->next = student->courses;
student->courses = new_course;
printf("Student %s has successfully enrolled in course %s.\n", student->name, course->name);
} else {
printf("Course %s is already full.\n", course->name);
}
}
void print_courses(Course *courses) {
printf("Course ID\tCourse Name\tCapacity\tEnrolled\n");
while (courses != NULL) {
printf("%d\t\t%s\t\t%d\t\t%d\n", courses->id, courses->name, courses->capacity, courses->enrolled);
courses = courses->next;
}
}
void print_students(Student *students) {
printf("Student ID\tStudent Name\tEnrolled Courses\n");
while (students != NULL) {
printf("%d\t\t%s\t\t", students->id, students->name);
Course *courses = students->courses;
while (courses != NULL) {
printf("%s ", courses->name);
courses = courses->next;
}
printf("\n");
students = students->next;
}
}
int main() {
Course *c1 = create_course("Math", 1, 3);
Course *c2 = create_course("Science", 2, 2);
Course *c3 = create_course("History", 3, 1);
Student *s1 = create_student("Alice", 1001);
Student *s2 = create_student("Bob", 1002);
Student *s3 = create_student("Charlie", 1003);
add_course(s1, c1);
add_course(s1, c2);
add_course(s1, c3);
add_course(s2, c1);
add_course(s2, c2);
add_course(s3, c1);
print_courses(c1);
print_courses(c2);
print_courses(c3);
print_students(s1);
print_students(s2);
print_students(s3);
return 0;
}
```
这个程序使用了两个结构体,一个是Course,表示课程,另一个是Student,表示学生。每个课程有一个ID、名称、容量和已选人数,每个学生有一个ID、名称和已选课程列表。我们可以使用add_course函数将学生添加到课程中,如果课程已满或学生已经在课程中,则会输出相应的错误信息。最后,我们可以使用print_courses和print_students函数分别打印所有课程和学生的信息。
希望这个示例代码能够帮助你理解如何使用链表设计一个学生选课系统。
阅读全文