鱼C论坛

 找回密码
 立即注册
查看: 1554|回复: 5

[已解决]求解答

[复制链接]
发表于 2022-3-27 14:30:46 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
3、(基础题)函数指针的应用:sin(x)、cos(x)、tan(x)都是三角函数,形参、函数返回结果都是double类型,它们的声明、定义已包含在math.h中。请编写编程实现如下功能:根据输入的整数(1、2、3)分别调用sin(x)、cos(x)、tan(x),x的值也需要输入,请补充程序所缺代码:
#include <stdio.h>
#include <math.h>
int main()
{
        int n;
        double x;
        printf("请输入整数1,2,3(分别调用sin(x)、cos(x)、tan(x)):");
        scanf("%d",&n);
        printf("请输入x的值:");
        scanf("%lf",&x);
                       (1)                 //定义指向函数的指针变量;
        void fun(double (*p)(double z),double x, int n)&#160;;//函数声明
                       (2)                 //调用fun()函数
        return 0;
}
//函数功能: 根据n的值(1,2,3)分别调用sin(x)、cos(x)、tan(x),并输出结果;n为其它值时,提示“输入的数据有误,不能调用任何函数!”
void fun(double (*p)(double z),double x, int n)
{
        //函数的实现代码
}
最佳答案
2022-3-27 18:23:15
kjdskaf 发表于 2022-3-27 18:10
不行啊,显示了,第40行

        使用了可能未初始化的本地指针变量“format”

