#include<iostream>
using std::ostream;
using std::cout;
using std::endl;
#include<cstdlib>
#include<cmath>
template< class T>
class complex
{
template< class T>
friend ostream &operator<<(ostream &, const complex &);
public:
complex(T r,T i);
complex operator+(const complex &)const;
complex operator-(const complex &)const;
complex operator*(const complex &)const;
complex operator/(const complex &)const;
const complex &operator=(const complex &);
const complex &operator+=(const complex &);
const complex &operator-=(const complex &);
const complex &operator*=(const complex &);
const complex &operator/=(const complex &);
complex &operator++();
complex const operator++(int);
complex &operator--();
complex const operator--(int);
const bool operator == (const complex &)const;
const bool operator != (const complex &)const;
private:
T real;
T imaginary;
};
template< class T>
complex<T>::complex(T r, T i):real(r), imaginary(i)//构造函数
{
}
template< class T>
complex<T> complex<T>::operator+(const complex<T> &operand2)const //重载操作符+
{
return complex(real+operand2.real, imaginary+operand2.imaginary);
}
template< class T>
complex<T> complex<T>::operator-(const complex &operand2)const //重载操作符-
{
return complex(real-operand2.real, imaginary-operand2.imaginary);
}
template< class T>
complex<T> complex<T>::operator*(const complex &operand2)const //重载操作符*
{
return complex(real*operand2.real-imaginary*operand2.imaginary, real*operand2.imaginary+imaginary*operand2.real);
}
template< class T>
complex<T> complex<T>::operator/(const complex &operand2)const //重载操作符/
{
T t;
t=operand2.real*operand2.real+operand2.imaginary*operand2.imaginary;
return complex((real*operand2.real+imaginary*operand2.imaginary)/t,
(imaginary*operand2.real-real*operand2.imaginary)/t);
}
template< class T>
const complex<T>& complex<T>::operator=(const complex &operand2)//重载操作符=
{
real=operand2.real;
imaginary=operand2.imaginary;
return *this;
}
template< class T>
const complex<T>& complex<T>::operator+=(const complex &operand2)//重载操作符+=
{
real+=operand2.real;
imaginary+=operand2.imaginary;
return *this;
}
template< class T>
const complex<T>& complex<T>::operator-=(const complex &operand2)//重载操作符-=
{
real-=operand2.real;
imaginary-=operand2.imaginary;
return *this;
}
template< class T>
const complex<T>& complex<T>::operator*=(const complex &operand2)//重载操作符*=
{
real=real*operand2.real-imaginary*operand2.imaginary;
imaginary=real*operand2.imaginary+imaginary*operand2.real;
return *this;
}
template< class T>
const complex<T>& complex<T>::operator/=(const complex &operand2)//重载操作符/=
{
T t;
t=operand2.real*operand2.real+operand2.imaginary*operand2.imaginary;
real=(real*operand2.real+imaginary*operand2.imaginary)/t;
imaginary=(imaginary*operand2.real-real*operand2.imaginary)/t;
return *this;
}
template< class T>
complex<T>& complex<T>::operator++()//重载前缀自增
{
real++; //先对对象各数据成员增1
imaginary++;
return *this; //再返回该对象
}
template< class T>
complex<T> const complex<T>::operator++(int i)//重载后缀自增
{
complex copy(real, imaginary); //先创建一个临时对象保存原对象的值
++(*this); //再对对象各数据成员增1
return copy; //最后返回该对象
}
template< class T>
complex<T>& complex<T>::operator--()//重载前缀自减
{
real--; //先对对象各数据成员增1
imaginary--;
return *this; //再返回该对象
}
template< class T>
complex<T> const complex<T>::operator--(int i)//重载后缀自减
{
complex copy(real, imaginary); //先创建一个临时对象保存原对象的值
--(*this); //再对对象各数据成员增1
return copy; //最后返回该对象
}
template< class T>
const bool complex<T>::operator == (const complex &operand2)const //重载操作符 ==
{
if(real == operand2.real && imaginary == operand2.imaginary)//当且仅当对象的实部与虚部都对应相等时,对象相等
return true;
else
return false;
}
template< class T>
const bool complex<T>::operator != (const complex &operand2)const //重载操作符 !=
{
return !(*this == operand2); //操作符 != 与 == 取值相反
}
template< class T>
ostream &operator<<(ostream &output, const complex<T> &operand) //重载操作符<< ,以a+bi形式输出
{
if(operand.real) //实部为0时不打印实部
{
cout << operand.real;
if(operand.imaginary<0) //虚部为负值时
cout << operand.imaginary << "i";
else //虚部为正值时
cout << "+" << operand.imaginary << "i";
}
else
cout << operand.imaginary << "i";
return output;
}
int main()
{
complex<int> c1(9, 7), c2(7, 9);
cout << " c1 = " << c1 << endl;
cout << " c2 = " << c2 << " \n " << endl;
cout << " c1 + c2 = ";
complex<int> c3 = c1 + c2;
cout << c3 << endl;
cout << " c1 - c2 = ";
c3 = c1 - c2;
cout << c3 << " \n " << endl;
cout << " c3 = ";
cout << c3 << endl;
cout << " \n ";
cout << "c4 = ++c3: " << endl;
complex<int> c4= ++c3;
if(c4 == c3)
cout << " c4等于c3 " << endl;
else
cout << " c4不等于c3" << endl;
cout << " c4 = ";
cout << c4 << endl;
cout << " c3 = ";
cout << c3 << " \n " << endl;
cout << " c4 = c3++ : " << endl;
c4 = c3++;
cout << c3 << " \n " << endl;
c4 = c3++;
if(c4 != c3)
cout << " c4不等于c3 " << endl;
else
cout << " c4等于c3 " << endl;
cout << " c4= ";
cout << c4 << endl;
cout << " c3= ";
cout << c3 << " \n " << endl;
cout << " c4 = --c3 :" << endl;
c4 = --c3;
cout << " c4= ";
cout << c4 << endl;
cout << " c3= ";
cout << c3 << " \n " << endl;
cout << " c4 = c3-- :" << endl;
c4 = c3--;
cout << " c4 = ";
cout << c4 << endl;
cout << " c3 = ";
cout << c3 << " \n " << endl;
cout << " c1 * c2 = ";
c3 = c1 * c2;
cout << c3 << endl;
cout << " c1 / c2 = ";
c3 = c1 / c2;
cout << c3 << " \n " << endl;
system("pause");
return 0;
}
c1 = 9+7i
c2 = 7+9i
c1 + c2 = 16+16i
c1 - c2 = 2-2i
c3 = 2-2i
c4 = ++c3:
c4等于c3
c4 = 3-1i
c3 = 3-1i
c4 = c3++ :
4+0i
c4不等于c3
c4= 4+0i
c3= 5+1i
c4 = --c3 :
c4= 4+0i
c3= 4+0i
c4 = c3-- :
c4 = 4+0i
c3 = 3-1i
c1 * c2 = 130i
c1 / c2 = 0i
请按任意键继续. . .
仅供参考。。。