很久不写博客了。第一次写博客是在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语言实现。

一些复数运算的C语言实现的更多相关文章

  1. C语言中复数运算及调用blas,lapack中复数函数进行科学计算

    C语言中常用的数据类型主要int, float ,double ,char 等,但在科学运算中复数扮演着重要角色.这里讲下C语言中的复数运算以及如何调用blas,lapack库中的复数函数来进行科学计 ...

  2. c++复习一:复数运算的简单实现。

    复数运算的简单实现. 程序很简单了.基本忘光了复数,重新了解了基本概念.如何在平面表示一个复数,复数的长度|x|=开根 a^2+b^2.和四则运算. 程序基本点: 封装和抽象: 1)封装成员数据,私有 ...

  3. 大整数加减运算的C语言实现

    目录 大整数加减运算的C语言实现 一. 问题提出 二. 代码实现 三. 效果验证 大整数加减运算的C语言实现 标签: 大整数加减 C 一. 问题提出 培训老师给出一个题目:用C语言实现一个大整数计算器 ...

  4. 算法笔记_047:复数运算(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 编程实现两个复数的运算.设有两个复数 和 ,则他们的运算公式为: 要求:(1)定义一个结构体类型来描述复数. (2)复数之间的加法.减法.乘法和除法 ...

  5. Java编写能完成复数运算的程序

    Java编写能完成复数运算的程序 题目简介: 整体分析: 界面分析: 实验代码: package complex; import java.awt.EventQueue; import javax.s ...

  6. Java练习 SDUT-4303_简单的复数运算(类和对象)

    简单的复数运算(类和对象) Time Limit: 2000 ms Memory Limit: 65536 KiB Problem Description 设计一个类Complex,用于封装对复数的下 ...

  7. 2017.11.21 基于JSP+Servlet+JavaBean实现复数运算(二)

    代码的实现 最基本的MVC模式 //input.jsp 输入界面 <%@ page language="java" import="java.util.*" ...

  8. Java实现复数运算

    1 问题描述 编程实现两个复数的运算.设有两个复数 和 ,则他们的运算公式为: 要求:(1)定义一个结构体类型来描述复数. (2)复数之间的加法.减法.乘法和除法分别用不用的函数来实现. (3)必须使 ...

  9. C++复数运算 重载

    近期整理下很久前写的程序,这里就把它放在博文中了,有些比较简单,但是很有学习价值. 下面就是自己很久前实现的复数重载代码,这里没有考虑特殊情况,像除法中,分母不为零情况. #include <i ...

随机推荐

  1. php curl封装类

    一个php curl封装的类,减少代码量,简化采集工作.这个类也是我工作的最常用的类之一.这里分享给大家.配合上phpquery,十分好用. <?php namespace iphp\core; ...

  2. linux初学(CentOS)之注意事项(一)

    linux严格区分大小写(命令,文件名,用户名等) linux所有内容以文件形式保存,包括硬件 硬盘文件是/dev/sd[a-p](a,p为盘符名) 光盘文件是/dev/sr0等 linux不靠扩展名 ...

  3. JQeury Image LazyLoad

    使用jquery插件实现图片延迟加载技术 http://www.cnblogs.com/szytwo/archive/2012/12/27/2836141.html EasyUI http://www ...

  4. delphi 对TThread扩充TSimpleThread

    对线程的使用,是每个开发者都应该熟练掌握的,也是进阶的重要一环. 可以这样说,没有线程,连界面假死的问题都解决不了,就更别谈并行处理来提高效率了. 本例对线程进行改进,打造一个基础的线程,以后线程应用 ...

  5. WIN ERROR:C:\Windows\System32\<LANG_NAME>\mstsc.exe.MUI

    Issue: When you upgrade Win7, you may found your remote desktop will not work. You may get following ...

  6. 8 个优秀的 Linux 图形图像及色彩工具

    8 个优秀的 Linux 图形图像及色彩工具 1. 硬件色彩分析器LPROF LPROF 是一个用于创建设备兼容,如相机.扫描仪.显示器的ICC兼容型材的颜色分析器.这些配置提供跨设备的色彩一致性.他 ...

  7. 2014第11周一word样式

    今天摸索使用了word的样式替换功能感觉不错,简单记录下: 1.将某一个样式的标题统一替换为另一样式,之前一般是格式化一个个找到标题设置格式, 今天才发现可以选中标题->在浮动框上单击样式或开始 ...

  8. java常量池理解

    String类两种不同的创建方式 String s1 = "zheng"; //第一种创建方式 String s2 = new String("junxiang" ...

  9. 简单QT界面信号图形化输入输出

    右键->转到槽,选择信号 就可以输入代码 右键->转到槽,选择信号 就可以输入代码 2个文本框接受输入数字,第3个文本框输出相加结果 void Dialog::on_pushButton_ ...

  10. jQuery load()和ready()

    ready与load谁先执行: 大家在面试的过程中,经常会被问到一个问题:ready与load那一个先执行,那一个后执行?答案是ready先执行,load后执行. DOM文档加载的步骤: 要想理解为什 ...