Computer Science Canada Equation solver |
Author: | Raknarg [ Sat Jul 27, 2013 9:42 pm ] | ||
Post subject: | Equation solver | ||
So i made a function here that takes in an equation as a string and solves it. So far it works perfectly, it can take in the basic operations and brackets and performs integer calculations. It seems really big to me and I think I might be doing it a bad way, so any comments would be appreciated.
|
Author: | Zren [ Sat Jul 27, 2013 10:11 pm ] | ||
Post subject: | RE:Equation solver | ||
format(s) could be rewritten as:
Basically filter out all whitespace when you reconstruct the string. |
Author: | Raknarg [ Sat Jul 27, 2013 10:15 pm ] |
Post subject: | RE:Equation solver |
Oh right, I forgot about that, thanks. Ill throw that in. |
Author: | Dreadnought [ Sun Jul 28, 2013 12:33 am ] | ||
Post subject: | Re: Equation solver | ||
It appears to have trouble with order of operations.
|
Author: | Raknarg [ Sun Jul 28, 2013 12:47 am ] |
Post subject: | RE:Equation solver |
No it passes the 2*3, just on line 99 it should have a 1 instead of length (newS). Copy and paste issues. But it's still being weird so i'll look into that |
Author: | Raknarg [ Sun Jul 28, 2013 12:53 am ] |
Post subject: | Re: Equation solver |
Fixed, replace the if on line 126 with this: newEq := newEq (1 .. firstNum - 1) + intstr (newNum) + newEq (lastNum + 1 .. *) I'll just post the whole thing again |
Author: | Raknarg [ Sun Jul 28, 2013 1:12 am ] |
Post subject: | RE:Equation solver |
If anyone else has some input, thatd be great as well. Are there better ways to write this? Has anyone else done this? |
Author: | Dreadnought [ Sun Jul 28, 2013 10:32 am ] | ||
Post subject: | Re: Equation solver | ||
A few other things I noticed (note I'm using your new version).
As for the design, I personally would have maybe the parsing and evaluating separate. I like the recursive evaluation style you used for brackets, I probably would have used it everywhere, but that's my opinion. |
Author: | Raknarg [ Sun Jul 28, 2013 11:47 am ] |
Post subject: | RE:Equation solver |
The recursive evaluation was the only way I could figure out how to solve it. And what do you mean by putting it everywhere? |
Author: | Raknarg [ Sun Jul 28, 2013 12:01 pm ] | ||
Post subject: | Re: Equation solver | ||
I knew Id run into an issue with those... The second one it's because I always floor the value when I should be rounding towards zero, I just threw in the function and replaced floor with it:
Fixed the second problem by fixing how it searches for the first number in the dividing/multiplying pair. |
Author: | Dreadnought [ Sun Jul 28, 2013 1:17 pm ] |
Post subject: | Re: Equation solver |
Raknarg wrote: I just threw in the function and replaced floor with it:
Why not just use div? Raknarg wrote: The recursive evaluation was the only way I could figure out how to solve it. And what do you mean by putting it everywhere? I meant that if you could also use it to evaluate what is to the arguments of binary operations (+, -, *, /). Basically an expression like 1 + 2 * 3 - 4 / 5 can be rewritten as (((1) + ((2) * (3))) - ((4) / (5))). Clearly we want to evaluate inner braces first and then outer braces. We simply begin with the outer braces and recursively apply our equationSolver to inner braces. |
Author: | Raknarg [ Sun Jul 28, 2013 2:18 pm ] |
Post subject: | RE:Equation solver |
Lol I thought of that after I made my function, oh well Oh yeah, that makes sense, so that way order of operations happens naturally |
Author: | Dreadnought [ Sun Jul 28, 2013 3:10 pm ] |
Post subject: | Re: Equation solver |
Of course I don't mean that you should always give equation solver expressions with braces everywhere, but that you can parse expressions in this way and recursively evaluate them naturally. |
Author: | Raknarg [ Sun Jul 28, 2013 3:29 pm ] |
Post subject: | RE:Equation solver |
Yes thats true. Didn't think of that. |