当前位置:网站首页>【C语言】如何很好的实现复数类型
【C语言】如何很好的实现复数类型
2022-06-28 11:36:00 【贾璞】
首先声明:在C99标准中已经支持了复数类型。
在此不谈论这个,想了解的可以直接去查一查,这里说一下实现复数类型及运算。其实也很简单,直接定义一个简单结构和相关的算数函数就OK了。
下面是实现过程:
#include "stdio.h"
#define Real(c) (c).real
#define Imag(c) (c).imag
typedef struct {
double real;
double imag;
}complex;
complex cpx_make(double real, double imag)
{
complex ret;
ret.real = real;
ret.imag = imag;
return ret;
}
complex cpx_add(complex a, complex b)
{
return cpx_make(Real(a) + Real(b), Imag(a) + Imag(b));
}
int main()
{
complex a = cpx_make(1, 2);
complex b = cpx_make(3, 4);
//亦可直接如下写法
printf("%f", cpx_add(cpx_make(1, 2), cpx_make(3, 4)));
return 0;
}
边栏推荐
- mysql-. SQL file phishing Online
- Web page tips this site is unsafe solution
- Redis hash hash type string (5)
- What is the main chain system?
- Deployment and optimization of vsftpd service
- Recommended practice sharing of Zhilian recruitment based on Nebula graph
- Packaging and publishing application of jetpack compose desktop version
- Day28 strict mode, string JS 2021.09.22
- MapReduce项目案例3——温度统计
- Mysql使用max函数查询不到最大值
猜你喜欢
Self use demo of basic component integration of fluent
For example, the visual appeal of the live broadcast of NBA Finals can be seen like this?
New listing of operation light 3.0 - a sincere work of self subversion across the times!
Graphics view framework for QT learning (to realize startup animation)
Class pattern and syntax in JS 2021.11.10
Web page tips this site is unsafe solution
Unity screenshot function
Day36 JS notes ecma6 syntax 2021.10.09
day39 原型链及页面烟花效果 2021.10.13
Database Series: is there any way to seamlessly upgrade the business tables of the database
随机推荐
recent developments
零基础C语言(一)
[Beijing University of Aeronautics and Astronautics] information sharing for the first and second examinations of postgraduate entrance examination
Machine learning project captcha based on verification code recognition_ Trainer operation practice
Cohere, a large model company, completed the round B financing of US $125million
Everyone can participate in open source! Here comes the most important developer activity in dragon lizard community
Fancy features and cheap prices! What is the true strength of Changan's new SUV?
Unity screenshot function
day37 js笔记 运动函数 2021.10.11
[sciter]: use of sciter FS module scanning file API and its details
太阳能无线LED显示屏的特点
Daily practice of C language - day 4: find the sum of all even numbers within 100
How to deploy the software testing environment?
Still using simpledateformat for time formatting? Be careful that the project collapses!
AcWing 606. Average 1 (implemented in C language)
day28 严格模式、字符串 js 2021.09.22
携手Cigent:群联为SSD主控固件引入高级网络安全防护特性
Day32 JS note event (Part 1) September 27, 2021
Swin, three degrees! Eth open source VRT: a transformer that refreshes multi domain indicators of video restoration
QML控件类型:TabBar