3
\$\begingroup\$

This question already has an answer here:

Challenge :

Given a string representing a mathematical expression return the result


Input :

A string representing a mathematical expression : '1 + 2 - 3 * (2/3 * 1)'.

Output :

The result after performing all operations.


Operators :

Possible operators

+     ---> plus
-     ---> minus
*     ---> multiplication
/     ---> division
(     ---> opening parenthesis 
)     ---> closing parenthesis

operator precedence :

Operators are always evaluated from left-to-right going from here ---> there, and * and / must be evaluated before + and -

Parenthesis take precedence over everything else and must be completed before all.

Parenthesis :

You should be able to support multiple(nested) levels of parenthesis.

e.g :

 1 - ( 2 * ( 3 / 9 + ( 2 * 6 ) ) )

White space :

Whitespace doesn't matter, you can have whitespace between operators or you can't (Consistency is required though)


Note :

  • Given input will always be valid. It will consist of nothing except

    • Numbers (0-9) of arbitary length
    • operators (+ , - , * , /)
    • parenthesis (( / )) nested
    • white space (optional with consistency)
  • Use of eval and Function or their equivalent counter-parts in any language is forbidden

  • using built-in functions designed specifically for this is also forbidden
  • all standard loopholes are forbidden by default.
  • You need to support floats to a reasonably high value (depending on the language) but after that integers will work. alternately after that floating point error (if reasonable) will be acceptable.
  • This is so shortest code in bytes for each language wins.

Examples :

 1 + 2 * 3              ---> 7
 10 * 2 / (2 - 3) * 2   ---> -10 (expression is (2*10) / ((2-3)*2) => 20/-2
 1-(2*(3/9+(2*6)-3))    ---> -53/3 (3/9 is the fraction and not the whole)
 -1 * --10              ---> -10 (support for unary minus)               

\$\endgroup\$

marked as duplicate by Keelan, Οurous, Rick Hitchcock, Sriotchilism O'Zaic code-golf May 20 '18 at 23:55

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • \$\begingroup\$ Is unary minus one of the operations ? \$\endgroup\$ – HatsuPointerKun May 20 '18 at 18:20
  • \$\begingroup\$ @HatsuPointerKun : Yep \$\endgroup\$ – Muhammad Salman May 20 '18 at 18:21
  • 1
    \$\begingroup\$ Should floating point numbers also be supported or only integer is okay ? \$\endgroup\$ – HatsuPointerKun May 20 '18 at 18:27
  • 1
    \$\begingroup\$ @StanStrum haha, nice one (but you would still need something like #&, which costs 2 bytes). But I think that's invalid because it relies on Mathematica's internal auto-evaluation. \$\endgroup\$ – JungHwan Min May 20 '18 at 21:48
  • 3
    \$\begingroup\$ Shouldn't your second example equal -40 instead of -10? \$\endgroup\$ – Rick Hitchcock May 20 '18 at 22:14

Browse other questions tagged or ask your own question.