+ 1

JS преобразование мат операций

Пишу простой транслятор ,как преобразовать символы мат операций из String. Пример "+" а должно получится просто +. Спасибо большое.

11th Apr 2021, 3:19 AM
Даниил Елин
Даниил Елин - avatar
3 Antworten
+ 2
Даниил Елин wrote, "I want don't uses this construction ,you can see bellow a part of code. I have "+" and it should be +(without "if* and etc.). Thanks. switch(output[i]){ case "+":st.push(num2 + num1); break; case "-":st.push(num2 - num1); break; case "/":st.push(num2 / num1); break; case "*":st.push(num2 * num1); break; }" Response: Are you trying to make the evaluator from scratch? If you don't like JavaScript's standard eval function because of its security issues, you could still use math.js's evaluate function here: https://mathjs.org/docs/expressions/parsing.html If you may be interested in math.js's evaluate method, also check my math expression evaluator here which depends on math.js: https://code.sololearn.com/Wa14229a21a1 If you want to make your own from scratch and support multiple operators like +, -, /, *, also consider order of operation rules like b.e.d.m.a.s. Making your own expression evaluator can quickly get almost as tricky as writing your own programming language if you want to support a lot of operators, brackets...
11th Apr 2021, 2:42 PM
Josh Greig
Josh Greig - avatar
+ 1
What is your question? I get the English as: "I am writing a simple translator how to convert symbols of mat operations from String. Example "+" and it should be just +. Thank you so much." I get that you're doing something but it doesn't say you're stuck on anything or want help. Did you run into a problem? Did you get stuck on something? If you just want to evaluate expressions like "3 + 4", you could use JavaScript's eval function. eval("3 + 4") returns 7. eval("3 * 4") returns 12 so eval isn't restricted to + but I'm not sure if that's a problem to you.
11th Apr 2021, 6:13 AM
Josh Greig
Josh Greig - avatar
0
I want don't uses this construction ,you can see bellow a part of code. I have "+" and it should be +(without "if* and etc.). Thanks. switch(output[i]){ case "+":st.push(num2 + num1); break; case "-":st.push(num2 - num1); break; case "/":st.push(num2 / num1); break; case "*":st.push(num2 * num1); break; }
11th Apr 2021, 6:34 AM
Даниил Елин
Даниил Елин - avatar