首页 新闻 会员 周边 捐助

以下程序如何修改才能正常运行?

0
悬赏园豆:5 [待解决问题]

#include<iostream.h>
template <class T1,class T2>
class complex
{
public:
 complex(){}
 complex(T1 r,T2 i){real=r;imag=i;}
 complex operator+(complex &c);
 complex operator-(complex &c);
 complex operator*(complex &c);
 complex operator/(complex &c);
 friend ostream& operator<< (ostream &output,complex &c);
private:
    T1 real;
 T2 imag;
};
template <class T1,class T2>
complex<T1,T2>::operator * (complex &c)
{
 return complex(real*real-imag*c.imag,real*c.imag +imag*c.real );
}
template <class T1,class T2>
complex<T1,T2> ::operator /(complex &c)
{
 return complex(real*c.real-imag*c.imag/c.real*c.real+c.imag*c.imag,real*c.imag+imag*c.real/c.real*c.real+c.imag*c.imag);
}
template <class T1,class T2>
complex<T1,T2>::operator-(complex &c)
{
 return complex(real-c.real,imag-c.imag );
}
template<class T1,class T2>
complex<T1,T2> ::operator+(complex &c)
{
 return complex(real+c.real,imag+c.imag);
}
template<class T1,class T2>
ostream& operator<<(ostream &output,complex &c)
{
   if(c.imag<0)
 output<<c.real<<c.imag<<'i'<<endl;
   else
    output<<c.real<<'+'<<c.imag<<'i'<<endl;
 
  
 return output;
}
void main()
{
 complex<int ,int> c1(1,2),c2(2,3),c3;
 c3=c1+c2;
 cout<<c3;
 c3=c1-c2;
 cout<<c3;
 c3=c1*c2;
 cout<<c3;
 c3=c1/c2;
 cout<<c3;
}

C++
foxing的主页 foxing | 初学一级 | 园豆:5
提问于:2012-07-05 18:21
< >
分享
所有回答(1)
0

#include<iostream>
using namespace std;

template <class T1,class T2>
class complex
{
public:
complex(){}
complex(T1 r,T2 i){real=r;imag=i;}
complex<T1, T2> operator+(complex<T1, T2> &c);
complex<T1, T2> operator-(complex<T1, T2> &c);
complex<T1, T2> operator*(complex<T1, T2> &c);
complex<T1, T2> operator/(complex<T1, T2> &c);

friend ostream& operator<< (ostream &output,complex<T1, T2> &c)
{
if(c.imag<0)
output<<c.real<<c.imag<<'i'<<endl;
else
output<<c.real<<'+'<<c.imag<<'i'<<endl;

return output;
}

private:
T1 real;
T2 imag;
};
template <class T1,class T2>
complex<T1, T2> complex<T1,T2>::operator * (complex<T1, T2> &c)
{
return complex(real*real-imag*c.imag,real*c.imag +imag*c.real );
}
template <class T1,class T2>
complex<T1, T2> complex<T1,T2> ::operator /(complex<T1, T2> &c)
{
return complex(real*c.real-imag*c.imag/c.real*c.real+c.imag*c.imag,real*c.imag+imag*c.real/c.real*c.real+c.imag*c.imag);
}
template <class T1,class T2>
complex<T1, T2> complex<T1,T2>::operator-(complex<T1, T2> &c)
{
return complex(real-c.real,imag-c.imag );
}
template<class T1,class T2>
complex<T1, T2> complex<T1,T2> ::operator+(complex<T1, T2> &c)
{
return complex(real+c.real,imag+c.imag);
}


void main()
{
complex<int ,int> c1(1,2),c2(2,3),c3;
c3=c1+c2;
cout<<c3;
c3=c1-c2;
cout<<c3;
c3=c1*c2;
cout<<c3;
c3=c1/c2;
cout<<c3;
}

zgpxgame | 园豆:211 (菜鸟二级) | 2012-07-07 19:54
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册