From 5486e82f4bb3137c5a8e13c85a9171ac53b0f44b Mon Sep 17 00:00:00 2001 From: Angel Dimitriev Date: Fri, 16 Apr 2021 15:51:04 +0300 Subject: Update Complex.cpp --- Sem. 08/Complex/Complex.cpp | 102 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) (limited to 'Sem. 08/Complex') diff --git a/Sem. 08/Complex/Complex.cpp b/Sem. 08/Complex/Complex.cpp index e69de29..c604d0a 100644 --- a/Sem. 08/Complex/Complex.cpp +++ b/Sem. 08/Complex/Complex.cpp @@ -0,0 +1,102 @@ +#include "Complex.h" + +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 From 54e11f32b9f29795823c1a8c8007800daa3f2514 Mon Sep 17 00:00:00 2001 From: Angel Dimitriev Date: Fri, 16 Apr 2021 15:51:15 +0300 Subject: Update Complex.h --- Sem. 08/Complex/Complex.h | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'Sem. 08/Complex') diff --git a/Sem. 08/Complex/Complex.h b/Sem. 08/Complex/Complex.h index e69de29..d76a2f3 100644 --- a/Sem. 08/Complex/Complex.h +++ b/Sem. 08/Complex/Complex.h @@ -0,0 +1,34 @@ +#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&); + + -- cgit v1.3.1-23-gd82d From 9aebeb0658de877e80ec73fd09d56f4b773b575b Mon Sep 17 00:00:00 2001 From: Angel Dimitriev Date: Fri, 16 Apr 2021 15:51:34 +0300 Subject: Update Source.cpp --- Sem. 08/Complex/Source.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'Sem. 08/Complex') diff --git a/Sem. 08/Complex/Source.cpp b/Sem. 08/Complex/Source.cpp index e69de29..32f9e17 100644 --- a/Sem. 08/Complex/Source.cpp +++ b/Sem. 08/Complex/Source.cpp @@ -0,0 +1,9 @@ +#include "Complex.h" + +int main() +{ + Complex c(3, 4); + Complex c2(3, 4); + + std::cout << c* c2; +} -- cgit v1.3.1-23-gd82d