Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions M3/commands.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include "commands.hpp"

#include <iostream>

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");
}
}
16 changes: 16 additions & 0 deletions M3/commands.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef M3_COMMANDS_HPP
#define M3_COMMANDS_HPP

#include <map>
#include <string>

#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
36 changes: 36 additions & 0 deletions M3/geometry_utils.cpp
Original file line number Diff line number Diff line change
@@ -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_;
}
28 changes: 28 additions & 0 deletions M3/geometry_utils.hpp
Original file line number Diff line number Diff line change
@@ -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
49 changes: 49 additions & 0 deletions M3/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <functional>
#include <iostream>
#include <limits>

#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 << "<UNKNOWN COMMAND>\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 << "<INVALID COMMAND>\n";
}
}
}
13 changes: 13 additions & 0 deletions common/clicker.cpp
Original file line number Diff line number Diff line change
@@ -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();
}
19 changes: 19 additions & 0 deletions common/clicker.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef M1_CLICKER_HPP
#define M1_CLICKER_HPP

#include <chrono>

namespace mas {
class Clicker
{
public:
Clicker();

double millisec() const;

private:
std::chrono::time_point< std::chrono::system_clock > start_;
};
}

#endif