教学计划编制

#include 
#include 
#include 
#include 
#include 
using namespace std;
#define N 100
#define M 10010
template
class Queue
{
public:
    bool IsFull() const;
    bool IsEmpty() const;
    T front();
    void Pop();
    void Push(T &e);
    Queue(int size);
    ~Queue();
private:
    T *base;
    int top,rear;
    int maxsize;
};
template
bool Queue::IsFull() const
{
    return rear == maxsize;
}
template
bool Queue::IsEmpty() const
{
    return rear == top;
}
template
Queue::Queue(int size):maxsize(size)
{
    base = new T[maxsize];
    if (base == NULL)
    {
        cout<<"Memory Allocation Failed!"<
void Queue::Pop()
{
    top++;
}
template
void Queue::Push(T &e)
{
    base[rear++] = e;
}
template
T Queue::front()
{
    return base[top];
}
template
Queue::~Queue()
{
    delete [] base;
}

int InDegree[N],res[N];
int S,C,n,l,num,op,ave,s1,tot;
bool flag;
bool vis[N];
struct node1
{
    char id[4],pre[N][4];
    int cre,k;
}course[N];
int head[N];
struct node
{
    int x,y,next;
}edge[M];
void add(int x,int y)
{
    l++;
    edge[l].x = x;edge[l].y = y;
    edge[l].next = head[x];head[x] = l;
}

int TopSort_Queue()//对课程进行拓扑排序
{
    memset(vis,0,sizeof(vis));
    Queue Q(N);
    for (int i=1;i<=n;i++)
        if (InDegree[i] == 0) Q.Push(i);
    while (!Q.IsEmpty())
    {
        int v = Q.front();
        res[++num] = v;
        vis[v] = 1;
        Q.Pop();
        for (int p=head[v];p;p = edge[p].next)
        {
            int u = edge[p].y;
            InDegree[u]--;
            if (InDegree[u] == 0) Q.Push(u);
        }
    }
    for (int i=1;i<=n;i++)
        if (!vis[i]) return 0;
    return 1;
}

void solve()
{
    memset(head,0,sizeof(head));
    memset(InDegree,0,sizeof(InDegree));
    l = 0;
    for (int i=1;i<=n;i++)//添加表示直接先修课程的有向边
    {
        for (int j = 1;j<=course[i].k;i++)
        {
            flag = 0;
            for (int i1 = 1;i1<=n;i1++)
                if (!strcmp(course[i1].id, course[i].pre[j]))
                {
                    add(i1,i);
                    InDegree[i]++;
                    flag = 1;
                }
            if (!flag)
            {
                printf("Failed! ID does not exist!");
                return ;
            }
        }
    }
    int tmp = TopSort_Queue();
    if (!tmp)
    {
        printf("Failed! There are errors in the relations of prerequisite courses!\n");
        return ;
    }
    tot = 0;
    s1 = 0;
    for (int i=1;i<=num;i++)
    {
        tot += course[res[i]].cre;
        if (tot > C)
        {
            s1++;
            tot = course[res[i]].cre;
        }
        if (course[res[i]].cre > C)
        {
            printf("Failed! The credit of %s course is too large!\n", course[res[i]].id);
            return ;
        }
    }
    if (s1 > S)
    {
        printf("Failed! There is no solution to this problem! The number of semesters is too small!\n");
        return ;
    }
    printf("Please input which way you want to arrange your courses\n");
    printf("The first one is to arrange courses equally for each semester\n");
    printf("The second one is to arrange courses as much as possible in the first few semesters\n");
    printf("Input 1 or 2 to choose the way and input 0 to exit\n");

    ofstream outFile;
    outFile.open("result.dat", ios::out);//建立输出文件result.dat

    while (~scanf("%d", &op))
    {
        if (op == 1)
        {
            outFile<<"The first way:\n";
            ave = num / S;
            s1 = 0;//表示各个学期
            int i = 1;
            while (i<=num)
            {
                s1++;
                int tot = 0;//累计一个学期的学分总和
                int j = i;
                while (j<=num && j-i+1<=ave && tot <= C)//保证每个学期安排的课程数目不超过平均数且在学分限制以内
                    tot += course[res[j++]].cre;
                outFile<<"No."<

你可能感兴趣的:(教学计划编制)