zoukankan      html  css  js  c++  java
  • 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;
    
    }
    

    运行结果如下:

  • 相关阅读:
    QFramework 使用指南 2020(二):下载与版本介绍
    QFramework 使用指南 2020 (一): 概述
    Unity 游戏框架搭建 2018 (二) 单例的模板与最佳实践
    Unity 游戏框架搭建 2018 (一) 架构、框架与 QFramework 简介
    Unity 游戏框架搭建 2017 (二十三) 重构小工具 Platform
    Unity 游戏框架搭建 2017 (二十二) 简易引用计数器
    Unity 游戏框架搭建 2017 (二十一) 使用对象池时的一些细节
    你确定你会写 Dockerfile 吗?
    小白学 Python 爬虫(8):网页基础
    老司机大型车祸现场
  • 原文地址:https://www.cnblogs.com/tedukuri/p/12882005.html
Copyright © 2011-2022 走看看