forked from Dua2022/repo1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram6.java
51 lines (46 loc) · 1.11 KB
/
Program6.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
package assignment1;
import java.util.Stack;
public class Program6 {
}
class PTI {
static boolean isOperator(char x)
{
switch(x)
{
case '+':
case '-':
case '*':
case '/':
case '^':
case '%':
return true;
}
return false;
}
public static String convert(String str)
{
Stack<String> stack = new Stack<>();
int l = str.length();
for(int i = l - 1; i >= 0; i--)
{
char c = str.charAt(i);
if (isOperator(c))
{
String op1 = stack.pop();
String op2 = stack.pop();
String temp = "(" + op1 + c + op2 + ")";
stack.push(temp);
}
else
{
stack.push(c + "");
}
}
return stack.pop();
}
public static void main(String[] args)
{
String exp = "*-A/BC-/AKL";
System.out.println("Infix : " + convert(exp));
}
}