From 7104c90385c02473bba9880b0fcd28ffbf1940bc Mon Sep 17 00:00:00 2001 From: Justsvetoslavov Date: Mon, 17 Jan 2022 21:47:35 +0200 Subject: Add Struct tasks. --- pract_SI/Pract. 10/task2/ListofPoints.h | 71 +++++++++++++++++++++++++++++++++ pract_SI/Pract. 10/task2/main.cpp | 23 +++++++++++ 2 files changed, 94 insertions(+) create mode 100644 pract_SI/Pract. 10/task2/ListofPoints.h create mode 100644 pract_SI/Pract. 10/task2/main.cpp (limited to 'pract_SI/Pract. 10/task2') diff --git a/pract_SI/Pract. 10/task2/ListofPoints.h b/pract_SI/Pract. 10/task2/ListofPoints.h new file mode 100644 index 0000000..79ab3b6 --- /dev/null +++ b/pract_SI/Pract. 10/task2/ListofPoints.h @@ -0,0 +1,71 @@ +#pragma once +#include +struct Point +{ + int x; + int y; + + void Print() + { + std::cout << x <<"; "<< y << std::endl; + } +}; + +struct ListofPoints +{ + const int RESIZE = 5; + int size=10; + int currentSize=0; + Point* arr = new Point[size]; + + void addPoint(const Point& point) + { + if (currentSize>=size) + resize(); + arr[currentSize] = point; + currentSize++; + } + + void resize() + { + int totalSize = RESIZE + size; + Point* arrTemp = new Point[totalSize]; + for (size_t i = 0; i < size; i++) + { + arrTemp[i] = arr[i]; + } + Free(); + size = totalSize; + arr = arrTemp; + arrTemp = nullptr; + } + + void clear() + { + Free(); + currentSize = 0; + size = 0; + } + + void pop() + { + int LastElement = currentSize - 1; + arr[LastElement].x = arr[LastElement].y = 0; + currentSize--; + } + + void Free() + { + delete[] arr; + arr = nullptr; + } + + + void Print() + { + for (unsigned i = 0; i < currentSize; i++) + { + arr[i].Print(); + } + } +}; diff --git a/pract_SI/Pract. 10/task2/main.cpp b/pract_SI/Pract. 10/task2/main.cpp new file mode 100644 index 0000000..0fcc55c --- /dev/null +++ b/pract_SI/Pract. 10/task2/main.cpp @@ -0,0 +1,23 @@ +#include "ListofPoints.h" + +int main() +{ + ListofPoints list; + Point A; + Point B; + A.x = A.y = 9; + B.x = B.y = 4; + + for (size_t i = 0; i < 10; i++) + { + list.addPoint(A); + } + list.addPoint(B); + list.Print(); + std::cout << list.size<