Advertisement
paster442

Untitled

Mar 7th, 2025
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.07 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdexcept>
  3. #include <cmath>
  4.  
  5. using namespace std;
  6.  
  7.  
  8. class Vector3D {
  9. private:
  10. double x, y, z;
  11.  
  12. public:
  13. Vector3D() {
  14. x = 0;
  15. y = 0;
  16. z = 0;
  17. }
  18.  
  19. Vector3D(double x_val, double y_val, double z_val) {
  20. x = x_val;
  21. y = y_val;
  22. z = z_val;
  23. }
  24.  
  25. Vector3D(const Vector3D& other) {
  26. x = other.x;
  27. y = other.y;
  28. z = other.z;
  29. }
  30.  
  31. ~Vector3D() {}
  32.  
  33. double norm() const {
  34. return sqrt(x * x + y * y + z * z);
  35. }
  36.  
  37. void setX(double x_val) {
  38. x = x_val;
  39. }
  40.  
  41. void setY(double y_val) {
  42. y = y_val;
  43. }
  44.  
  45. void setZ(double z_val) {
  46. z = z_val;
  47. }
  48.  
  49. double getX() const {
  50. return x;
  51. }
  52.  
  53. double getY() const {
  54. return y;
  55. }
  56.  
  57. double getZ() const {
  58. return z;
  59. }
  60.  
  61. Vector3D& operator+=(const Vector3D& other) {
  62. x = x + other.x;
  63. y = y + other.y;
  64. z = z + other.z;
  65. return *this;
  66. }
  67.  
  68. Vector3D operator+(const Vector3D& other) const {
  69. return Vector3D(x + other.x, y + other.y, z + other.z);
  70. }
  71.  
  72. Vector3D& operator*=(double scalar) {
  73. x = x * scalar;
  74. y = y * scalar;
  75. z = z * scalar;
  76. return *this;
  77. }
  78.  
  79. Vector3D operator*(double scalar) const {
  80. return Vector3D(x * scalar, y * scalar, z * scalar);
  81. }
  82.  
  83. friend Vector3D operator*(double scalar, const Vector3D& v) {
  84. return v * scalar;
  85. }
  86.  
  87. double operator*(const Vector3D& other) const {
  88. return x * other.x + y * other.y + z * other.z;
  89. }
  90. };
  91.  
  92.  
  93. void checkexception() {
  94. if(cin.fail()) {
  95. throw invalid_argument("Error receiving input");
  96. }
  97. }
  98.  
  99. /*
  100. class VectorArray {
  101. private:
  102. vector <Vector3D> data;
  103.  
  104. public:
  105. // Constructor & Destructor
  106. VectorArray() {}
  107.  
  108. VectorArray(const VectorArray& other) {
  109. for(int i = 0; i < data.size(); i++) {
  110. data[i] = other[i];
  111. }
  112. } // : data(other.data) {}
  113. ~VectorArray() {}
  114.  
  115. // Overload [] operator
  116. Vector3D& operator[](int index) {
  117. //if (index >= data.size()) throw out_of_range("Index out of bounds");
  118. return data[index];
  119. }
  120.  
  121. const Vector3D& operator[](int index) const {
  122. //if (index >= data.size()) throw out_of_range("Index out of bounds");
  123. return data[index];
  124. }
  125.  
  126. // Insert function
  127. void insert(int index, const Vector3D& v) {
  128. //if (index > data.size()) throw out_of_range("Index out of range");
  129. data.insert(data.begin() + index, v);
  130. }
  131.  
  132. // Erase function
  133. void erase(size_t index, size_t count = 1) {
  134. //if (index >= data.size()) throw out_of_range("Index out of bounds");
  135. data.erase(data.begin() + index, data.begin() + min(index + 1, data.size()));
  136. }
  137.  
  138.  
  139. void print() const {
  140. for (const auto& v : data) {
  141. v.print();
  142. }
  143. }
  144.  
  145. };
  146. */
  147.  
  148.  
  149.  
  150. class VectorArray {
  151. private:
  152. Vector3D* data;
  153. int size;
  154. int capacity;
  155.  
  156. void resize(int new_capacity) {
  157. if (new_capacity < size) {
  158. throw logic_error("New capacity cannot be less than current size");
  159. }
  160. Vector3D* new_data = new Vector3D[new_capacity];
  161. for (int i = 0; i < size; ++i) {
  162. new_data[i] = data[i];
  163. }
  164. delete[] data;
  165. data = new_data;
  166. capacity = new_capacity;
  167. }
  168.  
  169. public:
  170. VectorArray() {
  171. size = 0;
  172. capacity = 1;
  173. data = new Vector3D[capacity];
  174. }
  175.  
  176. VectorArray(int initial_size) {
  177. size = initial_size;
  178. capacity = initial_size;
  179. if (initial_size < 0) {
  180. throw invalid_argument("Initial size cannot be negative");
  181. }
  182. data = new Vector3D[capacity];
  183. }
  184.  
  185. VectorArray(const VectorArray& other) {
  186. size = other.size;
  187. capacity = other.capacity;
  188. data = new Vector3D[capacity];
  189. for (int i = 0; i < size; ++i) {
  190. data[i] = other.data[i];
  191. }
  192. }
  193.  
  194. ~VectorArray() {
  195. delete[] data;
  196. }
  197.  
  198. Vector3D& operator[](int index) {
  199. if (index < 0 || index >= size) {
  200. throw out_of_range("Index out of bounds");
  201. }
  202. return data[index];
  203. }
  204.  
  205. const Vector3D& operator[](int index) const {
  206. if (index < 0 || index >= size) {
  207. throw out_of_range("Index out of bounds");
  208. }
  209. return data[index];
  210. }
  211.  
  212. void insert(int index, const Vector3D& vec) {
  213. if (index < -1 || index >= size) {
  214. throw out_of_range("Invalid insertion index");
  215. }
  216. if (size == capacity) {
  217. resize(capacity * 2);
  218. }
  219. for (int i = size - 1; i > index; --i) {
  220. data[i + 1] = data[i];
  221. }
  222. data[index + 1] = vec;
  223. size++;
  224. }
  225.  
  226. void erase(int index, int count) {
  227. if (index < 0 || index >= size) {
  228. throw out_of_range("Invalid starting index for erase");
  229. }
  230. if (count < 0 || index + count > size) {
  231. throw invalid_argument("Invalid count for erase");
  232. }
  233. for (int i = index; i < size - count; ++i) {
  234. data[i] = data[i + count];
  235. }
  236. size -= count;
  237. }
  238.  
  239. int getSize() const {
  240. return size;
  241. }
  242. };
  243.  
  244.  
  245. int main() {
  246. try {
  247. cout << "\nTesting VectorArray class:" << endl;
  248. VectorArray arr(2);
  249. arr[0] = Vector3D(1, 2, 3);
  250. arr[1] = Vector3D(4, 5, 6);
  251.  
  252. cout << "Initial array:" << endl;
  253. for (int i = 0; i < arr.getSize(); ++i) {
  254. cout << "Vector " << i << ": ("
  255. << arr[i].getX() << ", "
  256. << arr[i].getY() << ", "
  257. << arr[i].getZ() << ")" << endl;
  258. }
  259.  
  260. arr.insert(0, Vector3D(7, 8, 9));
  261. cout << "\nAfter inserting (7, 8, 9) after index 0:" << endl;
  262. for (int i = 0; i < arr.getSize(); ++i) {
  263. cout << "Vector " << i << ": ("
  264. << arr[i].getX() << ", "
  265. << arr[i].getY() << ", "
  266. << arr[i].getZ() << ")" << endl;
  267. }
  268.  
  269. arr.erase(1, 1);
  270. cout << "\nAfter erasing 1 element from index 1:" << endl;
  271. for (int i = 0; i < arr.getSize(); ++i) {
  272. cout << "Vector " << i << ": ("
  273. << arr[i].getX() << ", "
  274. << arr[i].getY() << ", "
  275. << arr[i].getZ() << ")" << endl;
  276. }
  277.  
  278. VectorArray arr_copy = arr;
  279. cout << "\nCopied array:" << endl;
  280. for (int i = 0; i < arr_copy.getSize(); ++i) {
  281. cout << "Vector " << i << ": ("
  282. << arr_copy[i].getX() << ", "
  283. << arr_copy[i].getY() << ", "
  284. << arr_copy[i].getZ() << ")" << endl;
  285. }
  286. } catch (const exception& e) {
  287. cerr << "Exception caught: " << e.what() << endl;
  288. }
  289.  
  290. return 0;
  291. }
  292.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement