From 5378c6b94eaa33fea6f425868c6f56d8420921f8 Mon Sep 17 00:00:00 2001 From: wldooost Date: Mon, 23 May 2022 23:02:00 +0300 Subject: [PATCH] =?UTF-8?q?<=D0=9A=D0=9C=D0=91=D0=9E-04-21=20=D0=9E=D1=81?= =?UTF-8?q?=D1=82=D1=80=D0=B5=D1=86=D0=BE=D0=B2=20=D0=92=D0=BB=D0=B0=D0=B4?= =?UTF-8?q?=D0=B8=D0=BC=D0=B8=D1=80>?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- animals/animal.cpp | 15 +++- animals/animal.h | 158 +++++++++++++++++++++++++++++++++++-- vectors/array.cpp | 28 ------- vectors/array.h | 21 ----- vectors/vector.cpp | 190 ++++++++++++++++++++++++++++++++++++++++----- vectors/vector.h | 122 ++++++++++++++++++++++++++--- 6 files changed, 446 insertions(+), 88 deletions(-) delete mode 100644 vectors/array.cpp delete mode 100644 vectors/array.h diff --git a/animals/animal.cpp b/animals/animal.cpp index 6745215..652fc86 100644 --- a/animals/animal.cpp +++ b/animals/animal.cpp @@ -2,6 +2,17 @@ using namespace std; -int main() { + +int main() +{ + Cat m(4, "Predator", 1.34, 9); + Cow mu(4, "herbivore", 30, 10); + cout << m; + cout << mu; + + Turtle t(4, true, "terrestre"); + Snake s(0, false, 10); + cout << t; + return 0; -} \ No newline at end of file +} diff --git a/animals/animal.h b/animals/animal.h index 83a1944..ad0d55c 100644 --- a/animals/animal.h +++ b/animals/animal.h @@ -1,18 +1,162 @@ #pragma once - #include +#include +#include + +using namespace std; + +class Animal +{ +protected: + int Paws; + + Animal(int paws) { Paws = paws; } -class Animal { public: - float weight; // kg + virtual string about() const + { + return ""; + } + + int setPaws(int paws_) { Paws = paws_; } + void getPaws() { cout << Paws; } + }; -class Mammal : public Animal { + +class Mammal : public Animal +{ +protected: + Mammal(int paws, string squad) : Animal(paws) { Squad = squad; } + + string Squad; + + virtual string about() const + { + stringstream ss; + ss << "Paws: " << Paws << ", " << "Squad: " << Squad << endl; + return ss.str(); + } + public: - float pregnancyDuration; // days + string setSquad(string squad_) { Squad = squad_; } + void getSquad() { cout << Squad; } + }; -class Cat : public Mammal { + +class Cat : public Mammal +{ +protected: + float Vibrissae_length; + int Lives_number; + public: - float vibrissaLength; // meters + Cat(int paws, string squad, float vibrissae_length, int lives_number) : Mammal(paws, squad) { Vibrissae_length = vibrissae_length; Lives_number = lives_number; } + + float setVibrissae_length(float vibrissae_length_) { Vibrissae_length = vibrissae_length_; } + void getVibrissae_length() { cout << Vibrissae_length; } + + int setLives_number(int lives_number_) { Lives_number = lives_number_; } + void getLives_number() { cout << Lives_number; } + + virtual string about() const + { + stringstream ss; + ss << "Paws: " << Paws << ", Squad: " << Squad << ", Vibrissae length: " << Vibrissae_length << ", Lives number: " << Lives_number << endl; + return ss.str(); + } +}; + + +class Cow : public Mammal +{ +protected: + int Spots_number; + int Milk_volume; + + +public: + Cow(int paws, string squad, int spots_number, int milk_volume) : Mammal(paws, squad) { Spots_number = spots_number; Milk_volume = milk_volume; } + Cow(int paws, string squad, int spots_number) : Mammal(paws, squad) { Spots_number = spots_number; Milk_volume = 0; } + + int setSpots_number(int spots_number_) { Spots_number = spots_number_; } + void getSpots_number() { cout << Spots_number; } + + int setMilk_volume(int milk_volume_) { Milk_volume = milk_volume_; } + void getMilk_volume() { cout << Milk_volume; } + + virtual string about() const + { + stringstream ss; + ss << "Paws: " << Paws << ", Squad: " << Squad << ", Spots number: " << Spots_number << ", Milk volume: " << Milk_volume << endl; + return ss.str(); + } }; + + +class Reptile : public Animal +{ +protected: + bool Shell; + + Reptile(int paws, bool shell) : Animal(paws) { Shell = shell; } + + virtual string about() const + { + stringstream ss; + ss << "Paws: " << Paws << ", " << "Shell: " << Shell << endl; + return ss.str(); + } + +public: + bool setShell(bool shell_) { Shell = shell_; } + void getShell() { cout << Shell; } + +}; + + +class Snake : public Reptile +{ +protected: + float Lenth; + +public: + Snake(int paws, bool shell, float lenth) : Reptile(paws, shell) { Lenth = lenth; } + + float setLenth(float lenth_) { Lenth = lenth_; } + void getLenth() { cout << Lenth; } + + virtual string about() const + { + stringstream ss; + ss << "Paws: " << Paws << ", Shell: " << Shell << ", Lenth: " << Lenth << endl; + return ss.str(); + } +}; + + +class Turtle : public Reptile +{ +private: + string Area; + +public: + Turtle(int paws, bool shell, string area) : Reptile(paws, shell) { Area = area; } + + string setArea(string area_) { Area = area_; } + void getArea() { cout << Area; } + + virtual string about() const + { + stringstream ss; + ss << "Paws: " << Paws << ", Shell: " << Shell << ", Area: " << Area << endl; + return ss.str(); + } + +}; + +inline ostream& operator<< (ostream& os, const Animal& animal) +{ + return os << animal.about(); +} diff --git a/vectors/array.cpp b/vectors/array.cpp deleted file mode 100644 index 555ccad..0000000 --- a/vectors/array.cpp +++ /dev/null @@ -1,28 +0,0 @@ -#include "array.h" - -using namespace std; - -// Написать шаблонные функции: -// 1. operator + (реализация внутри класса) -// 2. operator * (вектор на скаляр и скаляр на вектор; реализация вне класса) -// 3. Нахождение среднего для двух векторов (реализация вне класса) - -ostream& operator <<(ostream& os, const Vector3d& v) { - return os << "{ " << v[0] << ", " << v[1] << ", " << v[2] << " }"; -} - -template -ostream& operator <<(ostream& os, const Vector& v) { - os << "{ " << v[0]; - for (size_t i = 1; i < Dimensions; i++) - os << ", " << v[i]; - return os << " }"; -} - -int main() { - Vector<12> v1; - v1[1] = v1[2] = 54; - Vector<12> v2 = v1; - cout << v2 << endl; - return 0; -} diff --git a/vectors/array.h b/vectors/array.h deleted file mode 100644 index 60f50b2..0000000 --- a/vectors/array.h +++ /dev/null @@ -1,21 +0,0 @@ -#pragma once - -#include - -class Vector3d { - float data[3]; -public: - Vector3d() { data[2] = data[1] = data[0] = 0; } - Vector3d(float value) { data[2] = data[1] = data[0] = value; } - float operator[] (int index) const { std::cerr << "const" << index << std::endl; return data[index]; } - float& operator[] (int index) { std::cerr << "non-const" << index << std::endl; return data[index]; } -}; - -template -class Vector { - float data[Dimensions]; -public: - Vector(float value = 0) { for (size_t i = 0; i < Dimensions; i++) data[i] = value; } - float operator[] (size_t index) const { return data[index]; } - float& operator[] (size_t index) { return data[index]; } -}; diff --git a/vectors/vector.cpp b/vectors/vector.cpp index 9777069..8a7d2f5 100644 --- a/vectors/vector.cpp +++ b/vectors/vector.cpp @@ -1,25 +1,179 @@ +#pragma once + #include -#include "vector.h" +#include "vectors.h" using namespace std; -ostream& operator <<(ostream& os, const vector3d& v) { - return os << "{ " << v[0] << ", " << v[1] << ", " << v[2] << " }"; + + +template +ostream& operator <<(ostream& os, const Vector& v) { + os << "{ " << v[0]; + for (size_t i = 1; i < Dimensions; i++) + os << ", " << v[i]; + return os << " }"; } -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; + +template +bool test_vector(Vector(&Arr)[5], Vector v1, Vector v2, float n) +{ + bool Res = 0; + bool t = true; + + cout << v1 << endl; + cout << v2 << endl; + + + for (int i = 0; i < 5; i++) + { + switch (i) { + case 0: + for (size_t j = 0; j < Dimensions; j++) + { + if (Arr[i][j] != v1[j] + v2[j]) + { + t = false; + } + + } + + if (t) + { + cerr << "Сумма: верно" << endl; + } + else { + cerr << "Сумма: неверно" << endl; + Res = 1; + } + break; + + case 1: + for (size_t j = 0; j < Dimensions; j++) + { + if (Arr[i][j] != v1[j] - v2[j]) + { + t = false; + } + + } + + if (t) + { + cerr << "Рзаность: верно" << endl; + } + else { + cerr << "Разность: неверно" << endl; + Res = 1; + } + break; + + case 2: + for (size_t j = 0; j < Dimensions; j++) + { + if (Arr[i][j] != v1[j] * n) + { + t = false; + } + + } + + if (t) + { + cerr << "Умножение на скалаяр: верно" << endl; + } + else { + cerr << "Умножение на скалаяр: верно" << endl; + Res = 1; + } + break; + + case 3: + for (size_t j = 0; j < Dimensions; j++) + { + if (Arr[i][j] != v1[j] / n) + { + t = false; + } + + } + + if (t) + { + cerr << "Деление на скалаяр: верно" << endl; + } + else { + cerr << "Деление на скалаяр: неверно" << endl; + Res = 1; + } + break; + + case 4: + for (size_t j = 0; j < Dimensions; j++) + { + if (Arr[i][j] != -v1[j]) + { + t = false; + } + + } + + if (t) + { + cerr << "Инвертирование знака: верно" << endl; + } + else { + cerr << "Инвертирование знака: неверно" << endl; + Res = 1; + } + break; + + } + + } + return Res; } + + + + +int main() { + Vector<5> v1(6); + Vector<5> v2(2); + Vector<5> Arr[5]; + Vector<6> v3; + + cout << v1 + v2 << endl; + Arr[0] = v1 + v2; + + cout << v1 - v2 << endl; + Arr[1] = v1 - v2; + + cout << v1 * 3 << endl; + Arr[2] = v1 * 3; + + cout << 4 * v2 << endl; + + cout << v1 / 3 << endl; + Arr[3] = v1 / 3; + + cout << 8 / v2 << endl; + + cout << -v2 << endl; + Arr[4] = -v1; + + cout << !v3 << endl; + + cout << !v2 << endl; + + Vector<5> v4; + v4 = average_value(v1, v2); + cout << v4 << endl; + + bool res = test_vector(Arr, v1, v2, 3); + + cout << res; + + return 0; +} \ No newline at end of file diff --git a/vectors/vector.h b/vectors/vector.h index 07587fb..915b54a 100644 --- a/vectors/vector.h +++ b/vectors/vector.h @@ -1,19 +1,117 @@ #pragma once -#include +template +class Vector { + float data[Dimensions]; +public: + Vector() { for (size_t i = 0; i < Dimensions; i++) data[i] = 0; } + Vector(float value) { for (size_t i = 0; i < Dimensions; i++) data[i] = value; } + float operator[] (size_t index) const { return data[index]; } + float& operator[] (size_t index) { return data[index]; } -class vector3d { - float data[3]; + Vector operator+ (Vector v) + { + Vector Res; + for (size_t i = 0; i < Dimensions; i++) + { + Res[i] = data[i] + v.data[i]; + } -public: - vector3d() { data[2] = data[1] = data[0] = 0; } - 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; } + return Res; + } + + Vector operator- (Vector v) + { + Vector Res; + for (size_t i = 0; i < Dimensions; i++) + { + Res[i] = data[i] - v.data[i]; + } + + return Res; + } + + Vector operator* (float n) + { + Vector Res; + for (size_t i = 0; i < Dimensions; i++) + { + Res[i] = data[i] * n; + } + + return Res; + } + + friend Vector operator* (float n, Vector v) + { + Vector Res; + for (size_t i = 0; i < Dimensions; i++) + { + Res[i] = v.data[i] * n; + } + + return Res; + } + + Vector operator/ (float n) + { + Vector Res; + for (size_t i = 0; i < Dimensions; i++) + { + Res[i] = data[i] / n; + } + + return Res; + } + + friend Vector operator/ (float n, Vector v) + { + Vector Res; + for (size_t i = 0; i < Dimensions; i++) + { + Res[i] = v.data[i] / n; + } + + return Res; + } + + Vector operator- () + { + Vector Res; + for (size_t i = 0; i < Dimensions; i++) + { + Res[i] = -data[i]; + } + + return Res; + } + + Vector operator! () + { + for (size_t i = 0; i < Dimensions; i++) + { + if (data[i] != 0) + { + Vector Res; + return Res; + } + } + + Vector Res(1); + return Res; + } + + friend Vector average_value(Vector v1, Vector v2) + { + Vector Res; + + for (size_t i = 0; i < Dimensions; i++) + { + Res.data[i] = (v1.data[i] + v2.data[i]) / 2; + } - float& operator[](int idx) { return data[idx]; } - float operator[](int idx) const { return data[idx]; } + return Res; + } - friend int main(int argc, char** argv); -}; -std::ostream& operator <<(std::ostream& os, const vector3d& v); +}; \ No newline at end of file