forked from nemomobile/qmlcalc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculator.js
114 lines (102 loc) · 3.8 KB
/
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
var rotateLeft = "\u2939"
var rotateRight = "\u2935"
var leftArrow = "\u2190"
var division = "\u00f7"
var multiplication = "\u00d7"
var squareRoot = "\u221a"
var plusminus = "\u00b1"
var curVal = 0
var memory = 0
var lastOp = ""
var timer = 0
var currentOperation = ""
function disabled(op) {
if (op == "." && calcwindow.displayText.toString().indexOf(".") != -1) {
return true
} else if (op == squareRoot && calcwindow.displayText.toString().search(/-/) != -1) {
return true
} else {
return false
}
}
function doOperation(op) {
console.log("Called with " + op)
if (disabled(op)) {
return
}
if (op.toString().length==1 && ((op >= "0" && op <= "9") || op==".") ) {
if (calcwindow.displayText.toString().length >= 14)
return; // No arbitrary length numbers
if (lastOp.toString().length == 1 &&
((lastOp >= "0" && lastOp <= "9") ||
lastOp == ".") ||
lastOp == leftArrow) {
if (calcwindow.displayText == "0")
calcwindow.displayText = op.toString()
else
calcwindow.displayText = calcwindow.displayText + op.toString()
} else {
calcwindow.displayText = op
}
lastOp = op
calcwindow.displayOperation = ""
return
}
lastOp = op
if (currentOperation == "+") {
calcwindow.displayText = Number(calcwindow.displayText.valueOf()) + Number(curVal.valueOf())
} else if (currentOperation == "-") {
calcwindow.displayText = Number(curVal) - Number(calcwindow.displayText.valueOf())
} else if (currentOperation == multiplication) {
calcwindow.displayText = Number(curVal) * Number(calcwindow.displayText.valueOf())
} else if (currentOperation == division) {
calcwindow.displayText = Number(Number(curVal) /
Number(calcwindow.displayText.valueOf())).toString()
} else if (currentOperation == "=") {
}
if (op == "+" || op == "-" || op == multiplication || op == division) {
calcwindow.displayOperation = op
currentOperation = op
curVal = calcwindow.displayText.valueOf()
return
}
currentOperation = ""
calcwindow.displayOperation = ""
curVal = 0
if (op == "1/x") {
calcwindow.displayText = (1 / calcwindow.displayText.valueOf()).toString()
} else if (op == "x^2") {
calcwindow.displayText = (calcwindow.displayText.valueOf() *
calcwindow.displayText.valueOf()).toString()
} else if (op == "Abs") {
calcwindow.displayText = (Math.abs(calcwindow.displayText.valueOf())).toString()
} else if (op == "Int") {
calcwindow.displayText = (Math.floor(calcwindow.displayText.valueOf())).toString()
} else if (op == plusminus) {
calcwindow.displayText = (calcwindow.displayText.valueOf() * -1).toString()
} else if (op == squareRoot) {
calcwindow.displayText = (Math.sqrt(calcwindow.displayText.valueOf())).toString()
} else if (op == "mc") {
memory = 0;
} else if (op == "m+") {
memory += calcwindow.displayText.valueOf()
} else if (op == "mr") {
calcwindow.displayText = memory.toString()
} else if (op == "m-") {
memory = calcwindow.displayText.valueOf()
} else if (op == leftArrow) {
calcwindow.displayText = calcwindow.displayText.toString().slice(0, -1)
if (calcwindow.displayText.length == 0) {
calcwindow.displayText = "0"
}
} else if (op == "Off") {
Qt.quit();
} else if (op == "C") {
calcwindow.displayText = "0"
} else if (op == "AC") {
curVal = 0
memory = 0
lastOp = ""
calcwindow.displayText ="0"
}
}