很久不写博客了。第一次写博客是在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. laravel的模块化是如何实现的

    laravel的模块化是如何实现的 在laravel提供的官方文档上,有一个这样的名词 服务提供者,文档中介绍了它在laravel框架中的角色,以及如何使用它,但却没有讲明服务提供者的本质--它是为了 ...

  2. CSS自学笔记(9):CSS拓展(二)

    CSS图片 当一个网页上有一张或多张图片,而且这些图片的尺寸比较大时,为了是网页布局更紧凑合理,我们可以将这些图片放到一个图片库里,可以有效的防止图片过大可能会对网页布局造成的不良影响. 通过CSS我 ...

  3. ApiDemos示例学习(2)——App->Activity->Animation

    现在介绍一下com.example.android.app包下的Animation示例. 关键类及函数: ActivityOption overridePendingTransition() make ...

  4. 利用 Windows Azure 实现“云优先”

    根据 IDC 的调查,云计算无疑为我们的合作伙伴提供了巨大的机会,预计到 2016 年,全球企业将在公共云服务上耗资 980 亿美元.在今天的休斯敦全球合作伙伴大会上,我们非常高兴能与合作伙伴共同寻求 ...

  5. hdu 4740

    题目链接 老虎左拐,老鼠右拐,碰到不能走的拐一次,如果还不能走就停下,自己走过的不能走,求相遇的坐标或-1 一个停下之后,另一个还可以走 #include <cstdio> #includ ...

  6. 网易云课堂_程序设计入门-C语言_第一周:简单的计算程序_1逆序的三位数

    1 逆序的三位数(5分) 题目内容: 程序每次读入一个正三位数,然后输出逆序的数字.注意,当输入的数字含有结尾的0时,输出不应带有前导的0.比如输入700,输出应该是7. 输入格式: 每个测试是一个3 ...

  7. iOS中UITextView键盘回收

    iOS开发中,发现UITextView没有像UITextField中textFieldShouldReturn:这样的方法,那么要实现UITextView关闭键盘,就必须使用其他的方法,下面是可以使用 ...

  8. 泛型、注解、log4j

    泛型.注解.log4j 泛型:将运行阶段的类型错误提前到编译阶段. 声明泛型必须两端的一致,要么左面有,要么右边有,两边都有的两边必须一致. 泛型方法: static 之后 返回类型之前进行声明 泛型 ...

  9. EF5 通用数据层 增删改查操作,泛型类(转)

    using System; using System.Collections.Generic; using System.Data.Entity.Infrastructure; using Syste ...

  10. httpclient response 重定向

    HTTPClient请求后,重定向后,获取重定向的URL. 方法一:重定向后获取URL import org.apache.http.HttpEntity; import org.apache.htt ...