• 全国计算机等级考试二级教程-C语言程序设计_第11章_对函数的进一步讨论


    无论传递什么参数,函数都有副本机制

    改变一个变量,需要传入变量的地址

    改变一个指针变量,需要传入指针变量的地址

    //int add(int a, int b);挖取函数声明

    //int ()(int a, int b);换成括号
    //int (*p)(int a, int b);加上*指针名

     1 #define _CRT_SECURE_NO_WARNINGS
     2 
     3 #include<stdio.h>
     4 #include<stdlib.h>
     5 
     6 int add(int a, int b)
     7 {
     8     return a + b;
     9 }
    10 
    11 main()
    12 {
    13     //int add(int a, int b);挖取函数声明
    14     //int ()(int a, int b);换成括号
    15     //int (*p)(int a, int b);加上*指针名
    16 
    17     int(*p)(int a, int b) = add;
    18     printf("%d", p(1, 2));
    19 
    20     system("pause");
    21 }

    例11.2

    通过给 trans 函数传送不同的函数名,求 tan x 和 cot x 值。

     1 #include <stdio.h>
     2 #include <math.h>
     3 double tran(double(*) (double), double(*) (double), double);        /* 函数说明语句 */
     4 main()
     5 {
     6     double y, v;
     7     v = 60 * 3.1416 / 180.0;
     8     y = tran(sin, cos, v);        /* 第一次调用 */
     9     printf("tan(60)=%10.6f
    ", y);
    10     y = tran(cos, sin, v);        /* 第二次调用 */
    11     printf("cot(60)=%10.6f
    ", y);
    12 }
    13 double tran(double(*f1) (double), double(*f2) (double), double x)
    14 {
    15     return (*f1) (x) / (*f2)(x);
    16 }

    例11.3

    用递归的方法求n!

    求n!可用以下数学关系表示:

    n!= 1           当n=0时

    n!= n * ( n - 1 )!  当n>0时

     1 #include <stdio.h>
     2 int fac(int n)
     3 {
     4     int t;
     5     if (n == 1 || n == 0)
     6     {
     7         return 1;
     8     }
     9     else
    10     {
    11         t = n*fac(n - 1);
    12         return t;
    13     }
    14 }
    15 main()
    16 {
    17     int m, y;
    18     printf("Enter m:");
    19     scanf("%d", &m);
    20     if (m < 0)
    21     {
    22         printf("Input data error !
    ");
    23     }
    24     else
    25     {
    26         y = fac(m);
    27         printf("
    %d!=%d
    ", m, y);
    28     }
    29 }

    例11.4

    用递归算法根据以下求平方根的迭代公式求某数 a 的平方根:

    x1 = (x0 + a / x0) / 2
     1 #include <stdio.h>
     2 #include <math.h>
     3 double mysqrt(double a, double x0)
     4 {
     5     double x1;
     6     x1 = (x0 + a / x0) / 2.0;
     7     if (fabs(x1 - x0) > 0.00001)
     8     {
     9         return mysqrt(a, x1);
    10     }
    11     else
    12     {
    13         return x1;
    14     }
    15 }
    16 main()
    17 {
    18     double a;
    19     printf("Enter a:");
    20     scanf("%lf", &a);
    21     printf("The sqrt of %f=%f
    ", a, mysqrt(a, 1.0));
    22 }

    11.12

    请编写递归函数,把输入的一个整数转换成二进制数输出。

     1 #include <stdio.h>
     2 void outninary(int a)
     3 {
     4     int d;
     5     d = a % 2;
     6     if (a != 0)
     7     {
     8         outninary(a / 2);
     9         printf("%d", d);
    10     }
    11 }
    12 main()
    13 {
    14     int a;
    15     scanf("%d", &a);
    16     outninary(a);
    17 }

    11.13

    请用递归算法,求 1+2+3+...+n,n 由键盘输入。

     1 #include <stdio.h>
     2 int sum(int n)
     3 {
     4     if (n != 0)
     5     {
     6         return n + sum(n - 1);
     7     }
     8     else
     9     {
    10         return 0;
    11     }
    12 }
    13 main()
    14 {
    15     int n, y;
    16     scanf("%d", &n);
    17     y = sum(n);
    18     printf("y=%d", y);
    19 }

    11.14

    请用递归算法,求数列 Fibonacci。求 n 阶 Fibonacci 数列的公式如下:

             1         当 n=0 时

    F(n)= 1         当 n=1 时

             F(n-1)+F(n-2)  当 n>1 时

     1 #include <stdio.h>
     2 int f(int n)
     3 {
     4     if (n == 0 || n == 1)
     5     {
     6         return 1;
     7     }
     8     else
     9     {
    10         return (f(n - 1) + f(n - 2));
    11     }
    12 }
    13 main()
    14 {
    15     int n, y;
    16     scanf("%d", &n);
    17     y = f(n);
    18     printf("y=%d", y);
    19 }
  • 相关阅读:
    【K8S】Kubernetes: --image-pull-policy always does not work
    【Maven插件】exec-maven-plugin
    【分布式事务】微服务架构下的分布式事务问题
    【Jenkins】新版本的特性:自定义流水线
    【Kibana】自定义contextPath
    【Zuul】Zuul过滤器参考资料
    【Spring】Springboot监听器,启动之后初始化工作
    【Spring】bean动态注册到spring
    【Java-JPA】让Springboot启动不检查JPA的数据源配置
    linux-批量杀死进程
  • 原文地址:https://www.cnblogs.com/denggelin/p/5424053.html
Copyright © 2020-2023  润新知