一个极坐标类

来源:互联网 发布:toki tori下载mac 编辑:程序博客网 时间:2024/05/12 03:04
#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;}