-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpressionEvaluator.java
More file actions
147 lines (124 loc) · 5.24 KB
/
ExpressionEvaluator.java
File metadata and controls
147 lines (124 loc) · 5.24 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
package semesterproject;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Stack;
public class ExpressionEvaluator extends JFrame {
private JTextField inputField;
private JTextArea outputArea;
private JButton evaluateButton;
public ExpressionEvaluator() {
setTitle("Expression Evaluator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600, 400);
setLocationRelativeTo(null);
setResizable(false);
getContentPane().setBackground(new Color(0x1E1E1E));
inputField = new JTextField();
inputField.setFont(new Font("Arial", Font.PLAIN, 18));
inputField.setForeground(Color.WHITE);
inputField.setBackground(new Color(0x2B2B2B));
inputField.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
outputArea = new JTextArea();
outputArea.setFont(new Font("Monospace", Font.PLAIN, 16));
outputArea.setForeground(new Color(0x00FF00));
outputArea.setBackground(new Color(0x2B2B2B));
outputArea.setEditable(false);
outputArea.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
evaluateButton = new JButton("Evaluate");
evaluateButton.setFont(new Font("Arial", Font.BOLD, 16));
evaluateButton.setForeground(Color.WHITE);
evaluateButton.setBackground(new Color(0x007ACC));
evaluateButton.setBorder(BorderFactory.createEmptyBorder(10, 20, 10, 20));
evaluateButton.addActionListener(e -> evaluateExpression());
JPanel mainPanel = new JPanel(new BorderLayout(20, 20));
mainPanel.setBackground(new Color(0x1E1E1E));
mainPanel.add(inputField, BorderLayout.NORTH);
mainPanel.add(new JScrollPane(outputArea), BorderLayout.CENTER);
mainPanel.add(evaluateButton, BorderLayout.SOUTH);
getContentPane().add(mainPanel, BorderLayout.CENTER);
}
private void evaluateExpression() {
String expression = inputField.getText().trim();
if(expression.isEmpty()) {
outputArea.setText("Error: Please enter an expression");
return;
}
try {
String postfix = convertToPostfix(expression);
double result = evaluatePostfix(postfix);
outputArea.setText("Input Expression: " + expression + "\n");
outputArea.append("Postfix Expression: " + postfix + "\n");
outputArea.append("Result: " + result);
} catch(Exception e) {
outputArea.setText("Error: Invalid Expression");
}
}
private String convertToPostfix(String infix) {
Stack<Character> stack = new Stack<>();
StringBuilder postfix = new StringBuilder();
int i = 0;
while(i < infix.length()) {
char c = infix.charAt(i);
if(Character.isDigit(c)) {
// Multi-digit number handling
while(i < infix.length() && (Character.isDigit(infix.charAt(i)) || infix.charAt(i)=='.')) {
postfix.append(infix.charAt(i));
i++;
}
postfix.append(' '); // space separator
continue;
} else if(c == '(') {
stack.push(c);
} else if(c == ')') {
while(!stack.isEmpty() && stack.peek() != '(') {
postfix.append(stack.pop()).append(' ');
}
if(!stack.isEmpty() && stack.peek() == '(') stack.pop();
} else if(c == '+' || c == '-' || c == '*' || c == '/') {
while(!stack.isEmpty() && precedence(c) <= precedence(stack.peek())) {
postfix.append(stack.pop()).append(' ');
}
stack.push(c);
}
i++;
}
while(!stack.isEmpty()) {
postfix.append(stack.pop()).append(' ');
}
return postfix.toString().trim();
}
private int precedence(char operator) {
switch(operator) {
case '+': case '-': return 1;
case '*': case '/': return 2;
default: return -1;
}
}
private double evaluatePostfix(String postfix) {
Stack<Double> stack = new Stack<>();
String[] tokens = postfix.split(" ");
for(String token : tokens) {
if(token.isEmpty()) continue;
if(token.matches("\\d+(\\.\\d+)?")) { // number with optional decimal
stack.push(Double.parseDouble(token));
} else {
if(stack.size() < 2) throw new RuntimeException("Invalid Expression");
double b = stack.pop();
double a = stack.pop();
switch(token) {
case "+": stack.push(a+b); break;
case "-": stack.push(a-b); break;
case "*": stack.push(a*b); break;
case "/": stack.push(a/b); break;
default: throw new RuntimeException("Unknown operator");
}
}
}
if(stack.isEmpty()) throw new RuntimeException("Invalid Expression");
return stack.pop();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new ExpressionEvaluator().setVisible(true));
}
}