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 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 stack = new Stack(); 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 Binary files /dev/null and b/bin/Calculator.class differ diff --git a/bin/GFrame$1.class b/bin/GFrame$1.class new file mode 100644 index 0000000..dcdfd79 Binary files /dev/null and b/bin/GFrame$1.class differ diff --git a/bin/GFrame$2.class b/bin/GFrame$2.class new file mode 100644 index 0000000..b3d4931 Binary files /dev/null and b/bin/GFrame$2.class differ diff --git a/bin/GFrame.class b/bin/GFrame.class new file mode 100644 index 0000000..603c2f8 Binary files /dev/null and b/bin/GFrame.class differ diff --git a/bin/Parser.class b/bin/Parser.class new file mode 100644 index 0000000..f370528 Binary files /dev/null and b/bin/Parser.class differ