From 947ec925cca3be5215b7d767e2e0d7f3e4da0ed1 Mon Sep 17 00:00:00 2001 From: Alexander Zhirov Date: Thu, 15 Jul 2021 00:29:27 +0300 Subject: [PATCH] lesson 8 --- lesson_8/exercise_1.hpp | 40 +++++++++++++ lesson_8/exercise_2.hpp | 48 +++++++++++++++ lesson_8/exercise_3.hpp | 127 ++++++++++++++++++++++++++++++++++++++++ lesson_8/main.cpp | 15 +++++ 4 files changed, 230 insertions(+) create mode 100644 lesson_8/exercise_1.hpp create mode 100644 lesson_8/exercise_2.hpp create mode 100644 lesson_8/exercise_3.hpp create mode 100644 lesson_8/main.cpp diff --git a/lesson_8/exercise_1.hpp b/lesson_8/exercise_1.hpp new file mode 100644 index 0000000..6cd8c13 --- /dev/null +++ b/lesson_8/exercise_1.hpp @@ -0,0 +1,40 @@ +#ifndef EXERCISE_1_HPP_ +#define EXERCISE_1_HPP_ + +class DivisionByZero +{ +private: + std::string m_message; +public: + DivisionByZero(std::string message = "") : m_message(message) {} + const std::string& getMessage() const { return m_message; } +}; + +namespace my +{ + +template +double div(T a, T b) +{ + if (b == 0) + throw DivisionByZero("Деление на ноль!"); + + return a / b; +} + +} + +void exercise_1() +{ + try + { + double result = my::div(10, 0); + std::cout << result << std::endl; + } + catch (const DivisionByZero& e) + { + std::cerr << e.getMessage() << std::endl; + } +} + +#endif diff --git a/lesson_8/exercise_2.hpp b/lesson_8/exercise_2.hpp new file mode 100644 index 0000000..88c5ec2 --- /dev/null +++ b/lesson_8/exercise_2.hpp @@ -0,0 +1,48 @@ +#ifndef EXERCISE_2_HPP_ +#define EXERCISE_2_HPP_ + +class Ex +{ +public: + double m_x; + + Ex(double x) : m_x(x) {}; +}; + +class Bar +{ +private: + double m_y; +public: + Bar(double y = 0.0) : m_y(y) {} + void set(double a) + { + if ((m_y + a) > 100) + throw Ex(a * m_y); + else + m_y = a; + } +}; + +void exercise_2() +{ + Bar a; + int b; + + try + { + while (true) + { + std::cin >> b; + if (b == 0) + break; + a.set(b); + } + } + catch (const Ex& e) + { + std::cout << e.m_x << std::endl; + } +} + +#endif diff --git a/lesson_8/exercise_3.hpp b/lesson_8/exercise_3.hpp new file mode 100644 index 0000000..7303865 --- /dev/null +++ b/lesson_8/exercise_3.hpp @@ -0,0 +1,127 @@ +#ifndef EXERCISE_3_HPP_ +#define EXERCISE_3_HPP_ + +class OffTheField +{ + int cur_pos_x; + int cur_pos_y; + int new_pos_x; + int new_pos_y; + std::string m_message; +public: + OffTheField(int c_x, int c_y, int n_x, int n_y) : + cur_pos_x(c_x), cur_pos_y(c_y), new_pos_x(n_x), new_pos_y(n_y), m_message("") + { + if (new_pos_x > 10 || new_pos_x < 1) + { + m_message += "Ошибка координаты X! Робот выходит за границу поля!\n"; + m_message += '[' + std::to_string(cur_pos_x) + ';' + std::to_string(cur_pos_y) + "] => [" + + std::to_string(new_pos_x) + ';' + std::to_string(new_pos_y) + "]\n"; + } + if (new_pos_y > 10 || new_pos_y < 1) + { + m_message += "Ошибка координаты Y! Робот выходит за границу поля!\n"; + m_message += '[' + std::to_string(cur_pos_x) + ';' + std::to_string(cur_pos_y) + "] => [" + + std::to_string(new_pos_x) + ';' + std::to_string(new_pos_y) + "]\n"; + } + } + + const std::string& getInfo() const + { + return m_message; + } +}; + +class IllegalCommand +{ +private: + int cur_pos_x; + int cur_pos_y; + int new_pos_x; + int new_pos_y; + std::string m_message; +public: + IllegalCommand(int c_x, int c_y, int n_x, int n_y) : + cur_pos_x(c_x), cur_pos_y(c_y), new_pos_x(n_x), new_pos_y(n_y), m_message("") + { + if (abs(cur_pos_x - new_pos_x) > 1) + { + m_message += "Ошибка координаты X! Шаг слишком велик!\n"; + m_message += '[' + std::to_string(cur_pos_x) + ';' + std::to_string(cur_pos_y) + "] => [" + + std::to_string(new_pos_x) + ';' + std::to_string(new_pos_y) + "]\n"; + } + + if (abs(cur_pos_y - new_pos_y) > 1) + { + m_message += "Ошибка координаты Y! Шаг слишком велик!\n"; + m_message += '[' + std::to_string(cur_pos_x) + ';' + std::to_string(cur_pos_y) + "] => [" + + std::to_string(new_pos_x) + ';' + std::to_string(new_pos_y) + "]\n"; + } + + if (cur_pos_x == new_pos_x && cur_pos_y == new_pos_y) + { + m_message += "Ошибка координаты X и Y! Нельзя шагнуть на текущее место!\n"; + m_message += '[' + std::to_string(cur_pos_x) + ';' + std::to_string(cur_pos_y) + "] => [" + + std::to_string(new_pos_x) + ';' + std::to_string(new_pos_y) + "]\n"; + } + } + + const std::string& getInfo() const + { + return m_message; + } +}; + +class Robot +{ +private: + int pos_x; + int pos_y; +public: + Robot(int x = 5, int y = 5) : pos_x(x), pos_y(y) + { + std::cout << "Начальная позиция робота: [" << pos_x << ';' << pos_y << ']' << std::endl; + } + + void move(int x, int y) + { + if (abs(pos_x - x) > 1 || abs(pos_y - y) > 1 || (pos_x == x && pos_y == y)) + throw IllegalCommand(pos_x, pos_y, x, y); + if (x > 10 || x < 1 || y > 10 || y < 1) + throw OffTheField(pos_x, pos_y, x, y); + + std::cout << "Робот шагнул [" << pos_x << ';' << pos_y << "] => [" << x << ';' << y << ']' << std::endl; + + pos_x = x; + pos_y = y; + } +}; + +void exercise_3() +{ + int x = 0, y = 0; + Robot r; + + while (true) + { + std::cin >> x >> y; + + if (x == 0 && y == 0) + break; + + try + { + r.move(x, y); + } + catch (const IllegalCommand& e) + { + std::cerr << e.getInfo() << std::endl; + } + catch (const OffTheField& e) + { + std::cerr << e.getInfo() << std::endl; + } + } +} + +#endif diff --git a/lesson_8/main.cpp b/lesson_8/main.cpp new file mode 100644 index 0000000..3dd38cc --- /dev/null +++ b/lesson_8/main.cpp @@ -0,0 +1,15 @@ +#include +#include "exercise_1.hpp" +#include "exercise_2.hpp" +#include "exercise_3.hpp" + +using namespace std; + +int main() +{ + exercise_1(); + exercise_2(); + exercise_3(); + + return 0; +}