#include "stdafx.h" #include #include class Function { public: Function(int _arg_quantity) : arg_quantity(_arg_quantity) { } virtual ~Function() {} virtual void* calculate(void *_arg1) = 0; virtual void* calculate(void *_arg1, void *_arg2) = 0; virtual void* calculate(void *_arg1, void *_arg2, void *_arg3) = 0; virtual void* calculate(void **_args) = 0; int get_arg_quantity() const { return arg_quantity; } private: int arg_quantity; }; class Function1 : public Function { public: Function1() : Function(1) { } private: void* calculate(void *_arg1, void *_arg2) { return nullptr; } void* calculate(void *_arg1, void *_arg2, void *_arg3) { return nullptr; } void* calculate(void **_args) { return nullptr; } }; class Function2 : public Function { public: Function2() : Function(2) { } private: void* calculate(void *_arg1) { return nullptr; } void* calculate(void *_arg1, void *_arg2, void *_arg3) { return nullptr; } void* calculate(void **_args) { return nullptr; } }; class Function3 : public Function { public: Function3() : Function(3) { } private: void* calculate(void *_arg1) { return nullptr; } void* calculate(void *_arg1, void *_arg2) { return nullptr; } void* calculate(void **_args) { return nullptr; } }; class FunctionN : public Function { public: FunctionN(int _args_quantity) : Function(_args_quantity) { } private: void* calculate(void *_arg1) { return nullptr; } void* calculate(void *_arg1, void *_arg2) { return nullptr; } void* calculate(void *_arg1, void *_arg2, void *_arg3) { return nullptr; } }; class Double : public Function1 { public: void* calculate(void *_arg1) { int tmp = *((int*)_arg1) * 2; return &tmp;; } }; class Add : public Function2 { public: void* calculate(void *_arg1, void *_arg2) { int tmp = *((int*)_arg1) + *((int*)_arg2); return &tmp; } }; class Constant { public: Constant (void* _value) : value(_value){ } void* get_value() const { return value; } private: void* value; }; class Variable { public: Variable(std::string _name) : value(nullptr), name(_name) { } void set_value(void* _value) { value = _value; } void* get_value() const { return value; } std::string get_name() const { return name; } private: void* value; std::string name; }; class Node_function; class Node_variable; class Node_constant; class Node { public: Node() : childrens_quantity(0), childrens(nullptr) { } Node (int _childrens_quantity) : childrens_quantity(_childrens_quantity), childrens(new Node*[_childrens_quantity]) { for (int i = 0; i < childrens_quantity; i++) { childrens[i] = nullptr; } } virtual ~Node() { for (int i = 0; i < childrens_quantity; i++) { delete childrens[i]; } delete[] childrens; } virtual void* get_value() const = 0; //protected: int childrens_quantity; Node **childrens; }; class Node_function : public Node { public: Node_function(Function *_function) : Node (_function->get_arg_quantity()), function(_function) { } void* get_value() const { void *result; switch (function->get_arg_quantity()) { case 1: result = function->calculate(childrens[0]->get_value()); break; case 2: result = function->calculate(childrens[0]->get_value(), childrens[1]->get_value()); break; case 3: result = function->calculate(childrens[0]->get_value(), childrens[1]->get_value(), childrens[2]->get_value()); break; default: void **args = new void*[function->get_arg_quantity()]; for (int i = 0; i < function->get_arg_quantity(); i++) { args[i] = childrens[i]->get_value(); } result = function->calculate(args); delete[] args; } return result; } //private: Function *function; }; class Node_variable : public Node { public: Node_variable( Variable *_variable) : Node (0), variable(_variable) { } void* get_value() const { return variable->get_value(); } //private: Variable *variable; }; class Node_constant : public Node { public: Node_constant(Constant *_constant) : Node (0), constant(_constant) { } void* get_value() const { return constant->get_value(); } //private: Constant *constant; }; void set_child(Node_function *_node, Function *_element, int _index) { if (_index < _node->childrens_quantity) { _node->childrens[_index] = new Node_function(_element); } } void set_child(Node_function *_node, Variable *_element, int _index) { _node->childrens[_index] = new Node_variable(_element); } void set_child(Node_function *_node, Constant *_element, int _index) { _node->childrens[_index] = new Node_constant(_element); } int main() { int a = 5; int b = 10; Function *d = new Double; Constant c1(&a); Constant c2(&b); Node *root = new Node_function(d); Node *child1 = new Node_constant(&c1); Node *child2 = new Node_constant(&c2); root->childrens[0] = child1; root->childrens[1] = child2; std::cout << *((int*)root->get_value()) << 'n'; std::cin.get(); return 0; }