Krolyxon
Commit 29eeb51

Branches

Refs →

jcal29eeb51

change how your interact with Parser

Author
krolxon
Date
2024-02-16 00:09 +0530
Parent
c62432e
Commit
29eeb5191923e2f1e590b9ad28683f7458e736b5

+69 −9

8 files changed

  1. M Calculator.java +3 −3
  2. A GFrame.java +54 −0
  3. M Parser.java +12 −6
  4. A bin/Calculator.class
  5. A bin/GFrame$1.class
  6. A bin/GFrame$2.class
  7. A bin/GFrame.class
  8. A bin/Parser.class
M Calculator.java View
@@ -1,5 +1,6 @@
11 public class Calculator {
22 public static void main(String[] args) {
3+ GFrame frame = new GFrame("Calculator");
34 String expr = "(84 / 4 * 3 - 9) * 2 + 1 / 5"; // 108.2
45 Parser p;
56 if (args.length == 0) {
@@ -7,8 +8,7 @@ public class Calculator {
78 } else {
89 p = new Parser(args[0]);
910 }
10− String postfix = p.toPostFix();
11− // System.out.println("pfix => \t " + postfix);
12− System.out.println(p.evalExpr(postfix));
11+ System.out.println("postfix => \t " + p.getPostfix());
12+ System.out.println(p.eval());
1313 }
1414 }
A GFrame.java View
@@ -0,0 +1,54 @@
1+import javax.swing.*;
2+import java.awt.Color;
3+import java.awt.Dimension;
4+import java.awt.event.*;
5+
6+public class GFrame extends JFrame {
7+ Parser p;
8+
9+ GFrame(String title) {
10+ this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
11+ this.setTitle(title);
12+ this.getContentPane().setBackground(Color.decode("#4E586e"));
13+ JButton b = new JButton();
14+ b.setBounds(130, 100, 100, 40);
15+ b.setBackground(Color.decode("#F78361"));
16+ b.setText("Evaluate");
17+
18+ JTextField tf = new JTextField();
19+ tf.setBounds(130, 50, 220, 40);
20+
21+ JButton bClear = new JButton();
22+ bClear.setBounds(250, 100, 100, 40);
23+ bClear.setBackground(Color.decode("#F78361"));
24+ bClear.setText("Clear");
25+
26+ this.add(b);
27+ this.add(bClear);
28+ this.add(tf);
29+ this.setSize(400, 500);
30+ this.setLayout(null);
31+ this.setVisible(true);
32+
33+ JLabel label = new JLabel("Enter Expression: ");
34+ label.setBounds(130, 20, 150, 40);
35+ this.add(label);
36+
37+ b.addActionListener(new ActionListener() {
38+ public void actionPerformed(ActionEvent e) {
39+ if (tf.getText().length() != 0) {
40+ p = new Parser(tf.getText());
41+ tf.setText(Double.toString(p.eval()));
42+ } else {
43+ tf.setText("No input");
44+ }
45+ }
46+ });
47+
48+ bClear.addActionListener(new ActionListener() {
49+ public void actionPerformed(ActionEvent e) {
50+ tf.setText("");
51+ }
52+ });
53+ }
54+}
M Parser.java View
@@ -8,13 +8,15 @@ import java.util.StringJoiner;
88
99 public class Parser {
1010 private String expr;
11+ private String postfix;
1112 private Stack<Character> operatorStack;
1213
1314 public Parser(String infixExpr) {
1415 expr = infixExpr.trim().replaceAll("\\s", ""); // remove whitespaces
16+ postfix = toPostFix();
1517 }
1618
17− public static boolean isOperand(char c) {
19+ private boolean isOperand(char c) {
1820 switch (c) {
1921 case '0':
2022 case '1':
@@ -32,11 +34,11 @@ public class Parser {
3234 }
3335 }
3436
35− public static boolean isOperand(String c) {
37+ private boolean isOperand(String c) {
3638 return isOperand(c.charAt(0));
3739 }
3840
39− public int getPresedence(char c) {
41+ private int getPresedence(char c) {
4042 switch (c) {
4143 case '^':
4244 return 3;
@@ -53,7 +55,7 @@ public class Parser {
5355 }
5456 }
5557
56− public boolean hasLeftAssociativity(char c) {
58+ private boolean hasLeftAssociativity(char c) {
5759 if (c == '^') {
5860 return false;
5961 } else {
@@ -61,7 +63,7 @@ public class Parser {
6163 }
6264 }
6365
64− public String toPostFix() {
66+ private String toPostFix() {
6567 operatorStack = new Stack<>();
6668 StringJoiner output = new StringJoiner(" ");
6769 StringBuilder operand = new StringBuilder();
@@ -136,7 +138,7 @@ public class Parser {
136138 }
137139 }
138140
139− public double evalExpr(String postfix) {
141+ public double eval() {
140142 Stack<Double> stack = new Stack<Double>();
141143 for (String c : postfix.split(" ")) {
142144 if (isOperand(c)) {
@@ -149,4 +151,8 @@ public class Parser {
149151 }
150152 return stack.pop();
151153 }
154+
155+ public String getPostfix() {
156+ return postfix;
157+ }
152158 }
A bin/Calculator.class View

[Binary file]

A bin/GFrame$1.class View

[Binary file]

A bin/GFrame$2.class View

[Binary file]

A bin/GFrame.class View

[Binary file]

A bin/Parser.class View

[Binary file]