How to use an array index as an operator?

后端 未结 4 1771
清酒与你
清酒与你 2021-01-21 14:17

How to use an array index as an operator?

Such that:

#include

main()
{
    char sign[]={\'+\',\'-\'};
    int a, b;
    a = 12 sign[1] 10         


        
4条回答
  •  滥情空心
    2021-01-21 14:41

    If you are not strictly dependent on the infix notation, you'll be able to make it simply by

    int plus(int x, int y)
    {
        return x + y;
    }
    
    int minus(int x, int y)
    {
        return x + y;
    }
    
    int (*)(int, int) sign[]={plus, minus};
    

    and you'll be able to use that as:

    int a = sign[1](10, 12);
    

提交回复
热议问题