标准C++复数运算类详解及使用例程

  转载自:

    http://blog.csdn.net/qingyang8513/article/details/50908788

目录(?)[-]

  1. 一complex类简介
  2. 二文件包含
  3. 三构造方法
  4. 四笛卡尔坐标系和极坐标系下有关函数
  5. 五指数对数幂平方根运算函数
  6. 六三角关系运算函数
  7. 七运算符重载
  8. 八使用例程
    1. 参考

 

 在C++中复数运算可以通过两种方式来实现:

    1)标准C++复数运算库:complex<typedef> ObjectName(realPart, imagePart);

    2)自定义复数运算类:包括复数的实部、虚部、四则运算、模运算、共轭等。

    后者可以根据需要自己定义,关于类的定义这里不再说明,具体的功能可以根据自己的需要去实现。这里介绍C++标准的复数运算类complex,网上已经有一些关于complex类的简单介绍,但大多都是比较粗略的说明,并没有提供完整而详细的介绍。这里参考了网上的一些资源,首先介绍complex类的定义,包括:对象的构造方法、算术运算、赋值运算符、指数运算、对数运算、幂运算、三角函数运算、输入输出重载等,然后给出了一个使用例程,用于对其使用方法的总结用户总结。

 

一、complex类简介

 

    C++复数运算由标准C++复数运算库(complex number mathematics library)来实现。complex类定义了标准的输入输出运算、算是运算、关系运算和赋值运算,同时还包括指数运算、对数运算、幂运算、平方根、三角函数(正弦,余弦,双曲正弦,双曲余弦)等,还包括笛卡尔坐标系到极坐标系转换的函数。

 

二、文件包含

 

1、 #include <complex.h>

2、 #include <math.h>

 

三、构造方法

 

1、complex为模板类,因此在定义一个复数时需要制定变量类型,如complex<double> cm(1,1);

2、定义时实部和虚部参数可以使用变量,如:

 

[cpp] view plain copy
 
  1. double a = 1;  
  2. double b = 1;  
  3. complex<double> cm(a, b);  



 

3、以下定义均合法:

    a) complex(): complex<double> c1;// c1 = (0,0);

    b) complex(double real, double<double> imag = 0.0): complex c2(1.0);// c2 = (1.0,0);

    c) complex<double> c3 = 3.4; // c3 = (3.4,0);

    d) complex c4 = 3.4 + complex(1.2, 3.5);

 

四、笛卡尔坐标系和极坐标系下有关函数

 

1、real();: friend double real(complex a);

2、 img();: friend double imag(complex a);

3、 abs();: friend double abs(complex a);

4、 norm();: friend double norm(complex a);

5、 arg();: friend double arg(complex a);

6、 conj();: friend complex conj(complex a);

7、 polar();: friend complex polar(double r,double t);

 

示例:

 

[cpp] view plain copy
 
  1. d = real(a);// 返回复数a的实部  
  2. d = imag(a);// 返回复数a的虚部  
  3. d = abs(a);// 返回复数a的模值/幅值  
  4. d = norm(a);// 返回复数a的模值平方  
  5. d = arg(a);// 返回复数a的幅角  
  6. z = conj(a);// 返回复数a的共轭复数  
  7. z = polar(r,t);// 复数的极坐标定义方式,r为幅值,t为幅角  



 

五、指数、对数、幂、平方根运算函数

 

1、 exp(); friend complex exp(complex a);

2、 log(); friend complex log(complex a);

3、 pow(); 四种

        a) friend complex pow(double a, complex b);

        b) friend complex pow(complex a, int b);

        c) friend complex pow(complex a, double b);

        d) friend complex pow(complex a,complex b);

4、 sqrt();friend complex sqrt(complex a);

 

六、三角关系运算函数

 

1、 sin(); friend complex sin(complex a);

2、 cos(); friend complex cos(complex a);

3、 sinh(); friend complex sinh(complex a);

4、 cosh(); friend complex cosh(complex a);

 

七、运算符重载

 

    运算符重载包括:+、-、*、/(包括+=、-=、*=、/=)、==、!=、<<、>>

 

 

八、使用例程

 

1、开发环境:Win7 (X64) / VS2010

2、创建新项目,编写代码如下:

 

[cpp] view plain copy
 
  1. #include <iostream>  
  2. #include <complex>  
  3. #include <math.h>  
  4.   
  5. using namespace std;  
  6.   
  7. void main()  
  8. {  
  9.     // 复数类对象定义  
  10.     cout << "复数类对象定义" << endl;  
  11.     double r = 1.0;  
  12.     double x = 1.0;  
  13.     complex<double> c1;  
  14.     complex<double> c2(1,1);  
  15.     complex<double> c3(r,x);  
  16.     complex<double> c4 = 2.0;  
  17.     complex<double> c5 = c4 + complex<double>(2,1);  
  18.   
  19.     cout << "c1 = " << c1 << endl;  
  20.     cout << "c2 = " << c2 << endl;  
  21.     cout << "c3 = " << c3 << endl;  
  22.     cout << "c4 = " << c4 << endl;  
  23.     cout << "c5 = " << c5 << endl << endl;  
  24.   
  25.     // 笛卡尔坐标系和极坐标系下有关函数  
  26.     cout << "笛卡尔坐标系和极坐标系下有关函数" << endl;  
  27.     cout << "c5实部real:" << c5.real() << endl;  
  28.     cout << "c5虚部imag:" << c5.imag() << endl;  
  29.     cout << "c5模值abs:" << abs(c5) << endl;  
  30.     cout << "c5模值平方norm:" << norm(c5) << endl;  
  31.     cout << "c5幅角arg:" << arg(c5) << endl;  
  32.     cout << "c5共轭复数conj:" << conj(c5) << endl;  
  33.     complex<double> z = polar(1.0, 3.14/6);  
  34.     cout << "复数极坐标定义polar:" << z << endl << endl;  
  35.   
  36.     // 运算符重载,四则运算  
  37.     cout << "运算符重载,四则运算" << endl;  
  38.     cout << "c2 + c5 = " << c2 + c5 << endl;  
  39.     cout << "c2 - c5 = " << c2 - c5 << endl;  
  40.     cout << "c2 * c5 = " << c2 * c5 << endl;  
  41.     cout << "c2 / c5 = " << c2 / c5 << endl << endl;  
  42.   
  43.     system("pause");  
  44. }  

 

 

3、运行结果如图1所示:

图1

 

参考:

1)http://blog.chinaunix.NET/uid-20559667-id-1924707.html

2)http://blog.csdn.net/qhs1573/article/details/12254205

 

posted @   博客园—哆啦A梦  阅读(1931)  评论(1编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示