diff options
Diffstat (limited to 'comp/work/58/example.c')
-rw-r--r-- | comp/work/58/example.c | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/comp/work/58/example.c b/comp/work/58/example.c new file mode 100644 index 0000000..1fb81b8 --- /dev/null +++ b/comp/work/58/example.c @@ -0,0 +1,54 @@ +#include <stdio.h> +#include <stdlib.h> + +typedef enum op { + ADD, + SUB, + MUL, + DIV, +} op; + +struct operation { + int a, b; + op o; + int (*funcs[2][2])(int, int); +}; + +int add(int a, int b) { + return a + b; +} +int sub(int a, int b) { + return a - b; +} +int mul(int a, int b) { + return a * b; +} +int Div(int a, int b) { + return a / b; +} + +int runop(struct operation *o) { + switch (o->o) { + case ADD: return o->funcs[0][0](o->a, o->b); + case SUB: return o->funcs[0][1](o->a, o->b); + case MUL: return o->funcs[1][0](o->a, o->b); + case DIV: return o->funcs[1][1](o->a, o->b); + } + return -1; +} + +struct operation *initop(int a, int b, op ope) { + struct operation *o = malloc(sizeof(struct operation)); + o->a = a; + o->b = b; + o->funcs[0][0] = &add; + o->funcs[0][1] = ⊂ + o->funcs[1][0] = &mul; + o->funcs[1][1] = &Div; + o->o = ope; +} + +int main() { + struct operation *o = initop(5, 5, MUL); + printf("%d\n", runop(o)); +} |