-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPREFIX_TO_POSTFIX.java
More file actions
28 lines (26 loc) · 873 Bytes
/
PREFIX_TO_POSTFIX.java
File metadata and controls
28 lines (26 loc) · 873 Bytes
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
package Ds.Achievers;
import java.util.Scanner;
import java.util.Stack;
public class PREFIX_TO_POSTFIX {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Stack s = new Stack();
String prefix = sc.next();
StringBuilder postfix = new StringBuilder();
for (int i = prefix.length() - 1; i >= 0; i--) {
char c = prefix.charAt(i);
if (c == '+' || c == '-' || c == '*' || c == '/') {
String s1= (String) s.peek();
s.pop();
String s2= (String) s.peek();
s.pop();
String temp = s1+s2+prefix.charAt(i);
s.push(temp);
}
else{
s.push(prefix.charAt(i)+"");
}
}
System.out.println("Postfix Expression is "+s.pop());
}
}