当前位置:网站首页>【C语言】封装接口(加减乘除)
【C语言】封装接口(加减乘除)
2022-06-12 03:43:00 【嵌入式职场】
目录
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("除数不能为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、测试入口
#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;
}输出结果展示:

边栏推荐
- ics-07
- 关于 国产麒麟Qt编译报错“xxx.pri has modification time xxxx s in the futrue“ 的解决方法
- 消息队列概述
- Mosaïque d'images basée sur la matrice de transformation
- R语言plotly可视化:使用plotly可视化简单线性回归模型的回归线(simple regression model linear regression plots)
- [Clickhouse column] user initialization of new library role
- 技术经济与企业管理 复习 第四章
- CA certificate and key pair application notes
- Community Conference | the mosn community will release version 1.0 and promote the evolution of the next generation architecture
- tcp 三次握手与四次挥手
猜你喜欢

Database selected 60 interview questions

Unity脚本出現missing時的解决方法

postgresql基本介绍以及部署使用

Unity脚本出现missing时的解决方法

JSP implementation of bank counter business performance evaluation system

成功解决:WARNING: There was an error checking the latest version of pip.
![[Hongmeng] use the timer to play a simple game of robbing red envelopes](/img/27/32b65dc90db7f6ece24ad39ff9b0ef.png)
[Hongmeng] use the timer to play a simple game of robbing red envelopes

$LastExitCode=0, but $?= False in PowerShell. Redirecting stderr to stdout gives NativeCommandError

2020-12-06

JSP实现银柜台业务绩效考核系统
随机推荐
Go 语法 变量
What is the difference between the gin framework of golang and the various methods of receiving parameters and various bindings?
[string] determine whether S2 is the rotation string 2 of S1
Cloud native overview
Summary -day11
Unity脚本出現missing時的解决方法
What is a request response pair called? [repeat] - what is a request response pair called? [duplicate]
Hacker + marathon =? Hacker marathon?
[Business Research Report] Research Report on super automation technology and application (2022) -- download link attached
Special materials | household appliances, white electricity, kitchen electricity
魏武帝 太祖知不可匡正,遂不复献言
顺序表与链表---初阶
PostgreSQL basic introduction and deployment
Kotlin starts the process, the difference between launch and async, and starts the process in sequence
[MySQL] MySQL installation
Sed command
[untitled]
What is the core of Web3?
Sequence list and linked list - primary level
[C language] dynamic memory allocation