blob: 1918ba6c04f31c3af4af8c4c32fa9adca0ff4432 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
/**
*
*/
package de.fhswf.in.inf.upnfx.util;
/**
* A class that calculates in UPN.
*
* @author $Author: $
* @version $Revision: $, $Date: $ UTC
*/
public class UPN
{
private ObservableDoubleStack upnStack = new ObservableDoubleStack();
private Double lastX;
/**
* 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(Operator operator)
{
if (operator == null)
{
throw new IllegalArgumentException("Operator can't be null.");
}
lastX = upnStack.peek();
operator.eval(upnStack);
}
public final void addDouble(Double newVal)
{
if (newVal == null)
{
throw new IllegalArgumentException("newVal can't be null.");
}
upnStack.push(newVal);
}
/**
* Get the result of a calculation done before.
*
* @return The top most value on the UPN stack.
*/
public final double getResult()
{
if (upnStack.isEmpty())
{
throw new IllegalArgumentException("UPN stack is empty.");
}
return upnStack.peek();
}
/**
* Return the ObservableDoubleStack to tie it to the view.
*
* @return The ObservableDoubleStack with the operands.
*/
public final ObservableDoubleStack getStack()
{
return upnStack;
}
/**
* Pushes the previous x onto the stack.
*
*/
public final void setLastX()
{
if(lastX != null)
{
upnStack.push(lastX);
}
}
/**
* Swap x and y on the stack.
*
*/
public final void swapXY()
{
if(upnStack.size() > 1)
{
double f1 = upnStack.pop();
double f2 = upnStack.pop();
upnStack.push(f1);
upnStack.push(f2);
}
}
}
|