From dd9988b5db6dc820c5fcd07f88513d126e926d22 Mon Sep 17 00:00:00 2001 From: "Ira W. Snyder" Date: Thu, 4 Oct 2007 13:23:14 -0700 Subject: [PATCH] Experimental Changes Signed-off-by: Ira W. Snyder --- direction.hpp | 2 +- elevator.cpp | 72 +++++++++++++++++++++++++++++++++++++++++++++------ elevator.hpp | 15 ++++++++--- position.cpp | 14 +++++++++- position.hpp | 4 +++ stop.cpp | 13 ++++++++++ stop.hpp | 4 +++ test.cpp | 41 ++++++++++++++++++++--------- 8 files changed, 139 insertions(+), 26 deletions(-) diff --git a/direction.hpp b/direction.hpp index d33e617..927016b 100644 --- a/direction.hpp +++ b/direction.hpp @@ -7,7 +7,7 @@ #ifndef DIRECTION_HPP #define DIRECTION_HPP -enum direction { IDLE, UP, DOWN }; +enum direction { IDLE, UP, DOWN, ALL }; #endif /* DIRECTION_HPP */ diff --git a/elevator.cpp b/elevator.cpp index b85fee9..5543b02 100644 --- a/elevator.cpp +++ b/elevator.cpp @@ -12,7 +12,7 @@ Elevator::Elevator () void Elevator::stop_at (int floor, enum direction _direction) { std::cout << "Adding Stop: Floor=" << floor << " Dir=" << _direction << std::endl; - //_stops.insert (Stop(floor, _direction)); + _stops.push_back (Stop(floor, _direction)); } @@ -32,7 +32,42 @@ void Elevator::stop_at (int floor, enum direction _direction) */ void Elevator::move () { - std::cout << "move()" << std::endl; + /* Check if we are at a stop. If we are: + * 1) Open the doors + * 2) Clear the call button + */ + if (currently_at_stop ()) + { + std::cout << "At a stop, right now" << std::endl; + return; + } + + /* Check if we have any more stops to attend to. If we do not, go IDLE */ + if (_stops.size() == 0) + { + _direction = IDLE; + return; + } + + /* Find the best direction to move in, and set our direction to that */ + if (_direction == IDLE) + _direction = find_best_direction (); + + /* Move */ + switch (_direction) + { + case UP: + _pos += elevator_step; + break; + case DOWN: + _pos -= elevator_step; + break; + default: + throw bad_direction (); + break; + } + + std::cout << "at position: " << _pos << std::endl; } enum direction Elevator::find_best_direction () @@ -44,23 +79,44 @@ enum direction Elevator::find_best_direction () std::vector::const_iterator it; int above, below; + /* Find out how many are above and below our current position */ for (above=0, below=0, it = _stops.begin(); it != _stops.end(); it++) { -#if 0 - if (_pos.lowerThan (*it)) + if (_pos < (*it)) above++; - if (_pos.higherThan (*it)) + if (_pos > (*it)) below++; -#endif } - std::cout << "above=" << above << " below=" << below << std::endl; + /* If we have nothing, then stay IDLE */ + if (above == 0 && below == 0) + return IDLE; - return UP; + /* If they are the same, always go up first. + * + * This comes from the design meeting on 2007-10-03 */ + if (above == below) + return UP; + + /* Otherwise, go in the direction that has the most stops */ + return above > below ? UP : DOWN; } +bool Elevator::currently_at_stop () +{ + std::vector::iterator it; + Stop current (_pos, _direction); + + for (it = _stops.begin (); it != _stops.end (); it++) + { + std::cout << "cas(): current=" << current << " it=" << *it << std::endl; + if (*it == current) + return true; + } + return false; +} diff --git a/elevator.hpp b/elevator.hpp index d78ecd8..cff6432 100644 --- a/elevator.hpp +++ b/elevator.hpp @@ -8,7 +8,6 @@ #define ELEVATOR_HPP #include -//#include #include #include "position.hpp" #include "direction.hpp" @@ -16,7 +15,7 @@ enum door_status { CLOSED, OPEN }; -const int elevator_step = 0.1; +const float elevator_step = 0.1; class bad_direction { }; @@ -37,7 +36,7 @@ class Elevator * PURPOSE: going in the given direction. * * REQUIRE: floor is a valid floor - * REQUIRE: direction is a valid direction + * REQUIRE: direction is either UP or DOWN * * PROMISE: The elevator will stop at the floor when it gets there */ @@ -64,9 +63,17 @@ class Elevator */ enum direction find_best_direction (); + /* + * PURPOSE: Figure out if we are currently at a Stop in _stops + * + * REQUIRE: Nothing + * + * PROMISE: Return true if we are at a Stop in _stops, false otherwise + */ + bool currently_at_stop (); + private: /* Storage for all of the places that we will be stopping */ - //std::set _stops; std::vector _stops; /* Storage for the current elevator position */ diff --git a/position.cpp b/position.cpp index 545eade..d38388b 100644 --- a/position.cpp +++ b/position.cpp @@ -33,6 +33,13 @@ Position& Position::operator+= (const float rhs) _major += major; _minor += minor; + /* Check for overflow */ + if (_minor >= 10) + { + _major += 1; + _minor -= 10; + } + return *this; } @@ -54,7 +61,6 @@ bool Position::operator< (const Position& rhs) return false; } -#include bool Position::operator> (const Position& rhs) { @@ -68,5 +74,11 @@ bool Position::operator> (const Position& rhs) return false; } +std::ostream& operator<< (std::ostream& os, Position& rhs) +{ + os << "Position: " << rhs._major << "." << rhs._minor; + return os; +} + /* vim: set ts=4 sts=4 sw=4 noexpandtab textwidth=112: */ diff --git a/position.hpp b/position.hpp index ef94846..02f4feb 100644 --- a/position.hpp +++ b/position.hpp @@ -7,6 +7,8 @@ #ifndef POSITION_HPP #define POSITION_HPP +#include + class Position { public: @@ -69,6 +71,8 @@ class Position bool operator< (const Position& rhs); bool operator> (const Position& rhs); + friend std::ostream& operator<< (std::ostream& os, Position& rhs); + protected: int _major; int _minor; diff --git a/stop.cpp b/stop.cpp index 6595d29..ce491fc 100644 --- a/stop.cpp +++ b/stop.cpp @@ -7,9 +7,22 @@ Stop::Stop (int floor, enum direction mydirection) // Intentionally Left Empty } +Stop::Stop (Position pos, enum direction mydirection) + : Position (pos) + , _direction (mydirection) +{ + // Intentionally Left Empty +} + bool Stop::operator== (Stop& rhs) { return (Position::operator==(rhs)) && (_direction == rhs._direction); } +std::ostream& operator<< (std::ostream& os, Stop& rhs) +{ + os << "Stop: " << "help me" << " dir=" << rhs._direction; + return os; +} + /* vim: set ts=4 sts=4 sw=4 noexpandtab textwidth=112: */ diff --git a/stop.hpp b/stop.hpp index 57839a2..c7256bc 100644 --- a/stop.hpp +++ b/stop.hpp @@ -21,6 +21,9 @@ class Stop : public Position */ Stop (int floor, enum direction mydirection); + + Stop (Position pos, enum direction mydirection); + /* * PURPOSE: Check if this and another Stop object is equivalent * @@ -29,6 +32,7 @@ class Stop : public Position * PROMISE: Return true if this and rhs are equivalent, false otherwise */ bool operator== (Stop& rhs); + friend std::ostream& operator<< (std::ostream& os, Stop& rhs); private: diff --git a/test.cpp b/test.cpp index e8dda09..9689cc1 100644 --- a/test.cpp +++ b/test.cpp @@ -1,22 +1,39 @@ #include using namespace std; -//#include "elevator.hpp" -#include "stop.hpp" +#include "elevator.hpp" int main (int argc, char *argv[]) { - Stop s1 (1, DOWN); - Stop s2 (2, DOWN); + Elevator e; - if (s1 > s2) - cout << "s1 > s2" << endl; - else if (s1 == s2) - cout << "s1 == s2" << endl; - else if (s1 < s2) - cout << "s1 < s2" << endl; - else - cout << "BAD BAD BAD" << endl; + e.stop_at (1, DOWN); + e.stop_at (2, DOWN); + e.move (); + e.move (); + e.move (); + e.move (); + e.move (); + e.move (); + e.move (); + e.move (); + e.move (); + e.move (); + e.move (); + e.move (); + e.move (); + e.move (); + e.move (); + e.move (); + e.move (); + e.move (); + e.move (); + e.move (); + e.move (); + e.move (); + e.move (); + e.move (); + e.move (); return 0; } -- 2.34.1