- 整理思维导图
- 在Complex类的基础上,完成^,>,~运算符的重载
- 在昨天作业myString类的基础上,完成+、关系运算符、逻辑运算符、输入输出运算符的重载
#include <iostream>
using namespace std;
class complex
{
int rel;
int vir;
public:
complex(){}
complex(int rel,int vir):rel(rel),vir(vir){}
void show()
{
cout << rel << "+" << vir << "i" << endl;
}
//算数运算符重载
//+
//complex operator+(complex c1);
friend complex operator+(const complex c1,const complex c2);
complex operator-(complex c1);
friend complex operator-(const complex c1,const complex c2);
//*
complex operator*(complex c1);
friend complex operator*(const complex c1,const complex c2);
//%
complex operator%(complex c1);
friend complex operator%(const complex c1,const complex c2);
// 关系运算符
//>
//bool operator>(complex c1);
friend bool operator>(const complex c1,const complex c2);
//<
// bool operator<(complex c1);
friend bool operator<(const complex c1,const complex c2);
//>=
bool operator>=(complex c1);
//friend operator>=(const complex c1,const complex c2);
// <=
//bool operator<=(complex c1);
friend bool operator<=(const complex c1,const complex c2);
// !=
bool operator!=(complex c1);
//逻辑运算符
//&&
bool operator&&(complex c1);
//++
friend complex operator++(complex c1,int);
//<<
friend ostream &operator<<(ostream &out,complex c1);
//>>
friend istream &operator >>(istream &in,complex &c1);
//
friend bool operator^(const complex c1,const complex c2);
//~
friend int operator~(const complex c1);
};
//算术运算符重载
//+
//complex complex::operator+(complex c1)
//{
// complex temp;
// temp.rel=this->rel+c1.rel;
// temp.vir=this->vir+c1.vir;
// return temp;
//}
complex operator+(const complex c1,const complex c2)
{
complex temp;
temp.rel=c1.rel+c2.rel;
temp.vir=c1.vir+c2.vir;
return temp;
}
//-
complex complex::operator-(complex c1)
{
complex temp;
temp.rel=this->rel-c1.rel;
temp.vir=this->vir-c1.vir;
return temp;
}
complex operator-(const complex c1,const complex c2)
{
complex temp;
temp.rel=c1.rel-c2.rel;
temp.vir=c2.vir-c2.vir;
return temp;
}
//*
complex complex::operator*(complex c1)
{
complex temp;
temp.rel=this->rel*c1.rel;
temp.vir=this->vir*c1.vir;
return temp;
}
complex operator*(const complex c1,const complex c2)
{
complex temp;
temp.rel=c1.rel*c2.rel;
temp.vir=c2.vir*c2.vir;
return temp;
}
//%
complex complex::operator%(complex c1)
{
complex temp;
if(c1.rel==0||c1.vir==0)
{
cout << "分母错误" << endl;
}
temp.rel=this->rel%c1.rel;
temp.vir=this->vir%c1.vir;
return temp;
}
complex operator%(const complex c1,const complex c2)
{
complex temp;
if(c2.rel==0||c2.vir==0)
{
cout << "分母错误" << endl;
}
temp.rel=c1.rel%c2.rel;
temp.vir=c1.vir%c2.vir;
return temp;
}
//关系运算符
// >
//bool complex::operator>(complex c1)
//{
// return this->rel>c1.rel;
//}
bool operator>(const complex c1,const complex c2)
{
return c1.rel>c2.rel;
}
// <
//bool complex::operator<(complex c1)
//{
// return this->rel<c1.rel;
//}
bool operator<(const complex c1,const complex c2)
{
return c1.rel<c2.rel;
}
//>=
bool complex::operator>=(complex c1)
{
if(this->rel>=c1.rel)
{
if(this->rel==c1.rel)
{
return this->vir==c1.vir;
}
else
{
return this->rel>c1.rel;
}
}
else
{
return this->rel>c1.rel;
}
}
// <=
bool operator<=(const complex c1,const complex c2)
{
if(c1.rel<=c2.rel)
{
if(c1.rel==c2.rel)
{
return c1.vir==c2.vir;
}
else
{
return c1.rel<=c2.rel;
}
}
else
{
return c1.rel<=c2.rel;
}
}
//!=
bool complex::operator!=(complex c1)
{
return this->rel!=c1.rel||this->vir!=c1.vir;
}
//逻辑运算符
//&&
bool complex ::operator&&(complex c1)
{
return (this->rel&&c1.rel)||(this->vir&&c1.vir);
}
//++
complex operator++(complex c1,int)
{
complex temp;
temp.rel=c1.rel++;
temp.vir=c1.vir++;
return temp;
}
//<<
ostream &operator<<(ostream &out,complex c1)
{
out<< c1.rel << "+" << c1.vir << "i" <<endl;
return out;
}
//>>
istream &operator >>(istream &in,complex &c1)
{
in >> c1.rel >> c1.vir;
return in;
}
//^
bool operator^(const complex c1,const complex c2)
{
if(c1.rel == c2.rel)
return false;
else
return true;
}
//~
int operator~(const complex c1)
{
int temp(~c1.rel);
return temp;
}
int main()
{
complex a1(1,5),a2(3,5);
// complex a3=a1+a2;
// a3.show();
complex a3=operator+(a1,a2);
a3.show();
//complex a4=a1-a2;
// a4.show();
complex a4=operator-(a1,a2);
a4.show();
//*
// complex a5=a1*a2;
// a5.show();
complex a5=operator*(a1,a2);
a5.show();
//%
// complex a6=a1%a2;
// a6.show();
complex a6=operator%(a1,a2);
a6.show();
//关系运算符
// >
cout << (a1>a2) << endl;
// <
cout << (a1<a2) << endl;
cout << (a1>=a2) << endl;
cout << (a1<=a2) << endl;
cout << (a1&&a2) <<endl;
complex a7=a1++;
a7.show();
cout << "out=" << a1 << endl;
cin >> a5 >>a6;
cout << (a1^a2) << endl;
int b1=10;
int b2=~b1;
cout << "~b1" << b2 << endl;
cout << "Hello World!" << endl;
return 0;
}