当前位置:网站首页>[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 .
边栏推荐
- 【经验之谈】关于维修电子设备的几点建议和经验
- Basic knowledge of communication network 01
- 数字图像理论知识(一)(个人浅析)
- Deploy ZABBIX automatically with saltstack
- leetcode day3 查找重复的电子邮箱
- Prometheus deployment
- 【微信小程序开发】页面导航与传参
- Special draft of Mir | common sense knowledge and reasoning: representation, acquisition and application (deadline on October 31)
- Leetcode Day2 consecutive numbers
- [network] cross area network communication learning classification and calculation of IPv4 address
猜你喜欢
![[wechat applet development] page navigation and parameter transmission](/img/10/76b6592fa9e71073831887c4fb6da9.png)
[wechat applet development] page navigation and parameter transmission

Cdga | how can the industrial Internet industry do a good job in data governance?

MATLAB实现的图像分割之边缘检测和连接

Saltstack advanced

Servlet学习笔记

Hebei: stabilizing grain and expanding beans to help grain and oil production improve quality and efficiency

中国能否在元宇宙的未来发展中取得突破,占领高地?

English translation Italian - batch English translation Italian tools free of charge

English Translation Spanish - batch English Translation Spanish tools free of charge

Read how to deploy highly available k3s with external database
随机推荐
Handan, Hebei: expand grassroots employment space and help college graduates obtain employment
云计算笔记part.1——系统管理
MySQL8 Encrypting InnoDB Tablespaces
Cloud computing notes part.2 - Application Management
架构基本概念和架构本质
leetcode day1 分数排名
Serial port receiving application ring buffer
XOR operation and its usage
Using Lex (Flex) to generate lexical analyzer of PL language
MySQL性能测试工具sysbench学习
Kubeedge releases white paper on cloud native edge computing threat model and security protection technology
河北邯郸:拓展基层就业空间 助力高校毕业生就业
Deploy LNMP automatically with saltstack
Source insight project import and use tutorial
MySQL performance testing tool sysbench learning
Leetcode day3 find duplicate email addresses
Leetcode Day5 delete duplicate email
Thoroughly understand bit operations -- and (&), not (~), or (|), XOR (^)
Design of air combat game based on qtgui image interface
基于C语言的信息管理系统和小游戏