按下列要求创建写程序
(1)创建一个复数类complex,该类数据成员是两个int型变量real和imaginary,以友元函数形式重载提取符和插入符。
(2)在重新定义提取符时加入判断输入数据是否合法的判断。如果出现非法输入时,则设置位以示输入错。输入格式为2+5i
数据可正可负,可只一个数值,未给出的数值为0。输出格式同输入。
(3)编写main()测试上述类complex的定义的提取符和插入符进行输入输出操作。
參考答案://这是我工程里的一个complex.h文件
//////////////////////////////////////////////////////////////////////////
// 类名 : Complex
// 产生时间: 2002/10/16
// 所属文件: complex.h
// 功能 : 对复数进行诸如+,-,*,/,+=,-=,*=,/=操作
//
//////////////////////////////////////////////////////////////////////////
#ifndef COMPLEX_H
#define COMPLEX_H
// 修改时间: 2003年7月24日 21:00
#include <cmath>
class Complex
{
public:
Complex() : _real(0), _imag(0) {}
explicit Complex( double r) : _real(r), _imag(0) {}
Complex(double r, double i) : _real(r), _imag(i) {}
Complex& operator+=(const double& d)
{
_real += d;
return *this;
}
Complex& operator+=(const Complex& c)
{
_real += c._real;
_imag += c._imag;
return *this;
}
Complex& operator-=(const double &d)
{
_real -= d;
return *this;
}
Complex& operator-=(const Complex& c)
{
_real -= c._real;
_imag -= c._imag;
return *this;
}
Complex& operator*=(const double& d)
{
_real *= d;
_imag *= d;
return *this;
}
Complex& operator*=(const Complex& c)
{
double re = _real;
double im = _imag;
_real = re * c._real - im * c._imag;
_imag = re * c._imag + im * c._real;
return *this;
}
Complex& operator/=(const double& d)
{
_real /= d;
_imag /= d;
return *this;
}
Complex& operator/=(const Complex& c)
{
double re = _real;
double im = _imag;
double d = c._real * c._real + c._imag * c._imag;
_real = (re * c._real + im * c._imag) / d;
_imag = (im * c._real - re * c._imag) / d;
return *this;
}
Complex Conj() const
{
return Complex(_real, -_imag);
}
double Real() const { return _real; }
double Imag() const { return _imag; }
void Real(const double& re) { _real = re ; }
void Imag(const double& im) { _imag = im ; }
void Set(const double& re, const double& im){ _real = re; _imag = im ; }
double Modsq() const { return _real*_real + _imag * _imag ; }
double Mod() const { return sqrt(_real*_real + _imag * _imag); }
private:
double _real;
double _imag;
};
inline Complex operator+(const Complex& c)
{
return Complex(c.Real(), c.Imag());
}
inline Complex operator-(const Complex& c)
{
return Complex(-c.Real(), -c.Imag());
}
inline Complex operator+(const Complex& c, const double& d)
{
return Complex(c.Real() + d, c.Imag());
}
inline Complex operator+(const double& d, const Complex& c)
{
return Complex(d + c.Real(), c.Imag());
}
inline Complex operator+(const Complex& c1, const Complex& c2)
{
return Complex(c1.Real() + c2.Real(), c1.Imag() + c2.Imag());
}
inline Complex operator-(const Complex& c, const double& d)
{
return Complex(c.Real() - d, c.Imag());
}
inline Complex operator-(const double& d, const Complex& c)
{
return Complex(d - c.Real(), -c.Imag());
}
inline Complex operator-(const Complex& c1, const Complex& c2)
{
return Complex(c1.Real() - c2.Real(), c1.Imag() - c2.Imag());
}
inline Complex operator*(const Complex& c, const double& d)
{
return Complex(c.Real() * d, c.Imag() * d);
}
inline Complex operator*(const double& d, const Complex& c)
{
return Complex(c.Real() * d, c.Imag() * d);
}
inline Complex operator*(const Complex& c1, const Complex& c2)
{
double real = c1.Real() * c2.Real() - c1.Imag() * c2.Imag();
double imag = c1.Real() * c2.Imag() + c1.Imag() * c2.Real();
return Complex(real, imag);
}
inline Complex operator/(const Complex& c, const double& d)
{
return Complex(c.Real() / d, c.Imag() / d);
}
inline Complex operator/(const double& d, const Complex& c)
{
double dd = c.Real() * c.Real() + c.Imag() * c.Imag();
return Complex((d * c.Real())/dd, (-d * c.Imag())/dd);
}
inline Complex operator/(const Complex& c1, const Complex& c2)
{
double d = c2.Real() * c2.Real() + c2.Imag() * c2.Imag();
double real = (c1.Real() * c2.Real() + c1.Imag() * c2.Imag()) / d;
double imag = (c1.Imag() * c2.Real() - c1.Real() * c2.Imag()) / d;
return Complex(real, imag);
}
inline double real(const Complex &c)
{
return c.Real();
}
inline double imag(const Complex &c)
{
return c.Imag();
}
inline double abs(const Complex &c)
{
return sqrt(c.Real() * c.Real() + c.Imag() * c.Imag());
}
inline double norm(const Complex &c)
{
return c.Real() * c.Real() + c.Imag() * c.Imag();
}
inline Complex conj(const Complex &c)
{
return Complex(c.Real(), -c.Imag());
}
#endif
如果在重载<<,即:
#include <stream>
using namespace std;
ostream& operator<<(ostream &os, const Complex &c)
{
os<<c.Real()<<"+"<<c.Image()<<"i";
return os;
}
如果将
friend ostream& operator<<(ostream &os, const Complex &c);
友员申明加入到complex类中,则函数中的c.Real(),c.Image()可直接使用数据成员代替,即:c._real, c._image;
istream& operator>>(istream &in, Complex &c)
{
char str[20];
cin>>str;
istringstream is(str);
try{
char c,d;
is>>c._real>>c>>b._image>>d;
if((c != '+') && (d != 'i')) exit(1);
}
catch(...){ exit(1)}
return in;
}
在类中加入,
friend istream& operator<<(istream &in, Complex &c);
以及头文件包含
#include <sstream>
还要使用标准的C++,上面程序不能在VC6下编译成功,因为它太老了,不符合ISO C++ 98年的标准!
注意!友员不是一个很好的使用方法,尽量避免使用友员。其实由于Real(), Image()是内联函数,和直接使用数据成员效率一样!可以不必使用友员!