Train.cpp
上传用户:stella1212
上传日期:2022-08-06
资源大小:567k
文件大小:4k
- #include "Train.h"
- #include"Stack.h"
- Train::Train(void)
- {
- train_length = 0;
- train_sequence = NULL;
- track_number = 0;
- }
- //train函数从使用者那里那里获取火车厢的个数,火车厢的原始序列以及可以使用的缓冲铁轨的个数
- bool Train::read()
- {
- cout<<endl;
- cout<<"The number of trian carriages you want you to rearrange is:"<<endl;
- cin >> train_length;
- while( train_length <= 0 || train_length != (int)train_length)
- {
- cout<<"The number of train carriages you have just input didn't satisfied requirement!"<<endl;
- cout<<"Please input another interger!"<<endl;
- cin>>train_length;
- }
-
- cout<<"The initial sequence of the train carriages is:"<<endl;
- //用于动态申请内存
- train_sequence = new int[train_length];
- //flag 做标记,用于控制输入
- int flag = 0;
- do{
- //输入火车车厢的序列
- for(int i = 0;i < train_length ; i++)
- {
- cin>>train_sequence[i];
- }
- //检测输入的火车车厢序列是否正确,如果不正确 就跳出循环,同时将flag标记为train_length;
- for(int i = 0;i < train_length ; i++)
- {
- if( train_sequence[i] <= 0||train_sequence[i] > train_length || train_sequence[i] != (int)train_sequence[i])
- {
- cout<<"The sequence of train carriages you have just input didn't satisfied requirement!"<<endl;
- cout<<"Please input another sequence!"<<endl;
- i = flag = train_length;
- break;
- }
- flag = 0;
- }
-
- //检查输入的火车序列中是否有相同编号的火车,如果有相同编号的火车则直接跳出循环
- int count = 0;
- for(int i = 0;i < train_length ;i++)
- {
- for(int j = (i + 1);j < train_length ;j++)
- {
- if(train_sequence[i] == train_sequence[j])
- {
- count++;
- }
- }
- }
- if(count != 0)
- {
- cout<<"The sequence of train carriages you have just input have "<<count<<" pairs of same number!"<<endl;
- cout<<"Please input another sequence!"<<endl;
- flag = 1;
- }
-
- }while(flag != 0);
-
- // 从用户处获得缓冲铁轨的个数并进行判断
- cout<<"Please input the number of tracks you require us to use:"<<endl;
- cin>>track_number;
- while(track_number != (int)track_number||track_number < 0 ||track_number >= train_length)
- {
- cout<<"The number of tracks you have input didn't satisfised requirement!"<<endl;
- cout<<"Please input another number!"<<endl;
- cin>>track_number;
- }
- return true;
- }
- //Railroad 重排入轨中的火车厢,然后将它们输出
- bool Train::Railroad()
- {
- Stack *track_rail = new Stack[track_number+1];//动态创建指向栈的数组,每个元素代表一个缓冲铁轨
- int NowOut = 1;//NowOut 代表将要输出的铁轨
- int minH = train_length + 1;//minH代表所有缓冲铁轨中最小的车厢标号
- int minS;//minS是与minH所在的缓冲铁轨
- for(int i = 0;i < train_length ;i++)
- {
- if(train_sequence[i] == NowOut)
- {
- cout<<"Move carriage " << train_sequence[i]<<" from input to output!"<<endl;
- NowOut++;
- while(NowOut == minH)
- {
- Output(minH,minS,track_rail);
- NowOut++;
- }
- }
- else
- {
- if(!Hold(train_sequence[i],minH,minS,track_rail))
- return false;
- }
- }
- return true;
- }
- //Output函数将minS中的minH火车输出,同时,找到新的minH和minS
- void Train::Output(int &minH,int &minS,Stack* track_rail)
- {
- int c;
- track_rail[minS].pop();
- cout<<"Move carriage "<<minH<<" from track "<<minS<<" to output!"<<endl;//输出车厢
- //重置minH和minS的值
- minH = train_length + 2;
- for(int i = 0;i <= track_number;i++)
- {
- if( track_rail[i].top(c) !=underflow)
- {
- if(!track_rail[i].empty() && c < minH)
- {
- minH = c;
- minS = i;
- }
- }
- }
- }
- //将c放入到一个铁轨中,如果没有可用的铁轨,则返回false;如果内存不足导致无法放置,那么返回一个异常
- bool Train::Hold( int c,int &minH, int &minS,Stack* track_rail)
- {
- // Stack *track_rail = new Stack[track_number];
- //为车厢c寻找最优的缓冲铁轨
- int BestTrack = 0;//最优铁轨
- int BestTop = train_length + 1;//最优铁轨的顶部元素
- int x_top;
- //扫描缓冲铁轨
- for(int i = 0;i <= track_number;i++)
- {
- if(!track_rail[i].empty())//铁轨不为空
- {
- track_rail[i].top(x_top);
- if( c < x_top && x_top < BestTop)
- {
- BestTrack = i ;
- BestTop = x_top;
- }
- }
- else if(!BestTrack)
- {
- BestTrack = i ;
- //i = track_number;
- }
- }
- //没有可用的铁轨返回FALSE;
- if(BestTrack == 0)
- return false;
- //将c输入到最优缓冲铁轨中
- track_rail[BestTrack].push(c);
- cout<<"Move carriage "<< c <<" from input to track "<< BestTrack <<endl;
- if(c < minH)
- {
- minH = c;
- minS = BestTrack;
- }
- return true;
- }
- Train::~Train(void)
- {
- delete []train_sequence;
- }