aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkrolxon <krolyxon@tutanota.com>2024-02-16 00:09:35 +0530
committerkrolxon <krolyxon@tutanota.com>2024-02-16 00:09:35 +0530
commit29eeb5191923e2f1e590b9ad28683f7458e736b5 (patch)
treeeb602c91e380e02f00a1cb63124d9109fc99638a
parentc62432ebdacb54f58b0908f11879235d15460cf0 (diff)
change how your interact with Parser
-rw-r--r--Calculator.java6
-rw-r--r--GFrame.java54
-rw-r--r--Parser.java18
-rw-r--r--bin/Calculator.classbin0 -> 1173 bytes
-rw-r--r--bin/GFrame$1.classbin0 -> 1021 bytes
-rw-r--r--bin/GFrame$2.classbin0 -> 653 bytes
-rw-r--r--bin/GFrame.classbin0 -> 1444 bytes
-rw-r--r--bin/Parser.classbin0 -> 3181 bytes
8 files changed, 69 insertions, 9 deletions
diff --git a/Calculator.java b/Calculator.java
index 43804c3..7070c87 100644
--- a/Calculator.java
+++ b/Calculator.java
@@ -1,5 +1,6 @@
public class Calculator {
public static void main(String[] args) {
+ GFrame frame = new GFrame("Calculator");
String expr = "(84 / 4 * 3 - 9) * 2 + 1 / 5"; // 108.2
Parser p;
if (args.length == 0) {
@@ -7,8 +8,7 @@ public class Calculator {
} else {
p = new Parser(args[0]);
}
- String postfix = p.toPostFix();
- // System.out.println("pfix => \t " + postfix);
- System.out.println(p.evalExpr(postfix));
+ System.out.println("postfix => \t " + p.getPostfix());
+ System.out.println(p.eval());
}
}
diff --git a/GFrame.java b/GFrame.java
new file mode 100644
index 0000000..b8ef7ed
--- /dev/null
+++ b/GFrame.java
@@ -0,0 +1,54 @@
+import javax.swing.*;
+import java.awt.Color;
+import java.awt.Dimension;
+import java.awt.event.*;
+
+public class GFrame extends JFrame {
+ Parser p;
+
+ GFrame(String title) {
+ this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+ this.setTitle(title);
+ this.getContentPane().setBackground(Color.decode("#4E586e"));
+ JButton b = new JButton();
+ b.setBounds(130, 100, 100, 40);
+ b.setBackground(Color.decode("#F78361"));
+ b.setText("Evaluate");
+
+ JTextField tf = new JTextField();
+ tf.setBounds(130, 50, 220, 40);
+
+ JButton bClear = new JButton();
+ bClear.setBounds(250, 100, 100, 40);
+ bClear.setBackground(Color.decode("#F78361"));
+ bClear.setText("Clear");
+
+ this.add(b);
+ this.add(bClear);
+ this.add(tf);
+ this.setSize(400, 500);
+ this.setLayout(null);
+ this.setVisible(true);
+
+ JLabel label = new JLabel("Enter Expression: ");
+ label.setBounds(130, 20, 150, 40);
+ this.add(label);
+
+ b.addActionListener(new ActionListener() {
+ public void actionPerformed(ActionEvent e) {
+ if (tf.getText().length() != 0) {
+ p = new Parser(tf.getText());
+ tf.setText(Double.toString(p.eval()));
+ } else {
+ tf.setText("No input");
+ }
+ }
+ });
+
+ bClear.addActionListener(new ActionListener() {
+ public void actionPerformed(ActionEvent e) {
+ tf.setText("");
+ }
+ });
+ }
+}
diff --git a/Parser.java b/Parser.java
index 074a6e0..bd2cde9 100644
--- a/Parser.java
+++ b/Parser.java
@@ -8,13 +8,15 @@ import java.util.StringJoiner;
public class Parser {
private String expr;
+ private String postfix;
private Stack<Character> operatorStack;
public Parser(String infixExpr) {
expr = infixExpr.trim().replaceAll("\\s", ""); // remove whitespaces
+ postfix = toPostFix();
}
- public static boolean isOperand(char c) {
+ private boolean isOperand(char c) {
switch (c) {
case '0':
case '1':
@@ -32,11 +34,11 @@ public class Parser {
}
}
- public static boolean isOperand(String c) {
+ private boolean isOperand(String c) {
return isOperand(c.charAt(0));
}
- public int getPresedence(char c) {
+ private int getPresedence(char c) {
switch (c) {
case '^':
return 3;
@@ -53,7 +55,7 @@ public class Parser {
}
}
- public boolean hasLeftAssociativity(char c) {
+ private boolean hasLeftAssociativity(char c) {
if (c == '^') {
return false;
} else {
@@ -61,7 +63,7 @@ public class Parser {
}
}
- public String toPostFix() {
+ private String toPostFix() {
operatorStack = new Stack<>();
StringJoiner output = new StringJoiner(" ");
StringBuilder operand = new StringBuilder();
@@ -136,7 +138,7 @@ public class Parser {
}
}
- public double evalExpr(String postfix) {
+ public double eval() {
Stack<Double> stack = new Stack<Double>();
for (String c : postfix.split(" ")) {
if (isOperand(c)) {
@@ -149,4 +151,8 @@ public class Parser {
}
return stack.pop();
}
+
+ public String getPostfix() {
+ return postfix;
+ }
}
diff --git a/bin/Calculator.class b/bin/Calculator.class
new file mode 100644
index 0000000..e358d74
--- /dev/null
+++ b/bin/Calculator.class
Binary files differ
diff --git a/bin/GFrame$1.class b/bin/GFrame$1.class
new file mode 100644
index 0000000..dcdfd79
--- /dev/null
+++ b/bin/GFrame$1.class
Binary files differ
diff --git a/bin/GFrame$2.class b/bin/GFrame$2.class
new file mode 100644
index 0000000..b3d4931
--- /dev/null
+++ b/bin/GFrame$2.class
Binary files differ
diff --git a/bin/GFrame.class b/bin/GFrame.class
new file mode 100644
index 0000000..603c2f8
--- /dev/null
+++ b/bin/GFrame.class
Binary files differ
diff --git a/bin/Parser.class b/bin/Parser.class
new file mode 100644
index 0000000..f370528
--- /dev/null
+++ b/bin/Parser.class
Binary files differ