用C语言完成一个教学计划编排系统在已经完成拓扑排序的基础上,完成 允许用户指定下列两种编排策略之一:一是使学生在各学期中的学习负担尽量均匀;二是使课程尽可能地集中在前几个学期中。,并给出我完整的C语言代码,用队列完成
时间: 2024-05-16 15:18:34 浏览: 20
以下是基于队列实现的教学计划编排系统,代码中包含两种编排策略:
```c
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX_COURSES 100 // 最大课程数
#define MAX_SEMESTERS 10 // 最大学期数
int courses[MAX_COURSES][MAX_COURSES]; // 课程间的依赖关系
int in_degree[MAX_COURSES]; // 课程的入度
int semester[MAX_COURSES]; // 每门课程的开课学期
int earliest[MAX_SEMESTERS]; // 每个学期最早能开的课程
int latest[MAX_SEMESTERS]; // 每个学期最晚能开的课程
typedef struct {
int data[MAX_COURSES];
int front;
int rear;
} Queue;
void init_queue(Queue *q) {
q->front = -1;
q->rear = -1;
}
bool is_empty(Queue *q) {
return q->front == -1;
}
bool is_full(Queue *q) {
return (q->rear + 1) % MAX_COURSES == q->front;
}
void enqueue(Queue *q, int data) {
if (is_full(q)) {
printf("Queue is full\n");
exit(1);
}
if (is_empty(q)) {
q->front = q->rear = 0;
} else {
q->rear = (q->rear + 1) % MAX_COURSES;
}
q->data[q->rear] = data;
}
int dequeue(Queue *q) {
if (is_empty(q)) {
printf("Queue is empty\n");
exit(1);
}
int data = q->data[q->front];
if (q->front == q->rear) {
q->front = q->rear = -1;
} else {
q->front = (q->front + 1) % MAX_COURSES;
}
return data;
}
void topology(int n, int strategy) {
Queue q;
init_queue(&q);
for (int i = 0; i < n; i++) {
if (in_degree[i] == 0) {
enqueue(&q, i);
semester[i] = 1;
}
}
int semesters = 1;
while (!is_empty(&q)) {
int size = q.rear - q.front + 1;
if (strategy == 1) { // 学习负担尽量均匀
for (int i = 0; i < size; i++) {
int course = dequeue(&q);
printf("Course %d, Semester %d\n", course + 1, semester[course]);
for (int j = 0; j < n; j++) {
if (courses[course][j] == 1) {
in_degree[j]--;
if (in_degree[j] == 0) {
enqueue(&q, j);
semester[j] = semesters + 1;
}
}
}
}
} else if (strategy == 2) { // 课程尽可能地集中在前几个学期
for (int i = 0; i < size; i++) {
int course = dequeue(&q);
printf("Course %d, Semester %d\n", course + 1, semester[course]);
for (int j = 0; j < n; j++) {
if (courses[course][j] == 1) {
in_degree[j]--;
if (in_degree[j] == 0) {
enqueue(&q, j);
if (semesters < 4) {
semester[j] = semesters + 1;
} else {
semester[j] = 4;
}
}
}
}
}
}
semesters++;
}
}
int main() {
int n;
printf("Enter the number of courses: ");
scanf("%d", &n);
printf("Enter the dependency matrix:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &courses[i][j]);
if (courses[i][j] == 1) {
in_degree[j]++;
}
}
}
int strategy;
printf("Enter the scheduling strategy (1 for evenly distributed, 2 for concentrated): ");
scanf("%d", &strategy);
topology(n, strategy);
return 0;
}
```
注意,这只是一个简单的实现,实际使用中还需要进行更多的测试和优化。