| 1 |
/* |
| 2 |
* Copyright 2013 Canonical Ltd. |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU Lesser General Public License as published by |
| 6 |
* the Free Software Foundation; version 3. |
| 7 |
* |
| 8 |
* This program is distributed in the hope that it will be useful, |
| 9 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
* GNU Lesser General Public License for more details. |
| 12 |
* |
| 13 |
* You should have received a copy of the GNU Lesser General Public License |
| 14 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
* |
| 16 |
* Author: Zsombor Egri <[email protected]> |
| 17 |
*/ |
| 18 |
|
| 19 |
.pragma library |
| 20 |
|
| 21 |
function Calculator(textInput, memoryIndicator) { |
| 22 |
var operationStage = 0; // 0-lvalue, 1-rvalue |
| 23 |
var lvalue = 0.0; |
| 24 |
var rvalue = 0.0; |
| 25 |
var lastOperation = -1; |
| 26 |
var memory = 0.0; |
| 27 |
var clearInputNext = true; |
| 28 |
|
| 29 |
textInput.text = lvalue; |
| 30 |
memoryIndicator.visible = false; |
| 31 |
|
| 32 |
function setValue(value) { |
| 33 |
if (operationStage === 0) { |
| 34 |
lvalue = parseFloat(value); |
| 35 |
return lvalue; |
| 36 |
} else if (operationStage === 1) { |
| 37 |
rvalue = parseFloat(value); |
| 38 |
return rvalue; |
| 39 |
} |
| 40 |
} |
| 41 |
function value() { |
| 42 |
return (operationStage === 0) ? lvalue : rvalue; |
| 43 |
} |
| 44 |
|
| 45 |
function clear() { |
| 46 |
operationStage = 1; |
| 47 |
lvalue = rvalue = 0; |
| 48 |
lastOperation = -1; |
| 49 |
textInput.text = 0.0; |
| 50 |
} |
| 51 |
function memClear() { |
| 52 |
memory = 0; |
| 53 |
memoryIndicator.visible = false; |
| 54 |
} |
| 55 |
function memRecall() { |
| 56 |
if (memory <= 0) |
| 57 |
return; |
| 58 |
textInput.text = setValue(memory); |
| 59 |
} |
| 60 |
function memAdd() { |
| 61 |
memory += value(); |
| 62 |
textInput.text = setValue(memory); |
| 63 |
memoryIndicator.visible = true; |
| 64 |
} |
| 65 |
function memDec() { |
| 66 |
memory -= value(); |
| 67 |
textInput.text = setValue(memory); |
| 68 |
memoryIndicator.visible = true; |
| 69 |
} |
| 70 |
|
| 71 |
// arythmetic functions |
| 72 |
function add() { |
| 73 |
lvalue += rvalue; |
| 74 |
} |
| 75 |
function dec() { |
| 76 |
lvalue -= rvalue; |
| 77 |
} |
| 78 |
function mul() { |
| 79 |
lvalue *= rvalue; |
| 80 |
} |
| 81 |
function div() { |
| 82 |
lvalue /= rvalue; |
| 83 |
} |
| 84 |
|
| 85 |
function sign() { |
| 86 |
textInput.text = setValue(-value()); |
| 87 |
} |
| 88 |
|
| 89 |
function invert() { |
| 90 |
textInput.text = setValue(1/value()); |
| 91 |
} |
| 92 |
function percent() { |
| 93 |
textInput.text = setValue(value() / 100); |
| 94 |
} |
| 95 |
function sqr() { |
| 96 |
textInput.text = setValue(Math.pow(value(), 2)); |
| 97 |
} |
| 98 |
function cube() { |
| 99 |
textInput.text = setValue(Math.pow(value(), 3)); |
| 100 |
} |
| 101 |
function power() { |
| 102 |
lvalue = Math.pow(lvalue, rvalue); |
| 103 |
} |
| 104 |
function powerOf2() { |
| 105 |
textInput.text = setValue(Math.pow(2, value())); |
| 106 |
} |
| 107 |
function factorial() { |
| 108 |
var rval = 1; |
| 109 |
for (var i = 2; i <= value(); i++) { |
| 110 |
rval *= i; |
| 111 |
} |
| 112 |
textInput.text = setValue(rval); |
| 113 |
} |
| 114 |
function sqrt() { |
| 115 |
textInput.text = setValue(Math.sqrt(value())); |
| 116 |
} |
| 117 |
function ln() { |
| 118 |
textInput.text = setValue(Math.log(value())); |
| 119 |
} |
| 120 |
function exp() |
| 121 |
{ |
| 122 |
textInput.text = setValue(Math.exp(value())); |
| 123 |
} |
| 124 |
function sin() { |
| 125 |
textInput.text = setValue(Math.sin(value())); |
| 126 |
} |
| 127 |
function cos() { |
| 128 |
textInput.text = setValue(Math.cos(value())); |
| 129 |
} |
| 130 |
function tan() { |
| 131 |
textInput.text = setValue(Math.tan(value())); |
| 132 |
} |
| 133 |
function cot() { |
| 134 |
textInput.text = setValue(1/Math.tan(value())); |
| 135 |
} |
| 136 |
function sec() { |
| 137 |
textInput.text = setValue(1/Math.cos(value())); |
| 138 |
} |
| 139 |
function csc() { |
| 140 |
textInput.text = setValue(1/Math.sin(value())); |
| 141 |
} |
| 142 |
|
| 143 |
var operations = [ |
| 144 |
{code: "+", operationFunc: add}, |
| 145 |
{code: "-", operationFunc: dec}, |
| 146 |
{code: "*", operationFunc: mul}, |
| 147 |
{code: "/", operationFunc: div}, |
| 148 |
|
| 149 |
{code: "+/-", controlFunc: sign}, |
| 150 |
{code: "c", controlFunc: clear}, |
| 151 |
|
| 152 |
{code: "mc", controlFunc: memClear}, |
| 153 |
{code: "mr", controlFunc: memRecall}, |
| 154 |
{code: "m+", controlFunc: memAdd}, |
| 155 |
{code: "m-", controlFunc: memDec}, |
| 156 |
|
| 157 |
{code: "%", controlFunc: percent}, |
| 158 |
{code: "1/x", controlFunc: invert}, |
| 159 |
{code: "x!", controlFunc: factorial}, |
| 160 |
{code: "x^2", controlFunc: sqr}, |
| 161 |
{code: "x^3", controlFunc: cube}, |
| 162 |
{code: "x^y", operationFunc: power}, |
| 163 |
{code: "2^x", controlFunc: powerOf2}, |
| 164 |
{code: "sqrt", controlFunc: sqrt}, |
| 165 |
{code: "ln", controlFunc: ln}, |
| 166 |
{code: "e^x", controlFunc: exp}, |
| 167 |
{code: "sin()", controlFunc: sin}, |
| 168 |
{code: "cos()", controlFunc: cos}, |
| 169 |
{code: "tan()", controlFunc: tan}, |
| 170 |
{code: "cot()", controlFunc: cot}, |
| 171 |
{code: "sec()", controlFunc: sec}, |
| 172 |
{code: "csc()", controlFunc: csc}, |
| 173 |
|
| 174 |
]; |
| 175 |
|
| 176 |
this.execute = function (operation) { |
| 177 |
if (operation === "=") { |
| 178 |
if (lastOperation > -1) { |
| 179 |
operations[lastOperation].operationFunc(); |
| 180 |
textInput.text = lvalue; |
| 181 |
} |
| 182 |
operationStage = 0; |
| 183 |
clearInputNext = true; |
| 184 |
} else { |
| 185 |
for (var i = 0; i < operations.length; i++) { |
| 186 |
if (operations[i].code === operation) { |
| 187 |
if (operations[i].operationFunc) { |
| 188 |
clearInputNext = true; |
| 189 |
if (operations[i].singleOp) { |
| 190 |
operations[i].operationFunc(); |
| 191 |
return; |
| 192 |
} |
| 193 |
|
| 194 |
lastOperation = i; |
| 195 |
if (operationStage === 0) { |
| 196 |
operationStage = 1; |
| 197 |
} else if (operationStage === 1) { |
| 198 |
// evaluate and continue receiving rvalue |
| 199 |
operations[i].operationFunc(lvalue, rvalue); |
| 200 |
textInput.text = lvalue; |
| 201 |
} |
| 202 |
} else if (operations[i].controlFunc) { |
| 203 |
operations[i].controlFunc(); |
| 204 |
clearInputNext = true; |
| 205 |
} |
| 206 |
|
| 207 |
return; |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
// if no opeartion was performed, add operation to input |
| 212 |
if (clearInputNext) { |
| 213 |
clearInputNext = false; |
| 214 |
textInput.text = ""; |
| 215 |
} |
| 216 |
textInput.text = setValue(textInput.text + operation); |
| 217 |
} |
| 218 |
} |
| 219 |
} |