注册 登录
编程论坛 C++教室

关于友元函数的一个问题

初学者1859 发布于 2006-12-18 22:27, 593 次点击
今天遇到一道习题:
用友元函数实现一个完善的复数类,重载+,-,/,=,+=,-=,*=,/=,并使其可以和double型数据混合运算并画出类图。
希望各位能给解答一下。
8 回复
#2
smartwind2006-12-19 11:13
你自己做一下,有错误再来问好了
#3
初学者18592006-12-19 11:50
我要是会做,就做了阿。就是不会做,才问的。
希望有好心的朋友帮个忙,就当自己练习一下。
谢谢
#4
tancui2006-12-19 12:53

你不会自己整啊

#5
maoguoqing2006-12-19 13:04

给你个例子吧。。没有实现完你的功能

////////////////////////////////////////////////////////////
//
// 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;
}


#6
初学者18592006-12-19 19:32
你可以不帮忙,但是请不要在这里指责别人。谢谢
#7
初学者18592006-12-19 19:35
以下是引用tancui在2006-12-19 12:53:28的发言:

你不会自己整啊

你可以不帮忙,但是请不要在这里指责别人。因为你不可能什么都会,你也会有疑问吧?
如果别人用这句话回复你,你会是怎样的心情?

#8
初学者18592006-12-19 19:39
谢谢,maoguoqing
感谢你的帮助
#9
初学者18592006-12-20 16:29

运行了一下,有一个错误:
--------------------Configuration: 456 - Win32 Debug--------------------
Compiling...
456.cpp
d:\debug\456\456.cpp(168) : fatal error C1010: unexpected end of file while looking for precompiled header directive
Error executing cl.exe.

456.exe - 1 error(s), 0 warning(s)


是哪里的问题啊?

1