Basically this is a expression evaluation problem. You can solve it by knowing precedence order of operators. This can go complex when parenthesis, factorials come into picture. You would need to use stack as well in order to solve these expressions. Answer can be found here.