65,211
社区成员
发帖
与我相关
我的任务
分享
#include<iostream.h>
template <class T>
class complex
{
private:
T real;
T imag;
public:
complex(T r=0,T 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);
complex operator*(const complex &a);
complex operator/(const complex &a);
friend complex operator++(complex &a);
complex operator++(int);
friend ostream& operator<<(ostream &s,const complex &c);
friend istream& operator>>(istream &s,complex &c);
};
template <class T>
void complex<T>::print()
{
cout<<real;
if(imag)
{
if(imag>0)cout<<"+";
cout<<imag<<"i";
cout<<endl;
}
}
template <class T>
complex<T> operator+(const complex<T> &a,const complex<T> &b)
{
complex<T> temp;
temp.real=a.real+b.real;
temp.imag=a.imag+b.imag;
return temp;
}
template <class T>
complex<T> operator-(const complex<T> &a,const complex<T> &b)
{
complex<T> temp;
temp.real=a.real-b.real;
temp.imag=a.imag-b.imag;
return temp;
}
template <class T>
complex<T> complex<T>::operator*(const complex<T> &a)
{
complex<T> temp;
temp.real=a.real*real-a.imag*imag;
temp.imag=a.real*imag+real*a.imag;
return temp;
}
template <class T>
complex<T> complex<T>::operator/(const complex<T> &a)
{
complex<T> temp;
temp.real=(real*a.real+a.imag*imag)/(a.real*a.real+a.imag*a.imag);
temp.imag=(real*(-a.imag)+imag*a.real)/(a.real*a.real+a.imag*a.imag);
return temp;
}
template <class T>
complex<T> operator++(complex<T> &a)
{
++a.real;
++a.imag;
return a;
}
template <class T>
complex<T> complex<T>::operator++(int)
{
complex<T> temp(*this);
real++;
imag++;
return temp;
}
/*template <class T>
ostream& operator<<(ostream &s,const complex<T> &c)
{
s<<c.real;
if(c.imag)
{
if(c.imag>0) s<<"+";
s<<c.imag<<"i";
s<<endl;
}
return s;
}*/
template <class T>
istream& operator>>(istream &s,complex<T> &c)
{
s>>c.real;
s>>c.imag;
return s;
}
int main()
{
complex <double> A1(2.3,4.6),A2(3.6,2.8);
complex <double> A3,A4,A5,A6;
A3=A1+A2;
A4=A1-A2;
A5=A1*A2;
A6=A1/A2;
cout<<"A1=";
cout<<A1;
cout<<endl<<"A2=";
cout<<A2;
cout<<endl<<"A3=A1+A2=";
cout<<A3;
cout<<endl<<"A4=A1-A2=";
cout<<A4;
cout<<endl<<"A5=A1*A2=";
cout<<A5;
cout<<endl<<"A6=A1/A2=";
cout<<A6;
A3=++A1;
cout<<endl<<"after A3=++A1"<<endl;
cout<<"A1=";
cout<<A1;
cout<<endl<<"A3=";
cout<<A3;
A4=A2++;
cout<<endl<<"after A4=A2++"<<endl;
cout<<"A2=";
cout<<A2;
cout<<endl<<"A4=";
cout<<A4;
return 0;
}
#include<iostream.h>
template <class T>
class complex
{
private:
T real;
T imag;
public:
complex(T r=0,T i=0)
{
real=r;
imag=i;
}
void print(ostream &s);
friend complex operator+(const complex &a,const complex &b);
friend complex operator-(const complex &a,const complex &b);
complex operator*(const complex &a);
complex operator/(const complex &a);
friend complex operator++(complex &a);
complex operator++(int);
friend ostream& operator<<(ostream &s,const complex &c);
friend istream& operator>>(istream &s,complex &c);
};
template <class T>
void complex<T>::print(ostream &s)
{
s<<real;
if(imag)
{
if(imag>0)s<<"+";
s<<imag<<"i";
s<<endl;
}
}
template <class T>
complex<T> operator+(const complex<T> &a,const complex<T> &b)
{
complex<T> temp;
temp.real=a.real+b.real;
temp.imag=a.imag+b.imag;
return temp;
}
template <class T>
complex<T> operator-(const complex<T> &a,const complex<T> &b)
{
complex<T> temp;
temp.real=a.real-b.real;
temp.imag=a.imag-b.imag;
return temp;
}
template <class T>
complex<T> complex<T>::operator*(const complex<T> &a)
{
complex<T> temp;
temp.real=a.real*real-a.imag*imag;
temp.imag=a.real*imag+real*a.imag;
return temp;
}
template <class T>
complex<T> complex<T>::operator/(const complex<T> &a)
{
complex<T> temp;
temp.real=(real*a.real+a.imag*imag)/(a.real*a.real+a.imag*a.imag);
temp.imag=(real*(-a.imag)+imag*a.real)/(a.real*a.real+a.imag*a.imag);
return temp;
}
template <class T>
complex<T> operator++(complex<T> &a)
{
++a.real;
++a.imag;
return a;
}
template <class T>
complex<T> complex<T>::operator++(int)
{
complex<T> temp(*this);
real++;
imag++;
return temp;
}
template <class T>
ostream& operator<<(ostream &s,const complex<T> &c)
{
c.print(s);
}
template <class T>
istream& operator>>(istream &s,complex<T> &c)
{
s>>c.real;
s>>c.imag;
return s;
}
int main()
{
complex <double> A1(2.3,4.6),A2(3.6,2.8);
complex <double> A3,A4,A5,A6;
A3=A1+A2;
A4=A1-A2;
A5=A1*A2;
A6=A1/A2;
cout<<"A1=";
cout<<A1;
cout<<endl<<"A2=";
cout<<A2;
cout<<endl<<"A3=A1+A2=";
cout<<A3;
cout<<endl<<"A4=A1-A2=";
cout<<A4;
cout<<endl<<"A5=A1*A2=";
cout<<A5;
cout<<endl<<"A6=A1/A2=";
cout<<A6;
A3=++A1;
cout<<endl<<"after A3=++A1"<<endl;
cout<<"A1=";
cout<<A1;
cout<<endl<<"A3=";
cout<<A3;
A4=A2++;
cout<<endl<<"after A4=A2++"<<endl;
cout<<"A2=";
cout<<A2;
cout<<endl<<"A4=";
cout<<A4;
return 0;
}
/*template <class T>
ostream& operator<<(ostream &s,const complex<T> &c)
{
s<<c.real;
if(c.imag)
{
if(c.imag>0) s<<"+";
s<<c.imag<<"i";
s<<endl;
}
return s;
}*/
/*template <class T>
ostream& operator<<(ostream &s,const complex<T> &c)
{
s<<c.real;
if(c.imag)
{
if(c.imag>0) s<<"+";
s<<c.imag<<"i";
s<<endl;
}
return s;
}*/