Much more simple to implement if you write your formula in Polish Notation [https://en.wikipedia.org/wiki/Polish_notation]