Welcome to PerlMonks! Writing an equation solver is a great way to learn (Perl) programming.

To get help with your code, please post it! See this node for information on how to help us help you.

How you approach the problem in general will depend on which level you want to begin.

The basic approach to this kind of problem is to parse the expression using a parser such as a Recursive descent parser to translate the string "197 = 5(4a + 10) + 4a" into a representation that the software can more easily understand and manipulate. The Perl module Parse::RecDescent can help you write such a parser; this might help get you started.

If you don't want to go to that low of a level, it looks like someone else has already written a parser, Math::Expr (haven't used this myself, YMMV). You could use this module to parse the equation, and then manipulate the resulting tree to solve your equation.

Of course, this wheel has already been invented too - a quick search brings up Math::Algebra::Symbols, among others, which can solve equations for you.

And going even further than that, there are full-featured math software packages available for free online, such as Sage online at https://cloud.sagemath.com/ , see in particular this tutorial: http://www.sagemath.org/doc/tutorial/tour_algebra.html . There's also Wolfram Alpha, see e.g. http://www.wolframalpha.com/examples/Algebra.html . If you want a software you can download and install, there's Maxima (I've found wxMaxima a useful interface).

See also a few other places this kind of question has been asked before: Parsing Math Strings, Perl and maths.


In reply to Re: solving multistep equations by Anonymous Monk
in thread solving multistep equations by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.