-
Notifications
You must be signed in to change notification settings - Fork 0
/
InfxToPost.java
135 lines (121 loc) · 3.47 KB
/
InfxToPost.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
package Converters;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.SystemColor;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.util.*;
import java.awt.event.ActionEvent;
public class InfxToPost extends JFrame {
private JPanel contentPane;
private JTextField input;
private JTextField output;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
InfxToPost frame = new InfxToPost();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public InfxToPost() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 779, 519);
contentPane = new JPanel();
contentPane.setBackground(new Color(0, 128, 128));
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("Infix To Postfix Converter");
lblNewLabel.setFont(new Font("Times New Roman", Font.PLAIN, 30));
lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel.setBounds(10, 25, 743, 49);
contentPane.add(lblNewLabel);
JLabel lblNewLabel_1 = new JLabel("Enter Infix Expression ");
lblNewLabel_1.setFont(new Font("Times New Roman", Font.PLAIN, 20));
lblNewLabel_1.setBounds(84, 117, 212, 31);
contentPane.add(lblNewLabel_1);
input = new JTextField();
input.setBounds(84, 159, 618, 31);
contentPane.add(input);
input.setColumns(10);
JLabel lblNewLabel_2 = new JLabel("Your Postfix Expression is");
lblNewLabel_2.setFont(new Font("Times New Roman", Font.PLAIN, 20));
lblNewLabel_2.setBounds(84, 271, 212, 31);
contentPane.add(lblNewLabel_2);
output = new JTextField();
output.setBounds(84, 330, 618, 31);
contentPane.add(output);
output.setColumns(10);
output.setEditable(false);
JButton convert = new JButton("Convert");
convert.addActionListener(new ActionListener() {
public int pre(char c) {
switch(c) {
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
}
return -1;
}
public void actionPerformed(ActionEvent e) {
String s = input.getText();
Stack<Character> stk = new Stack<>();
String ans = "";
for(int i=0;i<s.length()-1;i++) {
if(Character.isLetterOrDigit(s.charAt(i))) {
ans+=s.charAt(i);
}
else if(s.charAt(i)=='(') {
stk.push(s.charAt(i));
}
else if(s.charAt(i)==')') {
while(!stk.isEmpty() && stk.peek()!='(') {
ans+=stk.pop();
}
stk.pop();
}
else {
while(!stk.isEmpty() && pre(s.charAt(i)) <= pre(stk.peek())) {
ans+=stk.pop();
}
stk.push(s.charAt(i));
}
}
while(!stk.isEmpty()) {
if(stk.peek()=='(') {
System.out.print("Invalid Expression");
}
ans+=stk.pop();
}
output.setText(ans);
}
});
convert.setFont(new Font("Times New Roman", Font.PLAIN, 20));
convert.setBounds(333, 217, 121, 31);
contentPane.add(convert);
}
}