我测试没有问题,如果真的不行,就不要这样写了,一行一行打印吧:
  1. #include <stdio.h>
  2. #include <math.h>
  3. int main()
  4. {
  5.     int n;
  6.     double x;
  7.     printf("请输入整数1,2,3(分别调用sin(x)、cos(x)、tan(x)):");
  8.     scanf("%d", &n);
  9.     printf("请输入x的值:");
  10.     scanf("%lf", &x);
  11.     double(*p)(double) = NULL;//定义指向函数的指针变量;
  12.     void fun(double (*p)(double z), double x, int n);//函数声明
  13.     fun(p, x, n);                //调用fun()函数
  14.     return 0;
  15. }
  16. //函数功能: 根据n的值(1,2,3)分别调用sin(x)、cos(x)、tan(x),并输出结果;n为其它值时,提示“输入的数据有误,不能调用任何函数!”
  17. void fun(double (*p)(double z), double x, int n)
  18. {
  19.     double y;
  20.     if (n == 1)
  21.     {
  22.         p = sin;
  23.         y = (*p)(x);
  24.         printf("sin(%.2f)=%f\n\n", x, y);
  25.     }
  26.     else if (n == 2)
  27.     {
  28.         p = cos;
  29.         y = (*p)(x);
  30.         printf("cos(%.2f)=%f\n\n", x, y);
  31.     }
  32.     else if (n == 3)
  33.     {
  34.         p = tan;
  35.         y = (*p)(x);
  36.         printf("tan(%.2f)=%f\n\n", x, y);
  37.     }

  38.     if ((n == 1) || (n == 2) || (n == 3))
  39.     {
  40.         ;
  41.     }
  42.     else
  43.         printf("输入数据有误,不能调用任何函数!\n");
  44. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-3-27 15:12:29 | 显示全部楼层
  1. #include <stdio.h>
  2. #include <math.h>

  3. int main() {
  4.         int n;
  5.         double x;
  6.         printf("请输入整数 1, 2, 3 (分别调用sin(x)、cos(x)、tan(x)):");
  7.         scanf("%d", &n);
  8.         printf("请输入 x 的值:");
  9.         scanf("%lf", &x);
  10.         double(*a)(double) = sin;
  11.         double(*b)(double) = cos;
  12.         double(*c)(double) = tan;
  13.         double(*p)(double) = NULL;
  14.         void fun(double (*p)(double z), double x, int n); // 函数声明
  15.         switch (n)
  16.         {
  17.         case 1:
  18.                 fun(a, x, n);
  19.                 break;
  20.         case 2:
  21.                 fun(b, x, n);
  22.                 break;
  23.         case 3:
  24.                 fun(c, x, n);
  25.                 break;
  26.         default:
  27.                 fun(p, x, n);
  28.                 break;
  29.         }
  30.         return 0;
  31. }

  32. void fun(double(*p)(double), double x, int n) {
  33.         if (n < 1 || n > 3) {
  34.                 printf("输入的数据有误,不能调用任何函数!");
  35.                 return;
  36.         }
  37.         printf("%lf", p(x));
  38. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-3-27 15:32:17 | 显示全部楼层

#include <stdio.h>
#include <math.h>
int main()
{
        int n;
        double x;
        printf("请输入整数1,2,3(分别调用sin(x)、cos(x)、tan(x)):");
        scanf("%d", &n);
        printf("请输入x的值:");
        scanf("%lf", &x);
        double(*p)(double) = NULL;//定义指向函数的指针变量;
                void fun(double (*p)(double z), double x, int n);//函数声明
                fun(p,x,n);                //调用fun()函数
                return 0;
}
//函数功能: 根据n的值(1,2,3)分别调用sin(x)、cos(x)、tan(x),并输出结果;n为其它值时,提示“输入的数据有误,不能调用任何函数!”
void fun(double (*p)(double z), double x, int n)
{
        double y;
        char* format;
        if (n == 1)
        {
                p = sin;
                format = "sin(%.2f)=%f\n\n";
        }
        else if (n == 2)
        {
                p = cos;
                format = "cos(%.2f)=%f\n\n";
        }
        else if (n == 3)
        {
                p = tan;
                format = "tan(%.2f)=%f\n\n";
        }

        if ((n == 1) || (n == 2) || (n == 3))
        {
                y = (*p)(x);
                printf("%s,%f,%f",format, x, y);
        }
        else
                printf("输入数据有误,不能调用任何函数!\n");
}
我这个哪里错了,该怎么修改?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-27 16:26:36 | 显示全部楼层
kjdskaf 发表于 2022-3-27 15:32
#include
#include
int main()

  1. #include <stdio.h>
  2. #include <math.h>
  3. int main()
  4. {
  5.     int n;
  6.     double x;
  7.     printf("请输入整数1,2,3(分别调用sin(x)、cos(x)、tan(x)):");
  8.     scanf("%d", &n);
  9.     printf("请输入x的值:");
  10.     scanf("%lf", &x);
  11.     double(*p)(double) = NULL;//定义指向函数的指针变量;
  12.     void fun(double (*p)(double z), double x, int n);//函数声明
  13.     fun(p, x, n);                //调用fun()函数
  14.     return 0;
  15. }
  16. //函数功能: 根据n的值(1,2,3)分别调用sin(x)、cos(x)、tan(x),并输出结果;n为其它值时,提示“输入的数据有误,不能调用任何函数!”
  17. void fun(double (*p)(double z), double x, int n)
  18. {
  19.     double y;
  20.     char* format;
  21.     if (n == 1)
  22.     {
  23.         p = sin;
  24.         format = "sin(%.2f)=%f\n\n";
  25.     }
  26.     else if (n == 2)
  27.     {
  28.         p = cos;
  29.         format = "cos(%.2f)=%f\n\n";
  30.     }
  31.     else if (n == 3)
  32.     {
  33.         p = tan;
  34.         format = "tan(%.2f)=%f\n\n";
  35.     }

  36.     if ((n == 1) || (n == 2) || (n == 3))
  37.     {
  38.         y = (*p)(x);
  39.         printf(format, x, y); // <------------- 改这里 -------------
  40.     }
  41.     else
  42.         printf("输入数据有误,不能调用任何函数!\n");
  43. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-3-27 18:10:22 | 显示全部楼层


不行啊,显示了,第40行

        使用了可能未初始化的本地指针变量“format”       
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-27 18:23:15 | 显示全部楼层    本楼为最佳答案   
kjdskaf 发表于 2022-3-27 18:10
不行啊,显示了,第40行

        使用了可能未初始化的本地指针变量“format”

我测试没有问题,如果真的不行,就不要这样写了,一行一行打印吧:
  1. #include <stdio.h>
  2. #include <math.h>
  3. int main()
  4. {
  5.     int n;
  6.     double x;
  7.     printf("请输入整数1,2,3(分别调用sin(x)、cos(x)、tan(x)):");
  8.     scanf("%d", &n);
  9.     printf("请输入x的值:");
  10.     scanf("%lf", &x);
  11.     double(*p)(double) = NULL;//定义指向函数的指针变量;
  12.     void fun(double (*p)(double z), double x, int n);//函数声明
  13.     fun(p, x, n);                //调用fun()函数
  14.     return 0;
  15. }
  16. //函数功能: 根据n的值(1,2,3)分别调用sin(x)、cos(x)、tan(x),并输出结果;n为其它值时,提示“输入的数据有误,不能调用任何函数!”
  17. void fun(double (*p)(double z), double x, int n)
  18. {
  19.     double y;
  20.     if (n == 1)
  21.     {
  22.         p = sin;
  23.         y = (*p)(x);
  24.         printf("sin(%.2f)=%f\n\n", x, y);
  25.     }
  26.     else if (n == 2)
  27.     {
  28.         p = cos;
  29.         y = (*p)(x);
  30.         printf("cos(%.2f)=%f\n\n", x, y);
  31.     }
  32.     else if (n == 3)
  33.     {
  34.         p = tan;
  35.         y = (*p)(x);
  36.         printf("tan(%.2f)=%f\n\n", x, y);
  37.     }

  38.     if ((n == 1) || (n == 2) || (n == 3))
  39.     {
  40.         ;
  41.     }
  42.     else
  43.         printf("输入数据有误,不能调用任何函数!\n");
  44. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-24 16:12

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表