-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalc.js
52 lines (49 loc) · 1014 Bytes
/
calc.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
var valstack = [];
var x='';
function number(value) {
document.cal.result.value += value;
x+=value;
if(value=='+') {
valstack.push(x);
x = '';
}
else if (value == '-') {
valstack.push(x);
x = '';
}
else if (value == '*') {
valstack.push(x);
x = '';
}
else if (value == '/') {
valstack.push(x);
x = '';
}
else if(value == '!') {
x = factorial(x);
valstack.push(x);
x = '';
}
}
function cle(value) {
document.cal.result.value = value;
x = '';
}
function evalua(value) {
var exp='';
valstack.push(x);
for(var i=0;i<valstack.length;i++) {
exp+=valstack[i];
}
document.cal.result.value = eval(exp);
while (valstack.length!=0) {
valstack.pop();
}
}
function factorial(value) {
var fact=1;
for(var i=1;i<=parseInt(value);i++) {
fact*=i;
}
return fact;
}