-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
51 lines (41 loc) · 1.08 KB
/
main.c
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
#include <stdio.h>
int sum(int, int);
int sub(int, int);
int mul(int, int);
int div(int, int);
int calc(int, int, int (*)(int, int));
int main(int argc, char const *argv[])
{
/* operations is an array of pointers to functions that receive two integers and return an integer */
int (*operations[4])(int, int) = { sum, sub, mul, div };
for (int i = 0; i < 4; i++) {
/* the result will change according to the operation used by calc function */
printf("Result: %d.\n", calc(10, 5, operations[i]));
}
return 0;
}
/* sum: sum two integral numbers. */
int sum(int x, int y)
{
return x + y;
}
/* sub: subtracts two integral numbers. */
int sub(int x, int y)
{
return x - y;
}
/* mul: multiplies two integral numbers. */
int mul(int x, int y)
{
return x * y;
}
/* div: divides two integral numbers. */
int div(int x, int y)
{
return x / y;
}
/* calc: receives two integral numbers and returns an integral number that is the result of the supplied operation. */
int calc(int x, int y, int (*operation)(int, int))
{
return (*operation)(x, y);
}