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:44:57 +08:00

89 lines
1.8 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引入出发序列\n", j+1,temp[j].front());
out.enqueue(temp[j].dequeue());
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;
}