change how your interact with Parser
This commit is contained in:
parent
c62432ebda
commit
29eeb51919
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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("");
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
18
Parser.java
18
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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue