Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdexcept>
- #include <cmath>
- using namespace std;
- class Vector3D {
- private:
- double x, y, z;
- public:
- Vector3D() {
- x = 0;
- y = 0;
- z = 0;
- }
- Vector3D(double x_val, double y_val, double z_val) {
- x = x_val;
- y = y_val;
- z = z_val;
- }
- Vector3D(const Vector3D& other) {
- x = other.x;
- y = other.y;
- z = other.z;
- }
- ~Vector3D() {}
- double norm() const {
- return sqrt(x * x + y * y + z * z);
- }
- void setX(double x_val) {
- x = x_val;
- }
- void setY(double y_val) {
- y = y_val;
- }
- void setZ(double z_val) {
- z = z_val;
- }
- double getX() const {
- return x;
- }
- double getY() const {
- return y;
- }
- double getZ() const {
- return z;
- }
- Vector3D& operator+=(const Vector3D& other) {
- x = x + other.x;
- y = y + other.y;
- z = z + other.z;
- return *this;
- }
- Vector3D operator+(const Vector3D& other) const {
- return Vector3D(x + other.x, y + other.y, z + other.z);
- }
- Vector3D& operator*=(double scalar) {
- x = x * scalar;
- y = y * scalar;
- z = z * scalar;
- return *this;
- }
- Vector3D operator*(double scalar) const {
- return Vector3D(x * scalar, y * scalar, z * scalar);
- }
- friend Vector3D operator*(double scalar, const Vector3D& v) {
- return v * scalar;
- }
- double operator*(const Vector3D& other) const {
- return x * other.x + y * other.y + z * other.z;
- }
- };
- void checkexception() {
- if(cin.fail()) {
- throw invalid_argument("Error receiving input");
- }
- }
- /*
- class VectorArray {
- private:
- vector <Vector3D> data;
- public:
- // Constructor & Destructor
- VectorArray() {}
- VectorArray(const VectorArray& other) {
- for(int i = 0; i < data.size(); i++) {
- data[i] = other[i];
- }
- } // : data(other.data) {}
- ~VectorArray() {}
- // Overload [] operator
- Vector3D& operator[](int index) {
- //if (index >= data.size()) throw out_of_range("Index out of bounds");
- return data[index];
- }
- const Vector3D& operator[](int index) const {
- //if (index >= data.size()) throw out_of_range("Index out of bounds");
- return data[index];
- }
- // Insert function
- void insert(int index, const Vector3D& v) {
- //if (index > data.size()) throw out_of_range("Index out of range");
- data.insert(data.begin() + index, v);
- }
- // Erase function
- void erase(size_t index, size_t count = 1) {
- //if (index >= data.size()) throw out_of_range("Index out of bounds");
- data.erase(data.begin() + index, data.begin() + min(index + 1, data.size()));
- }
- void print() const {
- for (const auto& v : data) {
- v.print();
- }
- }
- };
- */
- class VectorArray {
- private:
- Vector3D* data;
- int size;
- int capacity;
- void resize(int new_capacity) {
- if (new_capacity < size) {
- throw logic_error("New capacity cannot be less than current size");
- }
- Vector3D* new_data = new Vector3D[new_capacity];
- for (int i = 0; i < size; ++i) {
- new_data[i] = data[i];
- }
- delete[] data;
- data = new_data;
- capacity = new_capacity;
- }
- public:
- VectorArray() {
- size = 0;
- capacity = 1;
- data = new Vector3D[capacity];
- }
- VectorArray(int initial_size) {
- size = initial_size;
- capacity = initial_size;
- if (initial_size < 0) {
- throw invalid_argument("Initial size cannot be negative");
- }
- data = new Vector3D[capacity];
- }
- VectorArray(const VectorArray& other) {
- size = other.size;
- capacity = other.capacity;
- data = new Vector3D[capacity];
- for (int i = 0; i < size; ++i) {
- data[i] = other.data[i];
- }
- }
- ~VectorArray() {
- delete[] data;
- }
- Vector3D& operator[](int index) {
- if (index < 0 || index >= size) {
- throw out_of_range("Index out of bounds");
- }
- return data[index];
- }
- const Vector3D& operator[](int index) const {
- if (index < 0 || index >= size) {
- throw out_of_range("Index out of bounds");
- }
- return data[index];
- }
- void insert(int index, const Vector3D& vec) {
- if (index < -1 || index >= size) {
- throw out_of_range("Invalid insertion index");
- }
- if (size == capacity) {
- resize(capacity * 2);
- }
- for (int i = size - 1; i > index; --i) {
- data[i + 1] = data[i];
- }
- data[index + 1] = vec;
- size++;
- }
- void erase(int index, int count) {
- if (index < 0 || index >= size) {
- throw out_of_range("Invalid starting index for erase");
- }
- if (count < 0 || index + count > size) {
- throw invalid_argument("Invalid count for erase");
- }
- for (int i = index; i < size - count; ++i) {
- data[i] = data[i + count];
- }
- size -= count;
- }
- int getSize() const {
- return size;
- }
- };
- int main() {
- try {
- cout << "\nTesting VectorArray class:" << endl;
- VectorArray arr(2);
- arr[0] = Vector3D(1, 2, 3);
- arr[1] = Vector3D(4, 5, 6);
- cout << "Initial array:" << endl;
- for (int i = 0; i < arr.getSize(); ++i) {
- cout << "Vector " << i << ": ("
- << arr[i].getX() << ", "
- << arr[i].getY() << ", "
- << arr[i].getZ() << ")" << endl;
- }
- arr.insert(0, Vector3D(7, 8, 9));
- cout << "\nAfter inserting (7, 8, 9) after index 0:" << endl;
- for (int i = 0; i < arr.getSize(); ++i) {
- cout << "Vector " << i << ": ("
- << arr[i].getX() << ", "
- << arr[i].getY() << ", "
- << arr[i].getZ() << ")" << endl;
- }
- arr.erase(1, 1);
- cout << "\nAfter erasing 1 element from index 1:" << endl;
- for (int i = 0; i < arr.getSize(); ++i) {
- cout << "Vector " << i << ": ("
- << arr[i].getX() << ", "
- << arr[i].getY() << ", "
- << arr[i].getZ() << ")" << endl;
- }
- VectorArray arr_copy = arr;
- cout << "\nCopied array:" << endl;
- for (int i = 0; i < arr_copy.getSize(); ++i) {
- cout << "Vector " << i << ": ("
- << arr_copy[i].getX() << ", "
- << arr_copy[i].getY() << ", "
- << arr_copy[i].getZ() << ")" << endl;
- }
- } catch (const exception& e) {
- cerr << "Exception caught: " << e.what() << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement