Skip to content

Commit

Permalink
func(func...)
Browse files Browse the repository at this point in the history
  • Loading branch information
dominhhai committed Jun 20, 2015
1 parent 4f52e55 commit 7715e28
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 38 deletions.
40 changes: 26 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,10 @@ abs[-1] + 2^3 = 9
sin(90) + 5 = 5.893996663600558
(PI - 1) * 3 = 6.424777960769379
PI + 1 + 10%3 = 5.141592653589793
```
## prefix
```
+ 1 -5 = -4
+ -1 5 = 4
+ -1 -5 = -6
+ abs[-1] 2 = 3
+ abs[-1] 2^3 = 9
+ - -1 |-3| 2^5 = 28
+ - -10 abs(-3) 2^5 = 18
+ - -1 abs(-3) pow[2, 5] = 28
+ sin(90) 5 = 5.893996663600558
* - PI 1 3 = 6.424777960769379
+ + PI 1 10%3 = 5.141592653589793
pow(5,|1 - 4|) = 125
max(10, 20 + 5, 30 - 40, min ( 40, 100)) = 40
max(10, 20 + 5, min ( 40, 100) * 4,30 - 40) = 160
```

## postfix
Expand All @@ -53,6 +42,29 @@ abs[-1] 2^3 + = 9
sin(90) 5 + = 5.893996663600558
PI 1 - 3 * = 6.424777960769379
PI 1 + 10%3 + = 5.141592653589793
. 5 . 1 3 - abs. pow. = 25
. 10 20 5 + 30 40 - . 40 100 min. max. = 40
. 10 20 5 + . 40 100 min. 4 * 30 40 - max. = 160
```

## prefix
```
+ 1 -5 = -4
+ -1 5 = 4
+ -1 -5 = -6
+ abs[-1] 2 = 3
+ abs[-1] 2^3 = 9
+ - -1 |-3| 2^5 = 28
+ - -10 abs(-3) 2^5 = 18
+ - -1 abs(-3) pow[2, 5] = 28
+ sin(90) 5 = 5.893996663600558
* - PI 1 3 = 6.424777960769379
+ + PI 1 10%3 = 5.141592653589793
.pow 5 .abs - 1 4 . . = 125
.max 10 + 20 5 - 30 40 .min 40 100 . . = 40
.max 10 + 20 5 * .min 40 100 . 4 - 30 40 . = 160
```

