My Expression √((400/100)^4) * 5 + 10/2
- For tokenization: we split up the terms, with operaters and numbers and add them to an arraylist designated for these terms.
- Tokenization: It goes through a string, looks for characters representing the tokens either a term or an operator. Tokens are what allow us to perform any operations and check precedents of operations and how we use numbers.
- [√, (, (, 400, /, 100, ), ^, 4, ), *, 5, +, 10, /, 2]
RPN Conversion
-
Initialize:
- operatorStack: []
- rpnTerms: []
-
Iterate through each term:
- √: Operator, push to operatorStack.
- operatorStack: [√]
- (: Parenthesis, push to operatorStack.
- operatorStack: [√, (]
- (: Parenthesis, push to operatorStack.
- operatorStack: [√, (, (]
- 400: Value, add to rpnTerms.
- rpnTerms: [400]
- /: Operator, push to operatorStack.
- operatorStack: [√, (, (, /]
- 100: Value, add to rpnTerms.
- rpnTerms: [400, 100]
- ): Pop until (, add to rpnTerms.
- operatorStack: [√, (]
- rpnTerms: [400, 100, /]
- ^: Operator, push to operatorStack.
- o peratorStack: [√, (, ^]
- 4: Value, add to rpnTerms.
- rpnTerms: [400, 100, /, 4]
- ): Pop until (, add to rpnTerms.
- operatorStack: [√]
- rpnTerms: [400, 100, /, 4, ^]
- *: Operator, push to operatorStack.
- operatorStack: [√, *]
- 5: Value, add to rpnTerms.
- rpnTerms: [400, 100, /, 4, ^, 5]
- +: Pop until an operator with lower precedence is found, then push +.
- operatorStack: [+]
- rpnTerms: [400, 100, /, 4, ^, 5, *, √]
- 10: Value, add to rpnTerms.
- rpnTerms: [400, 100, /, 4, ^, 5, *, √, 10]
- /: Operator, push to operatorStack.
- operatorStack: [+, /]
- 2: Value, add to rpnTerms.
- rpnTerms: [400, 100, /, 4, ^, 5, *, √, 10, 2]
- Empty the Stack:
Pop remaining operators to rpnTerms.
- operatorStack: []
- rpnTerms: [400, 100, /, 4, ^, 5, *, √, 10, 2, /, +]
Calculations
-
We’ll go through the Reverse Polish Notation (RPN) expression [400, 100, /, 4, ^, 5, *, √, 10, 2, /, +] step-by-step to evaluate it.
-
Initialize:
- calcStack: []
- Iterate through each term:
-Term: 400
Action: Push 400 to calcStack. calcStack: [400.0] Term: 100
Action: Push 100 to calcStack. calcStack: [400.0, 100.0] Term: /
Action: We push / to the operator stack, Pop 100 and 400, perform 400 / 100, then pop / from the operator stack, push result. Calculation: 400 / 100 = 4 calcStack: [4.0] Term: 4
Action: Push 4 to calcStack. calcStack: [4.0, 4.0] Term: ^
Action: Pop 4 and 4, push 4 to the operator stack, perform 4 ^ 4, then pop ^ from the operator stack, push result. Calculation: 4 ^ 4 = 256 calcStack: [256.0] Term: √
Action: Pop 256, push √ to the operator stack, perform √256, pop the √ from the operator stack, push result. Calculation: √256 = 16 calcStack: [16.0] Term: 5
Action: Push 5 to calcStack. calcStack: [16.0, 5.0] Term: *
Action: Push * to the operator stack. Pop 5 and 16, perform 16 * 5, pop *, push result. Calculation: 16 * 5 = 80 calcStack: [80.0] Term: 10
Action: Push 10 to calcStack. calcStack: [80.0, 10.0] Term: 2
Action: Push 2 to calcStack. calcStack: [80.0, 10.0, 2.0] Term: /
Action: Push / to operator stack, Pop 2 and 10, perform 10 / 2, pop /, push result. Calculation: 10 / 2 = 5 calcStack: [80.0, 5.0] Term: +
Action: Push + to operator stack. Pop 5 and 80, perform 80 + 5, pop + from operator stack, push result. Calculation: 80 + 5 = 85 calcStack: [85.0] Final Result:
The final result is the remaining value on the calcStack. Final Result: 85.00
Summary The calculation of the RPN expression [400, 100, /, 4, ^, 5, *, √, 10, 2, /, +] yields a final result of 85. This process involved evaluating the expression in a stack-based manner, adhering to the correct order of operations without needing parentheses to dictate precedence.