#include<iostream> using namespace std; class complex { public: complex() { } complex(double real,double imag)//构造函数 { _real=real; _imag=imag; } ~complex()//析构函数 { } void print() { cout<<"实部:"<<_real<<"虚部:"<<_imag<<endl; } bool operator>(complex& c)const { if(_real>c._real) return true; else if(_real==c._real) { if(_imag>c._imag) { return true; } else return false; } else return false; } bool operator<(complex& c)const { if(_real<c._real) { return true; } else if(_real==c._real) { if(_imag<c._imag) return true; else return false; } else return false; } bool operator>=(complex& c)const { if(*this<c) return false; else return true; } bool operator<=(complex& c)const { if(*this>c) return false; else return true; } bool operator==(complex& c)const { if((*this>c)||(*this<c)) return false; else return true; } bool operator!=(complex& c) { if(*this==c) return false; else return true; } complex operator++(int)//后置加加 { complex temp=*this; _real++; return temp; } complex& operator++()//前置加加 { _real++; return *this; } complex& operator+=(double x) { _real+=x; return (complex&)*this; } protected: double _real; double _imag; }; void test()//测试operator>和operator< { complex c(4,2); complex c1(5,2); bool x = false; c.print(); c1.print(); x=c >c1; cout<<x<<endl; x=c <c1; cout<<x<<endl; } void test1()//测试operator>= operator<= { complex c2(1,1); complex c3(0,1); complex c4(1,1); bool x = 0; c2.print(); c3.print(); x=c2>=c3; cout<<x<<endl; x=c3>=c2; cout<<x<<endl; x=c4>=c2; cout<<x<<endl; x=c2<=c3; cout<<x<<endl; x=c3<=c2; cout<<x<<endl; x=c4<=c2; cout<<x<<endl; } void test2()//测试operator== operator!= { complex c2(1,1); complex c3(0,1); complex c4(1,1); bool x = 0; c2.print(); c3.print(); c4.print(); x=c2==c3; cout<<x<<endl; x=c3==c2; cout<<x<<endl; x=c4==c2; cout<<x<<endl; x=c2!=c3; cout<<x<<endl; x=c3!=c2; cout<<x<<endl; x=c4!=c2; cout<<x<<endl; } void test3()//测试++ { complex c2(1,1); complex c3(0,1); c3=c2++; c2.print(); c3.print(); ++c2; c2.print(); c3 +=6; c3.print(); } int main() { test(); test1(); test2(); test3(); return 0; }
复数类:complex c++
猜你喜欢
转载自blog.csdn.net/fangxiaxin/article/details/74097198
今日推荐
周排行