in reply to How would you solve a user-inputted simple one-variable Linear Equation

The first and perhaps hardest thing to do is to parse the input. You need to write some form of lexer to tokenize the input. This can be done with regexes eating up progressively the input (with the /g modifier).

But rather than trying to explain myself probably in a poor fashion, I would suggest that you take a look at chapter 8 ("Parsing") of Mark Jason Dominus' excellent book, Higher Order Perl (http://hop.perl.plover.com/). This chapter is available on-line here: http://hop.perl.plover.com/chap08.html.

You probably don't need to go in all the complexity of that chapter, but you should find a lot of ideas which you can then implement in your own simple way for what you need.

Je suis Charlie.
  • Comment on Re: How would you solve a user-inputted simple one-variable Linear Equation
  • Download Code

Replies are listed 'Best First'.
Re^2: How would you solve a user-inputted simple one-variable Linear Equation
by AnomalousMonk (Archbishop) on Apr 25, 2015 at 22:24 UTC

    The entire HOP is a free download.


    Give a man a fish:  <%-(-(-(-<

      Yes, sure ++. And the first link I posted is the start page for the entire HOP download. But I pointed especially to chapter 8, Parsing, because of the general subject, parsing, because of the lexer examples and because of the calculator example, which goes a long way toward solving the OP problem. And also because I think this chapter can, to a large extent, be read without having read the previous chapters.

      Having said that, I would really recommend reading the entire book, one of the best books about CS I've read in the last decade, it has definitely changed my way of programming, even in other languages than Perl. And BTW, I first read it on-line, but I quickly decided to buy a dead-tree version, it is really worth the money.

      Je suis Charlie.

        I just wanted to let people know it wasn't the one-chapter teaser that you often get from publishers, e.g., O'Reilly.

        I agree that HOP is that rarest of creatures, a CS book that's a good read. BTW, congratulations if you got through the whole thing on-line; I have the electronic version for quick reference, but I'm of the generation that needs deadwood for comfortable, concentrated study. (Update: And I've still got my slide-rule! And you kids get off my lawn!)


        Give a man a fish:  <%-(-(-(-<