1.1.0 • Published 6 years ago
little-calculator v1.1.0
安装
npm install little-calculator使用
const calculator = new Calculator()
const exp = '( 3 + 13) / 2 + 5 - 3 * 2'
calculator.compute(exp) // 返回7
const exp1 = '3 + -2 * 2 + 2^2 + abs(-2)'
calculator.compute(exp1) // 返回5支持运算符
- 简单四则运算:
+ - * /,数字支持前导“+”,“-” abs:开平方sqrt:开平方exp:e的x次ln:e为底数的对数log10:10为底数的对数sin:求sin xcos:求cos xtan:求tan x%:除模取余^:求x的y次方
原理
利用栈将中缀表达式转化为后缀表达式,博客说明
代码依据《数据结构教程(第4版)李春葆》3.1.4小节设计