-
Notifications
You must be signed in to change notification settings - Fork 8
/
swisscalc.calc.calculator.js
229 lines (200 loc) · 7.78 KB
/
swisscalc.calc.calculator.js
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
//
// Eric Morgan
// Copyright (c) 2014.
//
// Class for implementing a basic/scientific calculator
/*
Usage:
var oc = swisscalc.lib.operatorCache;
var calc = new swisscalc.calc.calculator();
// Calculate: 12 + 45 =
calc.addDigit("1");
calc.addDigit("2");
calc.addBinaryOperator(oc.AdditionOperator);
calc.addDigit("4");
calc.addDigit("5");
calc.equalsPressed();
alert(calc.getMainDisplay()); // 57
calc.clear();
*/
var swisscalc = swisscalc || {};
swisscalc.calc = swisscalc.calc || {};
swisscalc.calc.calculator = function() {
this._state = 0; // STATE_AWAITING_OPERATOR
this._evaluator = new swisscalc.lib.shuntingYard();
this._mainDisplay = new swisscalc.display.numericDisplay();
this._memoryDisplay = new swisscalc.display.memoryDisplay();
};
// Constants...
swisscalc.calc.calculator.STATE_AWAITING_OPERAND = 0; // Don't use. Use STATE_AWAITING_OPERATOR instead
swisscalc.calc.calculator.STATE_AWAITING_OPERATOR = 0;
swisscalc.calc.calculator.STATE_ENTERING_OPERAND = 1;
swisscalc.calc.calculator.STATE_ENTERING_OPERATOR = 2;
// Sets the current state of the calculator.
swisscalc.calc.calculator.prototype.setState = function(state) {
this._state = state;
};
// Pushes the value of _mainDisplay onto the operand stack.
swisscalc.calc.calculator.prototype.pushDisplay = function() {
var val = this._mainDisplay.getDisplayValue();
this._evaluator.addOperand(val);
};
// Adds the given digit, or starts the display over if applicable.
// Only send 0...9 or . (decimal). Must be a string. State dependent.
swisscalc.calc.calculator.prototype.addDigit = function(digit) {
if (this._state == swisscalc.calc.calculator.STATE_AWAITING_OPERATOR)
{
this._mainDisplay.clear();
this._mainDisplay.addDigit(digit);
this.setState(swisscalc.calc.calculator.STATE_ENTERING_OPERAND);
}
else if (this._state == swisscalc.calc.calculator.STATE_ENTERING_OPERAND)
{
this._mainDisplay.addDigit(digit);
this.setState(swisscalc.calc.calculator.STATE_ENTERING_OPERAND);
}
else if (this._state == swisscalc.calc.calculator.STATE_ENTERING_OPERATOR)
{
this._mainDisplay.clear();
this._mainDisplay.addDigit(digit);
this.setState(swisscalc.calc.calculator.STATE_ENTERING_OPERAND);
}
};
// Removes the last character if applicable. State dependent.
swisscalc.calc.calculator.prototype.backspace = function() {
if (this._state == swisscalc.calc.calculator.STATE_AWAITING_OPERATOR)
{
this.setState(swisscalc.calc.calculator.STATE_AWAITING_OPERATOR);
}
else if (this._state == swisscalc.calc.calculator.STATE_ENTERING_OPERAND)
{
this._mainDisplay.backspace();
this.setState(swisscalc.calc.calculator.STATE_ENTERING_OPERAND);
}
else if (this._state == swisscalc.calc.calculator.STATE_ENTERING_OPERATOR)
{
this.setState(swisscalc.calc.calculator.STATE_ENTERING_OPERATOR);
}
};
// Clears everything and returns to initial state
swisscalc.calc.calculator.prototype.clear = function() {
this._mainDisplay.clear();
this._evaluator.clear();
this.setState(swisscalc.calc.calculator.STATE_AWAITING_OPERATOR);
};
// Clears the display. Does not change state. (Like pressing CE on a calculator)
swisscalc.calc.calculator.prototype.clearEntry = function() {
this._mainDisplay.clear();
};
// Pushes display, evaluates, and updates display.
swisscalc.calc.calculator.prototype.equalsPressed = function() {
this.pushDisplay();
var result = this._evaluator.evaluate();
this._mainDisplay.setDisplayValue(result);
this.setState(swisscalc.calc.calculator.STATE_AWAITING_OPERATOR);
};
// Adds parenthesis and clears display.
swisscalc.calc.calculator.prototype.openParen = function() {
this._evaluator.addOpenParen(swisscalc.lib.operatorCache.OpenParenOperator);
this._mainDisplay.clear();
this.setState(swisscalc.calc.calculator.STATE_AWAITING_OPERATOR);
};
// If in a sub-expression, pushes display, applies parenthesis, and updates display.
swisscalc.calc.calculator.prototype.closeParen = function() {
// Ignore if not in sub-expression...
if (!this._evaluator.inSubExpression())
return;
this.pushDisplay();
this._evaluator.addCloseParen(swisscalc.lib.operatorCache.CloseParenOperator);
this._mainDisplay.setDisplayValue(this._evaluator.popOperand());
this.setState(swisscalc.calc.calculator.STATE_AWAITING_OPERATOR);
};
// Just displays the constant on the screen.
swisscalc.calc.calculator.prototype.addNullaryOperator = function(nullaryOperator) {
var val = nullaryOperator.evaluate();
this._mainDisplay.setDisplayValue(val);
this.setState(swisscalc.calc.calculator.STATE_AWAITING_OPERATOR);
};
// Negation is a special type of unary operator because the user must be allowed to continue typing the number.
swisscalc.calc.calculator.prototype.negate = function() {
if (this._state == swisscalc.calc.calculator.STATE_AWAITING_OPERATOR)
{
this.addUnaryOperator(swisscalc.lib.operatorCache.NegateOperator);
}
else if (this._state == swisscalc.calc.calculator.STATE_ENTERING_OPERAND)
{
this._mainDisplay.negate();
this.setState(swisscalc.calc.calculator.STATE_ENTERING_OPERAND);
}
else if (this._state == swisscalc.calc.calculator.STATE_ENTERING_OPERATOR)
{
this.addUnaryOperator(swisscalc.lib.operatorCache.NegateOperator);
}
};
// Adds the given unary operator. Do NOT send this function a NegateOperator; use negate() instead.
swisscalc.calc.calculator.prototype.addUnaryOperator = function(unaryOperator) {
this.pushDisplay();
this._evaluator.addUnaryOperator(unaryOperator);
this._mainDisplay.setDisplayValue(this._evaluator.popOperand());
this.setState(swisscalc.calc.calculator.STATE_AWAITING_OPERATOR);
};
// Adds the given binary operator.
swisscalc.calc.calculator.prototype.addBinaryOperator = function(binaryOperator) {
if (this._state == swisscalc.calc.calculator.STATE_AWAITING_OPERATOR)
{
this.pushDisplay();
this._evaluator.addBinaryOperator(binaryOperator);
this._mainDisplay.setDisplayValue(this._evaluator.peekOperand());
this.setState(swisscalc.calc.calculator.STATE_ENTERING_OPERATOR);
}
else if (this._state == swisscalc.calc.calculator.STATE_ENTERING_OPERAND)
{
this.pushDisplay();
this._evaluator.addBinaryOperator(binaryOperator);
this._mainDisplay.setDisplayValue(this._evaluator.peekOperand());
this.setState(swisscalc.calc.calculator.STATE_ENTERING_OPERATOR);
}
else if (this._state == swisscalc.calc.calculator.STATE_ENTERING_OPERATOR)
{
// If entering an operator, we must have already had one, so we can pop..
this._evaluator.popOperator();
this._evaluator.addBinaryOperator(binaryOperator);
this._mainDisplay.setDisplayValue(this._evaluator.peekOperand());
this.setState(swisscalc.calc.calculator.STATE_ENTERING_OPERATOR);
}
};
// Returns the current display on the _mainDisplay.
swisscalc.calc.calculator.prototype.getMainDisplay = function() {
return this._mainDisplay.getCurrentDisplay();
};
//
// *** MEMORY FUNCTIONS ***
//
// Clears the memory.
swisscalc.calc.calculator.prototype.memoryClear = function() {
this._memoryDisplay.memoryClear();
};
// Adds current display to memory.
swisscalc.calc.calculator.prototype.memoryPlus = function() {
var val = this._mainDisplay.getDisplayValue();
this._memoryDisplay.memoryPlus(val);
};
// Subtracts current display from memory.
swisscalc.calc.calculator.prototype.memoryMinus = function() {
var val = this._mainDisplay.getDisplayValue();
this._memoryDisplay.memoryMinus(val);
};
// Sets memory to the display.
swisscalc.calc.calculator.prototype.memorySet = function() {
var val = this._mainDisplay.getDisplayValue();
this._memoryDisplay.memorySet(val);
};
// Displays memory on the display and waits for operator.
swisscalc.calc.calculator.prototype.memoryRecall = function() {
// Ignore if memory not set...
if (!this._memoryDisplay.hasMemory())
return;
var val = this._memoryDisplay.memoryRecall();
this._mainDisplay.setDisplayValue(val);
this.setState(swisscalc.calc.calculator.STATE_AWAITING_OPERATOR);
};