Skip to content

Latest commit

 

History

History
41 lines (22 loc) · 765 Bytes

File metadata and controls

41 lines (22 loc) · 765 Bytes
title Algorithm4 Java Solution 1.3.11
date 2019-07-04 05:47:10 +0800
draft false
tags
JAVA
categories
TECH
archives

1.3.11

Problem:

Write a program EvaluatePostfix that takes a postfix expression from standard input, evaluates it, and prints the value. (Piping the output of your program from the previous exercise to this program gives equivalent behavior to Evaluate.

Solution:

create a stack stores doubles

read expression from left to right

push number into stack

if reads an operator

then pop two numbers from the stack, then compute the result

op1 op op2, push the result back to stack, until to the end of string expr

finally, pop the last number in the stack, it is the final result.

Reference: