forked from Revnth/Lab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvert.java
More file actions
151 lines (143 loc) · 2.86 KB
/
Convert.java
File metadata and controls
151 lines (143 loc) · 2.86 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import java.io.*;
import java.util.Stack;
class OurStack
{
char arr[]=new char[100];
int top=-1;
void push(char c)
{
arr[++top]=c;
}
char pop()
{
return arr[top--];
}
char peek()
{
return arr[top];
}
boolean isEmpty()
{
return (top==-1)?true:false;
}
}
public class Convert
{
public static void main(String s[]) throws IOException
{
String exp;
int ch,z;
BufferedReader k =new BufferedReader(new InputStreamReader(System.in));
while(true)
{
System.out.println("\nConvert\n1:Infix to Postfix\n2:Infix to Prefix\n3:Prefix to Postfix\n4:Exit\n");
ch =Integer.parseInt(k.readLine());
switch(ch)
{
case 1:
System.out.println("\nEnter the infix expression: ");
exp= k.readLine();
System.out.println("Postfix expression: "+ toPostfix(exp));
break;
case 2:
System.out.println("\nEnter the infix expression: ");
exp= k.readLine();
System.out.println("Prefix expression: "+ toPrefix(exp));
break;
case 3:
System.out.println("\nEnter the prefix expression: ");
exp= k.readLine();
System.out.println("Postfix expression: "+ PretoPost(exp));
break;
case 4:
System.exit(0);
default:
System.out.println("Invalid\n");
break;
}
}
}
static int priority(char x)
{
if(x=='+'||x=='-')
return 1;
if(x=='*'||x=='/')
return 2;
return 0;
}
public static String toPostfix(String exp)
{
OurStack st=new OurStack();
char symbol;
String postfix="";
for(int i=0;i<exp.length();++i)
{
symbol=exp.charAt(i);
if(Character.isLetter(symbol))
postfix= postfix+ symbol;
else if(symbol=='(')
st.push(symbol);
else if(symbol==')')
{
while(st.peek()!='(')
{
postfix= postfix+ st.pop();
}
st.pop();
}
else
{
while(!st.isEmpty() && !(st.peek()=='(') && priority(symbol)<=priority(st.peek()))
postfix= postfix+ st.pop();
st.push(symbol);
}
}
while(!st.isEmpty())
postfix= postfix+ st.pop();
return postfix;
}
public static String reverse(String exp)
{
String rev="";
char[] arrexp= new String(exp).toCharArray();
for(int i=arrexp.length-1;i>=0;i--)
{
if(arrexp[i]=='(')
arrexp[i]=')';
else if(arrexp[i]==')')
arrexp[i]='(';
rev=rev+arrexp[i];
}
return rev;
}
public static String toPrefix(String exp)
{
System.out.println(reverse(exp));
return reverse(toPostfix(reverse(exp)));
}
public static String PretoPost(String exp)
{
Stack<String> st= new Stack<String>();
String rev=reverse(exp);
String symbol,op1,op2;
String postfix="";
for(int i=0;i<rev.length();++i)
{
symbol=rev.charAt(i)+"";
if(Character.isLetter(rev.charAt(i)))
{
st.push(symbol+"");
}
else
{
op1=st.pop();
op2=st.pop();
String temp=op1+op2+symbol;
st.push(temp);
}
}
while(!st.isEmpty())
postfix=postfix+ st.pop();
return postfix;
}
}