C语言在VS2017编译器下实现复数的运算(自编自定义函数)

您所在的位置:网站首页 project的复数 C语言在VS2017编译器下实现复数的运算(自编自定义函数)

C语言在VS2017编译器下实现复数的运算(自编自定义函数)

2023-05-28 08:01| 来源: 网络整理| 查看: 265

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); }


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3