-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDSA1.java
239 lines (204 loc) · 6.83 KB
/
DSA1.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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package DSA;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class DSA1 {
static int precedence(char c){
switch (c){
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
}
return -1;
}
public static StringBuilder infixToPreFix(String expression){
StringBuilder result = new StringBuilder();
StringBuilder input = new StringBuilder(expression);
input.reverse();
Stack<Character> stack = new Stack<>();
char [] charsExp = new String(input).toCharArray();
for (int i = 0; i < charsExp.length; i++) {
if (charsExp[i] == '(') {
charsExp[i] = ')';
i++;
}
else if (charsExp[i] == ')') {
charsExp[i] = '(';
i++;
}
}
for (int i = 0; i <charsExp.length ; i++) {
char c = charsExp[i];
//check if char is operator or operand
if(precedence(c)>0){
while(stack.isEmpty()==false && precedence(stack.peek())>=precedence(c)){
result.append(stack.pop());
}
stack.push(c);
}else if(c==')'){
char x = stack.pop();
while(x!='('){
result.append(x);
x = stack.pop();
}
}else if(c=='('){
stack.push(c);
}else{
result.append(c);
}
}
for (int i = 0; i <=stack.size() ; i++) {
result.append(stack.pop());
}
return result.reverse();
}
public String infixToPostfix(String s) {
String postfix = "";
Stack<Character> st = new Stack<Character>();
char ch[] = s.toCharArray();
for (char c : ch) {
if (c != '+' && c != '-' && c != '*' && c != '/' && c != '(' && c != ')') {
postfix = postfix + c;
} else if (c == '(') {
st.push(c);
} else if (c == ')') {
while (!st.isEmpty()) {
char t = st.pop();
if (t != '(') {
postfix = postfix + t;
} else {
break;
}
}
} else if (c == '+' || c == '-' || c == '*' || c == '/') {
if (st.isEmpty()) {
st.push(c);
}
else {
while (!st.isEmpty()) {
char t = st.pop();
if (t == '(') {
st.push(t);
break;
}
else if (c == '+' || c == '-' || c == '*' || c == '/') {
if (getPriority(t) < getPriority(c)){
st.push(t);
break;
}
else{
postfix = postfix+ t;
}
}
}
st.push(c);
}
}
}
while (!st.isEmpty()){
postfix = postfix+ st.pop();
}
return (postfix);
}
public int getPriority(char c){
if (c=='+' || c=='-'){
return 1;
}
else {
return 2;
}
}
public static void main(String[] args) {
A obj1= new A();
DSA1 o = new DSA1();
String s1 ="a+b*(a*b-c)";
System.out.println(o.infixToPostfix(s1));
System.out.println(obj1.infixToPrefix(s1));
Frame f = new Frame("INFIX TO PREFIX & POSTFIX");
f.setBounds(0,0,500,500);
f.setSize(500,500);
f.setBackground(Color.black);
f.setVisible(true);
f.setLayout(null);
JPanel p = new JPanel();
ImageIcon background=new ImageIcon("");
Image img=background.getImage();
Image temp=img.getScaledInstance(500,500,Image.SCALE_SMOOTH);
background=new ImageIcon(temp);
JLabel back=new JLabel(background);
back.setLayout(null);
back.setBounds(0,0,500,500);
p.setBounds(0,0,500,500);
p.add(back);
f.add(p);
Label l1 = new Label("CONVERT INFIX TO PREFIX & POSTFIX");
l1.setAlignment(Label.CENTER);
l1.setBounds(100,100,300,30);
l1.setBackground(Color.pink);
p.add(l1);
Label l2 = new Label("Enter the expression");
l2.setBounds(60,180,160,30);
l2.setBackground(Color.pink);
l2.setAlignment(Label.CENTER);
p.add(l2);
TextField t1 = new TextField();
t1.setBounds(270,180,160,30);
t1.setBackground(Color.pink);
p.add(t1);
Label l3= new Label();
l3.setBounds(140,340,220,30);
l3.setBackground(Color.pink);
p.add(l3);
Button b1 = new Button("convert to postfix");
b1.setBackground(Color.pink);
b1.setBounds(280,260,130,30);
p.add(b1);
Button b2 = new Button("convert to prefix");
b2.setBackground(Color.pink);
b2.setBounds(90,260,130,30);
p.add(b2);
b1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
l3.setText(o.infixToPostfix(t1.getText()));
}
});
b2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String st = t1.getText();
l3.setText(String.valueOf(infixToPreFix(st.toString())));
}
});
f.addWindowListener(new WindowListener() {
@Override
public void windowOpened(WindowEvent e) {
}
@Override
public void windowClosing(WindowEvent e) {
f.dispose();
}
@Override
public void windowClosed(WindowEvent e) {
}
@Override
public void windowIconified(WindowEvent e) {
}
@Override
public void windowDeiconified(WindowEvent e) {
}
@Override
public void windowActivated(WindowEvent e) {
}
@Override
public void windowDeactivated(WindowEvent e) {
}
});
}
}