Re: arithmetic calculation
Quote:
Originally posted by wombat
Well my problem actually concerns arithmetic calculation. (+,- ,*,/) I need to ask the user for input and then show them the result. eg.
input : 12 +3 /5
result : 1
I'm not asking anybody for code, just some ideas on how to go about it.
I had a basic idea about it but it doesnt seem to work. I think you're suppose to store the input into an array, and run through each element of the array, correct?
Mathematical expression parsing is a bit more complex than storing the bits in an array and going through them.
First of all, analysis of expressions is much easier if the expressions are presented to the analyser program in something called Reverse Polish Notation (RPN). This is where the operands preceed the operator. For example in standard infix notation, you might write 2 + 5, this would be 2 5 + in RPN. One good thing about this system is that it forms a parentheses-free system. It is also well suited to computer manipulation because its format suits itself to use with a stack (first-in, last-out) data structure.
Let's consider the infix expression, (3 + 5) * (9 - 7). To evaluate this, we would apply the rules of precedence, i.e. parentheses, exponentiation, multiplication and division etc. We would thus be able to evaluate this expression to 16. Let's consider how we actually do that in our mind.
First we get the number 3 and then get the number 5. We add them together and store the result (8). We then get the number 9 and the number 7, storing the result of their subtraction (2). Finally, we multiply them together to get the final result of 16. We have really just converted this expression to RPN form in our head in order to process it. The RPN form is 3 5 + 9 7 - *. This form, reading from left to right is much more descriptive of the logical steps taken to process the statement, first add 3 and 5 (3 5 +), then subtract 7 from 9 (9 7 -), and then multiply those two results together (*). You can see that RPN form reads in the logical order of evaluation, and there is therefore no need for parentheses.
This logical order leads to two very simple rules for evaluating an RPN statement:
1. The next symbol encountered must be loaded on to the stack if it is an operand (e.g. a number or variable).
2. If the next symbol encountered is an operator (e.g. '+', '-' etc.), then carry out the required operation on the top two items of the stack. The result of this operation must then be left on the top of the stack.
I wish that RPN notation was used instead of infix notation as it's so much more logical, but that's just me trying to change the world again :).
This is all very good, but I expect you will want your users to input an infix expression, so you need to know how to convert from an infix expression to RPN. This is actually not too hard either. You can use a binary tree data structure.
I don't have time currently to go through the process of infix -> RPN conversion, but I'm sure a Google search would yield helpful results.
I hope I've given you a starter...
Good Luck,