Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef COMPLEX_HPP_
- #define COMPLEX_HPP_
- #include <iostream>
- #include <cstdlib>
- #include <cmath>
- using namespace std;
- class Complex
- {
- private:
- double real;
- double imag;
- public:
- Complex(double=0,double=0);
- double getReal() const
- {
- return real;
- }
- double getImag() const
- {
- return imag;
- } //zwraca cz. urojon¹
- double getModule() const
- {
- double R,I;
- R=real;
- I=imag;
- return sqrt(R*R+I*I);
- }
- double getPhase() const
- {
- double R, I, z, cos;
- R=real;
- I=imag;
- z=sqrt(R*R+I*I);
- cos=R/z;
- if(I>0)
- return (acos(cos)*180)/M_PI; //JESLI RADIANY, TO SAM ARCSIN
- else
- return (((acos(cos)*180)/M_PI)-360)*(-1);
- } //zwraca fazê
- friend ostream& operator << (ostream& out, const Complex& C)
- {
- if(C.real!=0)
- out<<C.real;
- else
- out<<"";
- if(C.imag==0 && C.real!=0)
- out<<"";
- else if(C.imag==0 && C.real==0)
- out<<0;
- else if(C.imag==1 && C.real!=0)
- out<<" + i";
- else if(C.imag==1 && C.real==0)
- out<<" i";
- else if(C.imag==-1)
- out<<" - i";
- else if(C.imag>0 && C.imag!=1 && C.real!=0)
- out<<" + "<<C.imag<<"i";
- else if(C.imag>0 && C.imag!=1 && C.real==0)
- out<<C.imag<<"i";
- else if(C.imag<0 && C.imag!=-1)
- out<<" - "<<(-1)*C.imag<<"i";
- out<<endl;
- return out;
- }
- bool operator == (const Complex& C) const
- {
- if(this->real==C.real && this->imag==C.imag)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- friend Complex operator + (const Complex& C1, const Complex& C2)
- {
- Complex tmp;
- tmp.real=C1.real+C2.real;
- tmp.imag=C1.imag+C2.imag;
- return tmp;
- }
- Complex& operator += (const Complex& C)
- {
- *this=*this+C;
- return *this;
- }
- friend Complex operator - (const Complex& C1, const Complex& C2)
- {
- Complex tmp;
- tmp.real=C1.real-C2.real;
- tmp.imag=C1.imag-C2.imag;
- return tmp;
- }
- Complex& operator -= (const Complex& C)
- {
- *this=*this-C;
- return *this;
- }
- friend Complex operator * (const Complex& C1, const Complex& C2)
- {
- Complex tmp;
- tmp.real=C1.real*C2.real-C1.imag*C2.imag;
- tmp.imag=C1.real*C2.imag+C1.imag*C2.real;
- return tmp;
- }
- Complex& operator *= (const Complex& C)
- {
- *this=*this*C;
- return *this;
- }
- friend Complex operator / (const Complex& C1, const Complex& C2)
- {
- Complex tmp, dzielna;
- double dzielnik;
- dzielna.real=C1.real*C2.real+C1.imag*C2.imag;
- dzielna.imag=C1.real*(-1)*C2.imag+C1.imag*C2.real;
- dzielnik=C2.real*C2.real+C2.imag*C2.imag;
- tmp.real=dzielna.real/dzielnik;
- tmp.imag=dzielna.imag/dzielnik;
- return tmp;
- }
- Complex& operator /= (const Complex& C)
- {
- *this=*this/C;
- return *this;
- }
- };
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement