Skip to content
Snippets Groups Projects
Select Git revision
  • afb47a1dd898b593df2db2a6d2d1f0121fdea375
  • master default
2 results

loesung-3-20160129-3.c

Blame
  • Forked from Peter Gerwinski / hp
    326 commits behind the upstream repository.
    loesung-3-20160129-3.c 680 B
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct
    {
      char symbol;
      int (*calculate) (int a, int b);
    }
    operation;
    
    operation *new_operation (void)
    {
      operation *op = malloc (sizeof (operation));
      op->symbol = '?';
      op->calculate = NULL;
      return op;
    }
    
    operation *new_plus (void)
    {
      return new_operation ();
    }
    
    operation *new_minus (void)
    {
      return new_operation ();
    }
    
    operation *new_times (void)
    {
      return new_operation ();
    }
    
    int main (void)
    {
      operation *op[4];
      op[0] = new_plus ();
      op[1] = new_minus ();
      op[2] = new_times ();
      op[3] = NULL;
      for (int i = 0; op[i]; i++)
        printf ("2 %c 3 = %d\n", op[i]->symbol, op[i]->calculate (2, 3));
      return 0;
    }