diff options
Add Struct tasks.
2 files changed, 94 insertions, 0 deletions
| -rw-r--r-- | pract_SI/Pract. 10/task2/ListofPoints.h | 71 | ||||
| -rw-r--r-- | pract_SI/Pract. 10/task2/main.cpp | 23 |
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 <iostream> +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<<std::endl; + list.pop(); + list.Print(); + list.clear(); + std::cout << list.size; + +}
\ No newline at end of file |
