当前位置:网站首页>How to use C language code to realize the addition and subtraction of complex numbers and output structure
How to use C language code to realize the addition and subtraction of complex numbers and output structure
2022-07-04 07:46:00 【Yisu cloud】
How to use it? C The language code realizes the addition and subtraction of complex numbers and the output structure
This article “ How to use it? C The language code realizes the addition and subtraction of complex numbers and the output structure ” Most people don't quite understand the knowledge points of the article , So I made up the following summary for you , Detailed content , The steps are clear , It has certain reference value , I hope you can gain something after reading this article , Let's take a look at this article “ How to use it? C The language code realizes the addition and subtraction of complex numbers and the output structure ” Article bar .
One 、 Addition and subtraction of complex numbers
#include<stdio.h>
typedef struct complex
{
int real; // Real component
int imag; // Imaginary part
}complex;
/*
function : Plural addition
Parameters : Two plurals
Return value : The sum of two plural numbers
*/
complex complexadd(complex x,complex y)
{
complex sum;
sum.real = x.real + y.real;
sum.imag = x.imag + y.imag;
return sum;
}
/*
function : Complex subtraction
Parameters : Two plurals
Return value : The difference between two complex numbers
*/
complex complexsub(complex x,complex y)
{
complex sum;
sum.real = x.real - y.real;
sum.imag = x.imag - y.imag;
return sum;
}
/*
function : Print plurals
Parameters : A plural
*/
void printfcomplex(complex x)
{
printf("%d",x.real);
if(x.imag > 0)
{
printf("+");
}
printf("%d\n",x.imag);
}
int main()
{
complex f1 = {3,-5};
complex f2 = {-5,8};
printfcomplex(f1);
printfcomplex(f2);
complex f3 = complexadd(f1,f2);
printfcomplex(complexadd(f1,f2));
printfcomplex(f3);
printfcomplex(complexsub(f1,f2));
return 0;
}Two 、 Sub documents complex.c
#include<stdio.h>
#include"complex.h"
/*
function : Plural addition
Parameters : Two plurals
Return value : The sum of two plural numbers
*/
complex complexadd(complex x,complex y)
{
complex sum;
sum.real = x.real + y.real;
sum.imag = x.imag + y.imag;
return sum;
}
/*
function : Complex subtraction
Parameters : Two plurals
Return value : The difference between two complex numbers
*/
complex complexsub(complex x,complex y)
{
complex sum;
sum.real = x.real - y.real;
sum.imag = x.imag - y.imag;
return sum;
}
/*
function : Print plurals
Parameters : A plural
*/
void printfcomplex(complex x)
{
printf("%d",x.real);
if(x.imag > 0)
{
printf("+");
}
printf("%di\n",x.imag);
}main.c
#include<stdio.h>
#include"complex.h" // complex The header file
int main()
{
complex f1 = {3,-5}; // Structure initialization
complex f2 = {-5,8};
printfcomplex(f1); // Print plurals
printfcomplex(f2);
complex f3 = complexadd(f1,f2);
printfcomplex(complexadd(f1,f2));
printfcomplex(f3);
printfcomplex(complexsub(f1,f2));
return 0;
}complex.h
#ifndef __COMPLEX_H__
#define __COMPLEX_H__
// Type declaration
typedef struct complex
{
int real; // Real component
int imag; // Imaginary part
}complex;
/*
function : Plural addition
Parameters : Two plurals
Return value : The sum of two plural numbers
*/
complex complexadd(complex x,complex y);
/*
function : Complex subtraction
Parameters : Two plurals
Return value : The difference between two complex numbers
*/
complex complexsub(complex x,complex y);
/*
function : Print plurals
Parameters : A plural
*/
void printfcomplex(complex x);
#endifThat's about “ How to use it? C The language code realizes the addition and subtraction of complex numbers and the output structure ” The content of this article , I believe we all have a certain understanding , I hope the content shared by Xiaobian will be helpful to you , If you want to know more about it , Please pay attention to the Yisu cloud industry information channel .
边栏推荐
- L1-027 rental (20 points)
- Comparison between applet framework and platform compilation
- Zephyr 学习笔记2,Scheduling
- Rapidjson reading and writing JSON files
- JVM中堆概念
- Literature collation and thesis reading methods
- rapidjson读写json文件
- How does dataframe calculate the average value of each row as another column
- 手写简易版flexible.js以及源码分析
- Oracle-存储过程与函数
猜你喜欢

Practice (9-12 Lectures)

Unity-Text上标平方表示形式+text判断文本是否为空

Zephyr 学习笔记1,threads

zabbix监控系统自定义监控内容

1. Qt入门

Email alarm configuration of ZABBIX monitoring system

Easy to understand: understand the time series database incluxdb

L2-013 red alarm (C language) and relevant knowledge of parallel search

MySQL中的文本處理函數整理,收藏速查

Oceanbase is the leader in the magic quadrant of China's database in 2021
随机推荐
Zephyr Learning note 2, Scheduling
This article is enough for learning advanced mysql
线性代数1.1
L1-023 output gplt (20 points)
OKR vs. KPI figure out these two concepts at once!
Unity-Text上标平方表示形式+text判断文本是否为空
L1-025 positive integer a+b (15 points)
【Go基础】2 - Go基本语句
1. Qt入门
Basic DOS commands
L2-013 red alarm (C language) and relevant knowledge of parallel search
【Go基础】1 - Go Go Go
The text box displays the word (prompt text) by default, and the text disappears after clicking.
Application of isnull in database query
zabbix 5.0监控客户端
2022 - 021arts: début du deuxième semestre
2022-021ARTS:下半年开始
Text processing function sorting in mysql, quick search of collection
Rapidjson reading and writing JSON files
Linear algebra 1.1