永发信息网

C++ 编写复数类Complex。

答案:1  悬赏:10  手机版
解决时间 2021-04-02 05:56
C++ 编写复数类Complex。
最佳答案
#include "iostream.h"
class complex
{
public:
complex()
{
real=imag=0.0;
}
complex(double r)
{
real=r;
imag=0.0;
}
complex(double r,double i)
{
real=r;
imag=i;
}
complex(complex&cp)
{
real=cp.real;
imag=cp.imag;
}
friend complex operator+(const complex&c1,const complex&c2);
friend complex operator-(const complex&c1,const complex&c2);
inline complex operator+=(const complex&c);
inline complex operator-=(const complex&c);
void print();
private:
double real,imag;
};

complex operator+(const complex&c1,const complex&c2)
{
return complex(c1.real+c2.real,c1.imag+c2.imag);
}

complex operator-(const complex&c1,const complex&c2)
{
return complex(c1.real-c2.real,c1.imag-c2.imag);
}

inline complex complex::operator+=(const complex&c)
{
return(real+c.real,imag+c.imag);
}
inline complex complex::operator-=(const complex&c)
{
return(real-c.real,imag-c.imag);
}
void complex::print()
{
if (imag<0)
{
cout< }
else
{
cout< }
}
int main()
{
complex c1(1, 2);// 定义一个值为1 + 2i的复数c1
complex c2(2);// 定义一个值为2的复数c2
complex c3(c1);// 用拷贝构造函数创建一个值同c1的新复数

c3.print();// 打印c3的值
c1=c1 + c2 + c3; // 将c1加上c2再加上c3赋值给c1
c2-=c3; // c2等于c3求负
c3 = c2 - c1;// c3等于c2减去c1
c3.print(); // 再打印运算后c3的值
return 0;
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
昆山有哪些比较好的酒店
什么是人工晶体,它是由哪些材料制成的?
2018惠州国企面试应该怎么准备?
笔记本集成显卡进魔兽世界一会就黑屏但是没死
人与人之间的情感,真的是麻烦出来的吗
佛教里的菠萝密什么意思
“为什么 为什么这个世界上要有神仙
自行车如何实现前轮滑?
请教几个问题:POM在注塑时直接加10%的玻纤可
哪个景区需要实景马术特技表演,我是专业做这
我怎么进不去珍爱网
求hp同人文如你所愿
求指点程序“两个整数按位与”第二个答案中为
会计毕业了可以干什么工作
短的怎么拼?
推荐资讯
我大学毕业的那一年家里曾经受人之托给我说了
服用草乌甲素片多长时间为一个疗程 用法用量
一件你和老师之间令你难忘的事
在捷信借12000元,6个还了5100多元还要还1800
女孩子一般最喜欢什么.最希望男孩子为她做什
炉石传说客户端安装时显示在尝试一项启动时出
“三三三运动”是的具体含义是什么?
1500W即热式电热水器有吗?
怎样薰艾条
Thelittleboyoftenmakeshismother________him
春雨惊春清谷天,夏满的意思
2015上汽大通气囊电脑板在那个位置
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?