2015 第9周專案1 複數類中的運算子過載(續)

2021-07-02 14:53:56 字數 2218 閱讀 1659

【專案1-複數類中的運算子過載(續)】

在複數類中的運算子過載

基礎上(1)再定義一目運算子 -,-c相當於0-c。

(2)定義complex類中的《和》運算子的過載,實現輸入和輸出,改造原程式中對運算結果顯示方式,使程式讀起來更自然。

#include using namespace std;

class complex

complex(double r,double i)

complex operator-();

friend complex operator+(complex &c1, complex &c2);

friend complex operator+(double d1, complex &c2);

friend complex operator+(complex &c1, double d2);

friend complex operator-(complex &c1, complex &c2);

friend complex operator-(double d1, complex &c2);

friend complex operator-(complex &c1, double d2);

friend complex operator*(complex &c1, complex &c2);

friend complex operator*(double d1, complex &c2);

friend complex operator*(complex &c1, double d2);

friend complex operator/(complex &c1, complex &c2);

friend complex operator/(double d1, complex &c2);

friend complex operator/(complex &c1, double d2);

friend ostream& operator << (ostream& output,const complex &c);

friend istream& operator >> (istream& input, complex &c);

private:

double real;

double imag;

};ostream &operator<<(ostream &output , const complex &c)

while(!((sign=='+'||sign=='-')&&i=='i'));

c.real=a;

c.imag=(sign=='+')?b:-b;

return input;

}complex complex::operator-()

complex operator+(complex &c1, complex &c2)

complex operator+(double d1, complex &c2)

complex operator+(complex &c1, double d2)

complex operator-(complex &c1, complex &c2)

complex operator-(double d1, complex &c2)

complex operator-(complex &c1, double d2)

//複數相乘:(a+bi)(c+di)=(ac-bd)+(bc+ad)i.

complex operator*(complex &c1, complex &c2)

complex operator*(double d1, complex &c2)

complex operator*(complex &c1, double d2)

//複數相除:(a+bi)/(c+di)=(ac+bd)/(c^2+d^2) +(bc-ad)/(c^2+d^2)i

complex operator/(complex &c1, complex &c2)

complex operator/(double d1, complex &c2)

complex operator/(complex &c1, double d2)

int main()

{ complex c1,c2,c3;

double d=11;

cout<<"c1: "<>c1;

cout<<"c2: "<>c2;

cout<<"c1="<

2015 第8周專案1 實現複數類中的運算子過載1

專案1 實現複數類中的運算子過載 1 請用類的成員函式,定義複數類過載運算子 使之能用於複數的加減乘除 code include include include include include include using namespace std class complex complex dou...

第9周專案1 複數類中的運算子過載(續)

檔名稱 test.cpp 作 者 王元陽 完成日期 2015 年 5 月 13 日 版 本 號 v1.0 問題描述 在複數類中的運算子過載基礎上 1 再定義一目運算子 c相當於0 c。2 定義complex類中的 和 運算子的過載,實現輸入和輸出,改造原程式中對運算結果顯示方式,使程式讀起來更自然。...

C 第9周 春 專案1 複數類

include using namespace std class complex complex double r,double i complex operator 實現輸入 輸出的運算子過載 friend ostream operator ostream output,const comple...