-6

我目前正在尝试使用布尔值输出方向。

1 是真的。0 为假。

1表示电梯上升。0 表示电梯下降。

我希望输出为:

节点#:时间戳。当前(用户楼层)。目的地(用户楼层)。方向(用户正在前进)。

Node 0 : 1 3 7 true
Node 1 : 1 2 9 true
Node 2 : 1 7 9 true
Node 3 : 2 4 6 true
Node 4 : 2 4 8 true
Node 5 : 2 1 17 true 
Node 6 : 5 1 15 true
Node 7 : 5 5 1 false
Node 8 : 6 17 4 false
Node 9 : 6 4 17 true

相反,我得到了输出:

Node 0 : 1 3 7 205
Node 1 : 1 2 9 205
Node 2 : 1 7 9 205
Node 3 : 2 4 6 205
Node 4 : 2 4 8 205
Node 5 : 2 1 17 205 
Node 6 : 5 1 15 205
Node 7 : 5 5 1 205
Node 8 : 6 17 4 205
Node 9 : 6 4 17 205

代码本身编译没有错误。我不知道我的逻辑哪里出错了。当我试图找出一种方法来分配用户提出电梯服务请求的方向(用户提出的请求)时。

reqNode *temp = new reqNode;

if(temp->start < temp->destination)
temp->set_dir(true);
else
temp->set_dir(false);

是我使用的逻辑。

使用的 T1.txt 文件:

1 3 7
1 2 9
1 7 9
2 4 6
2 4 8
2 1 17
5 1 15
5 5 1
6 17 4
6 4 17

这是我的文件(完整的代码)

binaryHeap.h 文件:

#include <vector>
using namespace std;


template <class Comparable>
class BinaryHeap
{

public:
BinaryHeap( ): Array( 11 ), theSize( 0 ){}


bool isEmpty( ) const
{
    return theSize == 0;
}

const Comparable & findMin( ) const
{
    if( isEmpty( ) )
    {
    cout << "heap empty" << endl; //throw UnderflowException( );
    //break;
    }
return Array[ 1 ];
}

void insert( const Comparable & x)
{
    Array[ 0 ] = x;   // initialize sentinel

    if( theSize + 1 == Array.size( ) )
      Array.resize( Array.size( ) * 2 + 1 );

    // Percolate up
    int hole = ++theSize;
        for( ; x < Array[ hole / 2 ]; hole /= 2 )
           Array[ hole ] = Array[ hole / 2 ];
           Array[ hole ] = x;
}

void deleteMin( )
{
    if( isEmpty( ) )
    {
    cout << "heap empty" << endl; //throw UnderflowException( );
    break;
    }
 Array[ 1 ] = Array[ theSize-- ];
 percolateDown( 1 );
}

void deleteMin( Comparable & minItem )
{
  minItem = findMin( );
  Array[ 1 ] = Array[ theSize-- ];
  percolateDown( 1 );
}

void makeEmpty( )
{
    theSize = 0;
}

private:

int theSize;  // Number of elements in heap
vector<Comparable> Array;    // The heap Array

void buildHeap( )
{
    for( int i = theSize / 2; i > 0; i-- )
    percolateDown( i );
}

void percolateDown( int hole )
{
  int child;
  Comparable tmp = Array[ hole ];

  for( ; hole * 2 <= theSize; hole = child )
  {
    child = hole * 2;
    if( child != theSize && Array[ child + 1 ] < Array[ child ] )
        child++;
    if( Array[ child ] < tmp )
        Array[ hole ] = Array[ child ];
    else
        break;
  }
 Array[ hole ] = tmp;
}
};

reqnode.h 文件:

class reqNode//create a node that takes in several properties.
{
public:

reqNode()
:direction(true)
{   
//default constructor
//initialize the memeber variables of this class.
priority = start = destination
= timestamp = start_time
= finish_time = -1;
set_dir(true);
}

reqNode(const reqNode &copy){       //copy constructor
priority = copy.priority;
start = copy.start;
destination = copy.destination;
timestamp = copy.timestamp;
start_time = copy.start_time;
finish_time = copy.finish_time;
}

reqNode & operator=(const reqNode & copy){ // operation overload
priority = copy.priority;
start = copy.start;
destination = copy.destination;
timestamp = copy.timestamp;
start_time = copy.start_time;
finish_time = copy.finish_time;

return *this;
}

bool operator<(const reqNode &rhs) const// operation overload
{
if(this->priority < rhs.priority)
return true;
else
return false;
}
void setPriority(int x){//assigning the priority to of whatever I declare "x" to be.
priority = x;
}

void calculate_priority()
{
if( start < destination )
priority = destination;  // Requests for higher floors will have lower priorities due to min heap structure
else
priority = 20 - destination;  // Requests for higher floors will have higher priorities
}

void set_dir(bool dir)
{
direction = dir;
}

//declare the member variables.
int priority, start, destination, 
timestamp, start_time, finish_time;
bool direction;
};

