////CComplexT.h
#ifndef CCOMPLEX_T_H
#define CCOMPLEX_T_H
#include "stdafx.h"
template<class _T>
class CComplexT
{
private:
_T real;
_T imag;
public:
void Show();
CComplexT(_T r=0,_T i=0);
CComplexT(CComplexT<_T>& c);
CComplexT<_T> operator +(const CComplexT<_T> &c);
CComplexT<_T> operator -(const CComplexT<_T> &c);
CComplexT<_T> operator *(const CComplexT<_T> &c);
CComplexT<_T> operator /(const CComplexT<_T> &c);
void operator +=(const CComplexT<_T> &c);
void operator -=(const CComplexT<_T> &c);
void operator *=(const CComplexT<_T> &c);
void operator /=(const CComplexT<_T> &c);
void operator =(const CComplexT<_T> &c);
bool operator ==(const CComplexT<_T> &c);
};
#include "stdafx.h"
#include "CComplexT.h"
#include "iostream"
using namespace std;
template<class _T>
CComplexT<_T>::CComplexT(_T r,_T i)
{
real=r;
imag=i;
}
template<class _T>
CComplexT<_T>::CComplexT(CComplexT<_T>& c)
{
real=c.real;
imag=c.imag;
}
template<class _T>
CComplexT<_T> CComplexT<_T>::operator +(const CComplexT<_T> &c)
{
CComplexT<_T> temp;
temp.real=real+c.real;
temp.imag=imag+c.imag;
return temp;
}
template<class _T>
CComplexT<_T> CComplexT<_T>::operator -(const CComplexT<_T> &c)
{
CComplexT<_T> temp;
temp.real=real-c.real;
temp.imag=imag-c.imag;
return temp;
}
template<class _T>
CComplexT<_T> CComplexT<_T>::operator *(const CComplexT<_T> &c)
{
CComplexT<_T> temp;
temp.real =real*c.real-imag*c.imag;//ac-bd
temp.imag =imag*c.real +real*c.imag;//bc+ad
return temp;
}
template<class _T>
CComplexT<_T> CComplexT<_T>::operator /(const CComplexT<_T> &c)
{
CComplexT<_T> temp;
temp.real=((real*c.real)+(imag*c.imag))/(c.real*c.real+c.imag*c.imag);
temp.imag=((imag*c.real)-(real*c.imag))/(c.real*c.real+c.imag*c.imag);
return temp;
}
template<class _T>
void CComplexT<_T>::operator +=(const CComplexT<_T> &c)
{
real+=c.real;
imag+=c.imag;
}
template<class _T>
void CComplexT<_T>::operator -=(const CComplexT<_T> &c)
{
real-=c.real;
imag-=c.imag;
}
template<class _T>
void CComplexT<_T>::operator *=(const CComplexT<_T> &c)
{
real =real*c.real-imag*c.imag;//ac-bd
imag =imag*c.real +real*c.imag;//bc+ad
}
template<class _T>
void CComplexT<_T>::operator /=(const CComplexT<_T> &c)
{
temp.real=((real*c.real)+(imag*c.imag))/(c.real*c.real+c.imag*c.imag);
temp.imag=((imag*c.real)-(real*c.imag))/(c.real*c.real+c.imag*c.imag);
}
template<class _T>
void CComplexT<_T>::operator =(const CComplexT<_T> &c)
{
real=c.real;
imag=c.imag;
}
template<class _T>
bool CComplexT<_T>::operator ==(const CComplexT<_T> &c)
{
if(this->real=c.real&&this->imag ==c.imag)
return true;
else return false;
}
template<class _T>
void CComplexT<_T>::Show()
{
cout<<real<<"+"<<imag<<"i"<<endl;
}
#endif
// CComplexT.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "CComplexT.h"
#include "iostream"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
CComplexT<int> c1(3,6);
CComplexT<int> c2(123,442);
CComplexT<double> c3(1.1,1.2);
CComplexT<double> c4(2.5,3.2);
CComplexT<int> cr1 =c1+c2;
//+
std::cout<<"c1+c2="<<std::endl;
cr1.Show();
CComplexT<double> cr2 =c3+c4;
std::cout<<std::endl;
std::cout<<"c3+c4="<<std::endl;
cr2.Show();
//-
cr1 =c1-c2;
std::cout<<"c1-c2="<<std::endl;
cr1.Show();
cr2 =c3-c4;
std::cout<<std::endl;
std::cout<<"c3-c4="<<std::endl;
cr2.Show();
//*
cr1 =c1*c2;
std::cout<<"c1*c2="<<std::endl;
cr1.Show();
cr2 =c3*c4;
std::cout<<std::endl;
std::cout<<"c3*c4="<<std::endl;
cr2.Show();
///
cr1 =c1/c2;
std::cout<<"c1/c2="<<std::endl;
cr1.Show();
cr2 =c3/c4;
std::cout<<std::endl;
std::cout<<"c3/c4="<<std::endl;
cr2.Show();
//+=
CComplexT<int> cr3 =c1-c2;
c1 -=c2;
std::cout<<"c1-=c2"<<std::endl;
c1.Show();
if (c1 == cr3)
{
std::cout<<"c1 the same as cr3";
}
system("pause");
return 0;
}
//其实可以重载<<和>>来模拟输入输出,这样会更简单管理些
//为了交作业,草草写的