-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinclude.c
More file actions
42 lines (33 loc) · 987 Bytes
/
include.c
File metadata and controls
42 lines (33 loc) · 987 Bytes
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
#include <stdio.h>
#include <string.h>
// the four arithmetic operations
// one of these functions is selected at runtime with a swicth or a function pointer
typedef struct {
char *function_name;
float (*pt2Func)(float, float);
} conv_t;
conv_t convertion_array[] = {
"Plus", Plus,
"Minus", Minus,
"Multiply", Multiply,
"Divide", Divide,
0, 0
};
// execute example code
void main()
{
char minus_str[] = "Divide";
conv_t *tmp;
float param1 = 5, param2 = 3, result;
for (tmp = convertion_array; tmp->function_name && strcmp(minus_str, tmp->function_name); tmp++);
if (tmp->function_name) {
result = tmp->pt2Func(param1, param2);
printf("Result = %f\n", result);
}
else
printf("Wrong function name\n", result);
}
float Plus (float a, float b) { return a+b; }
float Minus (float a, float b) { return a-b; }
float Multiply (float a, float b) { return a*b; }
float Divide (float a, float b) { return a/b; }