-
Notifications
You must be signed in to change notification settings - Fork 10
/
calc.js
executable file
·83 lines (71 loc) · 1.75 KB
/
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
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
// TODO handle the overflow
// ES6 support?
// define math operators
const OPERATOR = {
'+': function add (o1, o2) {
return o1 + o2
},
'-': function substract (o1, o2) {
return o1 - o2
},
'*': function multiply (o1, o2) {
return o1 * o2
},
':': function divide (o1, o2) {
return o1 / o2
},
'%': function modulo (o1, o2) {
return o1 % o2
},
'/': function fractal (o1, o2) {
return o1 / o2
}
}
function factorial (n) {
if (n === 0) return 1
else return n * factorial(n - 1)
}
// define math common constants, and functions
const MATH = Math
const EXTEND = {
'^': function pow (exp) {
var i = exp.indexOf('^')
if (i === -1) return null
var o1 = exp.substring(0, i)
var o2 = exp.substring(i + 1, exp.length)
return Math.pow(o1, o2)
},
'|': function abs (exp) {
var i = exp.indexOf('|')
if (i === -1) return null
return Math.abs(exp.substring(1, exp.length - 1))
},
'!': function fact (exp) {
if (exp.charAt(exp.length - 1) !== '!') return null
return factorial(parseInt(exp.substr(0, exp.length - 1), 10))
}
}
function isOperator (token) {
return OPERATOR.hasOwnProperty(token)
}
function isNumber (num) {
return !isNaN(num)
}
function isConst (token) {
return MATH.hasOwnProperty(token) && isNumber(MATH[token])
}
exports = module.exports = function calc (cmd) {
var argv = arguments
var length = argv.length
if (length === 1) return MATH[cmd]
if (argv.length === 3 && isOperator(cmd)) {
return OPERATOR[cmd](argv[1], argv[2])
}
return MATH[cmd].apply(null, Array.prototype.slice.call(argv, 1))
}
exports.OPERATOR = OPERATOR
exports.MATH = MATH
exports.EXTEND = EXTEND
exports.isOperator = isOperator
exports.isNumber = isNumber
exports.isConst = isConst