-
Notifications
You must be signed in to change notification settings - Fork 0
/
PostFix.java
45 lines (36 loc) · 1.1 KB
/
PostFix.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/****************************************************
PostFix Expression Evaluation
****************************************************/
import java.util.Scanner;
import java.util.Stack;
class PostFixExp
{
void postCalc( String str)
{
Scanner sc=new Scanner(str);
Stack<Double> vals=new Stack<Double>();
while(sc.hasNext())
{
String exp=sc.next();
if(exp.equals("+")) vals.push(vals.pop()+vals.pop());
else if(exp.equals("-")) vals.push(vals.pop()-vals.pop());
else if(exp.equals("*")) vals.push(vals.pop() * vals.pop());
else if(exp.equals("/")) vals.push(vals.pop() / vals.pop());
else vals.push(Double.parseDouble(exp));
}
System.out.println(vals.pop());
}
}
class PostFix
{
public static void main(String []args)
{
String str=" 3 4 5 + *";
String str1="1 2 3 4 5 * + 6 * * +";
String str2="7 16 16 16 * * * 5 16 16 * * 3 16 * 1 + + +";
PostFixExp obj=new PostFixExp();
obj.postCalc(str);
obj.postCalc(str1);
obj.postCalc(str2);
}
}