C++ 實現(xiàn)一個復(fù)數(shù)類的實例代碼
要求
實現(xiàn)⼀個復(fù)數(shù)類 Complex
。 Complex
類包括兩個 double
類型的成員 real
和 image
,分別表示復(fù)數(shù)的實部和虛部。
對 Complex
類,重載其流提取、流插⼊運算符,以及加減乘除四則運算運算符。
重載流提取運算符 >>
,使之可以讀⼊以下格式的輸⼊(兩個數(shù)值之間使⽤空⽩分隔),將第⼀個數(shù)值存為復(fù)數(shù)的實部,將第⼆個數(shù)值存為復(fù)數(shù)的虛部:
-1.1 2.0 +0 -4.5
重載流插⼊運算符 <<
,使之可以將復(fù)數(shù)輸出為如下的格式⸺實部如果是⾮負(fù)數(shù),則不輸出符號位;輸出時要包含半⻆左右⼩括號:
(-1.1+2.0i) (0-4.5i)
每次輸⼊兩個復(fù)數(shù),每個復(fù)數(shù)均包括由空格分隔的兩個浮點數(shù),輸⼊第⼀個復(fù)數(shù)后,鍵⼊回⻋,然后繼續(xù)輸⼊第⼆個復(fù)數(shù)。
輸出兩個復(fù)數(shù),每個復(fù)數(shù)占⼀⾏;復(fù)數(shù)是由⼩括號包圍的形如 (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)
提示
需要注意,復(fù)數(shù)的四則運算定義如下所示:
加法法則: ( 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
類的友元函數(shù)
此外,在輸出的時候,你需要判斷復(fù)數(shù)的虛部是否⾮負(fù)⸺例如輸⼊ 3 1.0
,那么輸出應(yīng)該為 3+1.0i
。這⾥向⼤家提供⼀種可能的處理⽅法:使⽤ ostream
提供的 setf()
函數(shù) ⸺它可以設(shè)置數(shù)值輸出的時候是否攜帶標(biāo)志位。例如,對于以下代碼:
ostream os; os.setf(std::ios::showpos); os << 12;
輸出內(nèi)容會是 +12
。
⽽如果想要取消前⾯的正號輸出的話,你可以再執(zhí)⾏:
os.unsetf(std::ios::showpos);
即可恢復(fù)默認(rèn)的設(shè)置(不輸出額外的正號)
代碼實現(xiàn)
#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 << "("; //判斷實部是否為正數(shù)或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; }
到此這篇關(guān)于C++ 實現(xiàn)一個復(fù)數(shù)類的文章就介紹到這了,更多相關(guān)C++ 復(fù)數(shù)類內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++右值引用與移動構(gòu)造函數(shù)基礎(chǔ)與應(yīng)用詳解
左值和右值都是針對表達(dá)式,左值是指表達(dá)式結(jié)束后依然存在的持久對象,右值是指表達(dá)式結(jié)束時就不再存在的臨時對象,下面這篇文章主要給大家介紹了關(guān)于C++11右值引用和移動語義的相關(guān)資料,需要的朋友可以參考下2023-02-02QT利用QPdfWriter實現(xiàn)繪制PDF(支持表單輸出)
這篇文章主要為大家詳細(xì)介紹了QT如何利用QPdfWriter實現(xiàn)繪制PDF,并可以支持表單輸出。文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-01-01