Facebook
From Soft Panda, 6 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 225
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <cmath>
  4.  
  5. class Function {
  6. public:
  7.         Function(int _arg_quantity) :
  8.                 arg_quantity(_arg_quantity) {
  9.         }
  10.  
  11.         virtual ~Function() {}
  12.  
  13.         virtual void* calculate(void *_arg1) = 0;
  14.         virtual void* calculate(void *_arg1, void *_arg2) = 0;
  15.         virtual void* calculate(void *_arg1,  void *_arg2, void *_arg3) = 0;
  16.         virtual void* calculate(void **_args) = 0;
  17.  
  18.         int get_arg_quantity() const { return arg_quantity; }
  19.  
  20. private:
  21.         int arg_quantity;
  22. };
  23.  
  24. class Function1 : public Function {
  25. public:
  26.         Function1() :
  27.                 Function(1) {
  28.         }
  29.  
  30. private:
  31.         void* calculate(void *_arg1, void *_arg2) { return nullptr; }
  32.         void* calculate(void *_arg1, void *_arg2, void *_arg3) { return nullptr; }
  33.         void* calculate(void **_args) { return nullptr; }
  34. };
  35.  
  36. class Function2 : public Function {
  37. public:
  38.         Function2() :
  39.                 Function(2) {
  40.         }
  41.  
  42. private:
  43.         void* calculate(void *_arg1) { return nullptr; }
  44.         void* calculate(void *_arg1, void *_arg2, void *_arg3) { return nullptr; }
  45.         void* calculate(void **_args) { return nullptr; }
  46. };
  47.  
  48. class Function3 : public Function {
  49. public:
  50.         Function3() :
  51.                 Function(3) {
  52.         }
  53.  
  54. private:
  55.         void* calculate(void *_arg1) { return nullptr; }
  56.         void* calculate(void *_arg1, void *_arg2) { return nullptr; }
  57.         void* calculate(void **_args) { return nullptr; }
  58. };
  59.  
  60. class FunctionN : public Function {
  61. public:
  62.         FunctionN(int _args_quantity) :
  63.                 Function(_args_quantity) {
  64.         }
  65.  
  66. private:
  67.         void* calculate(void *_arg1) { return nullptr; }
  68.         void* calculate(void *_arg1, void *_arg2) { return nullptr; }
  69.         void* calculate(void *_arg1, void *_arg2, void *_arg3) { return nullptr; }
  70. };
  71.  
  72. class Double : public Function1 {
  73. public:
  74.         void* calculate(void *_arg1) {
  75.                 int tmp = *((int*)_arg1) * 2;
  76.                
  77.                 return &tmp;;
  78.         }
  79. };
  80.  
  81. class Add : public Function2 {
  82. public:
  83.         void* calculate(void *_arg1, void *_arg2) {
  84.                 int tmp = *((int*)_arg1) + *((int*)_arg2);
  85.  
  86.                 return &tmp;
  87.         }
  88. };
  89.  
  90. class Constant {
  91. public:
  92.         Constant (void* _value) :
  93.                 value(_value){
  94.         }
  95.  
  96.         void* get_value() const {
  97.                 return value;
  98.         }
  99.  
  100. private:
  101.         void* value;
  102. };
  103.  
  104. class Variable {
  105. public:
  106.         Variable(std::string _name) :
  107.                 value(nullptr),
  108.                 name(_name) {
  109.         }
  110.  
  111.         void set_value(void* _value) {
  112.                 value = _value;
  113.         }
  114.  
  115.         void* get_value() const {
  116.                 return value;
  117.         }
  118.  
  119.         std::string get_name() const {
  120.                 return name;
  121.         }
  122.  
  123. private:
  124.         void* value;
  125.         std::string name;
  126. };
  127.  
  128. class Node_function;
  129. class Node_variable;
  130. class Node_constant;
  131.  
  132. class Node {
  133. public:
  134.         Node() :
  135.                 childrens_quantity(0),
  136.                 childrens(nullptr) {
  137.         }
  138.  
  139.         Node (int _childrens_quantity) :
  140.                 childrens_quantity(_childrens_quantity),
  141.                 childrens(new Node*[_childrens_quantity]) {
  142.                 for (int i = 0; i < childrens_quantity; i++) {
  143.                         childrens[i] = nullptr;
  144.                 }
  145.         }
  146.        
  147.         virtual ~Node() {
  148.                 for (int i = 0; i < childrens_quantity; i++) {
  149.                         delete childrens[i];
  150.                 }
  151.                 delete[] childrens;
  152.         }
  153.  
  154.         virtual void* get_value() const = 0;
  155.  
  156. //protected:
  157.         int childrens_quantity;
  158.         Node **childrens;
  159. };
  160.  
  161. class Node_function : public Node {
  162. public:
  163.         Node_function(Function *_function) :
  164.                 Node (_function->get_arg_quantity()),
  165.                 function(_function)     {
  166.                
  167.         }
  168.  
  169.         void* get_value() const {
  170.                 void *result;
  171.  
  172.                 switch (function->get_arg_quantity()) {
  173.                 case 1:
  174.                         result = function->calculate(childrens[0]->get_value());
  175.                         break;
  176.  
  177.                 case 2:
  178.                         result = function->calculate(childrens[0]->get_value(), childrens[1]->get_value());
  179.                         break;
  180.  
  181.                 case 3:
  182.                         result = function->calculate(childrens[0]->get_value(), childrens[1]->get_value(), childrens[2]->get_value());
  183.                         break;
  184.  
  185.                 default:
  186.                         void **args = new void*[function->get_arg_quantity()];
  187.  
  188.                         for (int i = 0; i < function->get_arg_quantity(); i++) {
  189.                                 args[i] = childrens[i]->get_value();
  190.                         }
  191.  
  192.                         result = function->calculate(args);
  193.                         delete[] args;
  194.                 }
  195.                
  196.                 return result;
  197.         }
  198.  
  199. //private:
  200.         Function *function;
  201. };
  202.  
  203. class Node_variable : public Node {
  204. public:
  205.         Node_variable( Variable *_variable) :
  206.                 Node (0),
  207.                 variable(_variable) {
  208.        
  209.         }
  210.  
  211.         void* get_value() const {
  212.                 return variable->get_value();
  213.         }
  214.  
  215. //private:
  216.         Variable *variable;
  217. };
  218.  
  219. class Node_constant : public Node {
  220. public:
  221.         Node_constant(Constant *_constant) :
  222.                 Node (0),
  223.                 constant(_constant) {
  224.         }
  225.  
  226.         void* get_value() const {
  227.                 return constant->get_value();
  228.         }
  229.  
  230. //private:
  231.         Constant *constant;
  232. };
  233.  
  234. void set_child(Node_function *_node, Function *_element, int _index) {
  235.         if (_index < _node->childrens_quantity) {
  236.                 _node->childrens[_index] = new Node_function(_element);
  237.         }
  238. }
  239.  
  240. void set_child(Node_function *_node, Variable *_element, int _index) {
  241.         _node->childrens[_index] = new Node_variable(_element);
  242. }
  243.  
  244. void set_child(Node_function *_node, Constant *_element, int _index) {
  245.         _node->childrens[_index] = new Node_constant(_element);
  246. }
  247.  
  248. int main() {
  249.         int a = 5;
  250.         int b = 10;
  251.         Function *d = new Double;
  252.         Constant c1(&a);
  253.         Constant c2(&b);
  254.         Node *root = new Node_function(d);
  255.         Node *child1 = new Node_constant(&c1);
  256.         Node *child2 = new Node_constant(&c2);
  257.  
  258.         root->childrens[0] = child1;
  259.         root->childrens[1] = child2;
  260.  
  261.         std::cout << *((int*)root->get_value()) << 'n';
  262.  
  263.         std::cin.get();
  264.     return 0;
  265. }