#include #include #include const int _binop=1, _number=5, _variable=6; struct node { int kind; int value; string name; vector sub; node(int k, int v=0, string n="") { kind=k; value=v; name=n; } node * add(node * p) { sub.push_back(p); return this; } }; node * num(int v) { return new node(_number, v); } node * var(string n) { return new node(_variable, 0, n); } node * binop(string o) { return new node(_binop, 0, o); } void print(node *n) { if (n==NULL) cout << "NULL!!!"; else if (n->kind == _number) cout << n->value; else if (n->kind == _variable) cout << n->name; else if (n->kind == _binop) { if (n->sub.size() != 2) cout << "ERROR IN +!!!"; else { cout << "("; print(n->sub[0]); cout << n->name; print(n->sub[1]); cout << ")"; } } else cout << "ERROR BAD KIND"; } void main() { // 2*a+4/5/6 node * root = binop("+") ->add(binop("*") ->add(num(3)) ->add(var("cat"))) ->add(binop("/") ->add(binop("/") ->add(num(8)) ->add(num(7))) ->add(num(6))); print(root); cout << "\n"; }