Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "komplex.hpp"
- Complex::Complex()
- {
- imag=0;
- real=0;
- }
- Complex::Complex(double r, double i)
- {
- imag=i;
- real=r;
- }
- Complex::Complex(const Complex &Z)
- {
- imag=Z.imag;
- real=Z.real;
- }
- double Complex::getReal()const
- {
- return real;
- }
- double Complex::getImag()const
- {
- return imag;
- }
- void Complex::setReal(double Real)
- {
- Complex::real=Real;
- }
- void Complex::setImag(double Imag)
- {
- Complex::imag=Imag;
- }
- Complex& Complex::operator=(const Complex &c1)
- {
- real=c1.real;
- imag=c1.imag;
- return *this;
- }
- Complex& Complex::operator+=(const Complex &c1)
- {
- real=real + c1.real;
- imag=imag + c1.imag;
- return *this;// vraca referencu na objekat na levu stranu znaka jednakosti
- }
- Complex& Complex::operator-=(const Complex &c1)
- {
- real=real - c1.real;
- imag=imag - c1.imag;
- return *this;
- }
- Complex& Complex::operator*=(const Complex &c1)
- {
- real=real * c1.real - c1.real * imag;
- imag=real * c1.imag + imag * c1.real;
- return *this;
- }
- Complex& Complex::operator/=(const Complex &c1)
- {
- real=(real * c1.real + c1.imag * imag)/(c1.real*c1.real + c1.imag*c1.imag);
- imag=(imag * c1.real - c1.imag * real)/(c1.real*c1.real + c1.imag*c1.imag);
- return *this;
- }
- const Complex& Complex::operator++() //prefiksni operator odmah uvecava i vraca vrednost
- {
- real++;
- imag++;
- return *this;//vraca objekat
- }
- const Complex Complex::operator++(int i) //postfiksna vraca lokalnu kopiju w, ne vraca uvecani objekat
- {
- Complex w(real, imag);
- real++;
- imag++;
- return w;
- }
- Complex operator+(const Complex &z1, const Complex &z2)
- {
- Complex w(z1.real+z2.real, z1.imag+z2.imag);
- return w;
- }
- Complex operator-(const Complex &z1, const Complex &z2)
- {
- Complex w(z1.real-z2.real, z1.imag-z2.imag);
- return w;
- }
- Complex operator*(const Complex &z1, const Complex &z2)
- {
- Complex w(z1.real*z2.real-z1.imag*z2.real, z1.real*z2.imag+z1.imag*z2.real);
- return w;
- }
- Complex operator/(const Complex &z1, const Complex &z2)
- {
- Complex w((z1.real*z2.real+z1.imag*z2.imag)/(z2.real*z2.real+z2.imag*z2.imag), (z1.imag*z2.real-z1.real*z2.imag)/(z2.real*z2.real+z2.imag*z2.imag));
- return w;
- }
- bool operator== (const Complex &z1, const Complex &z2)
- {
- return (z1.real==z2.real)&&(z1.imag==z2.imag);
- }
- bool operator!= (const Complex &z1, const Complex &z2)
- {
- bool p=(z1.real==z2.real)&&(z1.imag==z2.imag);
- return !p;
- }
- ostream& operator<<(ostream &out, const Complex &z1)
- {
- if(z1.imag==0 && z1.real!=0)
- {
- out<<z1.real;
- }
- if(z1.real==0 && z1.imag!=0)
- {
- out<<z1.imag<<"i";
- }
- if(z1.real!=0 && z1.imag>0)
- {
- out<<z1.real<<"+"<<z1.imag;
- }
- if(z1.real!=0 && z1.imag<0)
- {
- out<<z1.real<<z1.imag;
- }
- return out;
- }
- istream& operator>>(istream &in, Complex &z)
- {
- in>>z.real;
- in>>z.imag;
- return in;
- }
Advertisement
Add Comment
Please, Sign In to add comment