From a53f4a93fa5ba8bbd6904ac1f755a907d17a9ae6 Mon Sep 17 00:00:00 2001 From: Sashkachick Date: Thu, 16 Jun 2022 13:04:07 +0300 Subject: [PATCH] Vector --- vectors/vector.cpp | 71 ++++++++++++++++++++++++++++++++++------------ vectors/vector.h | 61 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 111 insertions(+), 21 deletions(-) diff --git a/vectors/vector.cpp b/vectors/vector.cpp index 9777069..9c74240 100644 --- a/vectors/vector.cpp +++ b/vectors/vector.cpp @@ -1,25 +1,60 @@ #include #include "vector.h" -using namespace std; +bool test_vector3d() +{ + vector3d v1(2, 3, 1), v2(2, 4, 0), v3(0, 0, 0); -ostream& operator <<(ostream& os, const vector3d& v) { - return os << "{ " << v[0] << ", " << v[1] << ", " << v[2] << " }"; + if ((v1 + v2) != vector3d(4, 7, 1)) + { + cerr << "Wrong operation binary + \n"; + return 0; + } + + if ((v1 * 3) != vector3d(6, 9, 3)) + { + cerr << "Wrong operation binary * on digit \n"; + return 0; + } + + if ((v2 / 2) != vector3d(1, 2, 0)) + { + cerr << "Wrong operation binary / on digit \n"; + return 0; + } + + if (-v2 != vector3d(-2, -4, 0)) + { + cerr << "Wrong operation unary - \n"; + return 0; + } + + if (!(v3) != vector3d(1, 1, 1) && !v2 != vector3d(0, 0, 0)) + { + cerr << "Wrong operation unary ! \n"; + return 0; + } + return 1; } + + int main(int argc, char** argv) { - vector3d v1, v2(12), v3(1, 3, 8); - v1[2] = 54; - //vector3d v4 = v1 + v2, v5 = v1 - v2, v6 = v1 * 0.5f; - //cout << "v4: " << v4 << endl << "v5: " << v5 << endl << "v6: " << v6 << endl; - - printf("address of v1: 0x%p, size: %zu bytes\n", &v1, sizeof(v1)); - printf("address of v1.data: 0x%p, size: %zu bytes\n", &v1.data, sizeof(v1.data)); - printf("address of v1.data[-1]: 0x%p, size: %zu bytes\n", &v1.data[-1], sizeof(v1.data[-1])); - printf("address of v1.data[0]: 0x%p, size: %zu bytes\n", &v1.data[0], sizeof(v1.data[0])); - printf("address of v1.data[1]: 0x%p, size: %zu bytes\n", &v1.data[1], sizeof(v1.data[1])); - printf("address of v1.data[2]: 0x%p, size: %zu bytes\n", &v1.data[2], sizeof(v1.data[2])); - printf("address of v1.data[2000]: 0x%p, size: %zu bytes\n", &v1.data[2000], sizeof(v1.data[2000])); - - return 0; -} + + vector3d v1(12), v2(1, 3, 8),v3(0,0,0); + vector3d v4(v3); + v2[1] = 5;//если по ссылке приходит, то присваиваем + cout << v3 + v2; + cout << v2 * 5; + cout << v2 / 3; + cout << v2 ; + if (v2 != v1) + cout << "correct != operator" << endl; + if (test_vector3d()) + return 0; + else + return 1; + + + +} \ No newline at end of file diff --git a/vectors/vector.h b/vectors/vector.h index 07587fb..801f48c 100644 --- a/vectors/vector.h +++ b/vectors/vector.h @@ -1,6 +1,7 @@ #pragma once #include +using namespace std; class vector3d { float data[3]; @@ -10,10 +11,64 @@ class vector3d { vector3d(float value) { data[2] = data[1] = data[0] = value; } vector3d(float a1, float a2, float a3) { data[0] = a1; data[1] = a2; data[2] = a3; } - float& operator[](int idx) { return data[idx]; } - float operator[](int idx) const { return data[idx]; } + vector3d operator+(const vector3d& other) { + return vector3d(data[0] + other.data[0],data[1] + other.data[1],data[2] + other.data[2]); + } + + vector3d operator*(const float digit) + { + return vector3d(data[0] * digit,data[1] * digit,data[2] * digit); + } + + vector3d operator/(const float digit) + { + return vector3d(data[0] / digit,data[1] / digit,data[2] / digit); + } + vector3d operator-() + { + return vector3d(-data[0], -data[1], -data[2]); + } + vector3d operator!() + { + if (data[0] == data[1] == data[2] == 0) + return vector3d(1, 1, 1); + return vector3d(0, 0, 0); + } + bool operator!=(const vector3d& v) + { + if (data[0] != v.data[0] || data[1] != v.data[1] || data[2] != v.data[2]) + return 1; + return 0; + } + + vector3d(const vector3d& v); + friend int main(int argc, char** argv); + friend ostream&operator<<(ostream& os, const vector3d& v); + + float& operator[](int idx) { return data[idx]; }// по ссылке понятно, что изменится сам объект + float operator[](int idx) const { return data[idx]; } // а тут возвращается просто число, которое соотвествует этому индексу, никак не меняет объект, поэтому const в конце=фу-я ничего не меняет + + }; +ostream &operator<<(ostream& os, const vector3d& v) +{ + os <<"{"<