首页 > 编程语言 > C++ 实现一个复数类的实例代码
2021
05-09

C++ 实现一个复数类的实例代码

要求

实现⼀个复数类 ComplexComplex 类包括两个 double 类型的成员 realimage ,分别表示复数的实部和虚部。

Complex 类,重载其流提取、流插⼊运算符,以及加减乘除四则运算运算符。

重载流提取运算符 >> ,使之可以读⼊以下格式的输⼊(两个数值之间使⽤空⽩分隔),将第⼀个数值存为复数的实部,将第⼆个数值存为复数的虚部:

 -1.1 2.0
 +0 -4.5

重载流插⼊运算符 << ,使之可以将复数输出为如下的格式⸺实部如果是⾮负数,则不输出符号位;输出时要包含半⻆左右⼩括号

(-1.1+2.0i)
 (0-4.5i)

每次输⼊两个复数,每个复数均包括由空格分隔的两个浮点数,输⼊第⼀个复数后,键⼊回⻋,然后继续输⼊第⼆个复数。

输出两个复数,每个复数占⼀⾏;复数是由⼩括号包围的形如 (a+bi) 的格式。注意不能输出全⻆括号

样例输⼊

-1.1 2.0
 0 -4.5 

样例输出

(-1.1+2i) (0-4.5i)
(-1.1-2.5i)
(-1.1+6.5i)
(9+4.95i)
(-0.444444-0.244444i)

提示

需要注意,复数的四则运算定义如下所示:

加法法则: ( a + b i ) + ( c + d i ) = ( a + c ) + ( b + d ) i (a + bi) + (c + di) = (a + c) + (b + d)i (a+bi)+(c+di)=(a+c)+(b+d)i减法法则: ( a + b i ) − ( c + d i ) = ( a − c ) + ( b − d ) i (a + bi) − (c + di) = (a − c) + (b − d)i (a+bi)−(c+di)=(a−c)+(b−d)i乘法法则: ( a + b i ) × ( c + d i ) = ( a c − b d ) + ( b c + a d ) i (a + bi) × (c + di) = (ac − bd) + (bc + ad)i (a+bi)×(c+di)=(ac−bd)+(bc+ad)i除法法则: ( a + b i ) ÷ ( c + d i ) = [ ( a c + b d ) / ( c 2 + d 2 ) ] + [ ( b c − a d ) / ( c 2 + d 2 ) ] i (a + bi) ÷ (c + di) = [(ac + bd)/(c^2 + d^2 )] + [(bc − ad)/(c^2 + d^2)]i (a+bi)÷(c+di)=[(ac+bd)/(c2+d2)]+[(bc−ad)/(c2+d2)]i

两个流操作运算符必须重载为 Complex 类的友元函数

此外,在输出的时候,你需要判断复数的虚部是否⾮负⸺例如输⼊ 3 1.0 ,那么输出应该为 3+1.0i 。这⾥向⼤家提供⼀种可能的处理⽅法:使⽤ ostream 提供的 setf() 函数 ⸺它可以设置数值输出的时候是否携带标志位。例如,对于以下代码:

ostream os;
os.setf(std::ios::showpos);
os << 12;

输出内容会是 +12

⽽如果想要取消前⾯的正号输出的话,你可以再执⾏:

os.unsetf(std::ios::showpos);

即可恢复默认的设置(不输出额外的正号)

代码实现

#include <iostream>
using namespace std;

const double EPISON = 1e-7;
class Complex
{
private:
  	double real;
  	double image;
public:
  	Complex(const Complex& complex) :real{ complex.real }, image{ complex.image } {

  	}
  	Complex(double Real=0, double Image=0) :real{ Real }, image{ Image } {

  	}
  	//TODO
    Complex operator+(const Complex c) {
        return Complex(this->real + c.real, this->image + c.image);
    }
    
    Complex operator-(const Complex c) {
        return Complex(this->real - c.real, this->image - c.image);
    }
    
    Complex operator*(const Complex c) {
        double _real = this->real * c.real - this->image * c.image;
        double _image = this->image * c.real + this->real * c.image;
        return Complex(_real, _image);
    }
    
    Complex operator/(const Complex c) {
        double _real = (this->real * c.real + this->image * c.image) / (c.real * c.real + c.image * c.image);
        double _image = (this->image * c.real - this->real * c.image) / (c.real * c.real + c.image * c.image);
        return Complex(_real, _image);
    }
    friend istream &operator>>(istream &in, Complex &c);
    friend ostream &operator<<(ostream &out, const Complex &c);
};

//重载>>
istream &operator>>(istream &in, Complex &c) {
    in >> c.real >> c.image;
    return in;
}

//重载<<
ostream &operator<<(ostream &out, const Complex &c) {
    out << "(";
    //判断实部是否为正数或0
    if (c.real >= EPISON || (c.real < EPISON && c.real > -EPISON)) out.unsetf(std::ios::showpos);
    out << c.real;
    out.setf(std::ios::showpos);
    out << c.image;
    out << "i)";
    return out;
}

int main() {
  	Complex z1, z2;
  	cin >> z1;
  	cin >> z2;
  	cout << z1 << " " << z2 << endl;
  	cout << z1 + z2 << endl;
  	cout << z1 - z2 << endl;
  	cout << z1*z2 << endl;
  	cout << z1 / z2 << endl;
  	return 0;
}

到此这篇关于C++ 实现一个复数类的文章就介绍到这了,更多相关C++ 复数类内容请搜索自学编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持自学编程网!

编程技巧