[cpp]
#include <iostream>
using namespace std;
template <class numtype>
class Complex
{public:
Complex(){real=0,imag=0;}
Complex(numtype r,numtype i){real=r;imag=i;}
Complex complex_add(Complex );//加法
Complex complex_sub(Complex );//減法
Complex complex_mul(Complex);//乘法
Complex complex_div(Complex);//除法
void display();
private:
numtype real;
numtype imag;
};
template <class numtype>
Complex<numtype> Complex<numtype>::complex_add(Complex<numtype> a)
{
Complex<numtype> c;
c.real=real+a.real;
c.imag=imag+a.imag;
return c;
}
template <class numtype>
Complex<numtype> Complex<numtype>::complex_sub(Complex<numtype> a)
{
Complex<numtype> c;
c.real=real-a.real;
c.imag=imag-a.imag;
return c;
}
template <class numtype>
Complex<numtype> Complex<numtype>::complex_mul(Complex<numtype> a)
{
Complex<numtype> c;
c.real=real*a.real-imag*a.imag;
c.imag=imag*a.real+real*a.imag;
return c;
}
template <class numtype>
Complex<numtype> Complex<numtype>::complex_div(Complex<numtype> a)
{
Complex<numtype> c;
numtype d;
d=a.real*a.real+a.imag+a.imag;
c.real=(real*a.real+imag*a.imag)/d;
c.imag=(imag*a.real-real*a.imag)/d;
return c;
}
template <class numtype>
void Complex<numtype>::display()
{
cout<<real<<","<<imag<<"i"<<endl;
}
int main( )
{
Complex<int> c1(3,4),c2(5,-10),c3;
c3=c1.complex_add(c2);
cout<<"c1+c2=";
c3.display( );
Complex<double> c4(3.1,4.4),c5(5.34,-10.21),c6;
c6=c4.complex_sub(c5);
cout<<"c4-c5=";
c6.display( );
Complex<int> a1(3,4),a2(5,-10),a3;
a3=a1.complex_mul(a2);
cout<<"a1*a2=";
a3.display( );
Complex<double> a4(3.1,4.4),a5(5.34,-10.21),a6;
a6=a4.complex_sub(a5);
cout<<"a4/a5=";
a6.display( );
system("pause");
return 0;
}
運行結果:
#include <iostream>
using namespace std;
template <class numtype>
class Complex
{public:
Complex(){real=0,imag=0;}
Complex(numtype r,numtype i){real=r;imag=i;}
Complex complex_add(Complex );//加法
Complex complex_sub(Complex );//減法
Complex complex_mul(Complex);//乘法
Complex complex_div(Complex);//除法
void display();
private:
numtype real;
numtype imag;
};
template <class numtype>
Complex<numtype> Complex<numtype>::complex_add(Complex<numtype> a)
{
Complex<numtype> c;
c.real=real+a.real;
c.imag=imag+a.imag;
return c;
}
template <class numtype>
Complex<numtype> Complex<numtype>::complex_sub(Complex<numtype> a)
{
Complex<numtype> c;
c.real=real-a.real;
c.imag=imag-a.imag;
return c;
}
template <class numtype>
Complex<numtype> Complex<numtype>::complex_mul(Complex<numtype> a)
{
Complex<numtype> c;
c.real=real*a.real-imag*a.imag;
c.imag=imag*a.real+real*a.imag;
return c;
}
template <class numtype>
Complex<numtype> Complex<numtype>::complex_div(Complex<numtype> a)
{
Complex<numtype> c;
numtype d;
d=a.real*a.real+a.imag+a.imag;
c.real=(real*a.real+imag*a.imag)/d;
c.imag=(imag*a.real-real*a.imag)/d;
return c;
}
template <class numtype>
void Complex<numtype>::display()
{
cout<<real<<","<<imag<<"i"<<endl;
}
int main( )
{
Complex<int> c1(3,4),c2(5,-10),c3;
c3=c1.complex_add(c2);
cout<<"c1+c2=";
c3.display( );
Complex<double> c4(3.1,4.4),c5(5.34,-10.21),c6;
c6=c4.complex_sub(c5);
cout<<"c4-c5=";
c6.display( );
Complex<int> a1(3,4),a2(5,-10),a3;
a3=a1.complex_mul(a2);
cout<<"a1*a2=";
a3.display( );
Complex<double> a4(3.1,4.4),a5(5.34,-10.21),a6;
a6=a4.complex_sub(a5);
cout<<"a4/a5=";
a6.display( );
system("pause");
return 0;
}
運行結果: