当前位置:网站首页>【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;
}
边栏推荐
- Int~long long indicates the maximum and minimum number
- Simulation of the Saier lottery to seek expectation
- IO stream of file and Base64
- Cohere, a large model company, completed the round B financing of US $125million
- Why do many people want to change careers as programmers, while some programmers want to change careers as others?
- day39 原型链及页面烟花效果 2021.10.13
- Day32 JS note event (Part 1) September 27, 2021
- Mysql使用max函数查询不到最大值
- 水果FL Studio/Cubase/Studio one音乐宿主软件对比
- Prepare for Jin San Yin Si I. testers without experience in automated testing projects should look at it quickly
猜你喜欢

Day39 prototype chain and page fireworks effect 2021.10.13

SEO优化的许多好处是与流量有直接关系

纯纯大怨种!那些年被劝退的考研专业

建立自己的网站(18)

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

多维度监控:智能监控的数据基础
![[Beijing University of Aeronautics and Astronautics] information sharing for the first and second examinations of postgraduate entrance examination](/img/06/df5a64441814c9ecfa2f039318496e.jpg)
[Beijing University of Aeronautics and Astronautics] information sharing for the first and second examinations of postgraduate entrance examination

Using soapUI to obtain freemaker's FTL file template

Everyone can participate in open source! Here comes the most important developer activity in dragon lizard community

Simple understanding of ThreadLocal
随机推荐
Day29 JS notes 2021.09.23
Oracle date format exception: invalid number
《运营之光3.0》全新上市——跨越时代,自我颠覆的诚意之作!
.NET混合开发解决方案24 WebView2对比CefSharp的超强优势
mysql-. SQL file phishing Online
Industry analysis - quick intercom, building intercom
QML control type: tabbar
Why do many people want to change careers as programmers, while some programmers want to change careers as others?
What is DAPP system development and analytical understanding
Solutions to connection failures and errors when accessing mysql8 using the SSM project
Unity屏幕截图功能
Fancy features and cheap prices! What is the true strength of Changan's new SUV?
[Beijing University of Aeronautics and Astronautics] information sharing for the first and second examinations of postgraduate entrance examination
Simple understanding of ThreadLocal
买股票在中金证券经理的开户二维码上开户安全吗?求大神赐教
行业分析| 快对讲,楼宇对讲
js中this的默认指向及如何修改指向 2021.11.09
Class pattern and syntax in JS 2021.11.10
Convert black mask picture to color annotation file
AcWing 610. Salary and bonus (implemented in C language)