用友元函数实现一个完善的复数类,重载+,-,/,=,+=,-=,*=,/=,并使其可以和double型数据混合运算并画出类图。
希望各位能给解答一下。
给你个例子吧。。没有实现完你的功能
////////////////////////////////////////////////////////////
//
// complex.h:declaration of the CComplex class
//
///////////////////////////////////////////////////////////
#include<iostream>
using namespace std;
class CComplex{
public:
CComplex(double r=0,double i=0);
double GetReal() const;
double GetImag() const;
void SetComplex(double r=0,double i=0);
private:
double real;
double imag;
};
//////////////////////////////////////////////////////////////////////////////
// declearation overload the operators
//////////////////////////////////////////////////////////////////////////////
CComplex operator + (const CComplex &complex1,const CComplex &complex2);
CComplex operator - (const CComplex &complex1,const CComplex &complex2);
CComplex operator * (const CComplex &complex1,const CComplex &complex2);
CComplex operator / (const CComplex &complex1,const CComplex &complex2);
istream &operator >> (istream &in,CComplex &complex);
ostream &operator << (ostream &out,const CComplex &complex);
//////////////////////////////////////////////////////////////
//
// complex.cpp:implement the CComplex class
//
/////////////////////////////////////////////////////////////
#include "complex.h"
CComplex::CComplex(double r,double i)
{
real = r;
imag = i;
}
double CComplex::GetImag() const
{
return imag;
}
double CComplex::GetReal() const
{
return real;
}
void CComplex::SetComplex(double r,double i)
{
real = r;
imag = i;
}
//////////////////////////////////////////////////////////////////////
// implement the overload operators
/////////////////////////////////////////////////////////////////////
CComplex operator + (const CComplex &complex1,const CComplex &complex2)
{
double real,imag;
real = complex1.GetReal()+complex2.GetReal();
imag = complex1.GetImag()+complex2.GetImag();
return CComplex(real,imag);
}
CComplex operator - (const CComplex &complex1,const CComplex &complex2)
{
double real,imag;
real = complex1.GetReal()-complex2.GetReal();
imag = complex1.GetImag()-complex2.GetImag();
return CComplex(real,imag);
}
CComplex operator * (const CComplex &complex1,const CComplex &complex2)
{
double real,imag;
real = complex1.GetReal()*complex2.GetReal()
- complex1.GetImag()*complex2.GetImag();
imag = complex1.GetReal()*complex2.GetImag()
+ complex1.GetImag()*complex2.GetReal();
return CComplex(real,imag);
}
CComplex operator / (const CComplex &complex1,const CComplex &complex2)
{
double real,imag;
if (0 == complex2.GetReal() && 0 == complex2.GetImag())
throw "0 can not be divisor!";
double denominator = complex2.GetReal()*complex2.GetReal()
+complex2.GetImag()*complex2.GetImag();
real = (complex1.GetReal()*complex2.GetReal()
+ complex1.GetImag()*complex2.GetImag())/denominator;
imag = (-complex1.GetReal()*complex2.GetImag()
+complex1.GetImag()*complex2.GetReal())/denominator;
return CComplex(real,imag);
}
istream &operator >> (istream &in,CComplex &complex)
{
char chI,chSign;
double real=1,imag=1;
in>>real;
if((chSign = getchar()))
{
//the real part is 0
if ('i' == chSign)
{
imag = real;
real = 0;
complex = CComplex(real,imag);
return in;
}
else if('\n' == chSign || ' ' == chSign)
{
//the imag part is 0
imag = 0;
complex = CComplex(real,imag);
return in;
}
}
//wrong input
if('+' != chSign && '-' != chSign ) throw "wrong input!";
//the two parts are not 0
in>>imag>>chI;
if ('-' == chSign) imag = -imag;
complex = CComplex(real,imag);
return in;
}
ostream &operator << (ostream &out,const CComplex &complex)
{
if (0 != complex.GetReal()) out<<complex.GetReal();
if (complex.GetImag() > 0 && complex.GetReal() != 0) out<<'+';
if (0 != complex.GetImag() && 1 != complex.GetImag()
&& -1 != complex.GetImag()) out<<complex.GetImag()<<'i';
if (1 == complex.GetImag()) out<<'i';
if (-1 == complex.GetImag()) out<<"-i";
if (0 == complex.GetReal() && 0 == complex.GetImag()) cout<<'0';
return out;
}