程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 7_5復數模板類

7_5復數模板類

編輯:C++入門知識

[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;
}
運行結果:

 

\

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved