当前位置:网站首页>[C language] encapsulation interface (addition, subtraction, multiplication and division)
[C language] encapsulation interface (addition, subtraction, multiplication and division)
2022-06-12 03:48:00 【Embedded workplace】
Catalog
1、operator.h
//
// Created by cjs0719 on 2022/6/11.
//
#ifndef TEST_OPERATOR_H
#define TEST_OPERATOR_H
typedef struct {
double (* Add)(double n, double m);
double (* Sub)(double n, double m);
double (* Mul)(double n, double m);
double (* Div)(double n, double m);
}FAN_OPERATOR;
extern FAN_OPERATOR FanOperator;
void InitOperator(void);
#endif //TEST_OPERATOR_H
2、operator.c
//
// Created by cjs0719 on 2022/6/11.
//
#include "operator.h"
#include <stdio.h>
#include <stdlib.h>
static double add(double n, double m)
{
return m + n;
}
static double sub(double n, double m)
{
return n - m;
}
static double mul(double n, double m)
{
return n * m;
}
static double divnum(double n, double m)
{
if(m == 0)
{
printf(" The divisor cannot be zero 0,m=%f",m);
return 0.0;
}
return n/m;
}
void InitOperator(void)
{
FanOperator.Add = add;
FanOperator.Sub = sub;
FanOperator.Mul = mul;
FanOperator.Div = divnum;
}3、 Test entrance
#include <stdio.h>
#include "operator.h"
FAN_OPERATOR FanOperator;
int main() {
InitOperator();
double addnum = FanOperator.Add(23,56);
printf("addnum:%.2f\n",addnum);
double subnum = FanOperator.Sub(12,23);
printf("subnum:%.2f\n",subnum);
double mulnum = FanOperator.Mul(20,30);
printf("mulnum:%.2f\n",mulnum);
double divnum = FanOperator.Div(24,3);
printf("divnum:%.2f\n",divnum);
return 0;
}The output shows :

边栏推荐
猜你喜欢

19. Optimized database query of tornado project

JSP实现银柜台业务绩效考核系统

JSP implementation of bank counter business performance evaluation system

Naive Bayes classification of scikit learn

Youcans' opencv lesson - 10 Image restoration and reconstruction

【鸿蒙】 使用定时器做一个简单的抢红包小游戏

Special materials | household appliances, white electricity, kitchen electricity

mysql/oracle 以唯一时间为分界,小于等于该时间求和,大于该时间求和

string manipulation:

【C语言】程序的内存四区模型
随机推荐
R语言write函数:使用write函数将数据写入RStudio控制台、write函数将数据写入新的Rstudio窗口(启动新的窗口)
R语言plotly可视化:plotly可视化基础二维直方图、自定义设置二维直方图的颜色、二维直方图是二元分布的直方图可视化(Basic 2D Histogram)
Mongodb essence summary
19. Optimized database query of tornado project
Unity脚本出现missing时的解决方法
[Bank Research Report] technology enabled retail finance carbon neutral development report (2022) - download link attached
[string] judge whether S2 is the rotation string of S1
Hacker + marathon =? Hacker marathon?
怎样拥有心灵的平和?获得一颗全新的心灵
Key points of code neatness (III)
(idea)the file size(2.85M) exceeds configured limit(2.56M).Code insight features are not available问题
Use of pnpm package management tool
[Business Research Report] 2021 global mobile game player white paper - download link attached
Functions (arguments, formal parameters, bubbling)
What is the difference between the gin framework of golang and the various methods of receiving parameters and various bindings?
【C语言】变量本质分析
关于 国产麒麟Qt编译报错“xxx.pri has modification time xxxx s in the futrue“ 的解决方法
R语言plotly可视化:使用plotly可视化简单线性回归模型的回归线(simple regression model linear regression plots)
Mysql/oracle takes the unique time as the boundary, and the sum is less than or equal to this time, and greater than this time
MySQL create user and authorize