From 7715e28cc33fdb2fdd0514e1d06e917faf2ac50a Mon Sep 17 00:00:00 2001 From: Hai Do Minh Date: Sat, 20 Jun 2015 14:41:24 +0900 Subject: [PATCH] func(func...) --- README.md | 40 ++++++++++++++++++++++++++-------------- package.json | 1 - postfixer.js | 22 +++++++++++++--------- test/2-postfixer.js | 19 +++++-------------- test/4-func.js | 22 ++++++++++++++++++++++ 5 files changed, 66 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index d70c5be..7b31eda 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/package.json b/package.json index bcbc5df..321e1d2 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,6 @@ }, "scripts": { "start": "node cli.js", - "stop": "tape test/2-postfixer.js | faucet", "test": "standard && tape test/*.js | faucet" }, "repository": { diff --git a/postfixer.js b/postfixer.js index 19cae95..9bc452d 100644 --- a/postfixer.js +++ b/postfixer.js @@ -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++) { @@ -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 === ',') { @@ -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) { @@ -162,7 +167,6 @@ exports = module.exports = function toPostfixNotation (exp) { out.push(o) } } - token = '' } function enableNega () { diff --git a/test/2-postfixer.js b/test/2-postfixer.js index 47e7b4b..eefaee9 100644 --- a/test/2-postfixer.js +++ b/test/2-postfixer.js @@ -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() }) diff --git a/test/4-func.js b/test/4-func.js index 3ad24a7..ed82dba 100644 --- a/test/4-func.js +++ b/test/4-func.js @@ -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() }) @@ -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() +})