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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
#include "complex.h"
#include <iostream>
#include <math.h>
using namespace std;
const Complex i(0,1);
Complex::Complex ()
{
set(0,0);
}
Complex::Complex ( double realPart )
{
set(realPart,0);
}
Complex::Complex ( double real, double imaginary )
{
set(real,imaginary);
}
double Complex::getr()
{
return real;
}
double Complex::geti()
{
return imaginary;
}
void Complex::set(double real, double imaginary)
{
this->real = real;
this->imaginary = imaginary;
}
Complex Complex::operator -() const
{
return Complex(real,-imaginary);
}
Complex Complex::operator -(const Complex& comp) const
{
double subReal,subImaginary;
subReal = real-comp.real;
subImaginary = imaginary-comp.imaginary;
return Complex(subReal,subImaginary);
}
Complex Complex::operator +(const Complex& comp) const
{
double sumReal,sumImaginary;
sumReal = real+comp.real;
sumImaginary = imaginary+comp.imaginary;
return Complex(sumReal,sumImaginary);
}
Complex Complex::operator *(const Complex& comp) const
{
double prodReal,prodimaginary;
prodReal = real*comp.real - imaginary*comp.imaginary;
prodimaginary = real*comp.imaginary + imaginary*comp.real;
return Complex(prodReal,prodimaginary);
}
Complex Complex::operator /(const Complex& comp) const
{
double quotReal,quotimaginary;
Complex numer;
Complex denom;
numer = *this*(-comp);
denom = comp*(-comp);
quotReal = numer.real/denom.real;
quotimaginary = numer.imaginary/denom.real;
return Complex(quotReal,quotimaginary);
}
bool Complex::operator ==(const Complex& comp)
{
return (real == comp.real)&&(imaginary == comp.imaginary);
}
ostream& operator<< (ostream& lhs, Complex& rhs)
{
lhs << rhs.getr() << "+" << rhs.geti() << "i";
return lhs;
}
istream& operator>> (istream& lhs, Complex& rhs)
{
lhs >> rhs.real >> rhs.imaginary;
return lhs;
}
Complex Complex::sqrt ( Complex comp, int method, int iteration) const
{
double srReal,srImaginary;
if(method == 0)
{
srReal = pow( pow( pow(comp.real,2)+pow(comp.imaginary,2), 0.5 ) + comp.real, 0.5 )/pow(2.0, 0.5);
srImaginary = pow( pow( pow(comp.real,2)+pow(comp.imaginary,2), 0.5 ) - comp.real, 0.5 )/pow(2.0, 0.5);
if(comp.imaginary < 0)
srImaginary *= -1;
}
return Complex(srReal,srImaginary);
}
Complex Complex::abs ( Complex c)
{
if(c.real < 0)
c.real = -c.real;
if(c.imaginary < 0)
c.imaginary = -c.imaginary;
return Complex(c.real,c.imaginary);
}
|