IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> C语言在VS2017编译器下实现复数的运算(自编自定义函数) -> 正文阅读

[C++知识库]C语言在VS2017编译器下实现复数的运算(自编自定义函数)

VS2017编译器关于C语言下,对复数的运算并不支持,利用complex.h头文件并不适用,通过自编的子函数,实现一般性的复数计算,精度与Matlab软件下对比,精度一致。
一、头文件ComplexCalculation.h,用于复数运算的一些函数头和定义

/*ComplexCalculation.h  用于复数运算的一些函数头和定义*/

#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.c,用于复数运算的一些函数

/*ComplexCalculation.c 用于复数运算的一些函数*/

#include "ComplexCalculation.h"
#include "math.h"
#include "stdio.h"

/*函数名:complexAdd
*说明:复数加法
*/
complexType    complexAdd(complexType a, complexType b)
{
	complexType result;
	result.Real = a.Real + b.Real;
	result.Imag = a.Imag + b.Imag;	
	return result;
}
	
/*函数名:complexSubtract
*说明:复数减法
*/
complexType    complexSubtract(complexType minuend, complexType subtrahend)
{
	complexType result;
	result.Real = minuend.Real - subtrahend.Real;
	result.Imag = minuend.Imag - subtrahend.Imag;
	return result;
}

/*函数名:complexMultiply
*说明:复数乘法
*/
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
*说明:复数除法
*其它: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
*说明:复数取模(绝对值)
*/
mathDouble    complexAbs(complexType a)
{
	return (sqrt(pow(a.Real, 2) + pow(a.Imag, 2)));
}

/*函数名:complexAngle
*说明:复数取相角
*/
mathDouble    complexAngle(complexType a)
{
/*是atan2而非atan,(-PI,PI] */
	return (atan2(a.Imag, a.Real));
}

/*函数名:complexByAbsAngle
*说明:通过模和相角合成复数
*/
complexType complexByAbsAngle(mathDouble r, mathDouble theta)
{
	complexType tmp_1, tmp_2;
	tmp_1.Real = 0;
	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

三、主函数通道,main.c

#include "ComplexCalculation.h"
#include "stdio.h"

int main(void)
{
	complexType a, b, c;
	a.Imag = 1.0;
	a.Real = 0;
	b.Real = -0.043369177255479;
	b.Imag = 0.0;

	c = complexAdd(a, b);
	printf("complexAdd: c.Real %20.15lf, c.Imag %20.15lf \r\n", c.Real, c.Imag);

	c = complexSubtract(a, b);
	printf("complexSubtract: c.Real %20.15lf, c.Imag %20.15lf \r\n", c.Real, c.Imag);

	c = complexMultiply(a, b);
	printf("complexMultiply: c.Real %20.15lf, c.Imag %20.15lf \r\n", c.Real, c.Imag);

	c = complexDivision(a, b);
	printf("complexDivision: c.Real %20.15lf, c.Imag %20.15lf \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 %20.15lf, a.Imag %20.15lf \r\n", c.Real, c.Imag);

	c= complexExp(a);
	printf("complexExp: c.Real %20.15lf, c.Imag %20.15lf \r\n", c.Real, c.Imag);
}
  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2021-07-13 17:16:52  更:2021-07-13 17:18:48 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年4日历 -2024/4/28 21:44:42-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码