#include<bits/stdc++.h>
using namespace std; //定义一个函数指针
typedef int (*Fun)(int,int); int add(int a,int b){return a+b;}
int sub(int a,int b){return a-b;}
int mul(int a,int b){return a*b;}
int _div(int a,int b){return b?a/b:-;} Fun fun(char op)
{
switch(op)
{
case '+':return add;
case '-':return sub;
case '*':return mul;
case '/':return _div;
default:return NULL;
}
return NULL;
} //方法1
//返回值为一个拥有两个int参数、返回类型为int的函数指针
int (*for_fun(char op))(int, int)
{
return fun(op);
}
//方法2
//直接调用函数
int fun_cal(int a,int b, char op)
{
Fun fp=fun(op);//定义一个函数指针,接收两个int整数
if(fp)
return fp(a,b);
else
return -;
} int main()
{
cout<<"100+20="<<fun_cal(,,'+')<<endl;
cout<<"100-20="<<(*for_fun('-'))(,)<<endl;
cout<<"100*20="<<fun_cal(,,'*')<<endl;
cout<<"100/20="<<(*for_fun('/'))(,)<<endl;
return ;
}

函数指针与typedef的更多相关文章

  1. c语言定义函数指针和typedef简写

    二种方法来定义函数指针 #include<stdio.h> #include<stdlib.h> #include<Windows.h> int add(int a ...

  2. typedef 函数指针 数组 std::function

    1.整型指针 typedef int* PINT;或typedef int *PINT; 2.结构体 typedef struct { double data;}DATA,  *PDATA;  //D ...

  3. 你必须知道的指针基础-7.void指针与函数指针

    一.不能动的“地址”—void指针 1.1 void指针初探 void *表示一个“不知道类型”的指针,也就不知道从这个指针地址开始多少字节为一个数据.和用int表示指针异曲同工,只是更明确是“指针” ...

  4. 【转载】C/C++ 函数指针 总结

    转载自:http://blog.csdn.net/shihui512/article/details/9787125 什么是函数指针函数指针的声明函数指针的赋值函数指针的使用将函数作为其他函数的参数在 ...

  5. C:函数指针、回调函数

    函数指针 是一个指针,指向函数的指针,指针存放的都是地址,所以函数指针存放的是函数的地址.数组名就是数组的首地址,函数名就是函数的首地址.与数组类似. 代码demo int (*p) (int ,in ...

  6. 1、C语言中的函数指针

    一 通常的函数调用 void MyFun(int x); //此处的申明也可写成:void MyFun( int ); int main(int argc, char* argv[]) { MyFun ...

  7. C语言结构体中的函数指针

      这篇文章简单的叙述一下函数指针在结构体中的应用,为后面的一系列文章打下基础 本文地址:http://www.cnblogs.com/archimedes/p/function-pointer-in ...

  8. C函数指针简单用例

    (1)函数指针:可以指向 一类 固定形参类型和返回值类型 的函数 的指针声明:int fun(int, int)    ||    \/int (*pfun)(int, int) pfun就是函数指针 ...

  9. C/C++ 函数指针使用总结

    一 函数指针介绍 函数指针指向某种特定类型,函数的类型由其参数及返回类型共同决定,与函数名无关.举例如下: int add(int nLeft,int nRight);//函数定义 该函数类型为int ...

随机推荐

  1. python 基础1

    一.python版本的介绍 python有两个大的版本2.X与3.X的版本,而在不久的将来将全面的进入3的版本.3的版本将比2的版本功能更加强大,而且也修复了大量的bug. 二.python的安装可以 ...

  2. linux之vim配置及使用示例

    作者:tongqingliu 转载请注明出处:http://www.cnblogs.com/liutongqing/p/7056193.html linux之vim配置及使用示例 vi的三种模式: 一 ...

  3. 运行Xcode时,提示:An error was encountered while running (Domain = FBSOpenApplicationErrorDomain, Code = 4)

    运行Xcode模拟器时,提示: An error was encountered while running (Domain = FBSOpenApplicationErrorDomain, Code ...

  4. poj1741(点分模板)

    #include<iostream> #include<cstring> #include<cmath> #include<cstdio> #inclu ...

  5. day17(tomcat的安装,HTTP)

    Tomcat Tomcat是个小型服务器,由apache组织编写,转为发布java程序而写. 安装步骤: 官网:http://tomcat.apache.org/ 下载地址:http://tomcat ...

  6. (数位dp)Bomb (hdu 3555)

    http://acm.hdu.edu.cn/showproblem.php?pid=3555     Problem Description The counter-terrorists found ...

  7. 【MySQL】死锁问题分析

    1.MySQL常用存储引擎的锁机制: MyISAM和MEMORY采用表级锁(table-level locking)   BDB采用页面锁(page-level locking)或表级锁,默认为页面锁 ...

  8. Hdu2204 Eddy's爱好 2017-06-27 16:11 43人阅读 评论(0) 收藏

    Eddy's爱好 Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Subm ...

  9. utf8.php

    <?php /** * */ class Utf8 { function __construct() { global $CFG; if( preg_match('/./u', '') === ...

  10. spring boot thymeleaf

    引入支持 <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spr ...