93 lines
1.9 KiB
C++
93 lines
1.9 KiB
C++
#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;
|
||
} |