| i did that project!
EXPLINATION! operands=1, 2, 3 , etc
operators=+ - / *
you need to use inverse polish notation for an equation cause that is how the computer does it. now you also need to have a sort of priority so that when you itterate the stack in which you store your operators you know what to do (mainly using cases).
so let's say you have something like 1+2+3*5/3. the first thing is that you say * and / are higher priority than + and -. so if 2 of the same operators come in the stack one after another you make the calculation with the operator which already was on the stack of operators betwen the last 2 operands and if a lower level operator comes on the stack of operators you do the previous operation on the operators stack after which you put on the operatros tasck the lower class operator that came! you need 2 stacks one for operators and one for operands. so let's see how taht would look like.
1+2+3*5/3
you read 1 and put it on the operands stack. you read plus you put it in the operator class. you read 2 you put it on the operand stack. you read plus....but the last element on the stack of operands is a + too (SAME CLASS OPERATOR) so what you do is make a plus operation (the 1st element in the operator stack which already was on the stack)of the last elements on the operands stack (last-1) and last and put the result on the operand stack and on the operator stack you only have a plus left the 2n plus that come in the equation.
you read 3 you put it on the operands stack. you read * and since it is higher priority you put it on the operand stack and don't do anything yet. you read 5 and put it on the operand stack and then you read / but you already have a *(SAME CLASS OPERATORS). so what you do is to do the * between the last two elements put on the operand stack and push the result on the operand stack. you read 3 and push it on the operand stack and now you are done with iterating the equation so what you do is do all the operations on the operator stack by taking the last 2 terms of the operand stack and pushing the result of the operation on the operands stack. when you finish itterating the operator stack all you ahve left is the final result in the operands stack.
i hope it helps!
if you need more help please say so by adding additional info to your question text. |