2012-10-30 3 views
-5

현재 부울 값을 사용하여 방향을 출력하려고합니다.부울 출력이 참 또는 거짓 대신 205입니다.

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 파일 :

#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 파일 :

여기

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 파일입니다

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). 
}; 

lazy.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(); 
} 
} 
} 

} 

} 

lazy.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 명의 사용자 요청을 가져 와서 속성 Timestamp (요청한 바닥) 및 사용자 (사용자의 원래 층) 속성을 가진 요청 노드를 생성합니다. 요청 만들기), 마지막으로 대상 층 (사용자가 원하는 대상)을 선택합니다. 나는 또한 내가 현재의 바닥과 목적지 층을 기반으로 사용자의 방향을 결정할 논리를 가지고 있다고 생각했다. 소년은 내가 틀렸다. 도움이 기꺼이 감사하겠습니다!

+2

tl; dr 아래로 좁히십시오. –

+1

0이 아닌 값은 모두 'true'로 간주됩니다. –

+1

이제 Makefile과 configure 스크립트를 추가하십시오. README를 추가 할 수도 있습니다. –

답변

9

205은 디버그 빌드로 작업 할 때 Visual Studio에서 초기화되지 않은 데이터의 가능한 채우기 중 하나 인 0xCD의 16 진수입니다.
코드를 디버깅하고이 초기화되지 않은 데이터의 출처를 추적해야합니다.
여기 아무도 디버그하지 않습니다.

+0

''저는 게으른 "게으름"이 " 저의 전체 C++ 클래스 할당을 디버그하십시오. " '' – Steve

+0

나는 이것이 오래되었다는 것을 알고있다. 그러나, 나는 shoosh에 동의한다. bool 변수가 true 또는 false로 초기화되지 않은 경우가 가장 많습니다. 나는 비슷한 것을 보았다. – iCode