-
Notifications
You must be signed in to change notification settings - Fork 0
/
complexnumber.cpp
43 lines (35 loc) · 1.5 KB
/
complexnumber.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include "complexnumber.h"
#include <math.h>
ComplexNumber::ComplexNumber(double real_, double imaginary_) :
m_real{real_}, m_imaginary{imaginary_} { }
double ComplexNumber::real() const {
return m_real;
}
double ComplexNumber::imaginary() const {
return m_imaginary;
}
double ComplexNumber::abs() const {
return std::sqrt(real() * real() + imaginary() * imaginary());
}
void ComplexNumber::evaluate(Stack<ComplexNumber>& operands_) const {
operands_.push(*this);
}
ComplexNumber ComplexNumber::operator+(const ComplexNumber& other_) const {
return ComplexNumber(real() + other_.real(), imaginary() + other_.imaginary());
}
ComplexNumber ComplexNumber::operator-(const ComplexNumber& other_) const {
return ComplexNumber(real() - other_.real(), imaginary() - other_.imaginary());
}
ComplexNumber ComplexNumber::operator*(const ComplexNumber& other_) const {
return ComplexNumber(real() * other_.real() - imaginary() * other_.imaginary(), real() * other_.imaginary() + imaginary() * other_.real());
}
ComplexNumber ComplexNumber::operator/(const ComplexNumber& other_) const {
auto tmp = ComplexNumber(other_.real(), -other_.imaginary());
auto tmp2 = *this * tmp;
tmp = tmp * other_;
return ComplexNumber(tmp2.real() / tmp.real(), tmp2.imaginary() / tmp.real());
}
std::ostream& operator<<(std::ostream& stream_, const ComplexNumber& complexNumber_) {
stream_ << "{" << complexNumber_.real() << "; " << complexNumber_.imaginary() << "}" << std::endl;
return stream_;
}