summaryrefslogtreecommitdiffstats
path: root/src/de/fhswf/in/inf/java1/aufgabe12/UPN.java
diff options
context:
space:
mode:
authorStefan Suhren <suhren.stefan@fh-swf.de>2015-01-06 11:43:01 +0100
committerStefan Suhren <suhren.stefan@fh-swf.de>2015-01-06 11:43:01 +0100
commitc1a240e873cb0e434ea98506d2f33b4a571e354a (patch)
tree71c6c90767a01a7b9b473f84ef54834c8f14b581 /src/de/fhswf/in/inf/java1/aufgabe12/UPN.java
parent44d4f449ee2b072ec62618d6e1cfa2bd419c4f77 (diff)
downloadJava1-c1a240e873cb0e434ea98506d2f33b4a571e354a.tar.gz
Java1-c1a240e873cb0e434ea98506d2f33b4a571e354a.zip
Assignment No.12 first complete implementation.
Diffstat (limited to 'src/de/fhswf/in/inf/java1/aufgabe12/UPN.java')
-rw-r--r--src/de/fhswf/in/inf/java1/aufgabe12/UPN.java132
1 files changed, 132 insertions, 0 deletions
diff --git a/src/de/fhswf/in/inf/java1/aufgabe12/UPN.java b/src/de/fhswf/in/inf/java1/aufgabe12/UPN.java
new file mode 100644
index 0000000..815e937
--- /dev/null
+++ b/src/de/fhswf/in/inf/java1/aufgabe12/UPN.java
@@ -0,0 +1,132 @@
+/**
+ *
+ */
+package de.fhswf.in.inf.java1.aufgabe12;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Stack;
+
+/**
+ * A class that calculates in UPN.
+ *
+ * @author $Author: $
+ * @version $Revision: $, $Date: $ UTC
+ */
+public class UPN
+{
+
+ private Stack<Double> upnStack = new Stack<>();
+
+ private static Map<String, Operator> operators = new HashMap<>();
+
+ static
+ {
+ operators.put("+", new Addition());
+ operators.put("-", new Subtraction());
+ operators.put("*", new Multiplication());
+ operators.put("/", new Division());
+ operators.put("%", new Modulo());
+ }
+
+ /**
+ * Empty constructor.
+ *
+ */
+ public UPN()
+ {
+ }
+
+ /**
+ * Reads a string and puts it on the UPN upnStack.
+ *
+ * @param upnString
+ * A space delimited string in UPN notation.
+ */
+ public final void calculate(String upnString)
+ {
+ if (upnString == null)
+ {
+ throw new IllegalArgumentException("UpnString can't be null.");
+ }
+ if (upnString.isEmpty())
+ {
+ throw new IllegalArgumentException("UpnString can't be empty.");
+ }
+
+ for (String string : upnString.split("\\s+"))
+ {
+ if (!string.isEmpty())
+ {
+ Operator operator = operators.get(string);
+
+ if (operator == null)
+ {
+ try
+ {
+ operator = new UnaryTemplate(string);
+ }
+ catch (IllegalArgumentException e)
+ {
+ if (!(e.getCause() instanceof NoSuchMethodException))
+ {
+ throw e;
+ }
+ }
+ if (operator == null)
+ {
+ try
+ {
+ operator = new BinaryTemplate(string);
+ }
+ catch (IllegalArgumentException e)
+ {
+ if (!(e.getCause() instanceof NoSuchMethodException))
+ {
+ throw e;
+ }
+ }
+
+ }
+ if (operator != null)
+ {
+ operators.put(string, operator);
+ operator.eval(upnStack);
+ }
+ else
+ {
+ try
+ {
+ upnStack.add(Double.valueOf(string));
+ }
+ catch (NumberFormatException e)
+ {
+ throw new IllegalArgumentException("String: \"" + string
+ + "\" is not a valid"
+ + " operator nor a valid operand.");
+ }
+ }
+ }
+ else
+ {
+ operator.eval(upnStack);
+ }
+ }
+ }
+ }
+
+ /**
+ * Get the result of a calculation done before.
+ *
+ * @return The top most value on the UPN stack.
+ */
+ public final double getResult()
+ {
+ if (upnStack.empty())
+ {
+ throw new IllegalArgumentException("UPN stack is empty.");
+ }
+ return upnStack.peek();
+ }
+
+}