From 90cabce172856326923f36c9a05fae01138d25aa Mon Sep 17 00:00:00 2001 From: "Ira W. Snyder" Date: Tue, 2 Oct 2007 17:33:08 -0700 Subject: [PATCH] Initial commit Signed-off-by: Ira W. Snyder --- Makefile | 12 ++++++++ direction.hpp | 14 +++++++++ elevator.cpp | 67 ++++++++++++++++++++++++++++++++++++++++ elevator.hpp | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++ position.cpp | 46 ++++++++++++++++++++++++++++ position.hpp | 77 ++++++++++++++++++++++++++++++++++++++++++++++ stop.cpp | 32 +++++++++++++++++++ stop.hpp | 43 ++++++++++++++++++++++++++ test.cpp | 24 +++++++++++++++ 9 files changed, 400 insertions(+) create mode 100644 Makefile create mode 100644 direction.hpp create mode 100644 elevator.cpp create mode 100644 elevator.hpp create mode 100644 position.cpp create mode 100644 position.hpp create mode 100644 stop.cpp create mode 100644 stop.hpp create mode 100644 test.cpp diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..c13f766 --- /dev/null +++ b/Makefile @@ -0,0 +1,12 @@ +test: test.o elevator.o position.o stop.o + g++ -o $@ $^ + +run: test + ./test + +clean: + rm -f *.o test + +all: test + +.PHONY: run clean all diff --git a/direction.hpp b/direction.hpp new file mode 100644 index 0000000..d33e617 --- /dev/null +++ b/direction.hpp @@ -0,0 +1,14 @@ +/* + * CS356 Project 01 -- Elevator Simulator + * + * Direction Enumeration + */ + +#ifndef DIRECTION_HPP +#define DIRECTION_HPP + +enum direction { IDLE, UP, DOWN }; + +#endif /* DIRECTION_HPP */ + +/* vim: set ts=4 sts=4 sw=4 noexpandtab textwidth=112: */ diff --git a/elevator.cpp b/elevator.cpp new file mode 100644 index 0000000..b65cef5 --- /dev/null +++ b/elevator.cpp @@ -0,0 +1,67 @@ +#include "elevator.hpp" + +Elevator::Elevator () + : _stops() + , _pos() + , _direction(IDLE) + , _door_status(CLOSED) +{ + // Intentionally Left Empty +} + +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)); +} + +/* + * Will check if direction needs to be changed, then change it. + * + * Will move this Elevator one step in the correct direction. + * + * The correct direction is defined based on the current direction: + * + * IDLE: Move in the direction that has the most stops + * - Ex: if there are 10 stops above us, and two below, move DOWN + * + * UP: Move upwards + * + * DOWN: Move downwards + */ +void Elevator::move () +{ + std::cout << "move()" << std::endl; +} + +enum direction Elevator::find_best_direction () +{ + /* Make sure that the current direction is IDLE */ + if (_direction != IDLE) + throw bad_direction(); + + std::vector::const_iterator it; + int above, below; + + for (above=0, below=0, it = _stops.begin(); it != _stops.end(); it++) + { + if (_pos.lowerThan (*it)) + above++; + + if (_pos.higherThan (*it)) + below++; + } + + std::cout << "above=" << above << " below=" << below << std::endl; + + return UP; +} + + + + + + + +/* vim: set ts=4 sts=4 sw=4 noexpandtab textwidth=112: */ diff --git a/elevator.hpp b/elevator.hpp new file mode 100644 index 0000000..d78ecd8 --- /dev/null +++ b/elevator.hpp @@ -0,0 +1,85 @@ +/* + * CS356 Project 01 -- Elevator Simulator + * + * Elevator Class Specification + */ + +#ifndef ELEVATOR_HPP +#define ELEVATOR_HPP + +#include +//#include +#include +#include "position.hpp" +#include "direction.hpp" +#include "stop.hpp" + + +enum door_status { CLOSED, OPEN }; +const int elevator_step = 0.1; + +class bad_direction { }; + +class Elevator +{ + public: + /* + * PURPOSE: Construct a new Elevator object + * + * REQUIRE: Nothing + * + * PROMISE: A new Elevator will be constructed + */ + Elevator (); + + /* + * PURPOSE: Tell the elevator to stop at the given floor, + * PURPOSE: going in the given direction. + * + * REQUIRE: floor is a valid floor + * REQUIRE: direction is a valid direction + * + * PROMISE: The elevator will stop at the floor when it gets there + */ + void stop_at (int floor, enum direction _direction); + + /* + * PURPOSE: The elevator will move 1/10th of a floor in the current + * PURPOSE: direction. + * + * REQUIRE: Nothing + * + * PROMISE: The elevator will move if it has floors to stop at, otherwise + * PROMISE: it will sit idle at its current place. + */ + void move (); + + protected: + /* + * PURPOSE: Find the direction we should move in + * + * REQUIRE: _direction must be IDLE + * + * PROMISE: The best direction to move will be returned + */ + enum direction find_best_direction (); + + private: + /* Storage for all of the places that we will be stopping */ + //std::set _stops; + std::vector _stops; + + /* Storage for the current elevator position */ + Position _pos; + + /* Stores the current direction */ + enum direction _direction; + + /* Stores the current door status */ + enum door_status _door_status; + +}; + +#endif /* ELEVATOR_HPP */ + +/* vim: set ts=4 sts=4 sw=4 noexpandtab textwidth=112: */ diff --git a/position.cpp b/position.cpp new file mode 100644 index 0000000..86dc08a --- /dev/null +++ b/position.cpp @@ -0,0 +1,46 @@ +#include "position.hpp" + +Position::Position () + : _major(0) + , _minor(0) +{ + // Intentionally Empty +} + +Position::Position (int initial_position) + : _major(initial_position) + , _minor(0) +{ + // Intentionally Empty +} + +bool Position::operator== (const Position& rhs) +{ + return (_major == rhs._major) && (_minor == rhs._minor); +} + + +bool Position::operator== (const int rhs) +{ + return (_major == rhs) && (_minor == 0); +} + +Position& Position::operator+= (const float rhs) +{ + int major = (int)rhs; + int minor = (int)((rhs - major) * 10); + + _major += major; + _minor += minor; + + return *this; +} + +Position& Position::operator-= (const float rhs) +{ + *this += -rhs; + + return *this; +} + +/* vim: set ts=4 sts=4 sw=4 noexpandtab textwidth=112: */ diff --git a/position.hpp b/position.hpp new file mode 100644 index 0000000..76fa6b6 --- /dev/null +++ b/position.hpp @@ -0,0 +1,77 @@ +/* + * CS356 Project 01 -- Elevator Simulator + * + * Position Class Specification + */ + +#ifndef POSITION_HPP +#define POSITION_HPP + +class Position +{ + public: + /* + * PURPOSE: Construct a new Position object + * + * REQUIRE: Nothing + * + * PROMISE: A new Position object will be created, that + * PROMISE: starts at position 0. + */ + Position (); + + /* + * PURPOSE: Construct a new Position object + * + * REQUIRE: Nothing + * + * PROMISE: A new Position object will be created, and will + * PROMISE: start at position initial_position + */ + Position (int initial_position); + + /* + * PURPOSE: Compare two position objects + * + * REQUIRE: rhs is a valid Position object + * + * PROMISE: True if rhs is at the same position, false otherwise + */ + bool operator== (const Position& rhs); + + /* + * PURPOSE: Compare a Position and a float + * + * REQUIRE: Nothing + * + * PROMISE: True if rhs is within 0.05 of this Position + */ + bool operator== (const int rhs); + + /* + * PURPOSE: Add to this Position + * + * REQUIRE: Nothing + * + * PROMISE: This Position will have the rhs added to it + */ + Position& operator+= (const float rhs); + + /* + * PURPOSE: Subtract from this Position + * + * REQUIRE: Nothing + * + * PROMISE: This Position will have the rhs added to it + */ + Position& operator-= (const float rhs); + + protected: + private: + int _major; + int _minor; +}; + +#endif /* POSITION_HPP */ + +/* vim: set ts=4 sts=4 sw=4 noexpandtab textwidth=112: */ diff --git a/stop.cpp b/stop.cpp new file mode 100644 index 0000000..1629074 --- /dev/null +++ b/stop.cpp @@ -0,0 +1,32 @@ +#include "stop.hpp" + +Stop::Stop (int floor, enum direction mydirection) + : _floor(floor) + , _direction(mydirection) +{ + // Intentionally Left Empty +} + +bool Stop::operator== (Stop& rhs) +{ + return (_floor == rhs._floor) && (_direction == rhs._direction); +} + +bool Stop::lowerThan (Position& rhs) +{ + return (_floor < rhs._floor); +} + +bool Stop::higherThan (Position& rhs) +{ + return (_floor > rhs._floor); +} + +#if 0 +bool Stop::operator< (Stop& rhs) +{ + return _floor < rhs._floor; +} +#endif + +/* vim: set ts=4 sts=4 sw=4 noexpandtab textwidth=112: */ diff --git a/stop.hpp b/stop.hpp new file mode 100644 index 0000000..21e9ac3 --- /dev/null +++ b/stop.hpp @@ -0,0 +1,43 @@ +/* + * CS356 Project 01 -- Elevator Simulator + * + * Stop Class Specification + */ + +#ifndef STOP_HPP +#define STOP_HPP + +#include "position.hpp" +#include "direction.hpp" + +class Stop +{ + public: + /* PURPOSE: Construct a new Stop object, and set the floor and direction + * + * REQUIRE: Nothing + * + * PROMISE: A new Stop object will be created + */ + Stop (int floor, enum direction mydirection); + + /* + * PURPOSE: Check if this and another Stop object is equivalent + * + * REQUIRE: rhs is a valid Stop object + * + * PROMISE: Return true if this and rhs are equivalent, false otherwise + */ + bool operator== (Stop& rhs); + + private: + /* Storage for the floor */ + Position _floor; + + /* Storage for the direction */ + enum direction _direction; +}; + +#endif /* STOP_HPP */ + +/* vim: set ts=4 sts=4 sw=4 noexpandtab textwidth=112: */ diff --git a/test.cpp b/test.cpp new file mode 100644 index 0000000..9a0bf7e --- /dev/null +++ b/test.cpp @@ -0,0 +1,24 @@ +#include +using namespace std; + +#include "elevator.hpp" + +int main (int argc, char *argv[]) +{ + Elevator e; + + e.stop_at (2, UP); + e.stop_at (3, UP); + e.stop_at (4, UP); + e.stop_at (5, DOWN); + e.stop_at (6, DOWN); + + e.move (); + + Elevator e2; + + e.stop_at (3, DOWN); + + return 0; +} + -- 2.34.1