C++ 查错 改编

来源:百度知道 编辑:UC知道 时间:2024/06/04 01:34:06
#include<iostream.h>
class Complex
{private:
float real;
float imag;
public:
Complex(float r=0,float i=0)
{
real=r;
imag=i;
}
void print();
friend Complex operator+(const Complex &a,const Complex &b);
friend Complex operator-(const Complex &a,const Complex &b);
friend Complex operator++(Complex &a);
Complex operator*(const Complex &a);
Complex operator/(const Complex &a);
Complex operator++(int);
friend istream & operator>>(istream & in,Complex & c);
friend ostream & operator>>(ostream & out,const Complex & c);
};
istream & operator>>(istream & in,Complex & s)
{
cout<<"input the real,imag of the complex:\n";
in>>s.real;
in>>s.imag;
return in;
}
ostream & operator<<(ostream & out, Complex & s)
{
out<<s.real;
if(s.imag!=0)
{ if(s.imag&g

1.编程模板类很简单,代码如下:
template <class T>
class Complex
{private:
T real;
T imag;
public:
Complex(T r=0.0,T i=0.0)
{
real=r;
imag=i;
}
void print();
friend Complex operator+(const Complex &a,const Complex &b);
friend Complex operator-(const Complex &a,const Complex &b);
friend Complex operator++(Complex &a);
Complex operator*(const Complex &a);
Complex operator/(const Complex &a);
Complex operator++(int);
friend istream & operator>>(istream & in,Complex & s)
{
cout<<"input the real,imag of the complex:\n";
in>>s.real;
in>>s.imag;
return in;
}
friend ostream & operator<<(ostream & out,const Complex & s)
{
out<<s.real;
if(s.imag!=0)
{ if(s.imag>0) cout<<"+";
out<<s.imag<<"i";
}
out<<endl;