C++实现复数四则运算

C++实现复数四则运算

ID:38117910

大小:12.95 KB

页数:3页

时间:2019-06-06

C++实现复数四则运算_第1页
C++实现复数四则运算_第2页
C++实现复数四则运算_第3页
资源描述:

《C++实现复数四则运算》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库

1、#include//usingnamespacestd;classComplex{public:Complex(){real=0;imag=0;}Complex(doubler,doublei):real(r),imag(i){}friendistream&operator>>(istream&,Complex&);//友元函数,用来输入复数friendostream&operator<<(ostream&,Complex&);//友元函数,用来输出复数friendComplexoper

2、ator+(Complex&c1,Complex&c2);//重载加法友元函数声明friendComplexoperator-(Complex&c1,Complex&c2);//重载减法友元函数声明friendComplexoperator*(Complex&c1,Complex&c2);//重载乘法友元函数声明friendComplexoperator/(Complex&c1,Complex&c2);//重载除法友元函数声明private:doublereal;doubleimag;};istream&ope

3、rator>>(istream&input,Complex&c){cout<<"请输入复数的实部:";input>>c.real;cout<<"请输入复数的虚部:";input>>c.imag;returninput;}ostream&operator<<(ostream&output,Complex&c){if(c.real==0&&c.imag==0){output<<0;}elseif(c.real==0&&c.imag!=0){output<

4、&c.imag==0){output<0){output<

5、){returnComplex(c1.real+c2.real,c1.imag+c2.imag);}Complexoperator-(Complex&c1,Complex&c2){returnComplex(c1.real-c2.real,c1.imag-c2.imag);}Complexoperator*(Complex&c1,Complex&c2){returnComplex(c1.real*c2.real-c1.imag*c2.imag,c1.real*c2.imag+c1.imag*c2.real);}

6、Complexoperator/(Complex&c1,Complex&c2){returnComplex((c1.real*c2.real+c1.imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag),(c2.real*c1.imag-c1.real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag));}voidsuan(Complex&c1,Complex&c2,Complex&c3){intflag=1;charc;cout<<"

7、菜单:"<>c;switch(c){case'1':cin>>c1;cout<<"您输入的复数是:"<<

8、c1<>c2;cout<<"您输入的复数是:"<

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。