Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // C high level skill
- #include <stdio.h>
- typedef struct {
- int sub;
- }sub_t;
- typedef struct s_type_int{
- int type; //int: 0, double: 1, ...
- int i;
- double d;
- sub_t s;
- int (*inc)(struct s_type_int* );
- double (*result)(struct s_type_int* );
- }type_int;
- double result(type_int *t)
- {
- if (t->type == 0) {
- return (int)(t->i - t->s.sub);
- } else if (t->type == 1) {
- return t->d - t->s.sub;
- }
- }
- int int_inc(type_int *t) // handler function
- {
- ++t->i;
- return 0;
- }
- int double_inc(type_int *t) // handler function
- {
- t->d++;
- return 0;
- }
- int sub_inc(type_int* t) // handler function
- {
- ++t->s.sub;
- return 0;
- }
- int main(int argc, char** argv)
- {
- type_int a = {0, 0, .0, 0};
- a.result = result;
- a.type = 0;
- a.inc = int_inc;
- a.inc(&a);
- a.inc(&a);
- a.inc(&a);
- a.inc(&a);
- a.inc(&a);
- a.inc(&a);
- a.inc = double_inc;
- a.inc(&a);
- a.inc = sub_inc;
- a.inc(&a);
- printf("%f\n", a.result(&a));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement