当前位置:网站首页>[C language] header file of complex number four operations and complex number operations
[C language] header file of complex number four operations and complex number operations
2022-07-28 20:03:00 【An ran_】
Catalog
One 、 Four operations of complex numbers
Two 、 Plural header file #include<complex.h>
3、 ... and 、 Some broken thoughts
One 、 Four operations of complex numbers
(1) computing method
Add :(a+bi)+(c+di)=(a+c)+(c+d)i【 The real part is added to the real part , Add the imaginary part and the imaginary step 】
Subtraction :(a+bi)-(c+di)=(a-c)+(c-d)i【 The real part is subtracted from the real part , The imaginary part is subtracted from the imaginary step 】
Multiplication :(a+bi)(c+di)=ac+adi+cdi+bdi*i=(ac-bd)+(bc+ad)i【 Ordinary polynomial multiplication ;i^2=-1】
division :(a+bi)/(c+di)=(a+bi)(c-di)/((c+di)(c+di))=((ac+bd)+(bc-ad)i)/(c^2+d^2)【 The conjugate complex number of the numerator and denominator multiplied by the denominator , Then there is multiplication , Next is multiplication 】
(2) Example : This question requires the preparation of procedures , Calculation 2 The sum of two plurals 、 Bad 、 product 、 merchant .
Input format :
Type in a line and press a1 b1 a2 b2 The format of 2 Plural C1=a1+b1i and C2=a2+b2i The real part and the virtual part of . Title assurance C2 Not for 0.
Output format :
Respectively in 4 In line with (a1+b1i) Operator (a2+b2i) = result Format order output 2 The sum of two plurals 、 Bad 、 product 、 merchant , The number is accurate to the decimal point 1 position . If the real or imaginary part of the result is 0, No output . If the result is 0, The output 0.0.
sample input 1:
2 3.08 -2.04 5.06
sample output 1:
(2.0+3.1i) + (-2.0+5.1i) = 8.1i (2.0+3.1i) - (-2.0+5.1i) = 4.0-2.0i (2.0+3.1i) * (-2.0+5.1i) = -19.7+3.8i (2.0+3.1i) / (-2.0+5.1i) = 0.4-0.6i
sample input 2:
1 1 -1 -1.01
sample output 2:
(1.0+1.0i) + (-1.0-1.0i) = 0.0 (1.0+1.0i) - (-1.0-1.0i) = 2.0+2.0i (1.0+1.0i) * (-1.0-1.0i) = -2.0i (1.0+1.0i) / (-1.0-1.0i) = -1.0
#include<stdio.h>
float a, b, c, d;
void sign(char op);
void operation(float e,float f,char op);
int main(void)
{
scanf("%f%f%f%f", &a, &b, &c, &d);
operation(a+c, b+d, '+');
operation(a-c, b-d, '-');
operation(a*c - b*d, a*d + b*c, '*');
operation((a*c + b*d) / (c*c + d*d), (b*c - a*d) / (c*c + d*d), '/');
return 0;
}
void sign(char op)
{
printf("(%.1f", a);
if(b>=0)
printf("+");
printf("%.1fi) %c (%.1f", b, op, c);
if(d>=0)
printf("+");
printf("%.1fi) = ", d);
}
void operation(float e,float f,char op)
{
sign(op);
if((int)(e*10))
printf("%.1f", e);
if((int)(f*10))
{
if ((f > 0) && (int)(e*10))
printf("+");
printf("%.1fi", f);
}
else if (!(int)(e*10))
printf("0.0");
printf("\n");
}
Two 、 Plural header file #include <complex.h>
(1)C There are three types in a language that can store plural numbers
float_Complex: Both the real part and the imaginary part are float type
double_Complex: Both the real part and the imaginary part are double type
long double_Complex: Both the real part and the imaginary part are long type
for example :double_Complex x;
notes :C99 Support for plural , No header files are required with this definition method .
(2) Add header file #include <complex.h>, You can use it complex Instead of _Complex, This header defines the imaginary part as 'I', So defining such a complex number can be like this float complex z=a+bI;
(3) macro
| Macro name | value |
|---|---|
| complex | _Complex |
| _Complex_I | An imaginary unit , The type is const float_Complex |
| I | _Complex_I |
there I Instead of Complex_I By analogy bool(#include <stdbool> Medium ) and Bool equally
The assignment method of complex numbers is as follows :doubel complex dc=2.0+3.5*I;
(4) Several functions
1.double real_part=creal(z);// obtain Z The real part of
2.double imag_part=cimag(z)// obtain Z The imaginary part of
Processing float and long double Type , use crealf() and creall(),cimagf() and cimagl().【 understand : With double complex Benchmarking 】
3. extension : Allow multiplication and division of complex numbers with absolute values , But there must be CX_LIMITED_RANGE Compilation tips for



4. Complex function expansion 
Classification of complex functions : Trigonometric functions 、 Hyperbolic function 、 Exponential and logarithmic functions 、 Power function and operation function .
The operation function is like this 
(2) Example : find the root of the quadratic equation
Particular attention : The solution of virtual root of quadratic equation in one variable : Turn negative numbers into positive numbers , But we need to add one after him i
therefore , Real component =-b/(2a), Imaginary part =sqrt(-dert)/(2a).
describe
Input from keyboard a, b, c Value , Program to calculate and output the univariate quadratic equation ax2 + bx + c = 0 The root of the , When a = 0 when , Output “Not quadratic equation”, When a ≠ 0 when , according to △ = b2 - 4ac Calculate and output the root of the equation in three cases .
Input description :
Multi group input , a line , Contains three floating point numbers a, b, c, Separated by a space , Represents a quadratic equation of one variable ax2 + bx + c = 0 The coefficient of .
Output description :
Enter... For each group , Output one line , Output univariate quadratic equation ax2 + bx +c = 0 The root of .
If a = 0, Output “Not quadratic equation”;
If a ≠ 0, There are three situations :
△ = 0, Then two real roots are equal , The output form is :x1=x2=....
△ > 0, Then the two real roots are unequal , The output form is :x1=...;x2=..., among x1 <= x2.
△ < 0, There are two virtual roots , The output :x1=* Real component - Imaginary part i;x2= Real component + Imaginary part i**, namely x1 The imaginary part coefficient of is less than or equal to x2 Imaginary part coefficient of , The real part is 0 You cannot omit . Real component = -b / (2a), Imaginary part = sqrt(-△ ) / (2*a)
All real parts shall be accurate to the decimal point 2 position , Numbers 、 There is no space between symbols .
Example 1
Input :
2.0 7.0 1.0
Output :
x1=-3.35;x2=-0.15
#include <stdio.h>
#include <math.h>
double zhengen(double m, double n, double d);// Realize the formula function of finding the root of a quadratic equation of one variable +
double fugen(double m, double n, double d);// Realize the formula function of finding the root of a quadratic equation of one variable -
double shibu(double m, double n);
double xubu(double m, double d);
int main()
{
double a = 0, b = 0, c = 0,x1=0,x2=0;
while(scanf("%lf %lf %lf", &a, &b, &c)!=EOF)
{
double derta = pow(b, 2) - 4 * a * c;
if (a != 0)// The first parameter is not 0
{
if (derta == 0)//①=0
{
if(b==0)// This can also be lost directly , Just a little more
printf("x1=x2=0.00\n");
else
{
x1 = x2 = zhengen(a,b,derta);
printf("x1=x2=%.2lf\n", x1);
}
}
else if (derta > 0)//②>0
{// Here is to assign the larger solution to the positive root , The smaller solution is assigned to the negative root
x1 =( zhengen(a, b, derta)> fugen(a, b, derta)) ? fugen(a, b, derta) : zhengen(a, b, derta);
x2 =(zhengen(a, b, derta) > fugen(a, b, derta)) ? zhengen(a, b, derta) : fugen(a, b, derta);
printf("x1=%.2lf;x2=%.2lf\n",x1,x2);
}
else if (derta < 0)//③<0—— imaginary root
{
if(xubu(a,derta)<0)// Pay attention to the difference between plus and minus signs
printf("x1=%.2lf%.2lfi;x2=%.2lf+%.2lfi\n",shibu(a, b),xubu(a, derta), shibu(a, b), -xubu(a, derta));
else
printf("x1=%.2lf-%.2lfi;x2=%.2lf+%.2lfi\n",shibu(a, b),xubu(a, derta), shibu(a, b), xubu(a, derta));
}
}
else
{
printf("Not quadratic equation");// Not a quadratic equation
}
}
return 0;
}
double zhengen(double m, double n, double d)// Realize the formula function of finding the root of a quadratic equation of one variable +
{
double x = 0;
x = (-n + sqrt(d)) / (2 * m);
return x;
}
double fugen(double m, double n, double d)// Realize the formula function of finding the root of a quadratic equation of one variable -
{
double x = 0;
x = (-n - sqrt(d)) / (2 * m);
return x;
}
double shibu(double m, double n)
{
double s = 0;
s = (-n) / (2 * m);
return s;
}
double xubu(double m, double d)
{
double xu = 0;
xu = sqrt(-d) / (2 * m);
return xu;
}
3、 ... and 、 Some broken thoughts
This summary is because I have accomplished the programming problem of quadratic equation of one variable , The virtual root is not particularly understood , Just look back , The original title said their method , I just didn't take it seriously . Then when I do the experiment in class today , I found that there are four operations of that complex number , I'm not very good at plural arithmetic myself , Add programming , double buff, My day , At that time, there was only one head , Two big , But I think of the problem of virtual root that I did before , Just think that I must break it this time . Can't be like before , Always find problems , Instead of solving the problem , Just wait for others to feed them , You may not eat this knowledge point . Finally, I watched myself slide a little , Others rise a little , My heart is not willing to , But there's nothing we can do , Run around .
This time I actively associate , It is a good start to actively summarize the content of the plural . come on. ~ Okay , That's the end of the bullshit , Let's review what we did this time
1. Arithmetic operation of complex numbers / arithmetic 【 Calculation rules and computer representation —— What we use here is 4 individual operation function , Parameters are two numbers and an operator , The two parameters are the real part and imaginary part calculated according to the operation rules of negative numbers ; Then a function that mainly prints the equation sign( Here we need to pay special attention to the printing of symbols inside, such as the real part / Positive and negative of imaginary part ==>+/-)】
2. One variable quadratic equation 【 The whole is divided into 2 In this case a>0 And a<0, among a>0 It is divided into 3 In this case , One is dert>0,dert<0,dert=0. then dert<0 The real part needs to be calculated (-b/2a) Deficiency part of harmony (sqrt(-dert)/2a); There are many calculations here , It will be more convenient for us to encapsulate it into a function ——zhengen+fugen+shibu+xubu】
3. Header file #include <complex.h> With understanding , Like defining 3 Two types of variables , Get a real part with a complex number , Get the imaginary part of a complex number . As for the rest, it's just expanding reading , There are no instances and application scenarios , Understanding will not be in place . for instance I、complex Etc. are defined by macros , There are also some complex functions ( usually c At the beginning , such as csin) Some understanding .
边栏推荐
- Nokia expands its 5g agreement with BT and will become its largest wireless access device supplier
- 软考高级考试中有五大证书,哪个更值得考?
- Netcoreapi operation excel table
- 河北邯郸:拓展基层就业空间 助力高校毕业生就业
- 冲刺金九银十丨熬夜半个月汇集大厂Android岗1600道面试真题
- 【微信小程序开发】页面导航与传参
- Implementation of memcpy in C language
- C language operators and input and output
- CDGA|工业互联网行业怎么做好数据治理?
- leetcode day3 查找重复的电子邮箱
猜你喜欢

党员故事|李青艾用漫画带动农民增收致富

Handan, Hebei: expand grassroots employment space and help college graduates obtain employment

【微信小程序开发】页面导航与传参

Concurrent programming, do you really understand?

Question bank and answers of the latest national fire-fighting facility operators (intermediate fire-fighting facility operators) in 2022

Idea properties file display \u solution of not displaying Chinese

克服“看牙恐惧”,我们用技术改变行业

Business visualization - let your flowchart "run" (4. Actual business scenario test)
![[NPP installation plug-in]](/img/6f/97e53116ec4ebc6a6338d125ddad8b.png)
[NPP installation plug-in]

认识中小型局域网WLAN
随机推荐
【NPP安装插件】
一文读懂如何部署具有外部数据库的高可用 K3s
CDGA|工业互联网行业怎么做好数据治理?
English translation Arabic - batch English translation Arabic tools free of charge
Implementation of memcpy in C language
Thoroughly understand bit operations - shift left, shift right
How does app automated testing achieve H5 testing
C language implementation of strncpy
Amazon launched Amazon one palm payment system, and the contactless palm vein recognition market is expected to explode
Oracle insert数据时字符串中有‘单引号问题
Codeignier framework implements restful API interface programming
Implementation of strstr in C language
Overcome the "fear of looking at teeth", and we use technology to change the industry
Advanced notes (Part 2)
通信网络基础知识01
Servlet learning notes
11. Learn MySQL union operator
English translation Portuguese - batch English conversion Portuguese - free translation and conversion of various languages
Intermediate soft test (system integration project management engineer) high frequency test site
[网络]跨区域网络的通信学习路由表的工作原理