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++ 復數類內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀:

    None Found