Делаешь каждую операцию классом
#include<stack>
#include<memory>
#include<iostream>
template<class numbers_type>
classs NumbersStack : public std:stack<numbers_type> {}
template<class numbers_type>
class Operation
{
public:
typedef std:auto_ptr< Operation<numbers_type> > ptr;
virtual void DoOperation(NumbersStack<numbers_type>& ns);
}
template<class numbers_type>
classs OpeationsStack : public std:stack<Operation<numbers_type>::ptr> {}
template<class numbers_type>
class OperationPlus : public Operation<numbers_type>
{
public:
void DoOperation(NumbersStack<numbers_type>& ns)
{
numbers_type op1 = ns.top();
ns.pop();
numbers_type op2 = ns.top();
ns.pop();
ns.push(op1 + op2);
}
}
template<class numbers_type>
void Calculate(NumbersStack<numbers_type>& ns, OpeationsStack<numbers_type>& os)
{
for(; !os.empty(); os.pop() )
{
os.top()->DoOperaton(ns);
}
}
typedef int nt;
void main()
{
NumbersStack<nt> numstack;
numstack.push(1);
numstack.push(2);
OperationsStack<nt> opstack;
opstack.push(new OperationPlus<nt>());
Calculate(numstack, opstack);
std:cout << "Result: " << numstack.top() << std:endl;
}