How to use an array index as an operator?

后端 未结 4 1768
清酒与你
清酒与你 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:30

    The simplest is to use a switch statement or if-else chain.

    #include<stdio.h>
    
    int main() {
        char sign[]={'+','-'};
        int a,b;
    
        switch (sign[1]) {
            case '+':
                a = 12 + 10;
                break;
    
            case '-':
                a = 12 - 10;
                break;
        }
    
        printf("%d",a);
    }
    

    If I was doing this, I'd probably end up moving some of the logic into a function though. Perhaps something like this:

    #include<stdio.h>
    
    int perform_operation(char op, int lhs, int rhs) {
        switch (op) {
            case '+':
                return lhs + rhs;
    
            case '-':
                return lhs - rhs;
    
            default:
                return 0;
        }
    }
    
    int main() {
        char sign[]={'+','-'};
        int a = perform_operation(sign[1], 12, 10);
        printf("%d",a);
    }
    
    0 讨论(0)
  • 2021-01-21 14:39

    You could use function pointers:

    //function:
    int addInt(int a, int b) {
        return a+b;
    }
    
    //define the pointer:
    int (*functionPtr)(int,int);
    
    main ()
    {
      functionPtr sign[5];
      sign[0] = &addInt;
    
       int a = 5, b = 8;
      //...
      int sum = sign[0](a,b);
      printf("%d", sum);
      return 0;
    }
    

    It's a little bit more complicated, and in most simple cases, you would want to use a switch statement to call different functions, but this is a very useful method when dealing with larger, more complex cases.

    An example of when I've used arrays of function pointers like this was when developing an image processing application, with vendors providing the algorithms for some pretty special processing.
    Their code was proprietry though, and so while we could attain dlls containing functions that could do the processing, we couldn't build it into our code. As any number of these dlls may be required, and we wanted the ability to add-remove functionality without re-compiling the entire system, we needed to be able to read the dlls and manage them. We used a method like above for doing this.

    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 2021-01-21 14:42

    I'd be tempted to use a ternary for this:

    a = 12 + (sign[1] == '-' ? -1 : +1) * 10;

    Of course this defaults to + but you could introduce earlier checks to guard against that.

    You could use this to form a macro:

    12 + SIGN(1) 10;

    with

    #define SIGN(s) (sign[(s)] == '-' ? -1 : +1) *

    which recovers the form of the expression in your question excepting the square brackets.

    0 讨论(0)
提交回复
热议问题