很久不写博客了。第一次写博客是在04年,最近的一次还是在大学时,在学校时,甚至还有过自己去买虚拟主机搭WordPress写博客的经历。现在工作时间越长,越发现积累的重要性。那么就从这里开始吧,重新开始写博客。

  最近打算写小算法,里面需要用到一些复数运算。贴一点复数运算的C语言实现代码。都是些很简单的东西。

  包括以下运算:

  复数加法、复数减法、复数乘法、复数除法、复数取模、复指数运算、复数取相角、模与相角合成复位。本人专业本职做硬件的,写程序没受过专业训练,勿吐槽。

 /*file        ComplexCalculation.h
*author Vincent Cui
*e-mail whcui1987@163.com
*version 0.1
*data 20-Oct-2014
*brief 用于复数运算的一些函数头和定义
*/ #ifndef _COMPLEXCALCULATION_H_
#define _COMPLEXCALCULATION_H_ #define ASSERT_ENABLE 1 #define IS_COMPLEX_DIVISOR_CORRENT(DIVISOR_REAL, DIVISOR_IMAG) ((DIVISOR_REAL != 0) || (DIVISOR_IMAG != 0)) typedef double mathDouble;
typedef unsigned char mathUint_8;
typedef unsigned short int mathUint_16;
typedef unsigned int mathUint_32; typedef struct _ReDefcomplex
{
mathDouble Real;
mathDouble Imag;
}complexType; complexType complexAdd(complexType a, complexType b);
complexType complexSubtract(complexType minuend, complexType subtrahend);
complexType complexMultiply(complexType a, complexType b);
complexType complexDivision(complexType dividend, complexType divisor);
mathDouble complexAbs(complexType a);
mathDouble complexAngle(complexType a);
complexType complexByAbsAngle(mathDouble r, mathDouble theta);
complexType complexExp(complexType a); #if ASSERT_ENABLE
#define assert_param(expr) ((expr) ? (void)0 : assert_failed((mathUint_8 *)__FILE__, __LINE__))
void assert_failed(mathUint_8* file, mathUint_32 line);
#else
#define assert_param(expr) ((void)0)
#endif #endif

ComplexCalculation.h

 /*file        ComplexCalculation.c
*author Vincent Cui
*e-mail whcui1987@163.com
*version 0.1
*data 20-Oct-2014
*brief 用于复数运算的一些函数
*/ #include "ComplexCalculation.h"
#include "math.h"
#include "stdio.h" /*函数名:complexAdd
*说明:复数加法
*输入:a,b两个复数
*输出:
*返回:a + b
*调用:
*其它:
*/
complexType complexAdd(complexType a, complexType b)
{
complexType result; result.Real = a.Real + b.Real;
result.Imag = a.Imag + b.Imag; return result;
} /*函数名:complexSubtract
*说明:复数减法
*输入:minuend被减数,subtrahend减数
*输出:
*返回:a - b
*调用:
*其它:
*/
complexType complexSubtract(complexType minuend, complexType subtrahend)
{
complexType result; result.Real = minuend.Real - subtrahend.Real;
result.Imag = minuend.Imag - subtrahend.Imag; return result;
} /*函数名:complexMultiply
*说明:复数乘法
*输入:a,b两个复数
*输出:
*返回:a * b
*调用:
*其它:
*/
complexType complexMultiply(complexType a, complexType b)
{
complexType result; result.Real = a.Real * b.Real - a.Imag * b.Imag;
result.Imag = a.Imag * b.Real + a.Real * b.Imag; return result;
} /*函数名:complexDivision
*说明:复数除法
*输入:dividend被除数,divisor除数
*输出:
*返回:a / b
*调用:
*其它:divisor的实部和虚部不能同时为0
*/
complexType complexDivision(complexType dividend, complexType divisor)
{
complexType result; /*断言,被除数的实部和虚部不能同时为零*/
assert_param(IS_COMPLEX_DIVISOR_CORRENT(divisor.Real, divisor.Imag)); result.Real = (mathDouble)(dividend.Real * divisor.Real + dividend.Imag * divisor.Imag) / \
(divisor.Real * divisor.Real + divisor.Imag * divisor.Imag);
result.Imag = (mathDouble)(dividend.Imag * divisor.Real - dividend.Real * divisor.Imag) / \
(divisor.Real * divisor.Real + divisor.Imag * divisor.Imag);
return result;
} /*函数名:complexAbs
*说明:复数取模
*输入:a复数
*输出:
*返回:复数的模
*调用:
*其它:
*/
mathDouble complexAbs(complexType a)
{
return (sqrt( pow(a.Real,) + pow(a.Imag,) ));
} /*函数名:complexAngle
*说明:复数取相角
*输入:a复数
*输出:
*返回:复数的相角
*调用:
*其它:
*/
mathDouble complexAngle(complexType a)
{
/*是atan2而非atan,(-PI,PI] */
return (atan2(a.Imag, a.Real));
} /*函数名:complexByAbsAngle
*说明:通过模和相角合成复数
*输入:r 模, theta 相角
*输出:
*返回:复数
*调用:
*其它:
*/
complexType complexByAbsAngle(mathDouble r, mathDouble theta)
{
complexType tmp_1,tmp_2; tmp_1.Real = ;
tmp_1.Imag = theta;
tmp_2 = complexExp(tmp_1);
tmp_2.Real *= r;
tmp_2.Imag *= r; return tmp_2;
} /*函数名:complexExp
*说明:复指数运算
*输入:a 复指数
*输出:
*返回:e的a次方
*调用:
*其它:使用欧拉公式 e^(jw) = cos(w) + j * sin(w)
*/
complexType complexExp(complexType a)
{
complexType result; result.Real = exp(a.Real) * cos(a.Imag);
result.Imag = exp(a.Real) * sin(a.Imag); return result;
} #if ASSERT_ENABLE
/*函数名:assert_failed
*说明:断言函数
*输入:
*输出:打印出错的位置
*返回:
*调用:
*其它:
*/
void assert_failed(mathUint_8* file, mathUint_32 line)
{
printf("Assert Error in File: %s \r\nLine: %d \r\n",file,line);
} #endif

