首页 > 代码库 > 火车车厢重排调度

火车车厢重排调度

 1 /*************************************************************** 2  *程序名称:火车车厢重排调度 3  *作    者:tbz 4  *完成日期:2014年10月18日 5  ***************************************************************/ 6 #include <queue> 7 #include <iostream> 8 #include "TrainScheduler.h" 9 using namespace std;10 11 int main()12 {13     // 获得来源列车的编号14     queue<int> src;15     cout << "Please enter a successive sequence that begins with one: (end with 0)" << endl;16     cout << endl;17     int carNumber;18     bool isLoop = true;19     do 20     {21         cin >> carNumber;22         if (carNumber != 0)23             src.push(carNumber);24         else 25             isLoop = false;26     } while (isLoop);27     28     // 进入车站,并调度29     Scheduler trainStation(&src);30     trainStation.work();31     return 0;32 }
main.cpp
 1 /*************************************************************** 2  *类库名称:调度器 3  *作    者:tbz 4  *完成日期:2014年10月18日 5  ***************************************************************/ 6 #ifndef _TRAIN_SCHEDULER_H_ 7 #define _TRAIN_SCHEDULER_H_ 8  9 #include <queue>10 #include <iostream>11 #include "BufferStack.h"12 using namespace std;13 14 class Scheduler15 {16 public:17     Scheduler(queue<int> * train);18     void work();19 20 private:21     queue<int> * train;22     BufferStack rest;23 };24 25 Scheduler::Scheduler(queue<int> * train)26 {27     this->train = train;28 }29 30 // 调度器开始工作31 void Scheduler::work()32 {33     int output = 0;34     cout << endl << "        Procedure:" << endl;35     while (!train->empty())36     {37         if ( (!output && train->front() == 1) 38             || (output && train->front() == output+1) )39         {40             output = train->front();41             cout << output << " : entrance -> exit" << endl;42         }43         else44         {45             int headTrain = train->front();46             rest.push(headTrain, &output);47         }48         train->pop();49     }50 51     rest.start();52 }53 54 #endif
TrainScheduler.h
 1 /*************************************************************** 2  *类库名称:缓存轨 3  *作    者:tbz 4  *完成日期:2014年10月18日 5  ***************************************************************/ 6 #ifndef _BUFFER_STACK_H_ 7 #define _BUFFER_STACK_H_ 8  9 #include <iostream>10 #include <stack>11 #include <vector>12 using namespace std;13 14 class BufferStack15 {16 public:17     void push(int number, int* currentTrain);18     void start();19     ~BufferStack();20 private:21     vector<stack<int> *> buffer;22     int * exitTrain;23 };24 25 void BufferStack::push(int number, int* currentTrain)26 {27     // 保存出口列车编号,以便后续使用28     exitTrain = currentTrain;29 30     // 定位31     int objectivePosition = -1;32     int latestTop = 0x0FFFFFFF;33     for (int i = 0; i < buffer.size(); ++i)34     {35         int stackTop = buffer[i]->top();36         if (stackTop == (*currentTrain)+1)37         {38             buffer[i]->pop();39             cout << stackTop << " : buffer[" << i+1 << "] -> exit" << endl;40             *currentTrain = stackTop;41         }42         stackTop = buffer[i]->top();43         if (stackTop > number && stackTop < latestTop)44         {45             latestTop = stackTop;46             objectivePosition = i;47         }48     }49 50     // 进轨51     if (objectivePosition+1)52     {53         buffer[objectivePosition]->push(number);54         cout << number << " : entrance -> buffer[" << objectivePosition+1 << "]" << endl;55     }56     else57     {58         buffer.push_back(new stack<int>);59         buffer.back()->push(number);60         cout << number << " : entrance -> buffer[" << buffer.size() << "]" << endl;61     }62 }63 64 // 剩余列车出轨65 void BufferStack::start()66 {67     bool isBreak = false;68     while (!isBreak)69     {70         isBreak = true;71         for (int i = 0; i < buffer.size(); ++i)72         {73             if (!buffer[i]->empty())74             {75                 isBreak = false;76                 int currentTop = buffer[i]->top();77                 if (currentTop == (*exitTrain + 1))78                 {79                     *exitTrain = currentTop;80                     buffer[i]->pop();81                     cout << currentTop << " : buffer[" << i+1 << "] -> exit" << endl;82                 }83             }84         }85         if (isBreak) 86             break;87     }88 }89 90 // 释放内存91 BufferStack::~BufferStack()92 {93     for (auto i : buffer)94         if (i)95             delete i;96 }97 98 #endif
BufferStack.h

 

火车车厢重排调度