题目
请编写一个类Complex,定义复数的加法、减法、乘法和除法运算,要求在编写该类时重载这些运算操作符,并重载I/O操作符,
以便输入和输出复数;
按要求写出完整的实验代码,可运行.
以便输入和输出复数;
按要求写出完整的实验代码,可运行.
提问时间:2020-05-22
答案
#include
using namespace std;
class Complex
{public:
Complex(){real=0;imag=0;}
Complex(double r,double i){real=r;imag=i;}
Complex operator+(Complex &c2);
Complex operator-(Complex &c2);
Complex operator*(Complex &c2);
Complex operator/(Complex &c2);
void display();
private:
double real;
double imag;
};
Complex Complex::operator+(Complex &c2)
{Complex c;
c.real=real+c2.real;
c.imag=imag+c2.imag;
return c;}
Complex Complex::operator-(Complex &c2)
{Complex c;
c.real=real-c2.real;
c.imag=imag-c2.imag;
return c;}
Complex Complex::operator*(Complex &c2)
{Complex c;
c.real=real*c2.real-imag*c2.imag;
c.imag=imag*c2.real+real*c2.imag;
return c;}
Complex Complex::operator/(Complex &c2)
{Complex c;
c.real=(real*c2.real+imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);
c.imag=(imag*c2.real-real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);
return c;}
void Complex::display()
{cout
using namespace std;
class Complex
{public:
Complex(){real=0;imag=0;}
Complex(double r,double i){real=r;imag=i;}
Complex operator+(Complex &c2);
Complex operator-(Complex &c2);
Complex operator*(Complex &c2);
Complex operator/(Complex &c2);
void display();
private:
double real;
double imag;
};
Complex Complex::operator+(Complex &c2)
{Complex c;
c.real=real+c2.real;
c.imag=imag+c2.imag;
return c;}
Complex Complex::operator-(Complex &c2)
{Complex c;
c.real=real-c2.real;
c.imag=imag-c2.imag;
return c;}
Complex Complex::operator*(Complex &c2)
{Complex c;
c.real=real*c2.real-imag*c2.imag;
c.imag=imag*c2.real+real*c2.imag;
return c;}
Complex Complex::operator/(Complex &c2)
{Complex c;
c.real=(real*c2.real+imag*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);
c.imag=(imag*c2.real-real*c2.imag)/(c2.real*c2.real+c2.imag*c2.imag);
return c;}
void Complex::display()
{cout
举一反三
我想写一篇关于奥巴马的演讲的文章,写哪一篇好呢?为什么好
奥巴马演讲不用看稿子.为什么中国领导演讲要看?
想找英语初三上学期的首字母填空练习……
英语翻译
1,人们染上烟瘾,最终因吸烟使自己丧命.
最新试题
- 1初中英语作文.Love
- 2铵盐加热能放出气体,铵盐的水溶液加热放气体吗
- 31.小军同学在实验室向一个收集满二氧化碳气体的质地较软的塑料瓶中加入某无色液体A,立即旋紧瓶盖,振荡,观察到瓶子变瘪了,请回答:
- 481(a+b)^2-4(a-b)^2等于多少
- 5小芳今年11岁,爸爸今年37岁,多少年后爸爸的年龄是小芳的3倍?
- 6在体育馆用英语怎么说
- 7一个长方形的游泳池,周长是200米,宽比长少20米,长和宽各多少米?计算歩骤.
- 8已知直线y=x+1与椭圆mx^2+ny^2=1(m>n>0)相交于A,B两点,若弦AB的中点的横坐标为-1/3,则双曲线x^2/m^2-y^
- 9英语作文——my favourite toy
- 10在浩瀚无垠的宇宙,感情真的像一粒灰尘吗?
热门考点
- 1在黑板上写(英文)
- 2语文作文小标题后能写事吗?
- 3This man builds boats for a hobby."for"可不可以用"as"来替换?
- 4干冰(固态的二氧化碳)能作制冷是因为( ).
- 5为什么说地图是地理学的第二语言GIS是地理学第三语言
- 6谁能告诉我3加2减5乘以0等于多少?- -
- 7送春宗璞阅读答案 本文写的是二月兰,为什么以送春为题
- 8LED灯是一种新型的高效节能光源,它的核心元件是发光二极管.二极管由下列哪种材料制成( ) A.陶瓷材料 B.半导体材料 C.金属材料 D.超导材料
- 9They all like travelling and now they are doing some s_______ in Tibet
- 103x的m+5次方乘以y的平方与x的立方乘以y的n次方的和是单项式,则n的m次方=?