Question: hello, I have a java project that involves reading postfix equations with decimals and putting them into a stack... the program needs to put numbers
hello,
I have a java project that involves reading postfix equations with decimals and putting them into a stack... the program needs to put numbers in a stack and then once a operator is introduced i.e.( +, -,*,/ ) it needs to pop off the first 2 numbers and do the equation i.e. ( 2 + 2 = 4 ) and put the result on top of the stack... it needs to do this for the link of the equation.
i have this so far but need help finishing it... thanks
import java.util.*;
import java.util.Stack;
public class Postfix {
public static void main(String[] args) {
String string = "2.2 6.14 + 3.5 6.2 * + " ; // an example equation... program needs to pop the first two and do addition then the next 2 and do multiplaction then the addition again
System.out.println(PostFixEval(string));
}
public static double PostFixEval(String postfixExpr) {
Stack
String[] items = postfixExpr.split(" ");
Double value1 = stack.pop();
Double value2 = stack.pop();
switch ()) { // I really want to use stack but i cant figure out how the loop to use with it.
case "+":
stack.push(value2 + value1);
break;
case "-":
stack.push(value2 - value1);
break;
case "*":
stack.push(value2 * value1);
break;
case "/":
stack.push(value2 / value1);
break;
}
return stack.pop();
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
