哎,真可怜,工作了,居然还要做作业!!! 两个模板的题目,这是其中一个复数的,第一次学习模板,如有问题,请大家指正 。
/*******************************************************/
CComplexT.h
/********************************************************/
#ifndef CCOMPLEXT_H
#define CCOMPLEXT_H
#include "stdafx.h"
#include <iostream>
using namespace std;
template <typename Type>
class CComplexT
{
public:
CComplexT(Type r=0,Type i=0);
~CComplexT(){};
CComplexT<Type> operator +(CComplexT<Type> other);
CComplexT<Type> operator -(CComplexT<Type> other);
CComplexT<Type> operator *(CComplexT<Type> other);
CComplexT<Type> operator /(CComplexT<Type> other);
void operator =(CComplexT<Type> other);
void printCComplexT();
private:
Type r;
Type i;
};
template<typename Type>
CComplexT<Type>::CComplexT(Type rNum,Type iNum)
//构造函数
{
r=rNum;
i=iNum;
}
template<typename Type>
CComplexT<Type> CComplexT<Type>::operator +(CComplexT<Type> other)
{
CComplexT<Type> temp;
temp.r=r+other.r;
temp.i=i+other.i;
return temp;
}
template<typename Type>
CComplexT<Type> CComplexT<Type>::operator -(CComplexT<Type> other)
{
CComplexT<Type> temp;
temp.r=r-other.r;
temp.i=i-other.i;
return temp;
}
template<typename Type>
CComplexT<Type> CComplexT<Type>::operator *(CComplexT<Type> other)
{
CComplexT<Type> temp;
temp.r=r*other.r-i*other.i;
temp.i=r*other.i+i*other.r;
return temp;
}
template<typename Type>
CComplexT<Type> CComplexT<Type>::operator /(CComplexT<Type> other)
{
CComplexT<Type> temp;
temp.r=((r*other.r)+(i*other.i))/(other.r*other.r+other.i*other.i);
temp.i=((i*other.r)-(r*other.i))/(other.r*other.r+other.i*other.i);
return temp;
}
template<typename Type>
void CComplexT<Type>::operator =(CComplexT<Type> other)
{
r=other.r;
i=other.i;
}
template<typename Type>
void CComplexT<Type>::printCComplexT()
{
if (i>0)
{
cout<<r<<"+"<<i<<"i"<<endl;
}
else if (i<0)
{
cout<<r<<i<<"i"<<endl;
}
else
{
cout<<r<<endl;
}
}
#endif
/**************************************************************/
CComplexT.cpp 测试模板代码
/**************************************************************/
// 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<float> test1(1,2);
//CComplexT<float> test2(3,4);
CComplexT<float> outCT(0,0);
float x,y;
cout<<"*********************************************"<<endl;
cout<<"简易复数模板实现,请按提示操作输入相关数字"<<endl;
cout<<"*********************************************"<<endl;
cout<<endl;
cout<<"输入a的实数部分:"<<endl;
cin>>x;
cout<<"输入a的虚数部分:"<<endl;
cin>>y;
CComplexT<float> test1(x,y);
cout<<"复数 a 为:"<<endl;
test1.printCComplexT();
cout<<"输入b的实数部分:"<<endl;
cin>>x;
cout<<"输入b的虚数部分:"<<endl;
cin>>y;
cout<<"复数 b 为:"<<endl;
CComplexT<float> test2(x,y);
test2.printCComplexT();
cout<<endl;
cout<<"运算结果 : "<<endl;
cout<<"a+b=";
outCT=test1+test2;
outCT.printCComplexT();
cout<<"a-b=";
outCT=test1-test2;
outCT.printCComplexT();
cout<<"a*b=";
outCT=test1*test2;
outCT.printCComplexT();
cout<<"a/b=";
outCT=test1/test2;
outCT.printCComplexT();
system("pause");
return 0;
}