Added support for (), added buttons, fixes

This commit is contained in:
Norbi Peti 2016-07-18 10:16:46 +02:00
parent 1a129c52bf
commit 0c807a8384
2 changed files with 41 additions and 29 deletions

View file

@ -1,6 +1,7 @@
package calculator; package calculator;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -11,8 +12,11 @@ public class Calc {
private static final Pattern ADD = Pattern.compile("([0-9\\.]+)\\+([0-9\\.]+)"); private static final Pattern ADD = Pattern.compile("([0-9\\.]+)\\+([0-9\\.]+)");
private static final Pattern SUBTRACT = Pattern.compile("([0-9\\.]+)\\-([0-9\\.]+)"); private static final Pattern SUBTRACT = Pattern.compile("([0-9\\.]+)\\-([0-9\\.]+)");
private static final Pattern PARENTHESIS = Pattern.compile("\\((.+)\\)");
public static Double calculate(String text) { public static Double calculate(String text) {
StringBuffer buf = new StringBuffer(text); StringBuffer buf = new StringBuffer(text);
replace(buf, PARENTHESIS, (a) -> calculate(a));
replace(buf, MULTIPLY, (a, b) -> a * b); replace(buf, MULTIPLY, (a, b) -> a * b);
replace(buf, DIVIDE, (a, b) -> a / b); replace(buf, DIVIDE, (a, b) -> a / b);
replace(buf, ADD, (a, b) -> a + b); replace(buf, ADD, (a, b) -> a + b);
@ -33,4 +37,13 @@ public class Calc {
.toString(doit.apply(Double.parseDouble(matcher.group(1)), Double.parseDouble(matcher.group(2))))); .toString(doit.apply(Double.parseDouble(matcher.group(1)), Double.parseDouble(matcher.group(2)))));
} }
} }
private static void replace(StringBuffer buf, Pattern pattern, Function<String, Double> doit) {
while (true) {
Matcher matcher = pattern.matcher(buf.toString());
if (!matcher.find())
break;
buf.replace(matcher.start(), matcher.end(), Double.toString(doit.apply(matcher.group(1))));
}
}
} }

View file

@ -20,6 +20,8 @@ public class Gui {
private JPanel panel; private JPanel panel;
private JTextField tf; private JTextField tf;
public static final String TYPE_CHARS = "+-*/.()";
public Gui() throws Exception { public Gui() throws Exception {
frame = new JFrame(); frame = new JFrame();
frame.setMinimumSize(new Dimension(300, 400)); frame.setMinimumSize(new Dimension(300, 400));
@ -30,17 +32,13 @@ public class Gui {
@Override @Override
public void keyTyped(KeyEvent e) { public void keyTyped(KeyEvent e) {
if (e.getKeyChar() == '+') for (int i = 0; i < TYPE_CHARS.length(); i++) {
tf.setText(tf.getText() + '+'); if (e.getKeyChar() == TYPE_CHARS.charAt(i)) {
else if (e.getKeyChar() == '-') tf.setText(tf.getText() + TYPE_CHARS.charAt(i));
tf.setText(tf.getText() + "-"); return;
else if (e.getKeyChar() == '*') }
tf.setText(tf.getText() + "*"); }
else if (e.getKeyChar() == '/') if (e.getKeyChar() == '\n') {
tf.setText(tf.getText() + "/");
else if (e.getKeyChar() == '.')
tf.setText(tf.getText() + ".");
else if (e.getKeyChar() == '\n') {
calc(); calc();
} else if (e.getKeyChar() == '\b' && tf.getText().length() > 0) } else if (e.getKeyChar() == '\b' && tf.getText().length() > 0)
tf.setText(tf.getText().substring(0, tf.getText().length() - 1)); tf.setText(tf.getText().substring(0, tf.getText().length() - 1));
@ -63,9 +61,16 @@ public class Gui {
panel = new JPanel(); panel = new JPanel();
frame.add(panel, BorderLayout.CENTER); frame.add(panel, BorderLayout.CENTER);
panel.setLayout(new GridLayout(4, 4)); panel.setLayout(new GridLayout(4, 4));
String str = addNumbers("+-*"); addAppendButtons("123+");
str += ".0"; addButton('C').addActionListener(new ActionListener() {
addAppendButtons(str);
@Override
public void actionPerformed(ActionEvent e) {
if (tf.getText().length() > 0)
tf.setText(tf.getText().substring(0, tf.getText().length() - 1));
}
});
addAppendButtons("456-(789*).0");
addButton('=').addActionListener(new ActionListener() { addButton('=').addActionListener(new ActionListener() {
@Override @Override
@ -84,17 +89,6 @@ public class Gui {
frame.dispose(); frame.dispose();
} }
private String addNumbers(String rowend) {
String str = "";
int x = 0;
for (int i = 1; i < 10; i++) {
str += i;
if (i % 3 == 0)
str += rowend.charAt(x++);
}
return str;
}
private void addAppendButtons(String btns) { private void addAppendButtons(String btns) {
for (int i = 0; i < btns.length(); i++) { for (int i = 0; i < btns.length(); i++) {
Character c = btns.charAt(i); Character c = btns.charAt(i);
@ -107,8 +101,8 @@ public class Gui {
tf.setText(tf.getText() + number); tf.setText(tf.getText() + number);
} }
}); });
} }
} }
private JButton addButton(Character c) { private JButton addButton(Character c) {
JButton num = new JButton(); JButton num = new JButton();
@ -120,10 +114,15 @@ public class Gui {
private void calc() { private void calc() {
try { try {
if (tf.getText().length() == 0) {
tf.setText("0.0");
return;
}
Double ret = Calc.calculate(tf.getText()); Double ret = Calc.calculate(tf.getText());
if (ret == null) if (ret == null)
JOptionPane.showMessageDialog(frame, "Unable to convert result to number!", "Error!", JOptionPane.showMessageDialog(frame,
JOptionPane.WARNING_MESSAGE); "Unable to convert result to number!\nThis is usually caused by a mistype, like \"5++5\"",
"Error!", JOptionPane.WARNING_MESSAGE);
else else
tf.setText(Double.toString(ret)); tf.setText(Double.toString(ret));
} catch (ArithmeticException e) { } catch (ArithmeticException e) {