More on complex numbers

This commit is contained in:
Balhau 2018-12-02 20:36:43 +00:00
parent c46007ac32
commit 291d0f1bc8
4 changed files with 63 additions and 2 deletions

View file

@ -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)

20
complex.cpp Normal file
View file

@ -0,0 +1,20 @@
#include <iostream>
#include <string>
#include <iomanip>
#include <sys/time.h>
#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;
}

View file

@ -1,6 +1,32 @@
#include "complex.hpp"
/**
* Constructor implementation
*/
Core::Bmath::Complex::Complex(Long real,Long imaginary){
this->real=real;
this->imaginary=imaginary;
};
};
/**
* 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
);
};

View file

@ -2,6 +2,9 @@
#define CORE_BMATH_COMPLEX
#include "../cpu/types.hpp"
#include <iostream>
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)";
}
};
};
};