Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions Misc/PostfixEvaluation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import java.util.Scanner;
import java.util.Stack;

/*
This Program will evaluate the value of given Postfix Expression
*/

public class PostfixEvaluation {
public static void main(String[] args) {
// Inserting Postfix Expression
System.out.println("Enter Postfix Expression");
Scanner s=new Scanner(System.in);
String exp=s.next();
int answer=PostEval(exp);
System.out.println("The Value of expression is : "+answer);
}

//Function to Evaluate Postfix Expression

private static int PostEval(String exp) {
Stack<Integer> helperStack=new Stack<>();
int i=0;
while(i<exp.length())
{
if(exp.charAt(i)=='*')
{
Integer A=helperStack.pop();
Integer B=helperStack.pop();
Integer C=B*A;
helperStack.push(C);
}
else if(exp.charAt(i)=='/')
{
Integer A=helperStack.pop();
Integer B=helperStack.pop();
Integer C=B/A;
helperStack.push(C);
}
else if(exp.charAt(i)=='^')
{
Integer A=helperStack.pop();
Integer B=helperStack.pop();
Integer C=(int) Math.pow((double) B,(double)A);
helperStack.push(C);
}
else if(exp.charAt(i)=='+')
{
Integer A=helperStack.pop();
Integer B=helperStack.pop();
Integer C=B+A;
helperStack.push(C);
}
else if(exp.charAt(i)=='-')
{
Integer A=helperStack.pop();
Integer B=helperStack.pop();
Integer C=B-A;
helperStack.push(C);
}
else
{
helperStack.push(Integer.parseInt(""+exp.charAt(i)));
}
i++;
}
int ans=helperStack.pop();
return ans;

}
}