Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Mathematical expression translator
#36
(04-29-2008, 10:57 AM)LPG link Wrote:I tried this from QB > C. i made it do this:
  • put spaces between expressions and operators
  • find the ^
  • search left until it finds a space. if it sees a ) forget spaces until it comes to a (
  • put "pow(" in
  • do the same going right but add ")"
  • put spaces in again

the only error is that it searches from left to right so for:
    (5^5)^5
it returns:
    (pow( 5, 5) )pow( ,5)
but for something like this:
    5^5
it returns:
    pow(5,5)

it should look from the inside brackets to the outside brackets but i don't know how to do that.

    LPG   
     

The way you described it won't work with the second example either.

You did do the right thing by going left to right, I think.

A few pointers of my own:
* Instead of adding spaces you could instead remove them. It is less mess when you try to parse it later.
* In the same manner, it's less trouble to parse later when you add ()s between the start and end of numbers that have ^ powers. So 5^2 would become (5)^2.

GENERAL pointers:
* You can either do an iterative parser or a recursive parser: a recursive parser would try to find whole power sets and recursive deeper for each new one, while an iterative parser would have to keep track of the "tree"...

Find the end of the expression (in this case ")^"), then find its end. In either recursive or iterative case, you keep searching for either the start of the expression (a "(") or the "end" of a new one --")^". In either case you need to counting non-power ending parentheses and ignore a corresponding amount of starting parentheses.
It gets just a little bit complicated but basically you want to be able to parse something like "(5*(5+2))^5"...
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
Reply


Messages In This Thread
Re: Mathematical expression translator - by Ralph - 04-23-2008, 05:40 AM
Re: Mathematical expression translator - by Ralph - 04-23-2008, 11:09 PM
Re: Mathematical expression translator - by Ralph - 04-24-2008, 02:56 AM
Re: Mathematical expression translator - by Ralph - 04-24-2008, 07:51 AM
Re: Mathematical expression translator - by Ralph - 04-25-2008, 07:40 PM
Re: Mathematical expression translator - by Ralph - 04-26-2008, 12:40 AM
Re: Mathematical expression translator - by Ralph - 04-26-2008, 04:33 AM
Re: Mathematical expression translator - by Ralph - 04-29-2008, 08:30 AM
Re: Mathematical expression translator - by LPG - 04-29-2008, 10:57 AM
Re: Mathematical expression translator - by LPG - 04-30-2008, 09:06 AM
Re: Mathematical expression translator - by Agamemnus - 08-06-2008, 01:17 AM

Forum Jump:


Users browsing this thread: 2 Guest(s)