C++语法综合 | 设计一个MyComplex类

简单的实现, 能够实现+-*/运算. 主要的时间花费在代码的复用上.

//
// Created by hellcat on 2020.05.13.
//
#include <iostream>
using namespace std;

class Complex {
public:
    Complex(double r = 0, double i = 0) : re(r), im(i) {}
    Complex& operator += (const Complex &);
    Complex& operator -= (const Complex &);
    Complex& operator *= (const Complex &);
    Complex& operator /= (const Complex &);

    Complex& operator++();   // Prefix increment
    Complex operator++(int); // Postfix increment
    Complex& operator--();
    Complex operator--(int);

    double real() const { return re; }
    double imag() const { return im; }
private:
    double re, im;
    friend double Dist(const Complex&);
    friend Complex& __doapl(Complex*, const Complex&); // +
    friend Complex& __doami(Complex*, const Complex&); // -
    friend Complex& __doaml(Complex*, const Complex&); // *
    friend Complex& __doadi(Complex*, const Complex&); // /

    friend ostream& operator << (ostream& os, const Complex&);
};

inline double norm(const Complex& x) {
    return x.real() * x.real() + x.imag() * x.imag();
}

inline Complex& __doapl(Complex* ths, const Complex& r) {
    ths->re += r.re;
    ths->im += r.im;
    return *ths;
}

inline Complex& __doami(Complex* ths, const Complex& r) {
    ths->re -= r.re;
    ths->im -= r.im;
    return *ths;
}

inline Complex& __doaml(Complex* ths, const Complex& r) {
    const double _r = ths->re * r.re - ths->im * r.im;
    ths->im = ths->re * r.im + ths->im * r.re;
    ths->re = _r;
    return *ths;
}

inline Complex& __doadi(Complex* ths, const Complex& r) {
    const double _r = ths->re * r.re + ths->im * r.im;
    const double _n = norm(r);
    ths->im = (ths->im * r.re - ths->re * r.im) / _n;
    ths->re = _r/_n;
    return *ths;
}

inline Complex& Complex::operator += (const Complex &x) {
    return __doapl(this, x);
}

inline Complex& Complex::operator -= (const Complex &x) {
    return __doami(this, x);
}

inline Complex &Complex::operator*=(const Complex &x) {
    return __doaml(this, x);
}

inline Complex &Complex::operator /= (const Complex &x) {
    return __doadi(this, x);
}

inline Complex operator + (const Complex& x, const Complex& y) {
    Complex _r = x;
    _r += y;
    return _r;
}

inline Complex operator + (const Complex& x, const double y) {
    return Complex(x.real()+y, x.imag());
}

inline Complex operator + (const double x, const Complex& y) {
    return Complex(x+y.real(), y.imag());
}

inline Complex operator - (const Complex& x, const Complex& y) {
    Complex _r = x;
    _r -= y;
    return _r;
}

inline Complex operator - (const Complex& x, const double y) {
    return Complex(x.real()-y, x.imag());
}

inline Complex operator - (const double x, const Complex& y) {
    return Complex(x-y.real(), y.imag());
}

inline Complex operator * (const Complex& x, const Complex& y) {
    Complex _r = x;
    _r *= y;
    return _r;
}

inline Complex operator / (const Complex& x, const Complex& y) {
    Complex _r = x;
    _r /= y;
    return _r;
}

inline ostream& operator << (ostream& os, const Complex& r) {
    cout<<'('<<r.re<<", "<<r.im<<')';
    return os;
}

inline Complex& Complex::operator++() {
    re++;
    im++;
    return *this;
}

inline Complex Complex::operator++(int) {
    Complex tmp = *this;
    ++*this;
    return tmp;
}

inline Complex& Complex::operator--() {
    re--;
    im--;
    return *this;
}

inline Complex Complex::operator--(int) {
    Complex tmp = *this;
    --*this;
    return tmp;
}

inline double Dist(const Complex& r) {
    return sqrt(r.re*r.re + r.im*r.im);
}

int main() {
    Complex c1(5, 4), c2(2, 10), c3;

    cout<<"c1 = "<<c1<<endl;
    cout<<"c2 = "<<c2<<endl;
    c1 += c2;
    cout<<"After c1 += c2, c1 = "<<c1<<endl;
    cout<<"c1 - 2 = "<<c1 - 2<<endl;
    cout<<"|c1| = "<<Dist(c1)<<endl;
    cout<<"|c2| = "<<Dist(c2)<<endl;
    cout<<"c1-- = "<<c1--<<endl;
    cout<<"++c2 = "<<++c2<<endl;

    c3 = c1 - c2;
    cout<<"c3 = c1 - c2 = "<<c3<<endl;
    c3 = c1 + c2;
    cout<<"c3 = c1 + c2 = "<<c3<<endl;
    c3 = c1 * c2;
    cout<<"c3 = c1 * c2 = "<<c3<<endl;
    c3 = c1 / c2;
    cout<<"c3 = c1 / c2 = "<<c3<<endl;

}

运行结果如下:

posted @ 2020-05-13 14:03  hellcat9  阅读(287)  评论(0编辑  收藏  举报