1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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));
}
|