calculate arithmetic expression
2022年5月1日小于 1 分钟
calculate arithmetic expression
Question
In 119. create a tokenizer, you are able to extract the tokens from a string with invalid spaces.
Now please calculate()
the result of the string. You can use the tokenizer you wrote before.
calculate('1 * (20 - 300 ) ')
// -280
calculate(' 1/0 ')
// Infinity
- the input expression is syntactically valid, containing non-negative integers,
+
,-
,*
,/
,(
,)
and spaces - Don't use
eval()
Code
/**
* @param {string} str
* @returns {Number}
*/
function calculate(str) {
// your code here
}