complexnumber calculator
parent
cc55c5d713
commit
cd950e8689
@ -0,0 +1,31 @@
|
|||||||
|
#ifndef COMPLEX_CPP
|
||||||
|
#define COMPLEX_CPP
|
||||||
|
|
||||||
|
#include "ComplexNumber.h"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
Complex::Complex(double real, double imaginary) {
|
||||||
|
this->real = real;
|
||||||
|
this->imaginary = imaginary;
|
||||||
|
}
|
||||||
|
|
||||||
|
double Complex::getReal() const {
|
||||||
|
return real;
|
||||||
|
}
|
||||||
|
|
||||||
|
double Complex::getImaginary() const {
|
||||||
|
return imaginary;
|
||||||
|
}
|
||||||
|
|
||||||
|
Complex Complex::operator+(const Complex& other) const {
|
||||||
|
double resultReal = real + other.real;
|
||||||
|
double resultImaginary = imaginary + other.imaginary;
|
||||||
|
return Complex(resultReal, resultImaginary);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::ostream& operator<<(std::ostream& os, const Complex& complex) {
|
||||||
|
os << complex.getReal() << "+" << complex.getImaginary() << "i";
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
@ -0,0 +1,17 @@
|
|||||||
|
#ifndef COMPLEX_H
|
||||||
|
#define COMPLEX_H
|
||||||
|
|
||||||
|
class Complex
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
double real;
|
||||||
|
double imaginary;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Complex(double real, double imaginary);
|
||||||
|
double getReal() const;
|
||||||
|
double getImaginary() const;
|
||||||
|
Complex operator+(const Complex& other) const;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@ -0,0 +1,13 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include "ComplexNumber.cpp"
|
||||||
|
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
Complex C1(2.0,4.0);
|
||||||
|
Complex C2(1.0,3.0);
|
||||||
|
|
||||||
|
std::cout << C1 << "+" << C2 << "=" << C1 + C2;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue