Hello.

Can anyone have a ready-made method for parsing a calculator line and evaluating an expression?
The input of the function is a string with an expression, and the output is the result
You need only 4 basic actions (+, -, *, /), without brackets and other things.

Thanks a lot in advance!

    2 answers 2

    Expression compiler

    expression: "x + 10 == 5 * y / (1 + z * 2)";

    you must be able to evaluate this expression for any x, y, and z values.

    And of course, the priorities of the operators must be taken into account.

    For the solution, you need to make a compiler, which builds an “Computable Expression” object on the line. This object will have a “calculate for these variable values” method.

    • I don’t need so much code, I’m wondering if there’s something unpretentious for 4 actions :) But anyway - thanks! - andrewshka

    Formula parser using recursive descent method .

    Here, when I wrote an article on Habré.

    I want to share with you the implementation of the algorithm "Method of recursive descent" on the example of writing a parser of formulas with support for variables and functions in the Java language

    • I don’t need so much code, I’m wondering if there’s something unpretentious for 4 actions :) But anyway - thanks! - andrewshka
    • 3
      ))))) can still people and code for you cut? throw out all unnecessary from there, if a really simple bike is needed, then just if else will help - a bit perverted, but quite real - gadfil
    • Yes, I do not ask for anything, I ask the finished one :) - andrewshka
    • 3
      @NeoTvidoz you have already given already 2 times - have a conscience! - Barmaley
    • 1 way: arrange a passage on the line, putting numbers into the array. the number starts with a space, +, or -. if after the number * or / you put the result in the array, before you check, check that after the number another number or end of the line, if * or / repeat again, add all the elements of the array 2 method: rewrite the line to another, but instead of *, / record the result and so on until the line does not contain the characters * and /. Then just add all the numbers, a - b == a + (-b), all these are wild bicycles that will yield accuracy and speed to normal algorithms - dare - gadfil