Hello everybody
I'm a student and having problems with an assignment - I have at the moment no idea how to achieve an appropriate solution.
I'm aware that you shouldn't help me by showing some code. That's okay for me since I want to solve it by myself but I think it would be helpful when I get a small hint how to think about this problem here:
Write a simple program that controls an elevator in an office building. The
office building has 24 floors (0--23) and 3 elevators. The floors are 3m
heigh and the elevators move with 6m/s. Opening and closing the elevator
doors costs 6 seconds each.
Let's assume that all elevators are parked on the ground floor(0) and that
the doors are open. If a passenger wants to get from the ground floor to the
top floor (23), it would take 6sec (closing) plus 23*3/6sec (moving the
elevator) plus 6sec (opening) the door (=23.5sec). Total delivery time
would be 23.5sec.
Assuming that after 10 seconds another passenger requests an elevator on
the 10th floor (also going to the top floor), you have two options, reuse the
first elevator (which currently is on the 8th floor) or send a different one. If
you reuse the first elevator, it would arrive at second 11 on the 10th floor,
open its door take in the passenger (sec~17), close the doors (sec~23),
move to the top floor (sec~29.5), and open the door (sec~35.5). Total
delivery would be 35.5sec+25.5sec.
Had you used another elevator, the total delivery time would have been
23.5sec+29.5sec (close door (sec~16), move to floor 10 (sec~21), open
door (sec~27), close door (sec~33), move to top floor (sec~33.5), open
door (sec~39.5)).
Your program should read the information about passengers from a file passed as command line paramter
(or cin if '-' is passed as filename). The file contains one line per passenger. Each line contains a triple of
time-of-request, floor-of-request, target-floor-of request. For determining the elevator to use for a given
passenger only consider whether the passenger is going up or down and not the target floor. The target
floor is only considered once the passenger got onto the elevator.
For instance in the above case, the file would look as follows:
0,0,23
10,10,23
Your program should output something similar to the following
0: passenger 1 request on floor 0 handled by e1
6: e1 door closed, moving up (p1-23)
10: passenger 2 request on floor 10 handled by e1
11: e1 stopped on floor 10
17: e1 door opened
23: e1 door closed moving up (p1-23, p2-23)
29.5: e1 stopped on floor 23
35.5: e1 door opened (p1-35.5s, p2-25.5s)
Total delivery time is 61s.
Here is my code so far:
main.cpp
#include <iostream>
#include <fstream>
#include "elevator_system.h"
using namespace std;
int main (int argc, char * const argv[]) {
elevator_system supercomputer;
string input = argv[1];
// reads from commandline
if (input == "-") {
bool exit;
while(!exit){
// get the input string
string input;
cout << "Triple: ";
getline(cin, input, '\n');
if (input == "q") {
exit = true;
break;
}
supercomputer.transform_input( input );
}
//print_array();
supercomputer.moving_elevators();
}
// reads from file
else {
ifstream in_stream;
in_stream.open( input.c_str() );
// check if the file is open
if ( ! in_stream.is_open() )
{
cout << "opening file \'" << input << "\' failed." << endl;
}
string tmp;
// stream reads a word each iteration
while( in_stream >> tmp )
{
supercomputer.transform_input( tmp );
}
// close file
in_stream.close();
}
}
elevator_system.cpp
/*
* system.cpp
* c++_06_ex2_elevator
*
*/
#include "elevator_system.h"
building build1("building1", 24, 3);
elevator el1("el1", 6.0, 0);
elevator el2("el2", 6.0, 0);
elevator el3("el3", 6.0, 0);
static double action[50][3];
static int row_counter;
static int timer;
elevator_system::elevator_system() {
//
}
void elevator_system::transform_input( string input ) {
int first_coma;
int comma_counter = 0;
string str;
// we iterate through the string
for (int i=0; i < input.length(); i++)
{
str = input.at(i);
if (!str.compare(",") && comma_counter < 1 ){
comma_counter++;
first_coma = i;
stringstream Stream;
Stream << input.substr(0, i);
int first;
Stream >> first;
action[row_counter][0] = first;
} else if (!str.compare(",") && comma_counter == 1) {
stringstream Stream_second;
Stream_second << input.substr(first_coma + 1, i);
int second;
Stream_second >> second;
action[row_counter][1] = second;
stringstream Stream_third;
Stream_third << input.substr(i+1, input.length() - 1);
int third;
Stream_third >> third;
action[row_counter][2] = third;
}
}
row_counter++;
}
void elevator_system::moving_elevators() {
// no idea how to use the splitted triple to determine which elevator should be used and how to print it correctly
}
elevator.cpp
/*
* elevator.cpp
* c++_06_ex2_elevator
*/
#include "elevator.h"
elevator::elevator(string el_name, int el_speed, int floor)
: name( el_name ), speed( el_speed ), floor_right_now( floor )
{
door_is_open = true;
}
string elevator::get_name() {
return name;
}
double elevator::get_speed() {
return speed;
}
double elevator::open_door() {
door_is_open = true;
return 6.0;
}
double elevator::close_door() {
door_is_open = false;
return 6.0;
}
// time to move from one floor to another without door opening / closing
double elevator::moving_time( int target_floor ) {
return ( ( target_floor - current_floor() ) * 3 ) / get_speed();
}
// total time to move from one floor to another including door opening / closing
double elevator::total_time_to_move_to(int target_floor ) {
static double total_time = 0;
if ( door_is_open ) {
total_time = close_door() + moving_time( target_floor ) + open_door();
} else {
total_time = open_door() + close_door() + moving_time( target_floor ) + open_door();
}
set_current_floor( target_floor );
return total_time;
}
int elevator::current_floor() {
return floor_right_now;
}
void elevator::set_current_floor( int end_floor ) {
floor_right_now = end_floor;
}
I've managed to read in a triple and split it correctly into three integers and put them into an array. But I have no idea how to proceed with this input after I read all in from the cin or file... I thought maybe I should decide for every tripple what to do and then put that decision into a queue and print the queue afterwards but that's seems to be a strange way for me.