1.思维导图
2.
#include <iostream>
using namespace std;
class Complex
{
int rel; //实部
int vir; //虚部
public:
Complex(){}
Complex(int rel,int vir):rel(rel),vir(vir){}
/* Complex operator+(const Complex c1);
Complex operator-(const Complex c1);
Complex operator*(const Complex c1);
Complex operator/(const Complex c1);*/
friend Complex operator+(const Complex c1,const Complex c2);
friend Complex operator-(const Complex c1,const Complex c2);
friend Complex operator*(const Complex c1,const Complex c2);
friend Complex operator/(const Complex c1,const Complex c2);
bool operator>(const Complex c1);
bool operator<(const Complex c1);
bool operator>=(const Complex c1);
bool operator<=(const Complex c1);
bool operator==(const Complex c1);
bool operator!=(const Complex c1);
bool operator!();
bool operator&&(const Complex c1);
bool operator||(const Complex c1);
Complex operator++();
Complex operator--();
friend Complex operator++(Complex &c1,int );
friend Complex operator--(Complex &c1,int );
friend ostream &operator<<(ostream &out,Complex c1);
friend istream &operator>>(istream &in,Complex &c1);
Complex operator^(Complex c1);
Complex operator<<(int a);
Complex operator>>(int a);
Complex operator~();
Complex show()
{cout << rel << "+"<< vir << "i" << endl;}
operator int()
{return this->rel;}
operator double()
{return this->rel;}
operator char()
{return 'a';}
int operator()(int a,int b )
{return a>b?a:b;}
};
//^,<<,>>,~
Complex Complex::operator^(Complex c1)
{
Complex temp;
temp.rel=(this->rel)^c1.rel;
temp.vir=(this->vir)^c1.vir;
return temp;
}
Complex Complex::operator<<(int a)
{
rel=rel<<a;
vir=vir<<a;
return *this;
}
Complex Complex::operator>>(int a)
{
rel=rel>>a;
vir=vir>>a;
return *this;
}
Complex Complex::operator~()
{
rel=~rel;
vir=~vir;
return *this;
}
//自增自减
Complex Complex::operator++()
{
++this->rel;
++this->vir;
return *this;
}
Complex Complex::operator--()
{
--this->rel;
--this->vir;
return *this;
}
Complex operator++(Complex &c1,int )
{
Complex temp=c1;
c1.rel++;
c1.vir++;
return temp;
}
Complex operator--(Complex &c1,int )
{
Complex temp=c1;
c1.rel--;
c1.vir--;
return temp;
}
bool Complex::operator!()
{
return !(this->rel || this->vir);
}
bool Complex::operator&&(const Complex c1)
{
return this->rel && c1.rel && this->vir && c1.vir;
}
bool Complex::operator||(const Complex c1)
{
return this->rel || c1.rel || this->vir || c1.vir;
}
bool Complex::operator>(const Complex c1)
{
return this->rel>c1.rel;
}
bool Complex::operator<(const Complex c1)
{
return this->rel<c1.rel;
}
bool Complex::operator>=(const Complex c1)
{
return this->rel>=c1.rel;
}
bool Complex::operator<=(const Complex c1)
{
return this->rel<=c1.rel;
}
bool Complex::operator==(const Complex c1)
{
return this->rel==c1.rel&&this->vir==c1.vir;
}
bool Complex::operator!=(const Complex c1)
{
return this->rel!=c1.rel||this->vir!=c1.vir;
}
//加减乘除
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 operator-(const Complex c1,const Complex c2)
{
Complex temp;
temp.rel = c1.rel - c2.rel;
temp.vir = c1.vir - c2.vir;
return temp;
}
Complex operator*(const Complex c1,const Complex c2)
{
Complex temp;
temp.rel = c1.rel * c2.rel-c1.vir * c2.vir;
temp.vir = c1.vir * c2.rel+c1.rel * c2.vir;
return temp;
}
Complex operator/(const Complex c1,const Complex c2)
{
Complex temp;
int a = c1.rel;
int b = c1.vir;
int c = c2.rel;
int d = c2.vir;
temp.rel = (a*c+b*d)/(c*c-d*d);
temp.vir = (b*c-a*d)/(c*c+d*d);
return temp;
}
/*
Complex Complex::operator+(const Complex c1)
{
Complex temp;
temp.rel = this ->rel+c1.rel;
temp.vir = this->vir +c1.vir;
return temp;
}
Complex Complex::operator-(const Complex c1)
{
Complex temp;
temp.rel = this->rel - c1.rel;
temp.vir = this->vir - c1.vir;
return temp;
}
Complex Complex::operator*(const Complex c1)
{
Complex temp;
temp.rel = this->rel * c1.rel;
temp.vir = this->vir * c1.vir;
return temp;
}
Complex Complex::operator/(const Complex c1)
{
Complex temp;
temp.rel = this->rel / c1.rel;
temp.vir = this->vir / c1.vir;
return temp;
}*/
//输出
ostream &operator<<(ostream &out,Complex c1)
{
out << c1.rel << "+" << c1.vir << "i" ;
return out;
}
//输出
istream &operator>>(istream &in,Complex &c1)
{
in >> c1.rel >> c1.vir;
return in;
}
int main()
{
Complex c1(4,6),c2(4,0),c3(1,2),c4;
// cin >> c3 >> c4 ;
// cout << c3 << endl << c4 << endl;
cout << (c1<<1) << endl << (c2>>1) << endl;
cout << ~c3 <<endl;
c4 = c1^c2 ;
cout << c4 <<endl;
return 0;
}
3.
//+
myString myString::operator+(const myString s1){
strcat(this->str,s1.str);
this->size=this->size+s1.size;
return *this;
}
//==
bool myString::operator==(const myString s1){
return !strcmp(this->str,s1.str);
}
//>=
bool myString::operator>=(const myString s1){
if(strcmp(this->str,s1.str)==0 || strcmp(this->str,s1.str)){
return 1;
}else
return 0;
}
//&&
bool myString::operator&&(const myString s1){
return this->str && s1.str;
}
//!
bool myString::operator!(){
return !this->size;
}
//输出
ostream &operator<<(ostream &out,myString s1){
out << s1.str << '\t' << s1.size ;
return out;
}
//输入
istream &operator>>(istream &in,myString s1){
in >> s1.str >> s1.size;
return in;
}