Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <memory>
- #include <cmath>
- using namespace std;
- template<typename T>
- class MyComplex{
- public://type
- //template argument
- typedef T ValueType;
- //proxy
- class ProxyAbs{
- friend class MyComplex;
- private:
- MyComplex &root;
- ProxyAbs(MyComplex &root): root(root){}
- public:
- operator ValueType(void)const{return sqrt(pow(root.real(),2)+pow(root.imag(),2));}
- ProxyAbs &operator =(const ValueType &rhs){root.real()=root.abs()*sin(root.arg()); root.imag()=root.abs()*cos(root.arg()); return *this;}
- ProxyAbs &operator +=(const ValueType &rhs){return *this = rhs + root.abs();}
- ProxyAbs &operator -=(const ValueType &rhs){return *this += -rhs;}
- ProxyAbs &operator *=(const ValueType &rhs){return *this = rhs * root.abs();}
- ProxyAbs &operator /=(const ValueType &rhs){return *this *= (1/rhs);}
- };
- class ProxyArg{
- friend class MyComplex;
- private:
- MyComplex &root;
- ProxyArg(MyComplex &root): root(root){}
- public:
- operator double(void)const{return atan2(root.real(), root.imag());}
- ProxyArg &operator =(double rhs){root.real()=double(root.abs())*sin(rhs); root.imag()=double(root.abs())*cos(rhs); return *this;}
- ProxyArg &operator +=(double rhs){return *this = rhs + double(root.arg());}
- ProxyArg &operator -=(double rhs){return *this + -rhs;}
- ProxyArg &operator *=(double rhs){return *this = rhs * double(root.arg());}
- ProxyArg &operator /=(double rhs){return *this * (1/rhs);}
- };
- private://field
- //pimpl idiom
- struct PImpl{
- ValueType real;
- ValueType imag;
- };
- std::auto_ptr<PImpl> pimpl;
- public://method
- //ctor
- MyComplex(const ValueType &real, const ValueType &imag): pimpl(new PImpl(real, imag)){}
- MyComplex(const MyComplex<ValueType> &src): pimpl(new PImpl(src.pimpl)){}
- //accessor
- const ValueType &real(void)const{return pimpl->real;}
- ValueType &real(void) {return pimpl->real;}
- const ValueType &imag(void)const{return pimpl->imag;}
- ValueType &imag(void) {return pimpl->imag;}
- const ProxyAbs &abs(void)const{return Abs(*this);}
- ProxyAbs &abs(void) {return Abs(*this);}
- const ProxyArg &arg(void)const{return Arg(*this);}
- ProxyArg &arg(void) {return Arg(*this);}
- //operator
- MyComplex<ValueType> &operator +(void){return MyComplex<ValueType>(*this);}
- MyComplex<ValueType> &operator -(void){return MyComplex<ValueType>(-real(), -imag());}
- MyComplex<ValueType> &operator =(const MyComplex<ValueType> &rhs){real() = rhs.real(); imag() = rhs.imag(); return *this;}
- MyComplex<ValueType> &operator =(const ValueType &rhs){real() = rhs; imag() = ValueType(); return *this;}
- MyComplex<ValueType> &operator +=(const MyComplex<ValueType> &rhs){return *this = MyComplex<ValueType>(real()+rhs.real(), imag()+rhs.imag());}
- MyComplex<ValueType> &operator +=(const ValueType &rhs){return *this = MyComplex<ValueType>(real()+rhs, imag());}
- MyComplex<ValueType> &operator -=(const MyComplex<ValueType> &rhs){real() += -rhs;}
- MyComplex<ValueType> &operator -=(const ValueType &rhs){return *this -= MyComplex<ValueType>(-rhs);}
- MyComplex<ValueType> &operator *=(const MyComplex<ValueType> &rhs){abs() *= rhs.abs(); arg() += rhs.arg();}
- MyComplex<ValueType> &operator *=(const ValueType &rhs){return *this *= MyComplex<ValueType>(1/rhs);}
- MyComplex<ValueType> &operator /=(const MyComplex<ValueType> &rhs){return *this /= MyComplex<ValueType>(-rhs);}
- MyComplex<ValueType> &operator /=(const ValueType &rhs){return *this /= MyComplex<ValueType>(rhs);}
- };
- template<typename T>MyComplex<T> &operator +(const MyComplex<T> &lhs, const MyComplex<T> &rhs){return MyComplex<T>(lhs) += rhs;}
- template<typename T>MyComplex<T> &operator +(const MyComplex<T> &lhs, const T &rhs){return MyComplex<T>(lhs) += rhs;}
- template<typename T>MyComplex<T> &operator -(const MyComplex<T> &lhs, const MyComplex<T> &rhs){return MyComplex<T>(lhs) -= rhs;}
- template<typename T>MyComplex<T> &operator -(const MyComplex<T> &lhs, const T &rhs){return MyComplex<T>(lhs) -= rhs;}
- template<typename T>MyComplex<T> &operator *(const MyComplex<T> &lhs, const MyComplex<T> &rhs){return MyComplex<T>(lhs) *= rhs;}
- template<typename T>MyComplex<T> &operator *(const MyComplex<T> &lhs, const T &rhs){return MyComplex<T>(lhs) *= rhs;}
- template<typename T>MyComplex<T> &operator /(const MyComplex<T> &lhs, const MyComplex<T> &rhs){return MyComplex<T>(lhs) /= rhs;}
- template<typename T>MyComplex<T> &operator /(const MyComplex<T> &lhs, const T &rhs){return MyComplex<T>(lhs) /= rhs;}
Add Comment
Please, Sign In to add comment