Sunteți pe pagina 1din 6

#include "complex.

h" #include <iostream> using namespace std; void rezolvare_gradul2_cu_coeficienti_complecsi(); void repetare(); complex :: complex(double x, double y) { real=x; imaginar=y; } complex :: complex(const complex &x) { real=x.real; imaginar=x.imaginar; } void complex :: schimbare_real(double x) { real=x; } void complex :: schimbare_imaginar(double x) { imaginar=x; } double complex :: get_real() { return real; } double complex :: get_imaginar() { return imaginar; } void complex :: citire(char x) { cout<<endl<<"Va rugam introduceti coeficientul "<<x<<" : "<<endl<<"partea re ala: ";cin>>real;cout<<"partea imaginara: ";cin>>imaginar;cout<<endl<<endl; } void complex :: afisare(int x) { cout<<endl<<"x"<<x<<"="; if(imaginar==0) cout<<real; else if(real==0) if(imaginar>0) if(imaginar!=1) cout<<"i*"<<imaginar; else cout<<"i"; else if(imaginar!=-1)

cout<<"-i*"<<-imaginar; else cout<<"-i"; else if(imaginar>0) if(imaginar!=1) cout<<real<<"+i*"<<imaginar; else cout<<real<<"+i"; else if(imaginar!=-1) cout<<real<<"-i*"<<-imaginar; else cout<<real<<"-i"; cout<<endl; } //ADD complex operator +(complex x) { return x; } complex operator +(complex x,complex y) { return complex(x.real+y.real,x.imaginar+y.imaginar); } complex operator +(double x,complex y) { return complex(x+y.real,y.imaginar); } complex operator +(complex x,double y) { return complex(x.real+y,x.imaginar); } //SUBSTRACT complex operator -(complex x) { return complex(-x.real,-x.imaginar); } complex operator -(complex x,complex y) { return complex(x.real-y.real,x.imaginar-y.imaginar); } complex operator -(double x,complex y) { return complex(x-y.real,-y.imaginar); } complex operator -(complex x,double y) { return complex(x.real-y,-x.imaginar); } //MULTIPLY complex operator *(complex x,complex y)

{ return complex(x.real*y.real-x.imaginar*y.imaginar,x.real*y.imaginar+x.imagi nar*y.real); } complex operator *(double x,complex y) { return complex(x*y.real,x*y.imaginar); } complex operator *(complex x,double y) { return complex(y*x.real,y*x.imaginar); } //DIVIDE complex operator /(complex x,complex y) { return complex((x.real*y.real-x.imaginar*y.imaginar)/(y.real*y.real-y.imagin ar*y.imaginar),(x.real*y.imaginar+x.imaginar*y.real)/(y.real*y.real-y.imaginar*y .imaginar)); } complex operator /(double x,complex y) { return complex(y.real*x/(y.real*y.real-y.imaginar*y.imaginar),(-x)*y.imagina r); } complex operator /(complex x,double y) { return complex(x.real/y,x.imaginar/y); } //EQUAL bool operator ==(complex x,complex y) { if(x.real==y.real && x.imaginar==y.imaginar) return true; else return false; } bool operator ==(complex x,double y) { if(x.imaginar==0 && x.real==y) return true; else return false; } bool operator ==(double x,complex y) { if(y.imaginar==0 && x==y.real) return true; else return false; } //NOT EQUAL bool operator !=(complex x,complex y) { if(x.real==y.real && x.imaginar==y.imaginar)

return false; else return true; } bool operator !=(complex x,double y) { if(x.imaginar==0 && x.real==y) return false; else return true; } bool operator !=(double x,complex y) { if(y.imaginar==0 && x==y.real) return false; else return true; } //WITH SELF void operator +=(complex& x,complex y) { x=x+y; } void operator +=(complex& x,double y) { x=x+y; } void operator -=(complex& x,complex y) { x=x-y; } void operator -=(complex& x,double y) { x=x-y; } void operator *=(complex& x,complex y) { x=x*y; } void operator *=(complex& x,double y) { x=x*y; } void operator /=(complex& x,complex y) { x=x/y; }

void operator /=(complex& x,double y) { x=x/y; } complex operator ^(complex x,int y) { while(y>1) {x*=x; y--; } return x; } //OTHER double abs(complex x) { return sqrt(x.real*x.real-x.imaginar*x.imaginar); } complex sqrt(complex x) { double m1,m2,D2,n,m; D2=16*x.get_real()*x.get_real()+16*x.get_imaginar()*x.get_imaginar(); if(D2<0) {cout<<endl<<endl<<"Nu exista rezolvare logic valida!"<<endl<<endl;repet are();} else { m1=(-4*x.get_real()+sqrt(D2))/8; m2=(-4*x.get_real()-sqrt(D2))/8; if(m1<0) {cout<<endl<<endl<<"Nu exista rezultate!"<<endl;repetare();} else { m=sqrt(m1); n=x.get_imaginar()/(2*m); } return complex(n,m); } } double sqrt(double x) { double st=0; double dr=x; double z=0; int acuratete = 100; while(acuratete != 0) { z=(st+dr)/2; if(z*z==x) { return z; } else if(z*z > x) dr = z; else //METODA BISECTIEI

st = z; acuratete--; } return z; }