Elevator2.cpp 文件:

#include <iostream>
#include <fstream>
#include <stdio.h> //allows for the usage of getchar
#include "Elevator2.h"

void Elevator::displayMessage()
{
cout <<"Welcome to University Towers " << endl << endl << endl<<        "----------------------------" << endl;
}

double Elevator::get_total_cost()
{
return total_cost;
}

double Elevator::get_total_wait_time()
{
return total_wait_time;
}

bool Elevator::get_direction()
{
return direction;
}

void Elevator::change_direction()
{
direction = !direction;
}

void Elevator::set_direction(bool new_direction)
{
direction = new_direction;
}

int Elevator::get_elevator_floor()
{
return elevator_floor;
}

void Elevator::increment_elevator_floor()
{
if(direction) //if direction is TRUE (elevator moving upward) increment
elevator_floor++;
else
elevator_floor--;
}

void Elevator::set_elevator_floor(int new_floor)
{
elevator_floor = new_floor;
}

void Elevator::readRequests()
{
ifstream myStream("T1.txt");

while(!myStream.eof())
{
int timestamp ,currentFloor, destinationFloor;


myStream >> timestamp >> currentFloor >> destinationFloor;
//cout<< endl <<"The current timestamp is "<< timestamp << "The current floor is " <<  currentFloor 
//  << " and the destination floor is " << destinationFloor << endl << endl;
//cout<< endl;

reqNode *temp = new reqNode;

//initialize request node object
temp->timestamp = timestamp;
temp->start = currentFloor;
temp->destination = destinationFloor;
temp->start_time = -1;
temp->finish_time = -1;

//temp->direction = ( (currentFloor < destinationFloor) ? 1 : 0 );
temp->calculate_priority();

if(temp->start < temp->destination)
temp->set_dir(true);
else
temp->set_dir(false);

request.push(*temp);//push nodes into the request bank
}
int i = 0;
while( !request.empty() )
{

 cout << "Node " << i << " : " << request.front().timestamp << " " <<  request.front().start << " " << request.front().destination
<< " " <<  request.front().direction << endl;

//printf_s("%d\n", request.front().direction);

request.pop();//popping the request in order to test
i++;
}

//bool test = false;

//cout << test;

}

Elevator2.h 文件:

#include <iostream>
#include "binaryHeap.h"
#include "reqnode.h"
#include <queue>
using namespace std;

class Elevator
{
public:

//Elevator();//Default constructor
Elevator(int initial_floor)//Each object starts with a initial floor aka the current  floor.
:total_wait_time(0), total_cost(0), elevator_floor(initial_floor),   direction(true)//initialize each object accordingly.
{
readRequests();//for each elevator object...created(i.e - request read, there is a  node created).
}

void displayMessage();//display greeting text.
double get_total_cost();//returns the number of floors the elevator travels.
double get_total_wait_time();//returns the number of floors the elevator travels to  get to user.

bool get_direction();  // get the current direction of the elevator.
void set_direction(bool);  // sets direction to desired direction of the user.

void change_direction();  // reverses direction of the elevator.

int get_elevator_floor();// return the elevator floor(currently on).
void set_elevator_floor(int); // sets according to the user's request.

void increment_elevator_floor();  // moves elevator one floor determined by direction(dependent on the set direction bool).

protected://only calls from inherited/friendship classes are aloud. Never calls from the Driver.

queue<reqNode> waiting_queue;//queue stores reqNode objects, we name this queue - waiting queue.
queue<reqNode> request;// ""                  ""                                - request.
BinaryHeap<reqNode> service_queue;// ""                        ""               - service queue.
queue<reqNode>  finished;//""                      ""                           - finished.

private:

double total_wait_time;//number of floors the elevator travels to get to user.
double total_cost;//number of floors the elevator travels while servicing.
int elevator_floor;//initial floor value.
bool direction;//iterating through the floors for the sabbath algorithm
void readRequests();//read the in the requests per user and store them(by way of the request queue/bank).  
};

懒惰的.cpp 文件:

#include "lazy.h"

