溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C++怎么實現一個復數類

發(fā)布時間:2021-04-19 10:30:25 來源:億速云 閱讀:147 作者:小新 欄目:開發(fā)技術

這篇文章主要介紹了C++怎么實現一個復數類,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

要求

實現?個復數類 ComplexComplex 類包括兩個 double 類型的成員 realimage ,分別表示復數的實部和虛部。

Complex 類,重載其流提取、流插?運算符,以及加減乘除四則運算運算符。

重載流提取運算符 >> ,使之可以讀?以下格式的輸?(兩個數值之間使?空?分隔),將第?個數值存為復數的實部,將第?個數值存為復數的虛部:

 -1.1 2.0
 +0 -4.5

重載流插?運算符 << ,使之可以將復數輸出為如下的格式?實部如果是?負數,則不輸出符號位;輸出時要包含半?左右?括號

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

每次輸?兩個復數,每個復數均包括由空格分隔的兩個浮點數,輸?第?個復數后,鍵?回?,然后繼續(xù)輸?第?個復數。

輸出兩個復數,每個復數占??;復數是由?括號包圍的形如 (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 。

?如果想要取消前?的正號輸出的話,你可以再執(zhí)?:

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++怎么實現一個復數類”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業(yè)資訊頻道,更多相關知識等著你來學習!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

c++
AI