-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInfixTOPostfix (1).java
48 lines (44 loc) · 1.62 KB
/
InfixTOPostfix (1).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
import java.util.Scanner;
import java.util.Stack;
public class InfixTOPostfix {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Infix");
String str = sc.nextLine();
Stack<Character> stk = new Stack<>();
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) >= 'a' && str.charAt(i) <= 'z' || str.charAt(i) >= 'A' && str.charAt(i) <= 'Z'
|| str.charAt(i) >= '0' && str.charAt(i) <= '9') {
System.out.print(str.charAt(i));
} else {
if (stk.empty()) {
stk.push(str.charAt(i));
} else {
switch (str.charAt(i)) {
case '/':
stk.push(str.charAt(i));
break;
case '*':
stk.push(str.charAt(i));
break;
case '%':
stk.push(str.charAt(i));
break;
case '+':
stk.push(str.charAt(i));
break;
case '-':
stk.push(str.charAt(i));
break;
default:
break;
}
}
}
}
for (int i = stk.size(); i > 0; i--) {
System.out.print(stk.pop());
}
sc.close();
}
}