Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Complex.h"
- #include <cmath>
- Complex::Complex()
- {
- re = 0;
- im = 0;
- }
- Complex::Complex(double re, double im)
- {
- this->re = re;
- this->im = im;
- }
- double Complex::getRe()
- {
- return this->re;
- }
- double Complex::getIm()
- {
- return this->im;
- }
- void Complex::setRe(double re)
- {
- this->re = re;
- }
- void Complex::setIm(double im)
- {
- this->im = im;
- }
- istream & operator>>(istream &f, Complex & c)
- {
- std::cout<<endl;
- std::cout<<"Introduceti partea reala"<<' ';
- f >> c.re;
- std::cout<<"Introduceti partea imaginara"<<' ';
- f >> c.im;
- return f;
- }
- ostream & operator<<(ostream & f, Complex c)
- {
- if(c.re==0)
- {
- if(c.im >= 0)
- {
- if(c.im==0)
- {
- cout<<'0';
- }
- if(c.im==1)
- {
- cout<<'i';
- }
- if(c.im!=0)
- {
- cout<<"i*"<<c.im;
- }
- }
- else cout<<"-i*"<<-c.im;
- }
- else
- {
- if(c.im >= 0)
- {
- if(c.im==0)
- {
- cout<<c.re;
- }
- if(c.im==1)
- {
- cout<<c.re<<"+i";
- }
- if(c.im > 0 && c.im !=0 && c.im != 1)
- {
- cout<<c.re<<"+i*"<<c.im;
- }
- }
- if(c.im < 0 && c.im != -1)
- cout<<c.re<<"-i*"<<-c.im;
- if(c.im == -1)
- {
- cout<<c.re<<"-i";
- }
- }
- return f;
- }
- Complex operator+(Complex a, Complex b)
- {
- return Complex(a.re + b.re, a.im + b.im);
- }
- Complex operator+(Complex a, double b)
- {
- return Complex(a.re + b, a.im);
- }
- Complex operator+(double a, Complex b)
- {
- return Complex(b.re + a, b.im);
- }
- Complex operator+(Complex a)
- {
- return a;
- }
- Complex operator-(Complex a, Complex b)
- {
- return Complex(a.re - b.re, a.im - b.im);
- }
- Complex operator-(Complex a, double b)
- {
- return Complex(a.re - b, a.im);
- }
- Complex operator-(double a, Complex b)
- {
- return Complex(b.re - a, b.im);
- }
- Complex operator-(Complex a)
- {
- return Complex(-a.re, -a.re);
- }
- Complex operator*(Complex a, Complex b)
- {
- return Complex(a.re*b.re - a.im*b.im, a.re*b.im + a.im*b.re);
- }
- Complex operator*(Complex a, double b)
- {
- return Complex(a.re*b, a.im*b);
- }
- Complex operator*(double a, Complex b)
- {
- return Complex(a*b.re, a*b.im);
- }
- Complex operator/(Complex a, Complex b)
- {
- if(b.im>0)
- {
- if( (a.re*b.re+a.im*b.im-a.im*b.re-a.re*b.im) != 0)
- {
- return Complex( (a.re*b.re+a.im*b.im)/(b.re*b.re+b.im*b.im), (a.im*b.re+a.re*b.im)/(b.re*b.re+b.im*b.im) );
- }
- else return Complex(0,0);
- }
- else
- {
- return Complex( (a.re*b.re+a.im*b.im)/(b.re*b.re+b.im*b.im), (a.im*b.re-a.re*b.im)/(b.im*b.re-b.im*b.re) );
- }
- }
- Complex operator/(Complex a, double b)
- {
- if(a.im!=0 )
- return Complex( a.re/b, a.im/b);
- }
- Complex operator/(double a, Complex b)
- {
- if(b.im>0)
- {
- return Complex( a*b.re/(b.re*b.re-b.im*b.im), a*b.im/(b.re*b.re-b.im*b.im) );
- }
- else
- {
- return Complex( a*b.re/(b.re*b.re+b.im*b.im), a*b.im/(b.re*b.re+b.im*b.im));
- }
- }
- bool operator==(Complex a, Complex b)
- {
- if(a.re==b.re && a.im==b.im)
- return true;
- else return false;
- }
- bool operator!=(Complex a, Complex b)
- {
- if(a.re!=b.re && a.im!=b.im)
- return true;
- else return false;
- }
- Complex & operator+=(Complex &a, Complex b)
- {
- a=Complex(a.re+b.re, a.im+b.im);
- return a;
- }
- Complex & operator+=(Complex &a, double b)
- {
- a=Complex(a.re+b, a.im);
- return a;
- }
- Complex & operator-=(Complex &a, Complex b)
- {
- a=Complex(a.re-b.re, a.im-b.im);
- return a;
- }
- Complex & operator-=(Complex &a, double b)
- {
- a=Complex(a.re-b, a.im);
- return a;
- }
- Complex & operator*=(Complex &a, Complex b)
- {
- a=Complex(a.re*b.re, a.im*b.im);
- return a;
- }
- Complex & operator*=(Complex &a, double b)
- {
- a=Complex(a.re*b, a.im*b);
- return a;
- }
- Complex & operator/=(Complex &a, Complex b)
- {
- if(b.re==0 && b.im==0)
- {
- a=Complex(0,0);
- }
- else
- a=Complex(a.re/b.re, a.im/b.im);
- return a;
- }
- Complex & operator/=(Complex &a, double b)
- {
- if(b==0)
- a=Complex(0,0);
- else
- a=Complex(a.re/b, a.im);
- return a;
- }
- Complex operator^(Complex a, int b)
- {
- Complex x=a;
- if(b==0)
- {
- return Complex(1,0);
- }
- else
- for ( int i=2; i<=b; i++)
- x=x*a;
- return x;
- }
- double absc(Complex c)
- {
- if(c.re!=0 && c.im!=0)
- return sqrt(c.re*c.re + c.im*c.im);
- else
- if(c.re == 0)
- return(c.im);
- else
- if(c.im == 0)
- return(c.re);
- }
- Complex sqrtc(Complex c)
- {
- if(c.im!= 0 && c.re!= 0)
- {
- if( (c.re+absc(c)) > 0 )
- {
- if( (-c.re + absc(c)) > 0 )
- {
- return Complex(sqrt(((c.re+absc(c))/2)),
- (abs(c.im)/c.im ) * sqrt( ( (-c.re + absc(c)) / 2) ) );
- }
- if( (-c.re + absc(c)) < 0 )
- {
- return Complex(sqrt(((c.re+absc(c))/2)),
- abs(c.im)/c.im * sqrt(-((-c.re + absc(c)) / 2)));
- }
- }
- else if( ((c.re+absc(c))/2) < 0 )
- {
- if( ((-c.re + absc(c)) / 2) > 0 )
- {
- return Complex(sqrt(((c.re+absc(c))/2)),
- abs(c.im)/c.im * sqrt( -((-c.re + absc(c)) / 2) ));
- }
- if( ((-c.re + absc(c)) / 2) < 0 )
- {
- return Complex(sqrt(-((c.re+absc(c))/2)),
- abs(c.im)/c.im * sqrt( -((-c.re + absc(c)) / 2) ));
- }
- }
- }
- else
- if(c.re==0)
- {
- if(c.im > 0)
- return Complex( 0, sqrt(c.im) );
- else if(c.im < 0)
- return Complex(0,sqrt(-c.im));
- }
- else
- if(c.im==0)
- {
- if(c.re > 0)
- return Complex(sqrt(c.re),0);
- else if (c.re < 0)
- return Complex(sqrt(c.re),0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement