generated from github/codespaces-blank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack6PostfixToInfix.java
59 lines (50 loc) · 1.36 KB
/
stack6PostfixToInfix.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import java.util.*;
class Stack {
private int size;
private String[] arr;
private int top;
public Stack(int size) {
this.size = size;
top = -1;
arr = new String[size];
}
public void push(String s) {
arr[++top] = s;
}
public String pop() {
return arr[top--];
}
public boolean isEmpty() {
return (top == -1);
}
public boolean isFull() {
return (top == size - 1);
}
}
public class stack6PostfixToInfix{
public static boolean isOperator(char s) {
if (s == '+' || s == '-' || s == '*' || s == '/') {
return true;
} else {
return false;
}
}
public static void main(String args[]){
String postfix;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the postfix expression:");
postfix = sc.nextLine();
Stack sob = new Stack(postfix.length());
for(int i=0;i<postfix.length();i++){
if(isOperator(postfix.charAt(i))==true){
String temp1 = sob.pop();
String temp2 = sob.pop();
sob.push("(" + temp2 + postfix.charAt(i) + temp1 + ")");
}
else{
sob.push(""+ postfix.charAt(i));
}
}
System.out.println("Infix Expression : " + sob.pop());
}
}