最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++ 實現(xiàn)一個復(fù)數(shù)類的實例代碼

 更新時間:2021年04月18日 15:34:00   作者:明朗晨光  
這篇文章主要介紹了C++ 實現(xiàn)一個復(fù)數(shù)類的實例代碼,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

要求

實現(xiàn)⼀個復(fù)數(shù)類 Complex 。 Complex 類包括兩個 double 類型的成員 realimage ,分別表示復(fù)數(shù)的實部和虛部。

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

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

 -1.1 2.0
 +0 -4.5

重載流插⼊運算符 << ,使之可以將復(fù)數(shù)輸出為如下的格式⸺實部如果是⾮負數(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ù)的虛部是否⾮負⸺例如輸⼊ 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ù)默認的設(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)文章

最新評論

阜康市| 商洛市| 宁安市| 马鞍山市| 郸城县| 土默特右旗| 林口县| 泽普县| 厦门市| 鹤壁市| 乃东县| 洞头县| 名山县| 太谷县| 汪清县| 松滋市| 齐河县| 溆浦县| 九龙县| 扶风县| 巴马| 合阳县| 莲花县| 周宁县| 乳山市| 集贤县| 榆林市| 股票| 平遥县| 开封市| 临汾市| 和田市| 朝阳县| 南部县| 衢州市| 东光县| 通渭县| 汝城县| 延安市| 黎城县| 汶上县|