diff --git a/M3/commands.cpp b/M3/commands.cpp new file mode 100644 index 0000000..713c64d --- /dev/null +++ b/M3/commands.cpp @@ -0,0 +1,49 @@ +#include "commands.hpp" + +#include + +void mas::createCircle(std::istream& in, std::map< std::string, Circle >& shapes) +{ + std::string name; + int radius = 0; + Point center{0.0, 0.0}; + + in >> name >> radius >> center.x >> center.y; + if (!in || radius <= 0 || shapes.find(name) != shapes.end()) { + throw std::invalid_argument("createCircle: invalid input"); + } + + shapes[name] = Circle{radius, center}; +} + +void mas::showCircle(std::istream& in, std::ostream& out, const std::map< std::string, Circle >& shapes) +{ + std::string name; + + in >> name; + Circle circle = shapes.at(name); + + out << circle.getRadius() << " (" << circle.getCenter().x << ' ' << circle.getCenter().y << ")\n"; +} + +void mas::showFrame(std::istream& in, std::ostream& out, const std::map< std::string, Circle >& shapes) +{ + std::string name; + + in >> name; + Circle circle = shapes.at(name); + + out << "(" << circle.getCenter().x - circle.getRadius() << ' ' << circle.getCenter().y - circle.getRadius() << ") "; + out << '(' << circle.getCenter().x + circle.getRadius() << ' ' << circle.getCenter().y + circle.getRadius() << ")\n"; +} + +void mas::spawnProcess(std::istream& in) +{ + std::string name; + int seed = 0; + + in >> name >> seed; + if (!in || seed < 0) { + throw std::invalid_argument("spawnProcess: invalid input"); + } +} diff --git a/M3/commands.hpp b/M3/commands.hpp new file mode 100644 index 0000000..60f49bf --- /dev/null +++ b/M3/commands.hpp @@ -0,0 +1,16 @@ +#ifndef M3_COMMANDS_HPP +#define M3_COMMANDS_HPP + +#include +#include + +#include "geometry_utils.hpp" + +namespace mas { + void createCircle(std::istream& in, std::map< std::string, Circle >& shapes); + void showCircle(std::istream& in, std::ostream& out, const std::map< std::string, Circle >& shapes); + void showFrame(std::istream& in, std::ostream& out, const std::map< std::string, Circle >& shapes); + void spawnProcess(std::istream& in); +} + +#endif diff --git a/M3/geometry_utils.cpp b/M3/geometry_utils.cpp new file mode 100644 index 0000000..87f57b8 --- /dev/null +++ b/M3/geometry_utils.cpp @@ -0,0 +1,36 @@ +#include "geometry_utils.hpp" + +mas::Circle::Circle(): + radius_(0.0), + center_({0.0, 0.0}) +{} + +mas::Circle::Circle(const Circle& rhs): + radius_(rhs.radius_), + center_({rhs.center_.x, rhs.center_.y}) +{} + +mas::Circle::Circle(int radius, Point center): + radius_(radius), + center_({center.x, center.y}) +{} + +mas::Circle& mas::Circle::operator=(const Circle& rhs) +{ + if (this != &rhs) { + radius_ = rhs.radius_; + center_ = rhs.center_; + } + + return *this; +} + +int mas::Circle::getRadius() +{ + return radius_; +} + +mas::Point mas::Circle::getCenter() +{ + return center_; +} diff --git a/M3/geometry_utils.hpp b/M3/geometry_utils.hpp new file mode 100644 index 0000000..c50c88b --- /dev/null +++ b/M3/geometry_utils.hpp @@ -0,0 +1,28 @@ +#ifndef M3_GEOMETRY_UTILS_HPP +#define M3_GEOMETRY_UTILS_HPP + +namespace mas { + struct Point + { + double x, y; + }; + + class Circle + { + public: + Circle(); + Circle(const Circle& rhs); + Circle(int radius, Point center); + + Circle& operator=(const Circle& rhs); + + int getRadius(); + Point getCenter(); + + private: + int radius_; + Point center_; + }; +} + +#endif diff --git a/M3/main.cpp b/M3/main.cpp new file mode 100644 index 0000000..892d4f0 --- /dev/null +++ b/M3/main.cpp @@ -0,0 +1,49 @@ +#include +#include +#include + +#include "commands.hpp" + +int main() +{ + using namespace mas; + + std::map< std::string, mas::Circle > shapes; + + std::map< std::string, std::function< void() > > commands; + try { + commands["circle"] = std::bind(createCircle, std::ref(std::cin), std::ref(shapes)); + // commands["set"] = std::bind(); + commands["show"] = std::bind(showCircle, std::ref(std::cin), std::ref(std::cout), std::cref(shapes)); + // commands["showset"] = std::bind(); + commands["frame"] = std::bind(showFrame, std::ref(std::cin), std::ref(std::cout), std::cref(shapes)); + // commands["frameset"] = std::bind(); + // commands["spawn"] = std::bind(); + // commands["area-on"] = std::bind(); + // commands["status"] = std::bind(); + // commands["wait"] = std::bind(); + } catch (const std::bad_alloc& e) { + std::cerr << e.what() << '\n'; + + return 1; + } + + std::string command; + while ((std::cin >> command) && !std::cin.eof()) { + try { + commands.at(command)(); + } catch (const std::out_of_range&) { + if (std::cin.fail()) { + std::cin.clear(std::cin.rdstate() ^ std::ios::failbit); + } + std::cin.ignore(std::numeric_limits< std::streamsize >::max(), '\n'); + std::cerr << "\n"; + } catch (...) { + if (std::cin.fail()) { + std::cin.clear(std::cin.rdstate() ^ std::ios::failbit); + } + std::cin.ignore(std::numeric_limits< std::streamsize >::max(), '\n'); + std::cerr << "\n"; + } + } +} diff --git a/common/clicker.cpp b/common/clicker.cpp new file mode 100644 index 0000000..19ce029 --- /dev/null +++ b/common/clicker.cpp @@ -0,0 +1,13 @@ +#include "clicker.hpp" + +mas::Clicker::Clicker(): + start_(std::chrono::high_resolution_clock::now()) +{} + +double mas::Clicker::millisec() const +{ + auto t = std::chrono::high_resolution_clock::now(); + std::chrono::duration< double, std::milli > f = t - start_; + + return f.count(); +} diff --git a/common/clicker.hpp b/common/clicker.hpp new file mode 100644 index 0000000..251efb5 --- /dev/null +++ b/common/clicker.hpp @@ -0,0 +1,19 @@ +#ifndef M1_CLICKER_HPP +#define M1_CLICKER_HPP + +#include + +namespace mas { + class Clicker + { + public: + Clicker(); + + double millisec() const; + + private: + std::chrono::time_point< std::chrono::system_clock > start_; + }; +} + +#endif