diff --git a/lesson_3/exercise_1.hpp b/lesson_3/exercise_1.hpp new file mode 100644 index 0000000..11bbc18 --- /dev/null +++ b/lesson_3/exercise_1.hpp @@ -0,0 +1,120 @@ +/* + * Для расчет площади фигур квадрат/ромб/прямоугольник/параллелограмм + * справедлива будет одна общая формула - произведение основания на высоту + */ + +#ifndef EXERCISE_1_HPP_ +#define EXERCISE_1_HPP_ + +#include + +#define PI 3.14159265358979323846 + +float getRadian(float degree) +{ + return PI / 180 * degree; +} + +class Figure +{ +public: + virtual float area() = 0; + virtual ~Figure() { }; +}; + +class Parallelogram : public Figure +{ +private: + float m_side_a; + float m_side_b; + float m_height; +protected: + float getSideA() const + { + return m_side_a; + } + + float getSideB() const + { + return m_side_b; + } +public: + Parallelogram(float side_a, float height, float side_b = 0.0) + : m_side_a(side_a), m_side_b(side_b), m_height(height) { } + + float area() + { + float area = m_side_a * m_height; + + std::cout << "Площадь параллелограмма равна " << area << std::endl; + + return area; + } +}; + +class Circle : public Figure +{ +private: + float m_radius; +public: + Circle(float radius) : m_radius(radius) { } + + float area() + { + float area = PI * pow(m_radius, 2); + + std::cout << "Площадь круга равна " << area << std::endl; + + return area; + } +}; + +class Rectangle : public Parallelogram +{ +public: + Rectangle(float side_a, float side_b) + : Parallelogram(side_a, side_b, side_b) { } + + float area() + { + float area = getSideA() * getSideB(); + + std::cout << "Площадь прямоугольника равна " << area << std::endl; + + return area; + } +}; + +class Square : public Parallelogram +{ +public: + Square(float side_a) + : Parallelogram(side_a, side_a, side_a) { } + + float area() + { + float area = getSideA() * getSideB(); + + std::cout << "Площадь квадрата равна " << area << std::endl; + + return area; + } +}; + +class Rhombus : public Parallelogram +{ +public: + Rhombus(float side_a, float side_b) + : Parallelogram(side_a, side_b, side_b) { } + + float area() + { + float area = getSideA() * getSideB(); + + std::cout << "Площадь ромба равна " << area << std::endl; + + return area; + } +}; + +#endif diff --git a/lesson_3/exercise_2.hpp b/lesson_3/exercise_2.hpp new file mode 100644 index 0000000..ee91b7b --- /dev/null +++ b/lesson_3/exercise_2.hpp @@ -0,0 +1,56 @@ +#ifndef EXERCISE_2_HPP_ +#define EXERCISE_2_HPP_ + +#include + +class Car +{ +private: + std::string m_company; + std::string m_model; +protected: + std::string getCompany() const + { + return m_company; + } + + std::string getModel() const + { + return m_model; + } +public: + Car(std::string company, std::string model) : m_company(company), m_model(model) + { + std::cout << "Транспортное средство компании " + getCompany() + ", модель " + getModel() << std::endl; + } +}; + +class PassengerCar : virtual public Car +{ +public: + PassengerCar(std::string company, std::string model) : Car(company, model) + { + std::cout << "Легковой автомобиль компании " + getCompany() + ", модель " + getModel() << std::endl; + } +}; + +class Bus : virtual public Car +{ +public: + Bus(std::string company, std::string model) : Car(company, model) + { + std::cout << "Автобус компании " + getCompany() + ", модель " + getModel() << std::endl; + } +}; + +class Minivan : public PassengerCar, public Bus +{ +public: + Minivan(std::string company, std::string model) : + Car(company, model), PassengerCar(company, model), Bus(company, model) + { + std::cout << "Минивэн компании " + getCompany() + ", модель " + getModel() << std::endl; + } +}; + +#endif diff --git a/lesson_3/exercise_3.hpp b/lesson_3/exercise_3.hpp new file mode 100644 index 0000000..057bbc0 --- /dev/null +++ b/lesson_3/exercise_3.hpp @@ -0,0 +1,225 @@ +#ifndef EXERCISE_3_HPP_ +#define EXERCISE_3_HPP_ + +#include + +class Fraction +{ +private: + bool is_zero; + bool is_integer; + int numerator; + int denominator; + + /* + * Поиск наибольшего общего делителя + * для числителя и знаменателя + */ + int NOD(int n1, int n2) + { + int div; + if (n1 == n2) return n1; + int d = n1 - n2; + if (d < 0) + { + d = -d; + div = NOD(n1, d); + } + else + div = NOD(n2, d); + + return div; + } + + /* + * Поиск наименьшего общего кратного + * для знаменателей + */ + int NOK(int n1, int n2) + { + return n1 * n2 / NOD(n1, n2); + } + + /* + * Функция сокращения дроби + */ + void reduceFraction(int& a, int& b) + { + int divisor = NOD(abs(a), abs(b)); + a = a / divisor; + b = b / divisor; + } +public: + Fraction () : numerator(0), denominator(0) + { + is_zero = true; + is_integer = true; + } + + Fraction(int num, int dnum) : numerator(num), denominator(dnum) + { + if (numerator == 0 || denominator == 0) + { + std::cout << "Числитель или знаменатель не может быть нулевым!\n" + << "Число будет инициализировано нулём!" << std::endl; + numerator = 0; + denominator = 0; + is_zero = true; + is_integer = true; + } + else + { + is_zero = false; + is_integer = (abs(numerator) == abs(denominator) || denominator == 1); + } + } + + friend std::ostream& operator<<(std::ostream&, const Fraction&); + + Fraction operator+(const Fraction&); + Fraction operator-(const Fraction&); + Fraction operator*(const Fraction&); + Fraction operator/(const Fraction&); + + Fraction operator-() const; + + bool operator==(const Fraction&); + bool operator!=(const Fraction&); + bool operator<(const Fraction&); + bool operator>=(const Fraction&); + bool operator>(const Fraction&); + bool operator<=(const Fraction&); +}; + +std::ostream& operator<<(std::ostream& s, const Fraction& f) +{ + if (f.is_integer) + { + if (f.is_zero) + s << f.numerator; + else + s << f.numerator / f.denominator; + } + else + s << f.numerator << '/' << f.denominator; + + return s; +} + +Fraction Fraction::operator+(const Fraction& f) +{ + if (f.is_zero) + return Fraction(numerator, denominator); + + if (is_zero) + return Fraction(f.numerator, f.denominator); + + int new_denominator = NOK(denominator, f.denominator); + int new_numerator = numerator * (new_denominator / denominator) + f.numerator * (new_denominator / f.denominator); + + reduceFraction(new_numerator, new_denominator); + + return Fraction(new_numerator, new_denominator); +} + +Fraction Fraction::operator-(const Fraction& f) +{ + if (f.is_zero) + return Fraction(numerator, denominator); + + if (is_zero) + return Fraction(-f.numerator, f.denominator); + + int new_denominator = NOK(denominator, f.denominator); + int new_numerator = numerator * (new_denominator / denominator) - f.numerator * (new_denominator / f.denominator); + + if (new_numerator == 0) + return Fraction(); + + reduceFraction(new_numerator, new_denominator); + + return Fraction(new_numerator, new_denominator); +} + +Fraction Fraction::operator*(const Fraction& f) +{ + if (is_zero || f.is_zero) + return Fraction(); + + int new_numerator = numerator * f.numerator; + int new_denominator = denominator * f.denominator; + + reduceFraction(new_numerator, new_denominator); + + return Fraction(new_numerator, new_denominator); +} + +Fraction Fraction::operator/(const Fraction& f) +{ + if (f.is_zero) + { + std::cout << " [На 0 делить нельзя!] "; + return Fraction(); + } + + if (is_zero) + return Fraction(); + + return *this * Fraction(f.denominator, f.numerator); +} + +Fraction Fraction::operator-() const +{ + return Fraction(-numerator, denominator); +} + +bool Fraction::operator==(const Fraction& f) +{ + if (is_zero || f.is_zero) + return numerator == f.numerator; + + int a_n = numerator; + int a_d = denominator; + + int b_n = f.numerator; + int b_d = f.denominator; + + reduceFraction(a_n, a_d); + reduceFraction(b_n, b_d); + + return a_n == b_n && a_d == b_d; +} + +bool Fraction::operator!=(const Fraction& f) +{ + return !(*this == f); +} + +bool Fraction::operator<(const Fraction& f) +{ + if (is_zero || f.is_zero) + return numerator < f.numerator; + + int new_denominator = NOK(denominator, f.denominator); + int a_n = numerator * (new_denominator / denominator); + int b_n = f.numerator * (new_denominator / f.denominator); + + return a_n < b_n; +} + +bool Fraction::operator>=(const Fraction& f) +{ + return (*this == f || !(*this < f)); +} + +bool Fraction::operator>(const Fraction& f) +{ + return (*this != f && !(*this < f)); +} + +bool Fraction::operator<=(const Fraction& f) +{ + return (*this == f || *this < f); +} + +#endif diff --git a/lesson_3/exercise_4.hpp b/lesson_3/exercise_4.hpp new file mode 100644 index 0000000..7563beb --- /dev/null +++ b/lesson_3/exercise_4.hpp @@ -0,0 +1,44 @@ +#ifndef EXERCISE_4_HPP_ +#define EXERCISE_4_HPP_ + +#include + +class Card +{ +public: + enum rank {ACE = 1, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING}; + enum suit {CLUBS, DIAMONDS, HEARTS, SPADES}; + + Card(rank r = ACE, suit s = SPADES, bool ifu = true): m_Rank(r), m_Suit(s), m_IsFaceUp(ifu) { } + + int GetValue() const; + void Flip(); + +// friend std::ostream& operator<<(std::ostream& s, const Card& aCard); + +private: + rank m_Rank; + suit m_Suit; + bool m_IsFaceUp; +}; + +int Card::GetValue() const +{ + int value = 0; + if (m_IsFaceUp) + { + value = m_Rank; + if (value > 10) + { + value = 10; + } + } + return value; +} + +void Card::Flip() +{ + m_IsFaceUp = !m_IsFaceUp; +} + +#endif diff --git a/lesson_3/main.cpp b/lesson_3/main.cpp new file mode 100644 index 0000000..c8a17cc --- /dev/null +++ b/lesson_3/main.cpp @@ -0,0 +1,130 @@ +#include +#include +#include +#include "exercise_1.hpp" +#include "exercise_2.hpp" +#include "exercise_3.hpp" +#include "exercise_4.hpp" + +using namespace std; + +void ex_1(); +void ex_2(); +void ex_3(); +void ex_4(); + +int main() +{ + ex_1(); + ex_2(); + ex_3(); + + return 0; +} + +void ex_1() +{ + Parallelogram Pgrm(15.2, 9.5); + Circle Crcl(14.5); + Rectangle Rctgl(14.0, 7.1); + Square Sqre(23.3); + Rhombus Rmbs(17.4, 13.2); + + vector> f; + + f.push_back(Pgrm); + f.push_back(Crcl); + f.push_back(Rctgl); + f.push_back(Sqre); + f.push_back(Rmbs); + + for (reference_wrapper
c : f) + c.get().area(); + + /* + * Площадь параллелограмма равна 144.4 + * Площадь круга равна 660.52 + * Площадь прямоугольника равна 99.4 + * Площадь квадрата равна 542.89 + * Площадь ромба равна 229.68 + */ +} + +void ex_2() +{ + Minivan m("Макдоналдс", "ABC"); + + /* + * Транспортное средство компании Макдоналдс, модель ABC + * Легковой автомобиль компании Макдоналдс, модель ABC + * Автобус компании Макдоналдс, модель ABC + * Минивэн компании Макдоналдс, модель ABC + */ +} + +void ex_3() +{ + Fraction a(3, 9); + Fraction b(2, 3); + Fraction c; // 0 + + cout << "a = " << a << endl; + cout << "b = " << b << endl; + cout << "c = " << c << endl; + cout << "[a + b] " << a << " + " << b << " = " << a + b << endl; + cout << "[a - b] " << a << " - " << b << " = " << a - b << endl; + cout << "[a * b] " << a << " * " << b << " = " << a * b << endl; + cout << "[a / b] " << a << " / " << b << " = " << a / b << endl; + cout << "[c + b] " << c << " + " << b << " = " << c + b << endl; + cout << "[b + c] " << b << " + " << c << " = " << b + c << endl; + cout << "[c - b] " << c << " - " << b << " = " << c - b << endl; + cout << "[b - c] " << b << " - " << c << " = " << b - c << endl; + cout << "[c * b] " << c << " * " << b << " = " << c * b << endl; + cout << "[b * c] " << b << " * " << c << " = " << b * c << endl; + cout << "[c / b] " << c << " / " << b << " = " << c / b << endl; + cout << "[b / c] " << b << " / " << c << " = " << b / c << endl; + cout << "-a = " << -a << endl; + cout << "[a == b] " << a << " == " << b << " is " << ((a == b) ? "true" : "false") << endl; + cout << "[a != b] " << a << " != " << b << " is " << ((a != b) ? "true" : "false") << endl; + cout << "[a < b] " << a << " < " << b << " is " << ((a < b) ? "true" : "false") << endl; + cout << "[a <= b] " << a << " <= " << b << " is " << ((a <= b) ? "true" : "false") << endl; + cout << "[a > b] " << a << " > " << b << " is " << ((a > b) ? "true" : "false") << endl; + cout << "[a >= b] " << a << " >= " << b << " is " << ((a >= b) ? "true" : "false") << endl; + cout << "[c == a] " << c << " == " << a << " is " << ((c == a) ? "true" : "false") << endl; + cout << "[c != a] " << c << " != " << a << " is " << ((c != a) ? "true" : "false") << endl; + cout << "[c < a] " << c << " < " << a << " is " << ((c < a) ? "true" : "false") << endl; + cout << "[c <= a] " << c << " <= " << a << " is " << ((c <= a) ? "true" : "false") << endl; + cout << "[c > a] " << c << " > " << a << " is " << ((c > a) ? "true" : "false") << endl; + cout << "[c >= a] " << c << " >= " << a << " is " << ((c >= a) ? "true" : "false") << endl; + + /* + * a = 3/9 + * b = 2/3 + * c = 0 + * [a + b] 3/9 + 2/3 = 1 + * [a - b] 3/9 - 2/3 = -1/3 + * [a * b] 3/9 * 2/3 = 2/9 + * [a / b] 3/9 / 2/3 = 1/2 + * [c + b] 0 + 2/3 = 2/3 + * [b + c] 2/3 + 0 = 2/3 + * [c - b] 0 - 2/3 = -2/3 + * [b - c] 2/3 - 0 = 2/3 + * [c * b] 0 * 2/3 = 0 + * [b * c] 2/3 * 0 = 0 + * [c / b] 0 / 2/3 = 0 + * [b / c] 2/3 / 0 = [На 0 делить нельзя!] 0 + * -a = -3/9 + * [a == b] 3/9 == 2/3 is false + * [a != b] 3/9 != 2/3 is true + * [a < b] 3/9 < 2/3 is true + * [a <= b] 3/9 <= 2/3 is true + * [a > b] 3/9 > 2/3 is false + * [a >= b] 3/9 >= 2/3 is false + * [c == a] 0 == 3/9 is false + * [c != a] 0 != 3/9 is true + * [c < a] 0 < 3/9 is true + * [c <= a] 0 <= 3/9 is true + * [c > a] 0 > 3/9 is false + * [c >= a] 0 >= 3/9 is false + */ +}