当前位置:网站首页>【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;
}
边栏推荐
- JS foundation 10
- Ali three sides: what is the difference between using on or where in the left join associated table and the condition
- AcWing 607. Average 2 (implemented in C language)
- Pre parsing, recursive functions and events in day25 JS 2021.09.16
- Redis hash hash type string (5)
- [Beijing University of Aeronautics and Astronautics] information sharing for the first and second examinations of postgraduate entrance examination
- day33 js笔记 事件(下)2021.09.28
- Which programming language will attract excellent talents?
- Web3 security serials (3) | in depth disclosure of NFT fishing process and prevention techniques
- 水果FL Studio/Cubase/Studio one音乐宿主软件对比
猜你喜欢

Day37 JS note motion function 2021.10.11

Practice and Thinking on the architecture of a set of 100000 TPS im integrated message system

.NET混合开发解决方案24 WebView2对比CefSharp的超强优势

The default point of this in JS and how to modify it to 2021.11.09

Necessary for beginners PR 2021 quick start tutorial, PR green screen matting operation method

2018 joint examination of nine provinces & Merging of line segment trees

水果FL Studio/Cubase/Studio one音乐宿主软件对比

Unity screenshot function

Remote login sshd service

Characteristics of solar wireless LED display
随机推荐
How to deploy the software testing environment?
Industry analysis - quick intercom, building intercom
如临现场的视觉感染力,NBA决赛直播还能这样看?
面试步骤的面试技巧
Redis hash hash type string (5)
Oracle date format exception: invalid number
Share the easy-to-use fastadmin open source system - practical part
Connectionreseterror: [winerror 10054] the remote host forced an existing connection to be closed
智联招聘基于 Nebula Graph 的推荐实践分享
js中this的默认指向及如何修改指向 2021.11.09
Graphics view framework for QT learning (to realize startup animation)
Swin, three degrees! Eth open source VRT: a transformer that refreshes multi domain indicators of video restoration
Day23 JS notes 2021.09.14
Timestamp and date conversion "suggested collection"
Ali three sides: what is the difference between using on or where in the left join associated table and the condition
FTP protocol for Wireshark packet capture analysis
The development and principle of the metacosmic system
day28 严格模式、字符串 js 2021.09.22
AcWing 610. Salary and bonus (implemented in C language)
Setinterval, setTimeout and requestanimationframe