Witam, muszę dopisać do podanego kodu funkcję liczącą moduł liczby zespolonej, oparty na przeciążeniu funkcji fabs, niestety zupełnie nie mam pomysłu w jaki sposób zabrać się za przeciążenie tej funkcji. Proszę o pomoc. :)
#include <iostream>
#include <math.h>
#include <cstdlib>
using namespace std;
class complex {
double re, im;
public:
complex(double x, double y)
{
re = x;
im = y;
}
complex(const complex &a)
{
this->re=a.re;
this->im=a.im;
}
complex operator+(const complex &a)
{
complex result(*this);
result.dodawanie(a);
return result;
}
complex operator*(const complex &a)
{
complex result(*this);
result.mnozenie(a);
return result;
}
complex operator-(const complex &a)
{
complex result(*this);
result.odejmowanie(a);
return result;
}
complex operator/(const complex &a)
{
complex result(*this);
result.dzielenie(a);
return result;
}
complex &operator=(const complex &a)
{
this->re=a.re;
this->im=a.im;
return *this;
}
void dodawanie(complex a)
{
setRe( getRe() + a.getRe() );
setIm( getIm() + a.getIm() );
}
void odejmowanie(complex a)
{
setRe( getRe() - a.getRe() );
setIm( getIm() - a.getIm() );
}
void mnozenie(complex a)
{ double tre, tim;
tre = getRe()*a.getRe() - getIm()*a.getIm() ;
tim = getRe()*a.getIm() + getIm()*a.getRe() ;
setRe(tre);
setIm(tim);
}
void dzielenie(complex a)
{ double mod2, tre, tim;
mod2=sqrt( a.getRe()*a.getRe() + a.getIm()*a.getIm() );
tre=(getRe()*a.getRe()+getIm()*a.getIm())/(mod2*mod2);
tim=(getIm()*a.getRe()-getRe()*a.getIm())/(mod2*mod2);
setRe(tre);
setIm(tim);
}
double getRe()
{
return re;
}
double getIm()
{
return im;
}
void setRe(double r)
{
re = r;
}
void setIm(double i)
{
im = i;
}
friend ostream& operator<<(ostream& out, complex& c)
{
out<<c.getRe() <<" + " << c.getIm()<<"i"<<endl;
return out;
}
};
int main()
{
double x,y;
cout << "Pierwsza liczba zespolona" << endl;
cout << "Czesc rzeczywista: ";
cin >> x;
cout << "Czesc urojona: ";
cin >> y;
complex a(x,y);
cout << "Druga liczba zespolona" << endl;
cout << "Czesc rzeczywista: ";
cin >> x;
cout << "Czesc urojona: ";
cin >> y;
complex b(x,y);
complex c(x,y);
cout<<endl;
cout << "DODANIE LICZB = ";
c=a+b;
cout<<c<<endl;
cout << "ROZNICA = ";
c=a-b;
cout<<c<<endl;
cout << "MNOZENIE = " ;
c=a*b;
cout<<c<<endl;
cout << "DZIELENIE = " ;
c=a/b;
cout<<c<<endl;
system("pause");
return 0;
}