billyct/happycalculator

语言: JavaScript

git: https://github.com/billyct/happycalculator

结果6的自然公式计算器如“1 + 2 + 3”,支持计算器的简单代码,即测量支持变量...
calculator for natural formula like "1+2+3" for result 6 ,support simple code for calculator,that meas support variab…
README.md (中文)

happycalculator

icon

自然公式的计算器,如结果6的“1 + 2 + 3”,支持自定义函数,如sqrt(20),无论你想要什么,支持公式循环,如sqrt(sqrt(20 + 20))

中文

安装

$ npm install --save happycalculator

或凉亭

$ bower install --save happycalculator,它将把happycalculator全局应用于你

用法

基础

var happycalculator = require('happycalculator');
var formula = '20 * ( 10 + 20 ) / 20';
happycalculator.convert(formula);
//output: ['20', '*', '(', '10', '+', '20', ')', '/', '20']
happycalculator.shunt(formula);
//output: [ '20', '10', '20', '+', '*', '20', '/' ]
happycalculator.calculate(formula);
//output: 30

提前

//sqrt__custom is default seted to '$1*$2'

happycalculator.calculate('sqrt__custom(10)*20/20');
//output 100

happycalculator.addFormulas({'sqrt__custom' : '$1 + $1'}); //
happycalculator.calculate('sqrt__custom(10)*20/20');
//output 20
happycalculator.calculate(sqrt__custom('sqrt__custom(10))*20/20');
//output 40
happycalculator.removeFormulas(); //remove formulas added,and reset the formulas default
happycalculator.calculate('sqrt__custom(10)*20/20');
//output 100

happycalculator.addFormulas({'custom' : '$1 + $2 +$3'});
happycalculator.calculate('custom(10, 20, 30)')
//output 60

//support formulas loop
happycalculator.calculate('sqrt__custom(sqrt__custom(10))');
//output 10000
happycalculator.calculate('sqrt__custom(sqrt__custom(2+2))');
//output 256


//support sin,cos,tan,sqrt which was Math functions
happycalculator.calculate('sin(30)');
//output 0.5

支持简单的代码

var code = `sum = $1 + $2;
a = 2;
b = 3;
sum(a,b)+a;
a = 3;
a+b;`
calculator.parse(code);
//output ['(2+3)+2', '3+3']
calculator.calculateCode(code);
//output [7, 6]

这就是现在所有简单的代码支持

API

转换(string_infix)

返回一个用“+ - * /”分割的数组,如果带有值的字符串(例如:a,ab,cos(20))也可以

分流(string_infix)

返回一个postinfx数组,转换了Shunting-yard算法,核心代码是从rosettacode编写的,我为项目更改了一些代码

计算(string_infix)

返回num计算

addFormulas(公式)

将自定义公式添加到计算器中,公式规则看起来像{'key'=>'$ 1 + $ 2 + $ 3'},键(1,2,3),默认公式函数有sqrt:'$ 1 * $ 1'现在,它的工作

removeFormulas()

删除您添加的所有自定义公式,默认为默认项目支持的公式

解析(string_code)

返回解析的string_code数组

calculateCode(string_code)

返回由解析的string_code计算的结果数组

执照

用©billyct

本文使用googletrans自动翻译,仅供参考, 原文来自github.com

en_README.md

happycalculator Build Status

icon

calculator for natural formula like "1+2+3" for result 6,support custom function like sqrt(20) also whatever you want,support formulas loop, like sqrt(sqrt(20+20))

中文

Install

$ npm install --save happycalculator

or bower

$ bower install --save happycalculator and it will apply happycalculator global to you

Usage

base

var happycalculator = require('happycalculator');
var formula = '20 * ( 10 + 20 ) / 20';
happycalculator.convert(formula);
//output: ['20', '*', '(', '10', '+', '20', ')', '/', '20']
happycalculator.shunt(formula);
//output: [ '20', '10', '20', '+', '*', '20', '/' ]
happycalculator.calculate(formula);
//output: 30

advance

//sqrt__custom is default seted to '$1*$2'

happycalculator.calculate('sqrt__custom(10)*20/20');
//output 100

happycalculator.addFormulas({'sqrt__custom' : '$1 + $1'}); //
happycalculator.calculate('sqrt__custom(10)*20/20');
//output 20
happycalculator.calculate(sqrt__custom('sqrt__custom(10))*20/20');
//output 40
happycalculator.removeFormulas(); //remove formulas added,and reset the formulas default
happycalculator.calculate('sqrt__custom(10)*20/20');
//output 100

happycalculator.addFormulas({'custom' : '$1 + $2 +$3'});
happycalculator.calculate('custom(10, 20, 30)')
//output 60

//support formulas loop
happycalculator.calculate('sqrt__custom(sqrt__custom(10))');
//output 10000
happycalculator.calculate('sqrt__custom(sqrt__custom(2+2))');
//output 256


//support sin,cos,tan,sqrt which was Math functions
happycalculator.calculate('sin(30)');
//output 0.5

support simple code

var code = `sum = $1 + $2;
a = 2;
b = 3;
sum(a,b)+a;
a = 3;
a+b;`
calculator.parse(code);
//output ['(2+3)+2', '3+3']
calculator.calculateCode(code);
//output [7, 6]

and that's all simple code support right now

API

convert(string_infix)

return an array that split with "+-*/",and if the string with values(example:a,ab,cos(20)) also that will be ok

shunt(string_infix)

return an array postinfx that with the Shunting-yard Algorithm converted,the core code was written from rosettacode and i change some code for the project

calculate(string_infix)

return the num calculated

addFormulas(formulas)

add custom formulas to the calculator,and the formula rule is looks like that{'key' => '$1+$2+$3'},key(1,2,3),default formula function has sqrt:'$1*$1' right now,its working on

removeFormulas()

remove all custom formulas you add in and defaults to the default project supported formulas

parse(string_code)

return array of string_code parsed

calculateCode(string_code)

return array of the result calculated by the string_code parsed

License

MIT © billyct