大虾们、这个C++怎么编啊、我是个菜鸟、 定义一个负数类Complex,使得下面代码能够工作、

代码:Complexc1(3,5);//用复数3+5i初始化c1Complexc2=4.5;//用实数4.5初始化c2c1.add(c2);//将c1和c2相加,结果保存... 代码:
Complex c1(3,5); //用复数3+5i初始化c1
Complex c2=4.5; //用实数4.5初始化c2
c1.add(c2); //将c1和c2相加,结果保存在c1中
c1.show(); //将c1输出(这时的结果应该是 7.5+5i)
展开
 我来答
匿名用户
2011-04-01
展开全部
;

using namespace std;

class CComplex
{
private:
float real, image;
public:
CComplex()
CComplex( float r, float 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;

}
另外,虚机团上产品团购,超级便宜
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2011-04-01
展开全部
这是C++的作业吧?!
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
burrenkiss
2011-03-29 · 超过13用户采纳过TA的回答
知道答主
回答量:47
采纳率:0%
帮助的人:37.3万
展开全部
#include<iostream>
using namespace std;

class Complex
{
public:
Complex(double real);
Complex(double real, double imag);
~Complex();

void add(Complex a);
void show();
private:
double m_real;
double m_imag;
};

Complex::Complex(double real)
{
m_real = real;
m_imag = 0;
}

Complex::Complex(double real, double imag)
{
m_real = real;
m_imag = imag;
}
Complex::~Complex()
{}

void Complex::add(Complex a)
{
this->m_real += a.m_real;
this->m_imag += a.m_imag;
}

void Complex::show()
{
cout<<m_real<<"+"<<m_imag<<"i"<<endl;
}

void main()
{
Complex c1(3, 5);
Complex c2 = 4.5;
c1.add(c2);
c1.show();

system("pause");

}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式