More on complex numbers
This commit is contained in:
parent
c46007ac32
commit
291d0f1bc8
4 changed files with 63 additions and 2 deletions
|
@ -17,6 +17,12 @@ add_executable(
|
||||||
sse.cpp
|
sse.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
|
add_executable(
|
||||||
|
complex
|
||||||
|
src/core/bmath/complex.cpp
|
||||||
|
complex.cpp
|
||||||
|
)
|
||||||
|
|
||||||
add_executable(c_time time.cpp)
|
add_executable(c_time time.cpp)
|
||||||
|
|
||||||
install(TARGETS cpplab RUNTIME DESTINATION bin)
|
install(TARGETS cpplab RUNTIME DESTINATION bin)
|
||||||
|
|
20
complex.cpp
Normal file
20
complex.cpp
Normal 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;
|
||||||
|
|
||||||
|
}
|
|
@ -1,6 +1,32 @@
|
||||||
#include "complex.hpp"
|
#include "complex.hpp"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor implementation
|
||||||
|
*/
|
||||||
Core::Bmath::Complex::Complex(Long real,Long imaginary){
|
Core::Bmath::Complex::Complex(Long real,Long imaginary){
|
||||||
this->real=real;
|
this->real=real;
|
||||||
this->imaginary=imaginary;
|
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
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
|
@ -2,6 +2,9 @@
|
||||||
#define CORE_BMATH_COMPLEX
|
#define CORE_BMATH_COMPLEX
|
||||||
|
|
||||||
#include "../cpu/types.hpp"
|
#include "../cpu/types.hpp"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
namespace Core{
|
namespace Core{
|
||||||
namespace Bmath{
|
namespace Bmath{
|
||||||
|
@ -13,7 +16,13 @@ namespace Core{
|
||||||
public:
|
public:
|
||||||
Complex(Long real,Long imaginary);
|
Complex(Long real,Long imaginary);
|
||||||
Complex& operator+=(const Complex& rightOp);
|
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)";
|
||||||
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue