fork download
  1. #include<stdio.h>
  2. #include<limits.h>
  3. #include<stdlib.h>
  4.  
  5. double add(double a, double b){ return a + b; }
  6. double mal(double a, double b){ return a * b; }
  7. double minus(double a, double b){ return a - b; }
  8. double divid(double a, double b){ return a / b; }
  9.  
  10. typedef double (*funcp)(double, double);
  11. typedef struct node_t{
  12. char op;
  13. funcp fn;
  14. struct node_t *next;
  15. } Node;
  16.  
  17.  
  18. Node* new_node(char c, funcp fn)
  19. {
  20. Node* ret = (Node *)malloc(sizeof(Node));
  21. ret->op = c;
  22. ret->fn = fn;
  23. ret->next = NULL;
  24. return ret;
  25. }
  26.  
  27. Node* add_node(Node *head, char c, funcp fn)
  28. {
  29. Node *next= new_node(c,fn);
  30. Node *idx = head;
  31. while(idx->next) idx = idx->next;
  32. idx->next = next;
  33. return head;
  34. }
  35.  
  36. Node* search(Node *node, const char c)
  37. {
  38. if (!node){ return NULL;}
  39. else if(c == node->op){ return node;}
  40. else { return search( node->next, c );}
  41. }
  42.  
  43. int main()
  44. {
  45. char d = '*';
  46. double val;
  47. Node *head = new_node(' ',NULL);
  48. add_node(head, '+', add);
  49. add_node(head, '*', mal);
  50. add_node(head, '-', minus);
  51. add_node(head, '/', divid);
  52. val = search(head, d)->fn(6,9);
  53. printf("%lf\n", val);
  54. return 0;
  55. }
Success #stdin #stdout 0s 2424KB
stdin
Standard input is empty
stdout
54.000000