Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Based on your example, I'd say that you want to "evaluate an expression", rather than "solve an equation". For evaluating an expression, you can probably find <a href="http://www.google.com/search?q=making%20a%20math%20expression%20evaluator&amp;rlz=1C5CHFA_enUS503US504&amp;aq=f&amp;oq=making%20a%20math%20expression%20evaluator&amp;aqs=chrome.0.57.5421j0&amp;sourceid=chrome&amp;ie=UTF-8#safe=active&amp;rlz=1C5CHFA_enUS503US504&amp;sclient=psy-ab&amp;q=how%20to%20evaluate%20a%20math%20expression&amp;oq=how%20to%20evaluate%20a%20math%20expression&amp;gs_l=serp.3..0j0i22i30l3.6651.11771.0.11961.35.33.1.0.0.1.252.3354.21j10j2.33.0.cappswebkhl..0.0...1.1.12.psy-ab.rlplls4vvfA&amp;pbx=1&amp;bav=on.2,or.r_cp.r_qf.&amp;bvm=bv.46226182,d.cGE&amp;fp=a979fbb9e0a03a50&amp;biw=1749&amp;bih=1025" rel="nofollow">many tutorials</a>. I'll break it down in brief though. You need to do a few steps.</p> <p>Starting with your string "1 + x * x", you need to break it into tokens. Specifically, break it down into: <code>"1", "+", "x", "*", "x"</code>. At this point, you can substitute your variables ("x") for their literal values ("2"), giving you <code>"1", "+", "2", "*", "2"</code></p> <p>Now you need to parse the expression. Based on <a href="http://en.wikipedia.org/wiki/Order_of_operations" rel="nofollow">order of operations PEMDAS</a> you need to create a tree data structure, where parenthetical clauses (stuff surrounded by parenthesis) are executed first, multiplication and division next, and then additions and subtraction last. Parsing is often not an easy task, and you may want to put together a simpler BNF grammar (though you can probably find a grammar for simple math expressions with some googling).</p> <p>Next, walk the tree, depth first, evaluating the operations as you go up the tree. Once you get to the top of the tree, you have your solution.</p> <p>If instead you want to "solve an equation", you're going to need something much more sophisticated, like <a href="http://en.wikipedia.org/wiki/Sage_%28mathematics_software%29" rel="nofollow">Sage</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload