Website Summary Files Commits Branches @commit @diff @stats
path: root / pract_SI / Pract. 10 / task2
diff options
context:
space:
mode:
Commit7104c90385c02473bba9880b0fcd28ffbf1940bc (patch)
Author
 Justsvetoslavov <justsvetoslavov@gmail.com>
2022-01-17 21:47:35 +0200
Committer
 Justsvetoslavov <justsvetoslavov@gmail.com>
2022-01-17 21:47:35 +0200
Files851a0e7f75b2cc097715ef74b2584e582524eb45 /pract_SI/Pract. 10/task2
Introduction_to_programming_FMI-7104c90385c02473bba9880b0fcd28ffbf1940bc.tar
Introduction_to_programming_FMI-7104c90385c02473bba9880b0fcd28ffbf1940bc.tar.gz
Introduction_to_programming_FMI-7104c90385c02473bba9880b0fcd28ffbf1940bc.zip
Parent97b4d62734acf7533a96b08bcc6044eb94847931 (diff)
Add Struct tasks.
2 files changed, 94 insertions, 0 deletions
-rw-r--r--pract_SI/Pract. 10/task2/ListofPoints.h71
-rw-r--r--pract_SI/Pract. 10/task2/main.cpp23
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