指向函数的指针/返回指针的函数

1.函数指针表示指向某一函数的指针,如下所示,

int add(int,int);

int (*pf)(int,int);

pf=add;

注意,因为函数add有两个参数,所以pf指针也必须有两个参数;且函数add的两个参数均为int型,所以pf的两个参数也必须与之匹配。如下例,

#include“stdio.h"
#include "conio.h"

 

int add(int,int);   //加法器
int substract(int,int);   //减法器
int multiply(int,int);   //乘法器
int divide(int,int);   //除法器
int (*operation)(int,int);   //函数指针

 

int main()
{
 int x,y,output;
 printf("请输入x和y的值:");
 scanf("%d %d",&x,&y);
 operation=add;
 output=(*operation)(x,y);
 printf("%d+%d=%d\n",x,y,output);

 

 operation=substract;
 output=(*operation)(x,y);
 printf("%d-%d=%d\n",x,y,output);

 

 operation=multiply;
 output=(*operation)(x,y);
 printf("%d*%d=%d\n",x,y,output);

 

 operation=divide;
 output=(*operation)(x,y);
 printf("%d/%d=%d\n",x,y,output);

 getch(); //体会它的作用
 return 0;
}

 

int add(int a,int b)
{
 return a+b;
}

 

int substract(int a,int b)
{
 return a-b;
}

 

int multiply(int a,int b)
{
 return a*b;
}

 

int divide(int a,int b)
{
 return a/b;
}

应用,通过指向函数的指针计算各种函数的定积分,计分方法为梯形积分法:

#include <iostream>
#include <cmath>

using namespace std;

//自定义函数:被积函数求值
double sinx(double x)
{
    return sin(x);
}
double e_sqrx(double x)
{
    return exp(-x*x);
}
double x_sqrx(double x)
{
    return 2*x/(1+x*x);
}

//自定义梯形法求定积分的函数
double integral(double a,double b,double (*fun)(double),int n)
{
    double h=(b-a)/n;
    double sum=((*fun)(a)+(*fun)(b))/2;
    int i;
    for(i=0;i<n;i++)
    {
        sum+=(*fun)(a+i*h);
    }
    sum *= h;
    return sum;
}

//主调函数,调用integral函数求定积分
int main()
{
    double s=integral(0,3.1415926/2,sinx,1000);
    cout<<"函数sinx(x)在[0,PI/2]区间的定积分="<<s<<endl;
    s=integral(0,1,e_sqrx,1000);
    cout<<"函数e的-x次平方在[0,1]区间的定积分="<<s<<endl;
    s=integral(-1,5,x_sqrx,1000);
    cout<<"函数2*x/(1+x*x)在[-1,5]区间的定积分="<<s<<endl;
    return 0;
}

运行结果:



使用函数指针切换加密方法:1.凯撒加密,即将将原来的小写字母用字母表中其后面的第3个字母的大写形式来替换,大写字母按同样规则用小写字母替换,可将字母表看成是首末衔接的。例如"AMDxyzXYZ" 加密为 "dpgABCabc"。2.单双号加密,即将字符串"abcde",根据单双号区分为两个字符串"ace"和"bd",再连接在一起成为密文"acebd"。

输入样例:

jacky

2


输出样例:

jcyak

#include <iostream>
#include <cmath>
#include <cstring>

using namespace std;

void caesar(char s[]);
void oddeven(char s[]);
void cipher(void(*f)(char s[]),char s[]);//形参为指向函数的指针,对应实参可为相应格式的函数名。

//凯撒加密
void caesar(char s[])
{
    int n=0;
    int len=strlen(s);
    do{
      if (s[n] == 0)
        s[n] = 32;
      else if (s[n]>64 && s[n]<88)
        s[n] = s[n]+35;
      else if (s[n]>87 && s[n]<91)
        s[n] = s[n]+11;
      else if (s[n]> 96 &&s[n]<120)
        s[n] = s[n]-29;
      else if (s[n] > 119)
        s[n] = s[n]-55;
      n++;
    }while(n<len);
    cout<<s<<endl;
}

//奇数加密
void oddeven(char s[])
{
    int len=strlen(s);
    char temp1[100]={'\0'};
    char temp2[100]={'\0'};
    int i=0,j=0,k=0;
    while(i <len)
    {
        if(i%2 == 0)
            temp1[j++]=s[i];
        else
            temp2[k++]=s[i];
        i++;
    }
    strcat(temp1,temp2);  //将temp2加到temp1的末尾
    cout<<temp1<<endl;
}
void cipher(void(*f)(char s[]),char s[])
{
    (*f)(s);
}
int main()
{
    int cmd;
    char s[100];
    cin>>s;
    cin>>cmd;
    switch(cmd)
    {
    case 1:
        cipher(caesar,s);  //密令1,则执行凯撒加密
        break;
    case 2:
        cipher(oddeven,s);  //命令2,则执行奇数加密
        break;
    default:
        break;
    }
    return 0;
}

2.返回指针的函数

函数,其返回值为指针。

#include <stdio.h>
int main()
{
    float score[3][4]={{60,70,80,90},{56,89,67,88},{34,78,90,66}};
    float *search(float(*pointer)[4],int ),*p;
    int i,m;
    printf("Enter the number of student:");
    scanf("%d",&m);
    printf("The score of No.%d are:\n",m);
    p=search(score,m);  //指向第m个学生的所在的行
    for(i=0;i<4;i++)
        printf("%5.2f\t",*(p+i));  //指向第m个学生的各个成绩
    return 0;
}
float *search(float (*pointer)[4],int n)
{
    float *pt;
    pt=*(pointer+n);
    return pt;
}
其原理表示如下:



运行结果:




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值