From 02b2fd6f94873b342dfb8df3a2b555d95eb0d7b8 Mon Sep 17 00:00:00 2001 From: Angeld55 Date: Mon, 23 Sep 2019 02:25:02 +0300 Subject: Add files via upload --- OperatorOverloading/Vector/Source.cpp | 14 ++++ OperatorOverloading/Vector/Vector.cpp | 145 ++++++++++++++++++++++++++++++++++ OperatorOverloading/Vector/Vector.h | 38 ++++++++- 3 files changed, 196 insertions(+), 1 deletion(-) create mode 100644 OperatorOverloading/Vector/Source.cpp create mode 100644 OperatorOverloading/Vector/Vector.cpp (limited to 'OperatorOverloading') diff --git a/OperatorOverloading/Vector/Source.cpp b/OperatorOverloading/Vector/Source.cpp new file mode 100644 index 0000000..02b3dd5 --- /dev/null +++ b/OperatorOverloading/Vector/Source.cpp @@ -0,0 +1,14 @@ +#include +#include "Vector.h" +int main() +{ + + + Vector v1(2); + + std::cin >> v1; + + std::cout << (v1); + + +} diff --git a/OperatorOverloading/Vector/Vector.cpp b/OperatorOverloading/Vector/Vector.cpp new file mode 100644 index 0000000..c0e8350 --- /dev/null +++ b/OperatorOverloading/Vector/Vector.cpp @@ -0,0 +1,145 @@ +#include "Vector.h" + +Vector::Vector() :coordinates(nullptr), length(0) +{} +Vector::Vector(int len) +{ + coordinates = new int[len]; + for (int i = 0; i < len; i++) + coordinates[i] = 0; + length = len; +} +void Vector::CopyFrom(const Vector& other) +{ + coordinates = new int[other.length]; + for (int i = 0; i < other.GetLen(); i++) + coordinates[i] = other.coordinates[i]; + length = other.length; +} +void Vector::Free() +{ + delete[] coordinates; + length = 0; +} +Vector::Vector(const Vector& other) +{ + CopyFrom(other); +} +Vector& Vector::operator=(const Vector& other) +{ + if (this != &other) + { + Free(); + CopyFrom(other); + } + return *this; +} +Vector::~Vector() +{ + Free(); +} + +int& Vector::operator[](int index) +{ + if (index < length) + return coordinates[index]; + else + throw 0; +} +void Vector::print() +{ + std::cout << '('; + for (int i = 0; i < length; i++) + std::cout << coordinates[i]<<" "; + std::cout <<')' < a*K=b (K е константа). +{ + double coeff = lhs.GetAtIndex(0) / rhs.GetAtIndex(0); + for (int i = 1; i < lhs.GetLen(); i++) + { + if (lhs.GetAtIndex(i) == 0 || rhs.GetAtIndex(i) == 0) + continue; + double Newfirst = lhs.GetAtIndex(i); + double Newcoeff = Newfirst / rhs.GetAtIndex(i); + if (abs(Newcoeff-coeff)>0.01) + return false; + } + return true; +} +bool operator|=(const Vector& lhs, const Vector& rhs) +{ + return lhs%rhs == 0; //скаларното произведение на два успоредни вектора е 0. +} + +std::ostream& operator<<(std::ostream& os, const Vector& v) +{ + for (int i = 0; i < v.GetLen(); ++i) + os << v.GetAtIndex(i) << " "; + return os; +} + +std::istream& operator>>(std::istream& ifs, Vector& v) +{ + for (int i = 0; i < v.GetLen(); ++i) + ifs >> v[i]; + return ifs; +} diff --git a/OperatorOverloading/Vector/Vector.h b/OperatorOverloading/Vector/Vector.h index b478595..067b03e 100644 --- a/OperatorOverloading/Vector/Vector.h +++ b/OperatorOverloading/Vector/Vector.h @@ -1 +1,37 @@ -s +#include +class Vector +{ +private: + int* coordinates; + int length; + void CopyFrom(const Vector& other); + void Free(); +public: + Vector(); + Vector(int len); + + Vector(const Vector& other); + Vector& operator=(const Vector& other); + ~Vector(); + + int GetLen() const; + + Vector& operator+=(const Vector& other); + Vector& operator-=(const Vector& other); + Vector& operator*=(int); + + friend std::ostream& operator<<(std::ostream&, const Vector& v); + friend std::istream& operator>>(std::istream&, Vector& v); + + int GetAtIndex(int index) const; + int& operator[](int index); // оператор за индексиране(като в масивите) vector a; a[4] -взима четвъртия кординат на вектора + void print(); +}; + +Vector operator+(const Vector& lhs, const Vector& rhs);// Събиране на два вектора +Vector operator-(const Vector& lhs, const Vector& rhs);// Събиране на два вектора + +int operator%(const Vector& lhs, const Vector& rhs); // Скаларно произведение на два вектора + +bool operator||(const Vector& lhs, const Vector& rhs); //проверява дали два вектора са успоредни +bool operator|=(const Vector& lhs, const Vector& rhs); // Дали два вектора са перпендикулярни \ No newline at end of file -- cgit v1.3.1-23-gd82d