ComplexCalculation.c

 #include "ComplexCalculation.h"
#include "stdio.h" int main(void)
{
complexType a,b,c;
a.Imag = 0.5;
a.Real = 2.5;
b.Real = ;
b.Imag = -; c = complexAdd(a,b);
printf("complexAdd: c.Real %f, c.Imag %f \r\n",c.Real,c.Imag);
c = complexSubtract(a,b);
printf("complexSubtract: c.Real %f, c.Imag %f \r\n",c.Real,c.Imag);
c = complexMultiply(a,b);
printf("complexMultiply: c.Real %f, c.Imag %f \r\n",c.Real,c.Imag);
c = complexDivision(a,b);
printf("complexDivision: c.Real %f, c.Imag %f \r\n",c.Real,c.Imag);
printf("Abs(c): %f\r\n",complexAbs(a));
printf("Angle(c): %f\r\n",complexAngle(a));
c = complexByAbsAngle(complexAbs(a),complexAngle(a));
printf("complexByAbsAngle: a.Real %f, a.Imag %f \r\n",c.Real,c.Imag); while();
}

main.c

下面是运行结果,在VS2012上运行的。

欢迎一起交流!

后面博客中我会写一些数字信号处理运算的C语言实现。

最新文章

  1. Android笔记——Android中visibility属性VISIBLE、INVISIBLE、GONE的区别
  2. 我读过的最好的epoll讲解--转自”知乎“
  3. jsp页面价格
  4. WKWebView与Js (OC版)
  5. linux消息队列的使用及内核实现原理
  6. json-c-0.9 的简单用法
  7. (转)各种排序算法的分析及java实现
  8. Unity3D脚本中文系列教程(七)
  9. 设计模式-工厂方法(Demo)
  10. Nginx重定向[Rewrite]配置 for wordpress & Discuz
  11. Basic Example of JMX Technology--转载
  12. AlertView + Block 的使用
  13. spring多数据源的配置
  14. JAVA-随机读写文件
  15. Javascript中的浅拷贝和深拷贝
  16. MSSQL 自定义函数详解
  17. 【Linux】 linux中的进程信息相关的一些内容
  18. 2017京东校招面试回忆(已成功拿到offer)
  19. RecyclerView下拉刷新上拉加载(二)
  20. 20.如何从app业务逻辑提炼api接口

热门文章

  1. windows下mysql数据库表名大小写不敏感
  2. 14-利用SVD简化数据
  3. Oracle EBS-SQL (WIP-6):检查任务已完成但状态是发放的任务.sql
  4. Android - Ant自动编译打包android项目 -- 1(转)
  5. 如何用ATL创建ActiveX控件
  6. Objective-C编程调试技巧
  7. linux学习之(三)-文件操作命令
  8. [置顶] Mysql存储过程入门知识
  9. SQL 字段里有逗号隔开的数据的取值
  10. 2014.12.06 ASP.NET 三级联动,添加员工,修改员工