Skip to content

Instantly share code, notes, and snippets.

@henrycunh
Created October 6, 2018 16:16
Show Gist options
  • Save henrycunh/f017115b40d7950ebabc6f98f55e6a39 to your computer and use it in GitHub Desktop.
Save henrycunh/f017115b40d7950ebabc6f98f55e6a39 to your computer and use it in GitHub Desktop.
complex numbers cpp implementation
#include <iostream>
#include <string>
class Complex {
private:
double a, b;
public:
Complex(){
a = b = 0;
}
Complex(double a){
this->a = a;
this->b = 0;
}
Complex(double a, double b){
this->a = a;
this->b = b;
}
/*
Getters
*/
double real() const { return a; }
double imag() const { return b; }
/*
Setters
*/
void real(double val) { this->a = val; }
void imag(double val) { this->b = val; }
/*
Operators Overloading
*/
/* Assignment Operator Overloading */
void operator=(const double a){
this->a = a;
this->b = 0;
}
void operator=(const Complex& other){
this->a = other.a;
this->b = other.b;
}
Complex(const char * m_str){
std::string str = m_str;
std::size_t space_pos = str.find(" ");
std::size_t i_pos = str.find("i");
if(space_pos != std::string::npos && i_pos != std::string::npos){
double real = std::stod( str.substr(0, space_pos) );
double imag = std::stod( str.substr(space_pos + 2, i_pos));
this->real( real );
this->imag( imag );
}
}
/* Sum Operator */
Complex operator+(const Complex& other){
return Complex( this->real() + other.real(), this->imag() + other.imag() );
}
Complex operator+(const double val){
return Complex( this->real() + val, this->imag() );
}
/* Subtraction Operator */
Complex operator-(const Complex& other){
return Complex( this->real() - other.real(), this->imag() - other.imag() );
}
Complex operator-(const double val){
return Complex( this->real() - val, this->imag() );
}
/* Multiplication Operator */
Complex operator*(const Complex& other){
return Complex(
// Real counterpart
(this->real() * other.real()) - (this->imag() * other.imag())
,
// Imaginary counterpart
(this->real() * other.imag()) + (this->imag() * other.real())
);
}
Complex operator*(const double val){
return Complex( this->real() * val, this->imag() );
}
/* Increment and Decrement Operator */
Complex operator++(int){
Complex temp(this->a, this->b);
temp.real(this->a++);
return temp;
}
Complex operator++(){
Complex temp(this->a, this->b);
temp.real(++this->a);
return temp;
}
Complex operator--(int){
Complex temp(this->a, this->b);
temp.real(this->a--);
return temp;
}
Complex operator--(){
Complex temp(this->a, this->b);
temp.real(--this->a);
return temp;
}
};
/*
STREAMS OPERATOR OVERLOADING
*/
std::ostream& operator<<(std::ostream& os, const Complex& num){
os << num.real() << " + " << num.imag() << "i";
return os;
}
std::istream& operator>>(std::istream& is, Complex& num){
double a, b;
is >> a >> b;
num.real(a);
num.imag(b);
return is;
}
int main(){
// Constructors
Complex a;
Complex b(1, 2);
Complex c(20);
Complex d = "3 + 4i";
std::cout
<< "Construtores" << std::endl
<< "a: " << a << std::endl
<< "b: " << b << std::endl
<< "c: " << c << std::endl
<< "d: " << d << std::endl << std::endl;
// Operations
std::cout
<< "Operacoes" << std::endl
<< "a + 4: " << a + 4 << std::endl
<< "b - 6: " << b - 6 << std::endl
<< "c * 3: " << c * 3 << std::endl
<< "d + a: " << d + a << std::endl
<< "c - b: " << c - b << std::endl
<< "b * d: " << b * d << std::endl;
// Streams
Complex temp;
std::cout << "Insira dois reais: ";
std::cin >> temp;
std::cout << "Numero Complexo: " << temp << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment