class Complex{
private:
double re;
double im;
public:
//构造函数
Complex():re(0),im(0){
cout<<"构造函数Complex"<<endl;
}
Complex(double re):re(re),im(0){
cout<<"构造函数Complex"<<endl;
}
Complex(double re,double im):re(re),im(im){
cout<<"构造函数Complex"<<endl;
}
Complex &operator =(Complex &other);
Complex(const Complex &other):re(other.re),im(other.im){}
//算数运算符重载
Complex operator +(Complex other);
Complex operator -(Complex other);
Complex operator *(Complex other);
//逻辑运算符重载
bool operator >(Complex other);
bool operator <(Complex other);
bool operator >=(Complex other);
//自增自减
Complex &operator ++();
Complex &operator --();
// Complex &operator ();
//show
void show()const;
};
Complex &Complex::operator =(Complex &other){
this->re=other.re;
this->im=other.im;
return *this;
}
Complex Complex::operator +(Complex other){
Complex temp;
temp.re = this->re + other.re;
temp.im = this->im + other.im;
return temp;
}
Complex Complex::operator -(Complex other){
Complex temp;
temp.re = this->re - other.re;
temp.im = this->im - other.im;
return temp;
}
Complex Complex::operator *(Complex other){
Complex temp;
temp.re = (this->re)*(other.re)-(this->im)*(other.im);
temp.im = (this->im)*(other.re)+(other.im)*(this->re);
return temp;
}
bool Complex::operator >(Complex other){
if(this->re != other.re){
return this->re > other.re;
}else{
return this->im > other.im;
}
}
bool Complex::operator <(Complex other){
if(this->re != other.re){
return this->re < other.re;
}else{
return this->im < other.im;
}
}
bool Complex::operator >=(Complex other){
if(this->re != other.re){
return this->re > other.re;
}else{
return this->im >= other.im;
}
}
Complex &Complex::operator ++(){
this->re++;
this->im++;
return *this;
}
Complex &Complex::operator --(){
this->re--;
this->im--;
return *this;
}
void Complex::show() const{
if(im>=0){
cout<<re<<'+'<<im<<'i'<<endl;
}else if(im<0){
cout<<re<<im<<'i'<<endl;
}
}
cpp第四次作业
最新推荐文章于 2024-10-05 12:07:50 发布