I have interpreter that solve Polish notation. I have all operations and numbers in tokens and I have a list of tokens. So for example - - 5 4 2
is a list with these tokens:
SubtractionToken, SubtractionToken, NumberToken, NumberToken, NumberToken, STOPToken.
Example tokens:
class SubstractToken : IBinaryOperation
{
public Number Interpret(Number value1, Number value2)
{
Number c = new Number(value1.Value() - value2.Value());
return c;
}
}
class Number : IToken
{
private int value;
public Number(int val)
{
value = val;
}
public int Value()
{
return value;
}
}
So, I cannot understand how to make recursive function to solve this problems. Because when I SubstractionToken.Inrerpret(value, value) I need to give the values from numberTokens
what should be substract from itself, but what happen if the next token is an operation token? or we have - 5 - 7 2
? I don't know how to implement such recursive function which will detect that the first operation should be made - 7 2 then - 5 and resultof(- 7 2), remember the results and back to the previous not done operations. Any help?