問題描述:
在(2)的基礎上,擴充套件+、-、*、/運算子的功能,使之能與double型資料進行運算。 設complex c; double d; c+d和d+c的結果為「將d視為實部為d的複數同c相加」,其他-、*、/運算子類似
程式:
#includeusing namespace std;
class complex
complex(double r,double i)
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);
void display();
private:
double real;
double imag;
};//下面定義成員函式
//複數相加: (a+bi)+(c+di)=(a+c)+(b+d)i
complex operator+(complex &c1,complex &c2)
complex operator+(double d1, complex &c2)
complex operator+(complex &c1, double d2)
//複數相減: (a+bi)-(c+di)=(a-c)+(b-d)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)+(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*c+d*d) +(bc-ad)/(c*c+d*d)i
complex operator/(complex &c1,complex &c2)
complex operator/(double d1, complex &c2)
complex operator/(complex &c1, double d2)
void complex::display()
{ cout
cout<
執行結果:
第十二周專案 1
分別定義teacher 教師 類和cadre 幹部 類,採用多重繼承方式由這兩個類派生出新類teacher cadre 教師兼幹部 要求 1 在兩個基類中都包含姓名 年齡 性別 位址 等資料成員。2 在teacher類中還包含資料成員title 職稱 在cadre類中還包含資料成員post 職務 在...
第十二周專案1
輸入描述 無 輸出描述 圖的基本運算 標頭檔案 ifndef graph h included define graph h included define maxv 100 最大頂點個數 define inf 32767 inf表示 typedef int infotype 以下定義鄰接矩陣型別 ...
第十二周專案2
j 輸入描述 無 輸出描述 描述的輸出內容 include include include graph.h 返回圖g中編號為v的頂點的出度 int outdegree algraph g,int v return n 輸出圖g中每個頂點的出度 void outds algraph g 輸出圖g中出度...