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

93 lines
1.9 KiB
C++
Raw Normal View History

2021-11-18 12:44:57 +00:00
#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())
{
2021-11-18 12:54:44 +00:00
printf("%d号车厢推入#%d缓冲轨 \n", come.front(), i + 1);
2021-11-18 12:44:57 +00:00
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)
{
2021-11-18 12:54:44 +00:00
printf("%d号车厢经#%d缓冲轨推入出发区出发队列为 ", j+1,temp[j].front());
2021-11-18 12:44:57 +00:00
out.enqueue(temp[j].dequeue());
2021-11-18 12:54:44 +00:00
for (int i = 0; i < out.size(); i++)
printf("%d ", out[i]);
printf("\n");
2021-11-18 12:44:57 +00:00
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;
}