1.1.0 • Published 5 years ago

little-calculator v1.1.0

Weekly downloads
1
License
ISC
Repository
-
Last release
5 years ago

安装

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 x
  • cos:求cos x
  • tan:求tan x
  • %:除模取余
  • ^:求x的y次方

原理

利用栈将中缀表达式转化为后缀表达式博客说明
代码依据《数据结构教程(第4版)李春葆》3.1.4小节设计