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;

}

运行结果如下:
C++语法综合 | 设计一个MyComplex类

原文链接: https://www.cnblogs.com/tedukuri/p/12882005.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

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

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/348167

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年3月2日 上午4:55
下一篇 2023年3月2日 上午4:55

相关推荐