9.3 (C++)

Define the Complex class.

输出
c7 = 35 + 55i
c8 = 15 + 15i
c9 = -450 + 850i
c10 = 1.9 + -0.3i
c11 = 20 + 20i
c12 = -10 + 20i
c3 = 26 + 37i
c4 = -6 + -11i
c5 = 220 + 740i
c6 = 5.2 + -1.4i
c1 != c2

主程序 (不能修改)

#include <iostream>
#include <cmath>
using namespace std;

#include "source.cpp"

int main(){
    Complex c1(25, 35);
    Complex c2(10, 20);
    Complex c3(1, 2);
    Complex c4(4, 9);
    Complex c5(34, 6);
    Complex c6(80, 90);
   
    Complex c7 = c1 + c2;
    Complex c8 = c1 - c2;
    Complex c9 = c1 * c2;
    Complex c10 = c1 / c2;
    Complex c11 = 10 + c2;
    Complex c12 = c2 - 20;
    cout << "c7 = " << c7.real() << " + " << c7.imag() << "i" << endl;
    cout << "c8 = " << c8.real() << " + " << c8.imag() << "i" << endl;
    cout << "c9 = " << c9.real() << " + " << c9.imag() << "i" << endl;
    cout << "c10 = " << c10.real() << " + " << c10.imag() << "i" << endl;
    cout << "c11 = " << c11.real() << " + " << c11.imag() << "i" << endl;
    cout << "c12 = " << c12.real() << " + " << c12.imag() << "i" << endl;
   
    c3 += c1;
    c4 -= c2;
    c5 *= c2;
    c6 /= c2;
    cout << "c3 = " << c3.real() << " + " << c3.imag() << "i" << endl;
    cout << "c4 = " << c4.real() << " + " << c4.imag() << "i" << endl;
    cout << "c5 = " << c5.real() << " + " << c5.imag() << "i" << endl;
    cout << "c6 = " << c6.real() << " + " << c6.imag() << "i" << endl;
   
    if(c1 == c2){
        cout << "c1 == c2" << endl;
    }
    if(c1 != c2){
        cout << "c1 != c2" << endl;
    }
}

我的答案

#include <iostream>
using namespace std;

class Complex {
public:
    Complex(double r = 0, double i = 0) : re(r), im(i) {}       //构造函数,带有默认实参
    Complex& operator += (const Complex& x);
    Complex& operator -= (const Complex& x);
    Complex& operator *= (const Complex& x);
    Complex& operator /= (const Complex& x);
    double real() const { return re; };
    double imag() const { return im; };
private:
    double re, im;

    friend Complex& _doapl(Complex* ths, const Complex& x);
    friend Complex& _doami(Complex* ths, const Complex& x);
    friend Complex& _doaml(Complex* ths, const Complex& x);
    friend Complex& _cdy(Complex* ths, const Complex& x);
    friend istream& operator >> (istream& is, Complex& x);    //因为>>操作要改变对象的数据,所以必须获得访问对象private区的权力
};

/*三个友元函数,它们访问了x的私有成员,所以这三个函数必须声明为Complex的友元*/
inline Complex& _doapl(Complex* ths, const Complex& x)
{
    ths->re += x.re;
    ths->im += x.im;
    return *ths;
}
inline Complex& _doami(Complex* ths, const Complex& x)
{
    ths->re -= x.re;
    ths->im -= x.im;
    return *ths;
}
inline Complex& _doaml(Complex* ths, const Complex& x) //复数的乘法不是简单的对应相乘!
{
    double temp = ths->re * x.re - ths->im * x.im;     //后面得到虚部会改变ths->re的值,所以需要临时变量temp来存放结果的re
    ths->im = ths->re * x.im + ths->im * x.re;
    ths->re = temp;
    return *ths;
}
inline Complex& _cdy(Complex* ths, const Complex& x) //复数的除法不是简单的对应相除!
{
    double tmp1 = ths->re;
    double tmp2 = ths->im;
    ths->re = (tmp1*x.re+tmp2*x.im)/(x.re*x.re+x.im*x.im);
    ths->im = (tmp2*x.re-tmp1*x.im)/(x.re*x.re+x.im*x.im);
    return *ths;
}
/*获得x的实部和虚部的(非成员)函数*/
inline double real(const Complex x)
{
    return x.real();
}
inline double imag(const Complex x)
{
    return x.imag();
}
/*正负号函数*/
inline Complex operator + (const Complex& x)
{
    return x;
}
inline Complex operator - (const Complex& x)
{
    return (-real(x), -imag(x));
}
/*+=、-=、*=、/=操作符重载函数(成员函数)*/
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 _cdy(this, x);
}
/*+、-(加减法)函数*/
inline Complex operator + (const Complex& x, const Complex& y)
{
    return Complex(real(x) + real(y), imag(x) + imag(y));
}
inline Complex operator + (const Complex& x, double y)
{
    return Complex(real(x) + y, imag(x));
}
inline Complex operator + (double x, const Complex& y)
{
    return Complex(x + real(y), imag(y));
}
inline Complex operator - (const Complex& x, const Complex& y)
{
    return Complex(real(x) - real(y), imag(x) - imag(y));
}
inline Complex operator - (const Complex& x, double y)
{
    return Complex(real(x) - y, imag(x));
}
inline Complex operator - (double x, const Complex& y)
{
    return Complex(x - real(y), -imag(y));
}


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

inline Complex operator / (const Complex& x, const Complex& y)
{
    return Complex((real(x)*real(y)+imag(x)*imag(y))/(real(y)*real(y)+imag(y)*imag(y)),(imag(x)*real(y)-real(x)*imag(y))/(real(y)*real(y)+imag(y)*imag(y)));
}


/*==、!=函数*/
inline bool operator == (const Complex& x, const Complex& y)
{
    return (real(x) == real(y) && imag(x) == imag(y));
}
inline bool operator == (const Complex& x, double y)
{
    return (real(x) == real(y) && imag(x) == 0);
}
inline bool operator == (double x, const Complex& y)
{
    return (x == real(y) && imag(y) == 0);
}
inline bool operator != (const Complex& x, const Complex& y)
{
    return (real(x) != real(y) || imag(x) != imag(y));
}
inline bool operator != (const Complex& x, double y)
{
    return (real(x) != y || imag(x) != 0);
}
inline bool operator != (double x, const Complex& y)
{
    return (x != real(y) || imag(y) != 0);
}
/*<<的重载函数*/
inline ostream& operator << (ostream& os, const Complex& x)
{
    os << '(' << real(x) << ',' << imag(x) << ')';
    return os;
}
/*>>的重载函数*/
inline istream& operator >> (istream& is, Complex& x)
{
    is >> x.re >> x.im;
    return is;
}
/*共轭复数函数conj*/
inline Complex conj(const Complex& x)
{
    return Complex(real(x), -imag(x));
}
/*复数求模函数hypot*/
#include <cmath>
inline double hypot(const Complex& x)
{
    return sqrt(real(x) * real(x) + imag(x) * imag(x));
}

//魔改自csdn: https://blog.csdn.net/chaoxiansheng91/article/details/115978459

上一篇: 9.4 (C++) 下一篇: 9.2 (C++)
支持 makedown语法