diff --git a/lb5/FIGURE.H b/lb5/FIGURE.H new file mode 100644 index 0000000..907f3b9 --- /dev/null +++ b/lb5/FIGURE.H @@ -0,0 +1,12 @@ +#ifndef FIGURE_H +#define FIGURE_H + +class Figure { +public: + virtual double Square() = 0; + virtual void Print() = 0; + virtual ~Figure() {}; +}; + +#endif /* FIGURE_H */ + diff --git a/lb5/MAIN.CPP b/lb5/MAIN.CPP new file mode 100644 index 0000000..726fcb7 --- /dev/null +++ b/lb5/MAIN.CPP @@ -0,0 +1,78 @@ +#include +#include +#include + +#include "FIGURE.H" +#include "TRIANGLE.H" +#include "RECTANGLE.H" +#include "SQUARE.H" + +#include "TSTACK.H" + + +int main(int argc, char** argv) { + + bool exitkey = false; + char firstkey; + std::shared_ptr
sptr; + TStack
cookie; + + + setlocale(LC_CTYPE, "rus"); + + + std::cout << "Всем бобра!" << std::endl; + std::cout << "Функции:\n" << std::endl; + std::cout << "T - ввод треугольника" << std::endl; + std::cout << "R - ввод прямоугольника" << std::endl; + std::cout << "S - ввод квадрата" << std::endl; + std::cout << "D - удалить элемент" << std::endl; + //std::cout << "A - вывести все элементы стека" << std::endl; + std::cout << "I - вывести все элементы стека" << std::endl; // Добавляем итератор + std::cout << "E! - выход" << std::endl << std::endl; + + firstkey = '0'; + while (exitkey == false) { + if (firstkey >= '0') printf("T>"); + scanf_s("%c", &firstkey); + + switch (firstkey) { + case 'T': + std::cout << "Введите параметры треугольника:" << std::endl; + sptr = std::make_shared(std::cin); + cookie.Push(sptr); + break; + case 'R': + std::cout << "Введите параметры прямоугольника:" << std::endl; + sptr = std::make_shared(std::cin); + cookie.Push(sptr); + break; + case 'S': + std::cout << "Введите параметры квадрата:" << std::endl; + sptr = std::make_shared(std::cin); + cookie.Push(sptr); + break; + case 'D': + cookie.Pop(); + break; + /* + case 'A': + std::cout << cookie << std::endl; + break; + */ + case 'I': + if (cookie.Empty() != true) { + for (auto tmp : cookie) tmp->Print(); + } else std::cout << "Стек пустой! Сперва добавьте чего-нибудь... съедобного" << std::endl << std::endl; + break; + case 'E': + scanf_s("%c", &firstkey); + if (firstkey == '!') { + cookie.Close(); + exitkey = true; + } else std::cout << "Не получено подтверждения!" << std::endl; + break; + } + } + return 0; +} \ No newline at end of file diff --git a/lb5/RECTANGLE.CPP b/lb5/RECTANGLE.CPP new file mode 100644 index 0000000..f327f8e --- /dev/null +++ b/lb5/RECTANGLE.CPP @@ -0,0 +1,58 @@ +#include "RECTANGLE.h" +#include +#include + +Rectangle::Rectangle() :Rectangle(0, 0) { +} + +Rectangle::Rectangle(size_t i, size_t j) : side_a(i), side_b(j) { + //std::cout << "Rectangle created: " << side_a << ", " << side_b << std::endl; +} + +Rectangle::Rectangle(std::istream &is) { + is >> side_a; + is >> side_b; +} + +Rectangle::Rectangle(const Rectangle &orig) { + //std::cout << "Rectangle copy created" << std::endl; + side_a = orig.side_a; + side_b = orig.side_b; +} + +double Rectangle::Square() { + double p = side_a * side_b; + return p; +} + +void Rectangle::Print() { + std::cout << "Прямоугольник:" << std::endl; + std::cout << "a=" << side_a << ", b=" << side_b << std::endl; +} + +Rectangle::~Rectangle() { + //std::cout << "Rectangle deleted" << std::endl; +} + +Rectangle& Rectangle::operator=(const Rectangle& right) { + if (this == &right) return *this; + //std::cout << "Triangle copied" << std::endl; + side_a = right.side_a; + side_b = right.side_b; + return *this; +} + +std::ostream& operator<<(std::ostream& os, const Rectangle& obj) { + os << "a=" << obj.side_a << ", b=" << obj.side_b; + return os; +} + +std::istream& operator>>(std::istream& is, Rectangle& obj) { + is >> obj.side_a; + is >> obj.side_b; + return is; +} + +bool Rectangle::operator==(const Rectangle& obj) const { + return ((side_a == obj.side_a) && (side_b == obj.side_b)); +} \ No newline at end of file diff --git a/lb5/RECTANGLE.H b/lb5/RECTANGLE.H new file mode 100644 index 0000000..e47c3d0 --- /dev/null +++ b/lb5/RECTANGLE.H @@ -0,0 +1,30 @@ +#ifndef RECTANGLE_H +#define RECTANGLE_H +#include +#include +#include "Figure.h" + +class Rectangle : public Figure { +public: + Rectangle(); + Rectangle(std::istream &is); + Rectangle(size_t i, size_t j); + Rectangle(const Rectangle &orig); + + double Square() override; + void Print() override; + + bool operator ==(const Rectangle &obj) const; + + friend std::ostream& operator<<(std::ostream& os, const Rectangle& obj); + friend std::istream& operator>>(std::istream& is, Rectangle& obj); + + Rectangle& operator =(const Rectangle &right); + + virtual ~Rectangle(); +private: + size_t side_a; + size_t side_b; +}; + +#endif diff --git a/lb5/SQUARE.CPP b/lb5/SQUARE.CPP new file mode 100644 index 0000000..33f863a --- /dev/null +++ b/lb5/SQUARE.CPP @@ -0,0 +1,53 @@ +#include "Square.h" +#include +#include + +SquareF::SquareF() : SquareF(0) { +} + +SquareF::SquareF(size_t i) : side_a(i){ + //std::cout << "Square created: " << side_a << std::endl; +} + +SquareF::SquareF(std::istream &is) { + is >> side_a; +} + +SquareF::SquareF(const SquareF &orig) { + //std::cout << "Square copy created" << std::endl; + side_a = orig.side_a; +} + +double SquareF::Square() { + double p = side_a * side_a; + return p; +} + +void SquareF::Print() { + std::cout << "Квадрат:" << std::endl; + std::cout << "a=b=c=d=" << side_a << std::endl; +} + +SquareF::~SquareF() { + //::cout << "Square deleted" << std::endl; +} + +SquareF& SquareF::operator=(const SquareF& right) { + if (this == &right) return *this; + side_a = right.side_a; + return *this; +} + +std::ostream& operator<<(std::ostream &os, const SquareF &obj){ + os << "a=b=c=d= " << obj.side_a; + return os; +} + +std::istream& operator>>(std::istream &is, SquareF &obj){ + is >> obj.side_a; + return is; +} + +bool SquareF::operator==(const SquareF& obj) const { + return (side_a == obj.side_a); +} \ No newline at end of file diff --git a/lb5/SQUARE.h b/lb5/SQUARE.h new file mode 100644 index 0000000..142fd04 --- /dev/null +++ b/lb5/SQUARE.h @@ -0,0 +1,31 @@ +#ifndef SQUARE_H +#define SQUARE_H +#include +#include +#include "Figure.h" + + +class SquareF : public Figure { +public: + SquareF(); + SquareF(std::istream &is); + SquareF(size_t i); + SquareF(const SquareF &orig); + + double Square() override; + void Print() override; + + bool operator ==(const SquareF &obj) const; + + friend std::ostream& operator<<(std::ostream& os, const SquareF& obj); + friend std::istream& operator>>(std::istream& is, SquareF& obj); + + SquareF& operator =(const SquareF &right); + + virtual ~SquareF(); +private: + size_t side_a; +}; + + +#endif \ No newline at end of file diff --git a/lb5/TITERATOR.H b/lb5/TITERATOR.H new file mode 100644 index 0000000..68f7782 --- /dev/null +++ b/lb5/TITERATOR.H @@ -0,0 +1,46 @@ +#ifndef TITERATOR_H +#define TITERATOR_H +#include +#include + +template +class TIterator +{ +public: + + TIterator(std::shared_ptr n) { + node_ptr = n; + } + + std::shared_ptr operator * () { + return node_ptr->GetFigure(); + } + + std::shared_ptr operator -> () { + return node_ptr->GetFigure(); + } + + void operator ++ () { + node_ptr = node_ptr->GetNext(); + } + + TIterator operator ++ (int) { + TIterator iter(*this); + ++(*this); + return iter; + } + + bool operator == (TIterator const& i) { + return node_ptr == i.node_ptr; + } + + bool operator != (TIterator const& i) { + return !(*this == i); + } + +private: + + std::shared_ptr node_ptr; +}; + +#endif \ No newline at end of file diff --git a/lb5/TRIANGLE.CPP b/lb5/TRIANGLE.CPP new file mode 100644 index 0000000..7b6197e --- /dev/null +++ b/lb5/TRIANGLE.CPP @@ -0,0 +1,86 @@ +#include "TRIANGLE.H" +#include +#include + +Triangle::Triangle() : side_a(0), side_b(0), side_c(0) { +} + +Triangle::Triangle(size_t i, size_t j, size_t k) : side_a(i), side_b(j), side_c(k) { + //std::cout << "Triangle created: " << side_a << ", " << side_b << ", " << side_c << std::endl; +} + +Triangle::Triangle(const Triangle& orig) { + //std::cout << "Triangle copy created" << std::endl; + side_a = orig.side_a; + side_b = orig.side_b; + side_c = orig.side_c; +} + +Triangle::Triangle(std::istream &is) { + is >> side_a; + is >> side_b; + is >> side_c; +} + +double Triangle::Square() { + double p = double(side_a + side_b + side_c) / 2.0; + return sqrt(p * (p - double(side_a))*(p - double(side_b))*(p - double(side_c))); +} + +void Triangle::Print() { + std::cout << "Треугольник:" << std::endl; + std::cout << "a=" << side_a << ", b=" << side_b << ", c=" << side_c << std::endl; +} + +Triangle::~Triangle() { + //std::cout << "Triangle deleted" << std::endl; +} + + +Triangle& Triangle::operator=(const Triangle& right) { + if (this == &right) return *this; + //std::cout << "Triangle copied" << std::endl; + side_a = right.side_a; + side_b = right.side_b; + side_c = right.side_c; + return *this; +} + +/* +Triangle& Triangle::operator++() { + side_a++; + side_b++; + side_c++; + return *this; +} + +Triangle operator+(const Triangle& left, const Triangle& right) { + return Triangle(left.side_a + right.side_a, left.side_b + right.side_b, left.side_c + right.side_c); +} + +*/ + +std::ostream& operator<<(std::ostream& os, const Triangle& obj) { + os << "a=" << obj.side_a << ", b=" << obj.side_b << ", c=" << obj.side_c; + return os; +} + +std::istream& operator>>(std::istream& is, Triangle& obj) { + is >> obj.side_a; + is >> obj.side_b; + is >> obj.side_c; + return is; +} + +bool Triangle::operator==(const Triangle& obj) const { + return ((side_a == obj.side_a) && (side_b == obj.side_b) && (side_c == obj.side_c)); +} + + + + + + + + + diff --git a/lb5/TRIANGLE.H b/lb5/TRIANGLE.H new file mode 100644 index 0000000..4f3c7f0 --- /dev/null +++ b/lb5/TRIANGLE.H @@ -0,0 +1,35 @@ +#ifndef TRIANGLE_H +#define TRIANGLE_H +#include +#include +#include "Figure.h" + +class Triangle : public Figure { +public: + Triangle(); + Triangle(std::istream &is); + Triangle(size_t i, size_t j, size_t k); + Triangle(const Triangle& orig); + + //Triangle& operator++(); + + double Square() override; + void Print() override; + + bool operator ==(const Triangle &obj) const; + + friend std::ostream& operator<<(std::ostream& os, const Triangle& obj); + friend std::istream& operator>>(std::istream& is, Triangle& obj); + //friend Triangle operator+(const Triangle& left, const Triangle& right); + + Triangle& operator =(const Triangle &right); + + virtual ~Triangle(); +private: + size_t side_a; + size_t side_b; + size_t side_c; +}; + +#endif /* TRIANGLE_H */ + diff --git a/lb5/TSTACK.CPP b/lb5/TSTACK.CPP new file mode 100644 index 0000000..e06da69 --- /dev/null +++ b/lb5/TSTACK.CPP @@ -0,0 +1,71 @@ +#include "TStack.h" +#include + +template +TStack::TStack() { + head = nullptr; +} + +/* +TStack::TStack(const TStack& orig) { + head = orig.head; +} +*/ + +template +void TStack::Push(std::shared_ptr &obj) { + std::shared_ptr> other = std::make_shared>(obj); + other->SetNext(head); + head = other; +} + +template +std::ostream& operator<<(std::ostream& os, const TStack& stack) { + std::shared_ptr> other = stack.head; + while (other != nullptr){ + other->GetFigure()->Print(); + other = other->GetNext(); + } + return os; +} + +template +bool TStack::Empty() { + return (head == nullptr); +} + +template +std::shared_ptr TStack::Pop() { + std::shared_ptr result; + if (head != nullptr) { + result = head->GetFigure(); + head = head->GetNext(); + } + return result; +} + +template +TStack::~TStack(){ + +} + +template +void TStack::Close(){ + while (!this->Empty()) { + this->Pop(); + } +} + +template +TIterator, T> TStack::begin() { + return TIterator, T>(head); +} + +template +TIterator, T> TStack::end() { + return TIterator, T>(nullptr); +} + +#include "FIGURE.H"; +template class TStack
; +template std::ostream& operator<<(std::ostream &out, const TStack
&obj); \ No newline at end of file diff --git a/lb5/TSTACK.H b/lb5/TSTACK.H new file mode 100644 index 0000000..362a898 --- /dev/null +++ b/lb5/TSTACK.H @@ -0,0 +1,28 @@ +#ifndef TSTACK_H +#define TSTACK_H + +#include "FIGURE.H" +#include "TSTACKITEM.H" +#include + +#include "TIterator.h" + +template +class TStack { +public: + TStack(); + + void Push(std::shared_ptr &obj); + bool Empty(); + std::shared_ptr Pop(); + template friend std::ostream& operator<<(std::ostream& os, const TStack& stack); + virtual ~TStack(); + void Close(); + + TIterator, T> begin(); + TIterator, T> end(); +private: + std::shared_ptr> head; +}; + +#endif diff --git a/lb5/TSTACKITEM.CPP b/lb5/TSTACKITEM.CPP new file mode 100644 index 0000000..56064c2 --- /dev/null +++ b/lb5/TSTACKITEM.CPP @@ -0,0 +1,42 @@ +#include "TSTACKITEM.H" +#include + +template +TStackItem::TStackItem(const std::shared_ptr &obj){ + this->item = obj; + this->next = nullptr; +} + +template +std::shared_ptr TStackItem::GetFigure() const{ + return this->item; +} + +template +std::shared_ptr> TStackItem::GetNext(){ + return this->next; +} + +template +void TStackItem::SetNext(std::shared_ptr> item){ + this->next = item; +} + +template +std::shared_ptr TStackItem::Remove() { + std::shared_ptr> overremove = this->next; + std::shared_ptr tmp = overremove->GetFigure(); + this->next = overremove->GetNext(); + return tmp; +} + +template +std::ostream& operator<<(std::ostream &os, const TStackItem &obj){ + os << obj.item << std::endl; + return os; +} + + +#include "FIGURE.H" +template class TStackItem
; +template std::ostream& operator<<(std::ostream &out, const TStackItem
&obj); diff --git a/lb5/TSTACKITEM.H b/lb5/TSTACKITEM.H new file mode 100644 index 0000000..4e4bda1 --- /dev/null +++ b/lb5/TSTACKITEM.H @@ -0,0 +1,28 @@ +#ifndef TSTACKITEM_H +#define TSTACKITEM_H + +#include +#include "FIGURE.H" + +template +class TStackItem { +public: + TStackItem(const std::shared_ptr &obj); + + template friend std::ostream& operator<<(std::ostream &os, const TStackItem &obj); + + std::shared_ptr GetFigure() const; + std::shared_ptr> GetNext(); + + void SetNext(std::shared_ptr> item); + + std::shared_ptr Remove(); + + virtual ~TStackItem() {}; +private: + std::shared_ptr item; + std::shared_ptr> next; +}; + +#endif /* TSTACKITEM_H */ +