c++模板----简易复数模板

哎,真可怜,工作了,居然还要做作业!!!     两个模板的题目,这是其中一个复数的,第一次学习模板,如有问题,请大家指正 。

 

 

/*******************************************************/

 

                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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值