zoukankan      html  css  js  c++  java
  • 指向函数的指针

    #include "stdio.h"
    #include 
    "conio.h"
    #define M 8
    float max(float a[],int n);    /*函数声明*/
    void main()
    {
      clrscr();
      
    float sumf,sump;
      
    float a[M]={11,2,-3,4.5,5,69,7,80};
      
    float (*p)(float a[],int);   /*定义指向函数的指针 p*/
      p
    =max;                       /*函数名(函数入口地址)赋给指针p*/
      sump
    =(*p)(a,M);              /*用指针方式调用函数*/
      sumf
    =max(a,M);               /*用函数名调用max()函数*/
      printf(
    "sump=%.2f\n",sump);
      printf(
    "sumf=%.2f\n",sumf);
    }


    float max(float a[],int n)
    {
      
    int k;
      
    float s;
      s
    =a[0];
      
    for(k=0;k<n;k++)
        
    if(s<a[k]) s=a[k];
      
    return s;
    }

    在主函数中输入三角形的两条直角边,求三角形的面积、斜边长

    #include "stdio.h"
    #include 
    "conio.h"
    #include 
    "math.h"
    float area(int,int);
    float length(int,int);
    float f(int,int,float (*p)(int,int));
    void main()
    {
      clrscr();
      
    int m,n;
      
    float s,l;
      
    float (*q)(int,int);
      scanf(
    "%d,%d",&m,&n);
      q
    =area;
      s
    =f(m,n,q);
      l
    =f(m,n,length);
      printf(
    "Area of the right triangle is %.2f\n",s);
      printf(
    "Length of the hypotenuse is %.2f\n",l);
    }


    float area(int a,int b)
    {
      
    float z;
      z
    =a*b/2;
      
    return z;
    }


    float length(int a,int b)
    {
      
    float z;
      z
    =sqrt(a*a+b*b);
      
    return z;
    }


    float f(int a,int b,float (*p)(int,int))
    {
      
    float y;
      y
    =(*p)(a,b);
      
    return y;
    }
  • 相关阅读:
    Leetcode OJ: Rotate List
    Leetcode OJ: Reverse Words in a String
    Effective C++读书笔记
    word改变下划线与文字的距离
    sql 取出表中不重复的值
    Iso文件用utrliso怎么安装
    Spring注入aspectJ切面
    Spring中利用java注解声明切面
    Spring面向切面编程
    spring中部分异常
  • 原文地址:https://www.cnblogs.com/qixin622/p/623845.html
Copyright © 2011-2022 走看看