# Installation
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
},
"scripts": {
"start": "node cli.js",
"stop": "tape test/2-postfixer.js | faucet",
"test": "standard && tape test/*.js | faucet"
},
"repository": {
Expand Down
22 changes: 13 additions & 9 deletions postfixer.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ exports = module.exports = function toPostfixNotation (exp) {
// 3. after an operator
var nega = true
// skip handle when meet Math's functions
var skip = null
var skip = []

var token = ''
for (var i = 0, j = exp.length; i < j; i++) {
Expand All @@ -58,8 +58,8 @@ exports = module.exports = function toPostfixNotation (exp) {
if (cur === SPACE) continue

// handle Math's functions
if (skip) {
if (cur === MATH_EXTEND[skip]) {
if (skip.length > 0) {
if (cur === MATH_EXTEND[skip[skip.length - 1]]) {
removeFunction()
continue
} else if (cur === ',') {
Expand Down Expand Up @@ -137,22 +137,27 @@ exports = module.exports = function toPostfixNotation (exp) {
if (!token) token = 'abs'
stack.push(token + MATH_EP)
token = ''
skip = cur
skip.push(cur)
}

function removeFunction () {
out.push(token)
if (token) {
out.push(token)
token = ''
}
while (stack.length > 0) {
var o = stack.pop()
out.push(o)
if (o.substr(o.length - MATH_EP.length) === MATH_EP) break
}
token = ''
skip = null
skip.pop()
}

function removeComma () {
out.push(token)
if (token) {
out.push(token)
token = ''
}
while (stack.length > 0) {
var o = stack.pop()
if (o.substr(o.length - MATH_EP.length) === MATH_EP) {
Expand All @@ -162,7 +167,6 @@ exports = module.exports = function toPostfixNotation (exp) {
out.push(o)
}
}
token = ''
}

function enableNega () {
Expand Down
19 changes: 5 additions & 14 deletions test/2-postfixer.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,13 @@ test('infix to postfix test', function (t) {

test('func: infix to postfix test', function (t) {
t.equal(postfixer('abs(1 - 2)'), '. 1 2 - abs.')
t.equal(postfixer('|1 - 2|'), '. 1 2 - abs.')
t.equal(postfixer('pow(5,|1 - 2|)'), '. 5 . 1 2 - abs. pow.')
t.equal(postfixer('min(10, 20, 30)'), '. 10 20 30 min.')
t.equal(postfixer('max(10, 20 + 5, 30 - 40)'), '. 10 20 5 + 30 40 - max.')
// t.equal(postfixer('1 + 2 * 3'), '1 2 3 * +')
// t.equal(postfixer('(1 + 2) * 3'), '1 2 + 3 *')
// t.equal(postfixer('(abs[1] + 2) * 3 + E'), 'abs[1] 2 + 3 * E +')
// t.equal(postfixer('(PI + 2) * 3 + E'), 'PI 2 + 3 * E +')
// t.equal(postfixer('(PI + 2/ 10) * 3 + (E-19 )'), 'PI 2 10 / + 3 * E 19 - +')
// t.equal(postfixer('(1 + 2 - 3*(PI + 10))'), '1 2 + 3 PI 10 + * -')
//
// t.equal(postfixer('1 + (-2)'), '1 -2 +')
// t.equal(postfixer('-1 + 2 + -3'), '-1 2 + -3 +')
// t.equal(postfixer('1 + - 2'), '1 -2 +')
// t.equal(postfixer('-1 + - 2'), '-1 -2 +')
// t.equal(postfixer('-1 - -3'), '-1 -3 -')
// t.equal(postfixer('|-1| - -3'), '|-1| -3 -')
// t.equal(postfixer('abs[-1] - -3'), 'abs[-1] -3 -')

t.equal(postfixer('max(10, 20 + 5, 30 - 40, min ( 40, 100))'), '. 10 20 5 + 30 40 - . 40 100 min. max.')
t.equal(postfixer('max(10, 20 + 5, min ( 40, 100) * 4,30 - 40)'), '. 10 20 5 + . 40 100 min. 4 * 30 40 - max.')

t.end()
})
22 changes: 22 additions & 0 deletions test/4-func.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ test('func - postfix test', function (t) {

t.equal(app('. 2 . 2 3 pow. - max.'), -6)

t.equal(app('. 1 2 - abs.'), 1)
t.equal(app('. 1 2 - abs.'), 1)
t.equal(app('. 5 . 1 3 - abs. pow.'), 25)
t.equal(app('. 10 20 30 min.'), 10)
t.equal(app('. 10 20 5 + 30 40 - max.'), 25)

t.equal(app('. 10 20 5 + 30 40 - . 40 100 min. max.'), 40)
t.equal(app('. 10 20 5 + . 40 100 min. 4 * 30 40 - max.'), 160)

t.end()
})

Expand All @@ -30,3 +39,16 @@ test('func - prefix test', function (t) {

t.end()
})

test('func - infix test', function (t) {
t.equal(app('abs(1 - 2)'), 1)
t.equal(app('|1 - 2|'), 1)
t.equal(app('pow(5,|1 - 4|)'), 125)
t.equal(app('min(10, 20, 30)'), 10)
t.equal(app('max(10, 20 + 5, 30 - 40)'), 25)

t.equal(app('max(10, 20 + 5, 30 - 40, min ( 40, 100))'), 40)
t.equal(app('max(10, 20 + 5, min ( 40, 100) * 4,30 - 40)'), 160)

t.end()
})

0 comments on commit 7715e28

Please sign in to comment.