要求
實作?個復數類 Complex , Complex 類包括兩個 double 型別的成員 real 和 image ,分別表示復數的實部和虛部,
對 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;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/277367.html
標籤:其他
上一篇:安卓SplashActivity閃屏頁面開發 kotlin
下一篇:2021-04-15
