diff --git a/ex8/ex8.cpp b/ex8/ex8.cpp index 4c33f30..ae884ac 100644 --- a/ex8/ex8.cpp +++ b/ex8/ex8.cpp @@ -1,112 +1,6 @@ #include "queue.hpp" +#include "train.hpp" #include -#include -#define JIE 10 - -class Train -{ -private: - int _v[10]; - Queue come[2]; - Queue temp[4]; - Queue 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"); - printf("#1到达轨 "); - for (int i = 0; i < 5; i++) - { - come[0].enqueue(_v[i]); - printf("%2d ", _v[i]); - } - printf("\n"); - printf("#2到达轨 "); - for (int i = 5; i < 10; i++) - { - come[1].enqueue(_v[i]); - printf("%2d ", _v[i]); - } - printf("\n"); - } - - bool orgnize() - { - int j = 0; - while (!come[1].empty() || !come[0].empty()) - { - for (int i = 0; i < 4;) - { - if (temp[i].back() < - (come[1].front() < come[0].front() ? - (come[1].front() ? come[1].front() : come[0].front()) : - (come[0].front() ? come[0].front() : come[1].front()))) - { - printf("%2d号车厢由#%d到达轨推入#%d缓冲轨 \n", - - (come[1].front() < come[0].front() ? - (come[1].front() ? come[1].front() : come[0].front()) : - (come[0].front() ? come[0].front() : come[1].front())), - - (come[1].front() < come[0].front() ? - (come[1].front() ? 2 : 1) : - (come[0].front() ? 1 : 2)), - - i + 1); - - temp[i].enqueue( - (come[1].front() < come[0].front() ? - (come[1].front() ? come[1].dequeue() : come[0].dequeue()) : - (come[0].front() ? come[0].dequeue() : come[1].dequeue()))); - } - else - { - j++; - i++; - if (j > 100) - 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("%2d号车厢经#%d缓冲轨推入出发区,出发队列为 ", temp[j].front(), j + 1); - 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; diff --git a/ex8/list.hpp b/ex8/list.hpp index a697e81..8b104c1 100644 --- a/ex8/list.hpp +++ b/ex8/list.hpp @@ -1,5 +1,5 @@ -#ifndef _LIST_ -#define _LIST_ +#ifndef _LIST_HPP_ +#define _LIST_HPP_ #include #include #define Node node * diff --git a/ex8/queue.hpp b/ex8/queue.hpp index c2b6984..2234165 100644 --- a/ex8/queue.hpp +++ b/ex8/queue.hpp @@ -1,5 +1,5 @@ -#ifndef _QUEUE_ -#define _QUEUE_ +#ifndef _QUEUE_HPP_ +#define _QUEUE_HPP_ #include #include "list.hpp" diff --git a/ex8/train.hpp b/ex8/train.hpp new file mode 100644 index 0000000..1667658 --- /dev/null +++ b/ex8/train.hpp @@ -0,0 +1,111 @@ +#ifndef _TRAIN_HPP_ +#define _TRAIN_HPP_ +#include +#include "queue.hpp" +#define JIE 10 + +class Train +{ +private: + int _v[10]; + Queue come[2]; + Queue temp[4]; + Queue 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"); + printf("#1到达轨 "); + for (int i = 0; i < 5; i++) + { + come[0].enqueue(_v[i]); + printf("%2d ", _v[i]); + } + printf("\n"); + printf("#2到达轨 "); + for (int i = 5; i < 10; i++) + { + come[1].enqueue(_v[i]); + printf("%2d ", _v[i]); + } + printf("\n"); + } + + bool orgnize() + { + int j = 0; + while (!come[1].empty() || !come[0].empty()) + { + for (int i = 0; i < 4;) + { + if (temp[i].back() < + (come[1].front() < come[0].front() ? + (come[1].front() ? come[1].front() : come[0].front()) : + (come[0].front() ? come[0].front() : come[1].front()))) + { + printf("%2d号车厢由#%d到达轨推入#%d缓冲轨 \n", + + (come[1].front() < come[0].front() ? + (come[1].front() ? come[1].front() : come[0].front()) : + (come[0].front() ? come[0].front() : come[1].front())), + + (come[1].front() < come[0].front() ? + (come[1].front() ? 2 : 1) : + (come[0].front() ? 1 : 2)), + + i + 1); + + + temp[i].enqueue( + (come[1].front() < come[0].front() ? + (come[1].front() ? come[1].dequeue() : come[0].dequeue()) : + (come[0].front() ? come[0].dequeue() : come[1].dequeue()))); + } + else + { + j++; + i++; + if (j > 40) + 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("%2d号车厢经#%d缓冲轨推入出发区,出发队列为 ", temp[j].front(), j + 1); + 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; + } +}; +#endif