Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. // C high level skill
  2.  
  3. #include <stdio.h>
  4.  
  5. typedef struct {
  6. int sub;
  7. }sub_t;
  8.  
  9. typedef struct s_type_int{
  10. int type; //int: 0, double: 1, ...
  11. int i;
  12. double d;
  13. sub_t s;
  14.  
  15. int (*inc)(struct s_type_int* );
  16. double (*result)(struct s_type_int* );
  17. }type_int;
  18.  
  19.  
  20. double result(type_int *t)
  21. {
  22. if (t->type == 0) {
  23. return (int)(t->i - t->s.sub);
  24. } else if (t->type == 1) {
  25. return t->d - t->s.sub;
  26. }
  27. }
  28.  
  29. int int_inc(type_int *t) // handler function
  30. {
  31. ++t->i;
  32. return 0;
  33. }
  34.  
  35. int double_inc(type_int *t) // handler function
  36. {
  37. t->d++;
  38. return 0;
  39. }
  40.  
  41. int sub_inc(type_int* t) // handler function
  42. {
  43. ++t->s.sub;
  44. return 0;
  45. }
  46.  
  47. int main(int argc, char** argv)
  48. {
  49. type_int a = {0, 0, .0, 0};
  50. a.result = result;
  51.  
  52. a.type = 0;
  53. a.inc = int_inc;
  54. a.inc(&a);
  55. a.inc(&a);
  56. a.inc(&a);
  57. a.inc(&a);
  58. a.inc(&a);
  59. a.inc(&a);
  60.  
  61. a.inc = double_inc;
  62. a.inc(&a);
  63.  
  64. a.inc = sub_inc;
  65. a.inc(&a);
  66.  
  67. printf("%f\n", a.result(&a));
  68. return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement