Guest User

Untitled

a guest
Jul 17th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.35 KB | None | 0 0
  1. #include <memory>
  2.  
  3. #include <cmath>
  4.  
  5. using namespace std;
  6.  
  7.  
  8.  
  9. template<typename T>
  10. class MyComplex{
  11. public://type
  12. //template argument
  13. typedef T ValueType;
  14.  
  15. //proxy
  16. class ProxyAbs{
  17. friend class MyComplex;
  18. private:
  19. MyComplex &root;
  20.  
  21. ProxyAbs(MyComplex &root): root(root){}
  22. public:
  23. operator ValueType(void)const{return sqrt(pow(root.real(),2)+pow(root.imag(),2));}
  24. ProxyAbs &operator =(const ValueType &rhs){root.real()=root.abs()*sin(root.arg()); root.imag()=root.abs()*cos(root.arg()); return *this;}
  25. ProxyAbs &operator +=(const ValueType &rhs){return *this = rhs + root.abs();}
  26. ProxyAbs &operator -=(const ValueType &rhs){return *this += -rhs;}
  27. ProxyAbs &operator *=(const ValueType &rhs){return *this = rhs * root.abs();}
  28. ProxyAbs &operator /=(const ValueType &rhs){return *this *= (1/rhs);}
  29. };
  30.  
  31. class ProxyArg{
  32. friend class MyComplex;
  33. private:
  34. MyComplex &root;
  35.  
  36. ProxyArg(MyComplex &root): root(root){}
  37. public:
  38. operator double(void)const{return atan2(root.real(), root.imag());}
  39. ProxyArg &operator =(double rhs){root.real()=double(root.abs())*sin(rhs); root.imag()=double(root.abs())*cos(rhs); return *this;}
  40. ProxyArg &operator +=(double rhs){return *this = rhs + double(root.arg());}
  41. ProxyArg &operator -=(double rhs){return *this + -rhs;}
  42. ProxyArg &operator *=(double rhs){return *this = rhs * double(root.arg());}
  43. ProxyArg &operator /=(double rhs){return *this * (1/rhs);}
  44. };
  45. private://field
  46. //pimpl idiom
  47. struct PImpl{
  48. ValueType real;
  49. ValueType imag;
  50. };
  51. std::auto_ptr<PImpl> pimpl;
  52. public://method
  53. //ctor
  54. MyComplex(const ValueType &real, const ValueType &imag): pimpl(new PImpl(real, imag)){}
  55. MyComplex(const MyComplex<ValueType> &src): pimpl(new PImpl(src.pimpl)){}
  56.  
  57. //accessor
  58. const ValueType &real(void)const{return pimpl->real;}
  59. ValueType &real(void) {return pimpl->real;}
  60. const ValueType &imag(void)const{return pimpl->imag;}
  61. ValueType &imag(void) {return pimpl->imag;}
  62. const ProxyAbs &abs(void)const{return Abs(*this);}
  63. ProxyAbs &abs(void) {return Abs(*this);}
  64. const ProxyArg &arg(void)const{return Arg(*this);}
  65. ProxyArg &arg(void) {return Arg(*this);}
  66.  
  67. //operator
  68. MyComplex<ValueType> &operator +(void){return MyComplex<ValueType>(*this);}
  69. MyComplex<ValueType> &operator -(void){return MyComplex<ValueType>(-real(), -imag());}
  70.  
  71. MyComplex<ValueType> &operator =(const MyComplex<ValueType> &rhs){real() = rhs.real(); imag() = rhs.imag(); return *this;}
  72. MyComplex<ValueType> &operator =(const ValueType &rhs){real() = rhs; imag() = ValueType(); return *this;}
  73. MyComplex<ValueType> &operator +=(const MyComplex<ValueType> &rhs){return *this = MyComplex<ValueType>(real()+rhs.real(), imag()+rhs.imag());}
  74. MyComplex<ValueType> &operator +=(const ValueType &rhs){return *this = MyComplex<ValueType>(real()+rhs, imag());}
  75. MyComplex<ValueType> &operator -=(const MyComplex<ValueType> &rhs){real() += -rhs;}
  76. MyComplex<ValueType> &operator -=(const ValueType &rhs){return *this -= MyComplex<ValueType>(-rhs);}
  77. MyComplex<ValueType> &operator *=(const MyComplex<ValueType> &rhs){abs() *= rhs.abs(); arg() += rhs.arg();}
  78. MyComplex<ValueType> &operator *=(const ValueType &rhs){return *this *= MyComplex<ValueType>(1/rhs);}
  79. MyComplex<ValueType> &operator /=(const MyComplex<ValueType> &rhs){return *this /= MyComplex<ValueType>(-rhs);}
  80. MyComplex<ValueType> &operator /=(const ValueType &rhs){return *this /= MyComplex<ValueType>(rhs);}
  81. };
  82.  
  83. template<typename T>MyComplex<T> &operator +(const MyComplex<T> &lhs, const MyComplex<T> &rhs){return MyComplex<T>(lhs) += rhs;}
  84. template<typename T>MyComplex<T> &operator +(const MyComplex<T> &lhs, const T &rhs){return MyComplex<T>(lhs) += rhs;}
  85. template<typename T>MyComplex<T> &operator -(const MyComplex<T> &lhs, const MyComplex<T> &rhs){return MyComplex<T>(lhs) -= rhs;}
  86. template<typename T>MyComplex<T> &operator -(const MyComplex<T> &lhs, const T &rhs){return MyComplex<T>(lhs) -= rhs;}
  87. template<typename T>MyComplex<T> &operator *(const MyComplex<T> &lhs, const MyComplex<T> &rhs){return MyComplex<T>(lhs) *= rhs;}
  88. template<typename T>MyComplex<T> &operator *(const MyComplex<T> &lhs, const T &rhs){return MyComplex<T>(lhs) *= rhs;}
  89. template<typename T>MyComplex<T> &operator /(const MyComplex<T> &lhs, const MyComplex<T> &rhs){return MyComplex<T>(lhs) /= rhs;}
  90. 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