void Lazy::setAttributes(reqNode Nodey) 
{

int costToserve = abs(Nodey.start - Nodey.destination);//the floors between user's start_floor and user's destination_floor.
int directionOf = (Nodey.start - Nodey.destination) * -1;//the direction the elevator is heading.
int priority = abs(Nodey.destination - get_elevator_floor());//already in the servicing queue.

}

void Lazy::simulation()
{
time = 0;

while( !request.empty() )  //  while request remain, do:
{
//  check for and read new requests into incoming/waiting queue
while(request.front().timestamp == time)  //  schedule new requests
{
waiting_queue.push(request.front());  // places next request into waiting queue
request.pop();
}

scheduler();  // inputs waiting requests into servicing queue based on elevator's current state

if( !service_queue.isEmpty() )  //  if( !service_queue.isEmpty() )
{
if( service_queue.findMin().direction != get_direction() )  //  if(top node of service queue's direction == current direction of the elevator)
           {                                    
change_direction();//then we change the direction.
}//  else set direction to top node's direction

increment_elevator_floor();  // increments/decrements elevator floor by one.


while( service_queue.findMin().destination == get_elevator_floor() )//check top node to see if it is done;  current floor is destination
{
reqNode * temp;

service_queue.deleteMin(*temp); // min item is serviced; pop from heap
temp->finish_time = time; 
finished.push(*temp);  //  place serviced node to finished bank
}

}
increment_elevator_floor();
time++;
}
}

void Lazy::scheduler()
{
// UPDATE:  PRIORITY TO BE CALCULATED BY REFERENCE POINT (DETERMINED BY DIRECTION) IN DRIVER BY CALL TO REQNODE METHOD

int size = waiting_queue.size(); //set to wait queue's size

if( !service_queue.isEmpty() )
{
for(int i = 0; i < size; i++)//check waiting queue for( i = 0, i < size, i ++)
{
if( waiting_queue.front().start == get_elevator_floor() && waiting_queue.front().direction == get_direction() )//if node's floor == current floor 
{
waiting_queue.front().start_time = time;
service_queue.insert( waiting_queue.front() );  //put in service queue
}
else
{
reqNode *temp = &waiting_queue.front(); //put back in queue
waiting_queue.push(*temp);
waiting_queue.pop();
}
}
}
else
{

if( !waiting_queue.empty() )//check waiting queue (if not empty)
{
/*  Elevator should take the direction of next request waiting and begin moving in that direction.
Elevator should pick up requests that are on the way and in same direction as next waiting request
*/
set_direction(waiting_queue.front().direction);

for(int i = 0; i < size; i++)//check waiting queue for( i = 0, i < size, i ++)
{
if( waiting_queue.front().start == get_elevator_floor() && waiting_queue.front().direction == get_direction() )//if node's floor == current floor 
{
waiting_queue.front().start_time = time;
service_queue.insert( waiting_queue.front() );  //put in service queue
}
else
{
reqNode *temp = &waiting_queue.front(); //put back in queue
waiting_queue.push(*temp);
waiting_queue.pop();
}
}   
}

}

}

懒惰的.h文件:

#include "Elevator2.h"

class Lazy : public Elevator
{   
public:
Lazy(int initial_floor)//initialize the same way you did your base class. same parameters.
:Elevator(initial_floor){}

void simulation();//carries out the normal elevator simulation

private:

void scheduler();//scheduler to handle. 
void setAttributes(reqNode);//set attributes of each request node.
int time;
};   

好的,这就是所有文件。哇,我觉得我的空格键被 4 倍的间距弄坏了!天哪。

最后是 driver2.cpp :(所有魔法发生的地方)

#include <iostream>
#include <fstream>
#include <string>
#include "lazy.h"
#include <algorithm>
using namespace std;



void setNode(reqNode nizzode, int priority)
{
nizzode.priority = priority;
}


int main()
{
Lazy lazy(1);
}

而已。

现在我的代码从 T1.txt 文件中接收 10 个用户请求,并生成一个请求节点,该节点具有时间戳属性(请求它的楼层)和当前楼层(提出请求的用户的原始楼层) ,最后是目的地楼层(用户想要的目的地)。我还认为我的逻辑是根据当前楼层和目标楼层来确定用户的方向。男孩是我错了。帮助将不胜感激!

4

1 回答 1

10

205是十六进制,0xCD在使用调试版本时,它是 Visual Studio 中未初始化数据的可能填充符之一。
您需要调试代码并追溯这些未初始化数据的来源。
这里没有人会为你调试它。

于 2012-10-30T16:44:27.473 回答