From 291d0f1bc83062987926938a9c46d7bb50eac0a5 Mon Sep 17 00:00:00 2001 From: Balhau Date: Sun, 2 Dec 2018 20:36:43 +0000 Subject: [PATCH] More on complex numbers --- CMakeLists.txt | 6 ++++++ complex.cpp | 20 ++++++++++++++++++++ src/core/bmath/complex.cpp | 28 +++++++++++++++++++++++++++- src/core/bmath/complex.hpp | 11 ++++++++++- 4 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 complex.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index b649d57..febd846 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,6 +17,12 @@ add_executable( sse.cpp ) +add_executable( + complex + src/core/bmath/complex.cpp + complex.cpp +) + add_executable(c_time time.cpp) install(TARGETS cpplab RUNTIME DESTINATION bin) diff --git a/complex.cpp b/complex.cpp new file mode 100644 index 0000000..771cdbc --- /dev/null +++ b/complex.cpp @@ -0,0 +1,20 @@ +#include +#include +#include +#include + +#include "src/core/bmath/complex.hpp" + +using namespace Core::Bmath; +using namespace std; + +int main(int argc, char** argcv){ + Complex c1(1,1); + Complex c2(2,2); + Complex c3=c1*c2; + Complex c4=c1+c2; + + cout << c1 << "*" << c2 << "=" << c3 << endl; + cout << c1 << "+" << c2 << "=" << c4 << endl; + +} \ No newline at end of file diff --git a/src/core/bmath/complex.cpp b/src/core/bmath/complex.cpp index 59ac869..d71cfef 100644 --- a/src/core/bmath/complex.cpp +++ b/src/core/bmath/complex.cpp @@ -1,6 +1,32 @@ #include "complex.hpp" +/** + * Constructor implementation + */ Core::Bmath::Complex::Complex(Long real,Long imaginary){ this->real=real; this->imaginary=imaginary; -}; \ No newline at end of file +}; + +/** + * Add complex number + */ +Core::Bmath::Complex& Core::Bmath::Complex::operator+= (const Complex& rightOp){ + this->real+=rightOp.real; + this->imaginary+=rightOp.imaginary; + return *this; +}; + +Core::Bmath::Complex Core::Bmath::Complex::operator+ (const Complex& rightOp){ + return Complex( + this->real+rightOp.real, + this->imaginary+rightOp.imaginary + ); +}; + +Core::Bmath::Complex Core::Bmath::Complex::operator* (const Complex& rigthOp){ + return Complex( + this->real*rigthOp.real-this->imaginary*rigthOp.imaginary, + this->imaginary*rigthOp.real+this->real*rigthOp.imaginary + ); +}; diff --git a/src/core/bmath/complex.hpp b/src/core/bmath/complex.hpp index 26faf61..06d3e58 100644 --- a/src/core/bmath/complex.hpp +++ b/src/core/bmath/complex.hpp @@ -2,6 +2,9 @@ #define CORE_BMATH_COMPLEX #include "../cpu/types.hpp" +#include + +using namespace std; namespace Core{ namespace Bmath{ @@ -13,7 +16,13 @@ namespace Core{ public: Complex(Long real,Long imaginary); Complex& operator+=(const Complex& rightOp); - friend Complex operator+(Complex leftOp,const Complex &rightOp); + Complex operator+(const Complex &rightOp); + Complex operator*(const Complex &rightOp); + + //Overload to enable toString operations + friend std::ostream& operator<<(std::ostream &stream, Core::Bmath::Complex const &c){ + return stream << "(" << c.real << ", " << c.imaginary << "i)"; + } }; }; };