1.昨天的基础上完成运算符重载
#include <iostream>
using namespace std;
class Complex
{
private:
int real;
int vir;
public:
//无参构造
Complex()
{
cout << "无参构造" << endl;
}
//有参构造
Complex(int a,int b):real(a),vir(b)
{
cout << "有参构造" << endl;
}
void show()
{
cout << real << "+" << vir << "i" << endl;
}
//+++++++++++++++++++++++++++
Complex operator+(const Complex &a)
{
Complex temp;
temp.real=this->real + a.real;
temp.vir=this->vir + a.vir;
return temp;
}
//----------------------------
Complex operator-(const Complex &b)
{
Complex temp;
temp.real=this->real - b.real;
temp.vir=this->vir - b.vir;
return temp;
}
//****************************
Complex operator*(const Complex &c)
{
Complex temp;
temp.real=this->real * c.real;
temp.vir=this->vir * c.vir;
return temp;
}
//(///)
Complex operator/(const Complex &d)
{
Complex temp;
temp.real=this->real / d.real;
temp.vir=this->vir / d.vir;
return temp;
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Complex operator%(const Complex &e)
{
Complex temp;
temp.real=this->real % e.real;
temp.vir=this->vir % e.vir;
return temp;
}
// //+= += += += += += += +=
// Complex operator+=(const Complex &e)
// {
// this->real=this->real + e.real;
// this->vir=this->vir + e.vir;
// return *this;
// }
// //-= -= -= -= -= -= -= -=
// Complex operator-=(const Complex &f)
// {
// this->real=this->real - f.real;
// this->vir=this->vir - f.vir;
// return *this;
// }
// //*= *= *= *= *= *= *= *=
// Complex operator*=(const Complex &g)
// {
// this->real=this->real * g.real;
// this->vir=this->vir * g.vir;
// return *this;
// }
// // /= /= /= /= /= /= /= /=
// Complex operator/=(const Complex &h)
// {
// this->real=this->real / h.real;
// this->vir=this->vir / h.vir;
// return *this;
// }
// // %= %= %= %= %= %= %= %=
// Complex operator%=(const Complex &i)
// {
// this->real=this->real % i.real;
// this->vir=this->vir % i.vir;
// return *this;
// }
// ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++
// Complex operator++()
// {
// ++this->real;
// ++this->vir;
// return *this;
// }
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// Complex operator!()
// {
// this->real=!this->real;
// this->vir=!this->vir;
// return *this;
// }
//- - - - - - - - - - - - - - - - -
// Complex operator-()
// {
// this->real=-this->real;
// this->vir=-this->vir;
// return *this;
// }
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Complex operator~()
// {
// this->real=~this->real;
// this->vir=~this->vir;
// return *this;
// }
bool operator >(Complex &m)
{
if(this->real==m.real)
{
return this->vir<m.vir;
}
else
{
return this->real>m.real;
}
}
bool operator==(Complex &p)
{
if(this->real==p.real)
{
return this->vir<p.vir;
}
else
{
return this->real>p.real;
}
}
friend Complex operator--(Complex &j,int);
friend bool operator<=(Complex &n,Complex &o);
friend bool operator!=(Complex &q,Complex &r);
};
//Complex operator--(Complex &j,int)
//{
// Complex temp;
// temp.real=j.real--;
// temp.vir=j.vir--;
// return temp;
//}
bool operator<=(Complex &n,Complex &o)
{
if(n.real==o.real)
{
return n.vir<o.vir;
}
else
{
return n.real>o.real;
}
}
bool operator!=(Complex &q,Complex &r)
{
if(q.real!=r.real)
{
return q.vir<r.vir;
}
else
{
return q.real>r.real;
}
}
int main()
{
Complex c1(4,11);
Complex c2(2,9);
//++++++++++++++++++
Complex c3;
c3=c1+c2;
c3.show();//6+20i
//------------------
Complex c4;
c4=c1-c2;
c4.show();//2+2i
//******************
Complex c5;
c5=c1*c2;
c5.show();//8+99i
//(//)
Complex c6;
c6=c1/c2;
c6.show();//2+1i
//%%%%%%%%%%%%%%%%%%%
Complex c7;
c7=c1%c2;
c7.show();//0+2i
// //+= += += += += +=
// c1+=c2;
// c1.show();//6+20i
// //-= -= -= -= -= -=
// c1-=c2;
// c1.show();//4+11i,以上面的结果位基础,需要把上面的c1+=c2注释,上面直接修改了值,影响主观判断
// //*= *= *= *= *=
// c1*=c2;
// c1.show();//8+99i
// // /= /= /= /= /=
// c1/=c2;
// c1.show();//4+11i
// // %= %= %= %= %=
// c1%=c2;
// c1.show();//0+2i
// ++ ++ ++ ++ ++ ++
// c1=++c2;
// c1.show();//3+10i
// -- -- -- -- -- --
// c1=c2--;
// c1.show();//2+9i
// c2.show();//1+8i
//!!!!!!!!!!!!!!!!!!!!!
// c1=!c1;
// c1.show();//0+0i
//- - - - - - - - -
// c1=-c1;
// c1.show();//-2+-9i
//~~~~~~~~~~~~~~~~~~
// c1=~c1;
// c1.show();//-5-+12i
//< < < < < < < < <
bool a=c1>c2;
cout << a << endl;//1
//<= <= <= <= <=
bool b=c1<=c2;
cout << b << endl;//1
//== == == == == ==
bool c=c1==c2;
cout << c <<endl;//1
//!= != != != !=
bool d=c1!=c2;
cout << d << endl;//0
return 0;
}