This repository has been archived on 2024-01-06. You can view files and clone it, but cannot push or open issues or pull requests.
justhomework/ex8/ex8.cpp
2021-11-18 20:54:44 +08:00

93 lines
1.9 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "queue.hpp"
#include <stdio.h>
#include <time.h>
#define JIE 6
class Train
{
private:
int _v[6];
Queue<int> come;
Queue<int> temp[4];
Queue<int> out;
public:
Train()
{
srand(time(NULL));
for (int i = 0; i < JIE; i++)
_v[i] = JIE - i;
for (int i = 0; i < JIE; i++)
swap(i, rand() % JIE);
printf("入队序列: \n");
for (int i = 0; i < JIE; i++)
{
come.enqueue(_v[i]);
printf("%d ", _v[i]);
}
printf("\n");
}
bool orgnize()
{
int j = 0;
while (!come.empty())
{
for (int i = 0; i < 4;)
{
if (temp[i].back() < come.front())
{
printf("%d号车厢推入#%d缓冲轨 \n", come.front(), i + 1);
temp[i].enqueue(come.dequeue());
}
else
{
j++;
i++;
if (j > 20)
return false;
}
}
}
return true;
}
int Out()
{
for (int i = 1; i <= JIE;)
{
for (int j = 0; j < 4;j++)
{
if(temp[j].front()==i)
{
printf("%d号车厢经#%d缓冲轨推入出发区出发队列为 ", j+1,temp[j].front());
out.enqueue(temp[j].dequeue());
for (int i = 0; i < out.size(); i++)
printf("%d ", out[i]);
printf("\n");
i++;
}
}
}
return 0;
}
protected:
void swap(int a, int b)
{
int temp = _v[a];
_v[a] = _v[b];
_v[b] = temp;
}
};
int main()
{
Train T;
T.orgnize() ? T.Out() : printf("编组失败\n");
return 0;
}