首页 >> 严选问答 >

computer的复数形式(定义一个复数类complex重载运算符)

2024-09-18 04:24:37 来源: 用户: 

您好,今天小编胡舒来为大家解答以上的问题。computer的复数形式,定义一个复数类complex重载运算符相信很多小伙伴还不知道,现在让我们一起来看看吧!

1、#include using namespace std;class CComplex{ private: float real, image; public: CComplex() { } CComplex( float r, float img ) { real = r; image = img; } CComplex( CComplex& another ) { real = another.real; image = another.image; } CComplex &operator = (CComplex &another ) { real = another.real; image = another.image; return *this; } CComplex operator +( CComplex & another ) { return CComplex( real+ another.real, image + another.image ); } CComplex operator -( CComplex & another ) { return CComplex( real- another.real, image - another.image ); } CComplex operator *( CComplex & another ) { CComplex prod; //prod = *this; prod.real = real*another.real - image*another.image; prod.image = real*another.image + image*another.real; return prod; //return CComplex( real+ another.real, image + another.image ); } CComplex operator /( CComplex & another ) { CComplex quot; float sq = another.real*another.real + another.image*another.image; quot.real = (real*another.real + image*another.image)/sq; quot.image = (image*another.real - real*another.image)/sq; return quot; }};void main(){ CComplex c1( 2, 3 ), c2( 3, 3 ); CComplex c4, c5, c6, c7; c4 = c1 + c2; c5 = c1 - c2; c6 = c1*c2; c7 = c1/c2;}。

本文就为大家分享到这里,希望小伙伴们会喜欢。

  免责声明:本文由用户上传,与本网站立场无关。财经信息仅供读者参考,并不构成投资建议。投资者据此操作,风险自担。 如有侵权请联系删除!

 
分享:
最新文章