四则运算式,先乘除后加减
数 0~9,+-*/,没有括号
1. 算法思路
低优先级操作符遇到其右侧高优先级操作符,右侧优先运算
相同优先级的操作符,左侧优先运算
高优先级操作符遇到其右低优先级操作符,左侧优先运算
2. 数据结构采用栈结构
3. 单元测试
let x = '3+2*3+6/3+6/2+9/3+5*3-8'
// 操作符优先级 0 ~ 1
let OPERATOR = [['+', 0], ['-', 0], ['*', 1], ['/', 1]]
function run (x) {
let nArr = []
let oArr = []
let iRet = ''
let i = 0 // 头指针
let sNToken = x.charCodeAt(i) - 48
let sPToken = ''
let nPriority = 0
if (sNToken < 0 || sNToken > 9) {
iRet = `Error at position ${i} : is not a number(${x.charAt(i)})!`
} else {
nArr.push(sNToken)
i = 1
for (; i < x.length - 1 ;) {
// 获取操作符
sPToken = x.charAt(i);
// 验证操作符,获取优先级
nPriority = validateOperator(sPTok