jcal29eeb51
change how your interact with Parser
+69 −9
8 files changed
- M Calculator.java +3 −3
- A GFrame.java +54 −0
- M Parser.java +12 −6
- A bin/Calculator.class
- A bin/GFrame$1.class
- A bin/GFrame$2.class
- A bin/GFrame.class
- A bin/Parser.class
| @@ -1,5 +1,6 @@ | |||
| 1 | 1 | public class Calculator { | |
| 2 | 2 | public static void main(String[] args) { | |
| 3 | + | GFrame frame = new GFrame("Calculator"); | |
| 3 | 4 | String expr = "(84 / 4 * 3 - 9) * 2 + 1 / 5"; // 108.2 | |
| 4 | 5 | Parser p; | |
| 5 | 6 | if (args.length == 0) { | |
| @@ -7,8 +8,7 @@ public class Calculator { | |||
| 7 | 8 | } else { | |
| 8 | 9 | p = new Parser(args[0]); | |
| 9 | 10 | } | |
| 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()); | |
| 13 | 13 | } | |
| 14 | 14 | } | |
| @@ -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 | + | } | |
| @@ -8,13 +8,15 @@ import java.util.StringJoiner; | |||
| 8 | 8 | ||
| 9 | 9 | public class Parser { | |
| 10 | 10 | private String expr; | |
| 11 | + | private String postfix; | |
| 11 | 12 | private Stack<Character> operatorStack; | |
| 12 | 13 | ||
| 13 | 14 | public Parser(String infixExpr) { | |
| 14 | 15 | expr = infixExpr.trim().replaceAll("\\s", ""); // remove whitespaces | |
| 16 | + | postfix = toPostFix(); | |
| 15 | 17 | } | |
| 16 | 18 | ||
| 17 | − | public static boolean isOperand(char c) { | |
| 19 | + | private boolean isOperand(char c) { | |
| 18 | 20 | switch (c) { | |
| 19 | 21 | case '0': | |
| 20 | 22 | case '1': | |
| @@ -32,11 +34,11 @@ public class Parser { | |||
| 32 | 34 | } | |
| 33 | 35 | } | |
| 34 | 36 | ||
| 35 | − | public static boolean isOperand(String c) { | |
| 37 | + | private boolean isOperand(String c) { | |
| 36 | 38 | return isOperand(c.charAt(0)); | |
| 37 | 39 | } | |
| 38 | 40 | ||
| 39 | − | public int getPresedence(char c) { | |
| 41 | + | private int getPresedence(char c) { | |
| 40 | 42 | switch (c) { | |
| 41 | 43 | case '^': | |
| 42 | 44 | return 3; | |
| @@ -53,7 +55,7 @@ public class Parser { | |||
| 53 | 55 | } | |
| 54 | 56 | } | |
| 55 | 57 | ||
| 56 | − | public boolean hasLeftAssociativity(char c) { | |
| 58 | + | private boolean hasLeftAssociativity(char c) { | |
| 57 | 59 | if (c == '^') { | |
| 58 | 60 | return false; | |
| 59 | 61 | } else { | |
| @@ -61,7 +63,7 @@ public class Parser { | |||
| 61 | 63 | } | |
| 62 | 64 | } | |
| 63 | 65 | ||
| 64 | − | public String toPostFix() { | |
| 66 | + | private String toPostFix() { | |
| 65 | 67 | operatorStack = new Stack<>(); | |
| 66 | 68 | StringJoiner output = new StringJoiner(" "); | |
| 67 | 69 | StringBuilder operand = new StringBuilder(); | |
| @@ -136,7 +138,7 @@ public class Parser { | |||
| 136 | 138 | } | |
| 137 | 139 | } | |
| 138 | 140 | ||
| 139 | − | public double evalExpr(String postfix) { | |
| 141 | + | public double eval() { | |
| 140 | 142 | Stack<Double> stack = new Stack<Double>(); | |
| 141 | 143 | for (String c : postfix.split(" ")) { | |
| 142 | 144 | if (isOperand(c)) { | |
| @@ -149,4 +151,8 @@ public class Parser { | |||
| 149 | 151 | } | |
| 150 | 152 | return stack.pop(); | |
| 151 | 153 | } | |
| 154 | + | ||
| 155 | + | public String getPostfix() { | |
| 156 | + | return postfix; | |
| 157 | + | } | |
| 152 | 158 | } | |
[Binary file]
[Binary file]
[Binary file]
[Binary file]
[Binary file]