اینم
برنامه عدد مختلط که با استفاده از اپراتور
(operator) نوشته شده.
با این برنامه میتونید نحوه بارگزاری اپراتور ها برای کلاس های مختلف را یاد
بگیرد.
اگر جایی اش هم توضیح میخواد بگید تا توضیح بدم
#include<iostream.h>
#include<math.h>
class complex
{
protected:
double x;// real part
double y;// image part
public:
complex(double real=0, double imag=0) {assign(real,imag);}
complex(complex& c);
void assign(double real=0, double imag=0);
double getreal() const {return x;}
double getimag() const {return y;}
//**********************************************
// From Now, declaring operators for the class
complex& operator =(complex& c);
complex& operator +=(complex& c);
complex& operator -=(complex& c);
bool friend operator ==(complex& c1,complex& c2);
bool friend operator >(complex& c1,complex& c2);
bool friend operator <(complex& c1,complex& c2);
friend complex operator *(complex& c1,complex& c2);
friend complex operator +(complex& c1,complex& c2);
friend complex operator -(complex& c1,complex& c2);
friend ostream& operator <<(ostream& os,complex& c);
};//end
complex::complex(complex& c)
{
x=c.x;
y=c.y;
}
void complex::assign(double real,double imag)
{
x=real;
y=imag;
}
complex& complex::operator =(complex& c)
{
x=c.x;
y=c.y;
return *this;
}
complex& complex::operator +=(complex& c)
{
x +=c.x;
y +=c.y;
return *this;
}
complex& complex::operator -=(complex& c)
{
x -=c.x;
y -=c.y;
return *this;
}
complex operator +(complex& c1,complex& c2)
{
complex result(c1);
result.x +=c2.x;
result.y +=c2.y;
return result;
}
bool operator ==(complex& c1,complex& c2)
{
if ((c1.x==c2.x)&&(c1.y==c2.y)) return true;
else return false;
}
bool operator >(complex& c1,complex& c2)
{
double a,b;
a=sqrt(pow(c1.x,2)+pow(c1.y,2));
b=sqrt(pow(c2.x,2)+pow(c2.y,2));
if (a>b) return true;
else return false;
}
bool operator <(complex& c1,complex& c2)
{
double a,b;
a=sqrt(pow(c1.x,2)+pow(c1.y,2));
b=sqrt(pow(c2.x,2)+pow(c2.y,2));
if (a<b) return true;
else return false;
}
complex operator -(complex& c1,complex& c2)
{
complex result(c1);
result.x -=c2.x;
result.y -=c2.y;
return result;
}
complex operator *(complex& c1,complex& c2)
{
complex result;
result.x =(c1.x*c2.x)+(c1.y*c2.y);
result.y =((c1.x*c2.y)-(c1.y*c2.x));
return result;
}
ostream& operator <<(ostream& os, complex& c)
{
if (c.y>0){ os<< “(” << c.x << ” + i” << c.y<< “)” ;} else if (c.y==0){ os<< “(“
<< c.x <<”)” ;} else
{os<< “(” << c.x << ” – i” << c.y*-1<< “)” ;}
return os;
}
//begin of an exapmle main()
main()
{
complex c1(3,5);
complex c2(7,5);
complex c3;
complex c4(2,3);
c3=c1+c2;
cout<< c1 << “+” << c2 << “=” << c3 <<”\n”;
cout<< c3 << “+” << c4 << “=”;
c3 += c4;
cout<< c3 << “\n” ;
complex c5(c1);
complex c6(c2);
complex c7;
c7=c5-c6;
cout<<c7<<”=”<<c5<<”-”<<c6<<endl;
complex c8(c1);
complex c9(c2);
complex c10;
c10=c8*c9;
cout<<c10<<”=”<<c8<<”*”<<c9<<endl;
if (c9==c2) cout<<”c9 is equal with c2\n”;
if (c10==c5) cout<<”c10 is equal with c5\n”;
if (c9>c2) cout<<”c9 is bigger than c2\n”;
if (c10>c5) cout<<”c10 is bigger than c5\n”;
return 0;
}