Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何使用函数指针执行算术运算?_C_Math_Function Pointers_Arithmetic Expressions - Fatal编程技术网

C 如何使用函数指针执行算术运算?

C 如何使用函数指针执行算术运算?,c,math,function-pointers,arithmetic-expressions,C,Math,Function Pointers,Arithmetic Expressions,所以..我知道如果我把*ptr作为函数f,那么 res = (*ptr)(a,b) is the same as res = f(a,b). 现在我的问题是,我必须读入3个整数。前两个是操作数,第三个是运算符,例如1=加,2=减,3=乘,4=除。如果没有if或switch语句,我怎么做呢 我在考虑两种可能的解决办法 创建4个指针,并将每个指针分别指向一个算术运算,但我仍然需要做一些输入 需要if或switch语句的验证 这并不是一个真正的解决方案,但是基本的想法可能是这样的。如果c=运算符,

所以..我知道如果我把*ptr作为函数f,那么

res = (*ptr)(a,b) is the same as res = f(a,b). 
现在我的问题是,我必须读入3个整数。前两个是操作数,第三个是运算符,例如1=加,2=减,3=乘,4=除。如果没有if或switch语句,我怎么做呢

我在考虑两种可能的解决办法

创建4个指针,并将每个指针分别指向一个算术运算,但我仍然需要做一些输入 需要if或switch语句的验证

这并不是一个真正的解决方案,但是基本的想法可能是这样的。如果c=运算符,那么我可以做一些类似res的事情= *但是我不认为C有这样的语法

样本输入

1 2 1

1 2 2

1 2 3

1 2 4
样本输出

 3

-1

 2

 0 
我的代码:

#include <stdio.h>

//Datatype Declarations
typedef int (*arithFuncPtr)(int, int);


//Function Prototypes
int add(int x, int y);


int main()
{
    int a, b, optype, res;

    arithFuncPtr ptr;

    //ptr points to the function add
    ptr = add;

    scanf("%i %i", &a, &b);

    res = (*ptr)(a, b);

    printf("%i\n", res);

    return 0;
}

int add(int x, int y)
{
    return x+y;
}

您可以将函数指针放入数组中

#include <stdio.h>

//Datatype Declarations
typedef int (*arithFuncPtr)(int, int);


//Function Prototypes
int add(int x, int y);
int sub(int x, int y);
int mul(int x, int y);
int div(int x, int y);

int main()
{
    int a, b, optype, res;

    arithFuncPtr ptr[4];

    //ptr points to the function
    ptr[0] = add;
    ptr[1] = sub;
    ptr[2] = mul;
    ptr[3] = div;

    scanf("%i %i %i", &a, &b, &optype);

    res = (ptr[optype - 1])(a, b);

    printf("%i\n", res);

    return 0;
}

int add(int x, int y)
{
    return x+y;
}  

int sub(int x, int y)
{
    return x-y;
}  

int mul(int x, int y)
{
    return x*y;
}  

int div(int x, int y)
{
    return x/y;
}  

要检查它,我必须使用if语句。我正试图找到一种方法来检查哪个操作符没有if语句。你如何创建一个函数指针数组,并根据你正在执行的操作,创造性地调用相应的函数索引?op-1,其中op是所需的操作,函数指针数组分别包括加法、减法、乘法和除法函数地址;线显然,这是一个家庭作业:P谢谢你的帮助:好的,不用担心,我在代码中看到了它的用法。非常感谢!实际上,我已经在没有arithFuncPtr ptr的情况下编写了这段代码[4]。一定是和typedef混淆了。不太清楚。太棒了,我写了完全一样的东西。为了防止越界,在没有ifs的情况下,可以使用模运算。这是一个问题,不值得再回答。