summaryrefslogtreecommitdiffstats
path: root/src/de/fhswf/in/inf/java1/aufgabe11/UPN.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/de/fhswf/in/inf/java1/aufgabe11/UPN.java')
-rw-r--r--src/de/fhswf/in/inf/java1/aufgabe11/UPN.java112
1 files changed, 68 insertions, 44 deletions
diff --git a/src/de/fhswf/in/inf/java1/aufgabe11/UPN.java b/src/de/fhswf/in/inf/java1/aufgabe11/UPN.java
index 25b939e..f8938fc 100644
--- a/src/de/fhswf/in/inf/java1/aufgabe11/UPN.java
+++ b/src/de/fhswf/in/inf/java1/aufgabe11/UPN.java
@@ -43,52 +43,43 @@ public class UPN
for (String string : upnString.split("\\s+"))
{
- try
- {
- upnStack.add(Double.valueOf(string));
- }
- catch (NumberFormatException e)
- {
- double operand1 = 0;
- double operand2 = 0;
- if (upnStack.size() < 2)
- {
- throw new IllegalStateException(
- "Two operands are needed for an operation.");
- }
- operand2 = upnStack.pop();
- operand1 = upnStack.pop();
+ Double[] operands = null;
- switch (string)
- {
- case "+":
- upnStack.add(operand1 + operand2);
- break;
- case "-":
- upnStack.add(operand1 - operand2);
- break;
- case "*":
- upnStack.add(operand1 * operand2);
- break;
- case "/":
- if (operand2 == 0)
- {
- throw new IllegalArgumentException("Divisor can't be 0.");
- }
- upnStack.add(operand1 / operand2);
- break;
- case "%":
- if (operand2 == 0)
- {
- throw new IllegalArgumentException("Divisor can't be 0.");
- }
- upnStack.add(operand1 % operand2);
- break;
- default:
+ switch (string)
+ {
+ case "+":
+ operands = fetch();
+ upnStack.add(operands[0] + operands[1]);
+ break;
+ case "-":
+ operands = fetch();
+ upnStack.add(operands[0] - operands[1]);
+ break;
+ case "*":
+ operands = fetch();
+ upnStack.add(operands[0] * operands[1]);
+ break;
+ case "/":
+ divisorZero();
+ operands = fetch();
+ upnStack.add(operands[0] / operands[1]);
+ break;
+ case "%":
+ divisorZero();
+ operands = fetch();
+ upnStack.add(operands[0] % operands[1]);
+ break;
+ default:
+ try
+ {
+ upnStack.add(Double.valueOf(string));
+ }
+ catch (NumberFormatException e)
+ {
throw new IllegalArgumentException("String: \"" + string
- + "\" is not a valid operator.");
- }
+ + "\" is not a valid operator nor a valid operand.");
+ }
}
}
}
@@ -102,9 +93,42 @@ public class UPN
{
if (upnStack.empty())
{
- throw new IllegalStateException("UPN stack is empty.");
+ throw new IllegalArgumentException("UPN stack is empty.");
}
return upnStack.peek();
}
+
+ /**
+ * Checks if Divisor will be zero.
+ *
+ */
+ private void divisorZero()
+ {
+ if (upnStack.peek() == 0.0)
+ {
+ throw new IllegalArgumentException("Divisor can't be 0.");
+ }
+ }
+
+ /**
+ * Checks if there are two values on the stack and returns them.
+ *
+ * @return The two top most doubles from the stack.
+ */
+ private Double[] fetch()
+ {
+ if (upnStack.size() < 2)
+ {
+ throw new IllegalArgumentException(
+ "Two operands are needed for an operation.");
+ }
+
+ Double[] ret = new Double[2];
+
+ ret[1] = upnStack.pop();
+ ret[0] = upnStack.pop();
+
+ return ret;
+ }
}