From ed559c25b36de32f28b9a40d3c24e9396aa60fdb Mon Sep 17 00:00:00 2001 From: Angeld55 Date: Mon, 23 Sep 2019 02:26:03 +0300 Subject: Rename OperatorOverloading/Complex.hpp to OperatorOverloading/Complex/Complex.hpp --- OperatorOverloading/Complex.hpp | 136 -------------------------------- OperatorOverloading/Complex/Complex.hpp | 136 ++++++++++++++++++++++++++++++++ 2 files changed, 136 insertions(+), 136 deletions(-) delete mode 100644 OperatorOverloading/Complex.hpp create mode 100644 OperatorOverloading/Complex/Complex.hpp (limited to 'OperatorOverloading') diff --git a/OperatorOverloading/Complex.hpp b/OperatorOverloading/Complex.hpp deleted file mode 100644 index 8a6edd5..0000000 --- a/OperatorOverloading/Complex.hpp +++ /dev/null @@ -1,136 +0,0 @@ -#include -class Complex -{ -private: - double real; - double im; - -public: - Complex(); - Complex(double real, double im); - - Complex getConjugated() const; //връща комплексно спрегнатото число на текущия обект. - - //assignment operators - // a+=b Към а се добавя стойността на b. а се променя , b не се. Операцията ще върне a за да може да се правят верижни изрази (a+=b+=c) - Complex& operator+=(const Complex&); - Complex& operator-=(const Complex&); - Complex& operator*=(const Complex&); - Complex& operator/=(const Complex&); - - - - //stream opeartors ( std::cout>> c1) (std::cin<>(std::istream&, Complex&); - -}; -// a+b. Операторът връща НОВА променлива(Complex), която е резултатът от операцията. -Complex operator+(const Complex&, const Complex&); -Complex operator-(const Complex&, const Complex&); -Complex operator*(const Complex&, const Complex&); -Complex operator/(const Complex&, const Complex&); - - - -Complex::Complex() -{ - real = 0; - im = 0; -} -Complex::Complex(double real, double im) -{ - this->real = real; - this->im = im; -} -Complex& Complex::operator+=(const Complex& other) -{ - real += other.real; - im += other.im; - return *this; -} -Complex& Complex::operator-=(const Complex& other) -{ - real -= other.real; - im -= other.im; - return *this; -} -Complex& Complex::operator*=(const Complex& other) -{ - int oldReal = real; - real = real*other.real - im*other.im; - im = oldReal*other.im + im * other.real; - return *this; -} -Complex& Complex::operator/=(const Complex& other) // a/=b -{ - Complex Conjugated = other.getConjugated(); //взимаме комплексно спрегнатата на другата дроб (b) - - Complex otherCopy(other);// копираме другата (b), за да не я промяняме. - - //Умножаваме двете по комплексно спрегнатата. - *this *= Conjugated; - otherCopy *= Conjugated; //Тук трябва да остане само реална част. - - if (otherCopy.real != 0) - { - real /= otherCopy.real; - im /= otherCopy.real; - } - - return *this; -} - -//След дефинирането на +=, -=, *= и /=, можем лесно да дефинираме +, -, * и /, преизползвайки вече написаните опратори. - - -Complex operator+(const Complex& lhs, const Complex& rhs) { //a+b - - Complex result(lhs); // Създаваме нов обект, който е копие на a (използваме копи-конструктора) - result += rhs; //към копието на a, добавяме b (Използваме вече дефинирания оператор +=) - - return result; -} -//Операторите -, *, / са аналогични с +. -Complex operator-(const Complex& lhs, const Complex& rhs) { - Complex result(lhs); - result -= rhs; - - return result; -} - -Complex operator*(const Complex& lhs, const Complex& rhs) { - - Complex result(lhs); - result *= rhs; - - return result; -} - -Complex operator/(const Complex& lhs, const Complex& rhs) { - - Complex result(lhs); - result /= rhs; - - return result; -} - -Complex Complex::getConjugated() const -{ - Complex result(*this); - result.im *= -1; - - return result; -} - -// за да можем да правим cout<>c1 или да четем от файл и др. -std::istream& operator>>(std::istream& ifs, Complex& r) { - - return ifs >> r.real >> r.im; -} diff --git a/OperatorOverloading/Complex/Complex.hpp b/OperatorOverloading/Complex/Complex.hpp new file mode 100644 index 0000000..8a6edd5 --- /dev/null +++ b/OperatorOverloading/Complex/Complex.hpp @@ -0,0 +1,136 @@ +#include +class Complex +{ +private: + double real; + double im; + +public: + Complex(); + Complex(double real, double im); + + Complex getConjugated() const; //връща комплексно спрегнатото число на текущия обект. + + //assignment operators + // a+=b Към а се добавя стойността на b. а се променя , b не се. Операцията ще върне a за да може да се правят верижни изрази (a+=b+=c) + Complex& operator+=(const Complex&); + Complex& operator-=(const Complex&); + Complex& operator*=(const Complex&); + Complex& operator/=(const Complex&); + + + + //stream opeartors ( std::cout>> c1) (std::cin<>(std::istream&, Complex&); + +}; +// a+b. Операторът връща НОВА променлива(Complex), която е резултатът от операцията. +Complex operator+(const Complex&, const Complex&); +Complex operator-(const Complex&, const Complex&); +Complex operator*(const Complex&, const Complex&); +Complex operator/(const Complex&, const Complex&); + + + +Complex::Complex() +{ + real = 0; + im = 0; +} +Complex::Complex(double real, double im) +{ + this->real = real; + this->im = im; +} +Complex& Complex::operator+=(const Complex& other) +{ + real += other.real; + im += other.im; + return *this; +} +Complex& Complex::operator-=(const Complex& other) +{ + real -= other.real; + im -= other.im; + return *this; +} +Complex& Complex::operator*=(const Complex& other) +{ + int oldReal = real; + real = real*other.real - im*other.im; + im = oldReal*other.im + im * other.real; + return *this; +} +Complex& Complex::operator/=(const Complex& other) // a/=b +{ + Complex Conjugated = other.getConjugated(); //взимаме комплексно спрегнатата на другата дроб (b) + + Complex otherCopy(other);// копираме другата (b), за да не я промяняме. + + //Умножаваме двете по комплексно спрегнатата. + *this *= Conjugated; + otherCopy *= Conjugated; //Тук трябва да остане само реална част. + + if (otherCopy.real != 0) + { + real /= otherCopy.real; + im /= otherCopy.real; + } + + return *this; +} + +//След дефинирането на +=, -=, *= и /=, можем лесно да дефинираме +, -, * и /, преизползвайки вече написаните опратори. + + +Complex operator+(const Complex& lhs, const Complex& rhs) { //a+b + + Complex result(lhs); // Създаваме нов обект, който е копие на a (използваме копи-конструктора) + result += rhs; //към копието на a, добавяме b (Използваме вече дефинирания оператор +=) + + return result; +} +//Операторите -, *, / са аналогични с +. +Complex operator-(const Complex& lhs, const Complex& rhs) { + Complex result(lhs); + result -= rhs; + + return result; +} + +Complex operator*(const Complex& lhs, const Complex& rhs) { + + Complex result(lhs); + result *= rhs; + + return result; +} + +Complex operator/(const Complex& lhs, const Complex& rhs) { + + Complex result(lhs); + result /= rhs; + + return result; +} + +Complex Complex::getConjugated() const +{ + Complex result(*this); + result.im *= -1; + + return result; +} + +// за да можем да правим cout<>c1 или да четем от файл и др. +std::istream& operator>>(std::istream& ifs, Complex& r) { + + return ifs >> r.real >> r.im; +} -- cgit v1.3.1-23-gd82d