#include <iostream> #include <complex> using namespace std; template<class T> struct BasicPolar { public: typedef BasicPolar self; BasicPolar() : m() {} BasicPolar(const self& x) : m(x.m) {} BasicPolar(const T& rho, const T& theta) : m(polar(rho, theta)) {} self operator-() { return Polar(-m); } self& operator+=(const self& x) { m += x.m; return *this; } self& operator-=(const self& x) { m -= x.m; return *this; } self& operator*=(const self& x) { m *= x.m; return *this; } self& operator/=(const self& x) { m /= x.m; return *this; } operator complex<T>() const { return m; } T rho() const { return abs(m); } T theta() const { return arg(m); } friend self operator+(self x, const self& y) { return x += y; } friend self operator-(self x, const self& y) { return x -= y; } friend self operator*(self x, const self& y) { return x *= y; } friend self operator/(self x, const self& y) { return x /= y; } friend bool operator==(const self& x, const self& y) { return x.m == y.m; } friend bool operator!=(const self& x, const self& y) { return x.m != y.m; } private: complex<T> m; }; typedef BasicPolar<double> Polar; int main(int argc, char** argv) { double rho = 3.0; double theta = 3.141592 / 2; Polar coord(rho, theta); cout << "rho = " << coord.rho() << ", theta = " << coord.theta() << endl; coord += Polar(4.0, 0.0); cout << "rho = " << coord.rho() << ", theta = " << coord.theta() << endl; system("pause"); return 0; }
一个极坐标类
最新推荐文章于 2021-01-07 23:37:44 发布