-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program7.java
40 lines (38 loc) · 970 Bytes
/
Program7.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
package assignment1;
import java.util.Stack;
public class Program7 {
}
class PtI
{
static boolean isOperand(char x)
{
return (x >= 'a' && x <= 'z') ||
(x >= 'A' && x <= 'Z');
}
static String getInfix(String exp)
{
Stack<String> s = new Stack<String>();
for (int i = 0; i < exp.length(); i++)
{
if (isOperand(exp.charAt(i)))
{
s.push(exp.charAt(i) + "");
}
else
{
String op1 = s.peek();
s.pop();
String op2 = s.peek();
s.pop();
s.push("(" + op2 + exp.charAt(i) +
op1 + ")");
}
}
return s.peek();
}
public static void main(String args[])
{
String exp = "ab*c+";
System.out.println( "postfix to infix = "+getInfix(exp));
}
}