新聞中心
我們先創(chuàng)建一個有利于后續(xù)操作的頭文件,把預(yù)處理命令和一些常用的的宏(之后會用到)封裝在頭文件"StdFile.h"里面。
以下為"StdFile.h"里面的信息:
#include#include#include#include#define _USE_MATH_DEFINES
#include#includeusing namespace std;
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef long long ElemType;
typedef int Index;
typedef int Num;
typedef int Step;
之后對復(fù)數(shù)的基本定義:
typedef struct
{
?Real real;
?Imag imag;
}Complex;
再創(chuàng)建一個頭文件"Complex.h",里面存放關(guān)于復(fù)數(shù)的一些基本方法。
以下為"Complex.h"里面的信息:
#include "StdFile.h"
typedef long double Real, Imag;
typedef long double RealNum;
typedef struct
{
Real real;
Imag imag;
}Complex;
//創(chuàng)建復(fù)數(shù)
Complex CreateComplex(Real x = 0, Imag y = 0)
{
Complex z = { 0,0 };
z.real = x;
z.imag = y;
return z;
}
//打印復(fù)數(shù)
void PrintComplex(Complex z)
{
if (z.real == 0)
printf("j%.2llf\n", z.imag);
else
printf("%.2llf+j%.2llf\n", z.real, z.imag);
}
//獲取實部
RealNum RealComplex(Complex z)
{
return z.real;
}
//獲取虛部
RealNum ImagComplex(Complex z)
{
return z.imag;
}
//共軛復(fù)數(shù)
Complex ConjugateComplex(Complex z)
{
Complex _z;
_z.real = z.real;
_z.imag = -z.imag;
return _z;
}
//復(fù)數(shù)的模
RealNum AbsComplex(Complex z)
{
RealNum r = 0;
r = sqrt(z.real * z.real + z.imag * z.imag);
return r;
}
//復(fù)數(shù)加法
Complex AddComplex(Complex z1, Complex z2)
{
Complex z3 = { 0,0 };
z3.real = z1.real + z2.real;
z3.imag = z1.imag + z2.imag;
return z3;
}
//復(fù)數(shù)減法
Complex SubComplex(Complex z1, Complex z2)
{
Complex z3 = { 0,0 };
z3.real = z1.real - z2.real;
z3.imag = z1.imag - z1.imag;
return z3;
}
//復(fù)數(shù)乘法
Complex MulComplex(Complex z1, Complex z2)
{
Complex z3 = { 0,0 };
z3.real = z1.real * z2.real - z1.imag * z2.imag;
z3.imag = z1.real * z2.imag + z1.imag * z2.real;
return z3;
}
//復(fù)數(shù)除法
Complex DivComplex(Complex z1, Complex z2)
{
Complex z3 = { 0,0 };
z3.real = (z1.real * z2.real + z1.imag * z2.imag) / (z2.real * z2.real + z2.imag * z2.imag);
z3.imag = (z1.imag * z2.real - z1.real * z2.imag) / (z2.real * z2.real + z2.imag * z2.imag);
return z3;
}
//復(fù)數(shù)的輻角
RealNum ArgComplex(Complex z)
{
RealNum r;
if (z.real >= 0 && z.imag == 0)
r = 0;
else if (z.real< 0 && z.imag == 0)
r = M_PI;
else if (z.real >= 0)
r = atan(z.imag / z.real);
else if (z.real< 0 && z.imag>0)
r = atan(z.imag / z.real) + M_PI;
else
r = atan(z.imag / z.real) - M_PI;
return r;
}
此方法不全,讀者可添加更多方法(比如復(fù)數(shù)的三角式,復(fù)數(shù)的指數(shù)式,復(fù)數(shù)的冪,復(fù)數(shù)的對數(shù),指數(shù)……)
以下為效果展示:
#include "Complex.h"
int main()
{
Complex Z1, Z2, Z;
Z = { 6,8 };
Z1 = { 1,0 };
Z2 = CreateComplex(2, 4);
PrintComplex(Z1);
PrintComplex(Z2);
PrintComplex(AddComplex(Z1, Z2));
cout<< "Z1+Z2的模 "<< AbsComplex(AddComplex(Z1, Z2))<< endl;
cout<< "Z的實部 "<< RealComplex(Z)<< ' '<< "Z的虛部"<< ImagComplex(Z)<< endl;
cout<< "打印Z/(Z1+Z2) ";
PrintComplex(DivComplex(Z, AddComplex(Z1, Z2)));
cout<< "Z的輻角主值 "<< ArgComplex(Z)<< endl;
}
顯示結(jié)果:
輻角主值以弧度制顯示,可能很難看出角度,優(yōu)化后可以把弧度轉(zhuǎn)化為角度:
//復(fù)數(shù)的輻角
RealNum ArgComplex(Complex z)
{
RealNum r;
if (z.real >= 0 && z.imag == 0)
r = 0;
else if (z.real< 0 && z.imag == 0)
r = M_PI;
else if (z.real >= 0)
r = atan(z.imag / z.real);
else if (z.real< 0 && z.imag>0)
r = atan(z.imag / z.real) + M_PI;
else
r = atan(z.imag / z.real) - M_PI;
return r/M_PI*180;
}
只需修改返回值即可。
修改后主函數(shù)的運行結(jié)果:
arctan(8/6)剛好就是53.1°,輻角顯示無誤。
以下是資源文件里面的文本文件"Help.Complex.txt"的內(nèi)容,為方法的使用大綱:
typedef struct
{
Real real;
Imag imag;
}Complex;
//創(chuàng)建復(fù)數(shù)
Complex CreateComplex(Real x = 0, Imag y = 0)
//打印復(fù)數(shù)
void PrintComplex(Complex z)
//獲取實部
RealNum RealComplex(Complex z)
//獲取虛部
RealNum ImagComplex(Complex z)
//共軛復(fù)數(shù)
Complex ConjugateComplex(Complex z)
//復(fù)數(shù)的模
RealNum AbsComplex(Complex z)
//復(fù)數(shù)加法
Complex AddComplex(Complex z1, Complex z2)
//復(fù)數(shù)減法
Complex SubComplex(Complex z1, Complex z2)
//復(fù)數(shù)乘法
Complex MulComplex(Complex z1, Complex z2)
//復(fù)數(shù)除法
Complex DivComplex(Complex z1, Complex z2)
//復(fù)數(shù)的輻角
RealNum ArgComplex(Complex z)
之后繼續(xù)更新關(guān)于順序表的相關(guān)方法。
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧
當(dāng)前文章:數(shù)據(jù)結(jié)構(gòu)(C語言描述)——復(fù)數(shù)-創(chuàng)新互聯(lián)
文章起源:http://fisionsoft.com.cn/article/ddedis.html