|
Post by honkytonk on Oct 5, 2018 11:42:06 GMT
Hello everyone, I would like to make a program, which, from an example expression: x = a + (b * c) -d / e (or other) Give the equivalent expressions of: a; b; c; d; e (in function of others vars) For that, I intend to give arbitrary values to the variables a; b; c ... ext, to calculate x. Then, do all combinations of variables, operator and parentheses; Eliminate non-coherent expressions (ex: a =) x (+) ...) Solve, compare, and select the right expressions. I take all the ideas and advice. Many thank for aid.
|
|
|
Post by B+ on Oct 5, 2018 12:44:37 GMT
Go for it! Man what a great challenge and learning experience!
I have left my droppings at other Basic forums including this one's previous incarnation.
If you want to lookup examples, it often goes under the title EVAL or EVALUATE.
I made extensive use of the WORD$() function for parsing numbers, functions, operators and the dreaded ()'s.
I started with handling + - * / ^ and % operators in the order they should be done in an expression without ()'s.
|
|
|
Post by honkytonk on Oct 5, 2018 17:39:54 GMT
Thank you for the encouragements; I have two programs taken here a long time ago and I sweat on it.
|
|
|
Post by tsh73 on Oct 6, 2018 15:23:10 GMT
Just want to say. While wheel is invented (all forms, several times over) it is interesting task anyway. So go ahead ant try. Now there is always problem with saying too much and not saying a word. Do you have an idea how to do it? It yes go and try (and show here ) If not - you can look into "recursive descent" and "shunting yard".
|
|
|
Post by B+ on Oct 6, 2018 15:48:09 GMT
Easy start:
evaluate: "12 + 13 + 51 + 102 + 99 + 501"
Then do evaluate: "3001 - 206 - 307 - 255"
Then do this evaluate: "500 + 300 - 619 - 217 + 99 - 50"
Then do: "203 * 13 + 99 / 5 - 22 * 100 - 2001 + 1000"
(numbers are chosen at random and don't have to be integers)
Just keep getting more versatile handling the binary operators (+ - * / % for modulus ^ for power)
Recursive decent means do all multiplication and division before descending to addition and subtraction (just like BASIC does) do all stuff inside inner most parenthesis before next inner most parenthesis... save ()'s for last because that's when things need to get recursive.
|
|
|
Post by B+ on Oct 6, 2018 16:57:02 GMT
I have a "starter" Eval function worked out already in code, handling the above expressions PLUS plugging in substitutions for variables.
But as tsh73 says "There is always a problem of saying too much and not saying a word."
honkytonk, perhaps if you show us the code you've found and / or highlight parts you are "sweating"...
|
|
|
Post by honkytonk on Oct 10, 2018 11:14:21 GMT
Just want to say. While wheel is invented (all forms, several times over) it is interesting task anyway. So go ahead ant try. Now there is always problem with saying too much and not saying a word. Do you have an idea how to do it? It yes go and try (and show here ) If not - you can look into "recursive descent" and "shunting yard". I searched for this wheel there without finding it. (lines 1 and 2 of my first message). Explanation of the process in the following lines. I was hoping for a tip/code of analysis / resolution of expressions. -: A textbox to enter the expression.(ex: x=a+(b*c)+d/e) -: Textboxes to enter variables values (ex: a=5; b=3; c=...ext) -: A textbox for the result (x=...) For starting.
|
|
|
Post by B+ on Oct 10, 2018 12:51:09 GMT
For starting, why not test code with INPUTs and PRINTs that way you can concentrate on the main event of writing code for evaluating expressions.
Or maybe you are looking for lessons on setting up windows with text boxes?
|
|
|
Post by honkytonk on Oct 10, 2018 14:44:46 GMT
For starting, why not test code with INPUTs and PRINTs that way you can concentrate on the main event of writing code for evaluating expressions. Or maybe you are looking for lessons on setting up windows with text boxes? What I'm looking for is someone who likes to code and who would do this part for me. It's a little daring, but it deserves to be clear.
|
|
|
Post by honkytonk on Oct 10, 2018 14:57:15 GMT
After: In "Eval function" (old code); i see:
fs$ = "sub( add( mul(pow(x(), 2) , 2), mul(x(),2)...ext...ext And more fast:
select case opstr$ case "add" : evalB = a + b case "sub" : evalB = a - b case "mul" : evalB = a * b case "div" Question: Why not ?:
select case opstr$ case "+" : evalB = a + b case "-" : evalB = a - b case "*" : evalB = a * b case "/"
|
|
|
Post by B+ on Oct 10, 2018 16:54:17 GMT
After: In "Eval function" (old code); i see: fs$ = "sub( add( mul(pow(x(), 2) , 2), mul(x(),2)...ext...ext And more fast: select case opstr$ case "add" : evalB = a + b case "sub" : evalB = a - b case "mul" : evalB = a * b case "div" Question: Why not ?: select case opstr$ case "+" : evalB = a + b case "-" : evalB = a - b case "*" : evalB = a * b case "/" Why not, indeed!!! It is much easier to understand with the binary operators form than all the nested functions. Sorry, I keep getting chewed out for potentially doing others home work. Doing it with binary operators (+-*/) is better because it is clearer to the user, but you have to isolate the "a" part AND the "b" part in code and that gets complicated if more () are involved. Doing it with functions like add(a, b) all the stuff is in the () container, easier to parse (usually) so it is more a beginner / starter method. Unless you leave () stuff for last, as I had tried to advise in above replies.
|
|
|
Post by tsh73 on Oct 11, 2018 10:33:45 GMT
|
|
|
Post by honkytonk on Oct 12, 2018 11:38:10 GMT
Thank you B + and tsh73 for the water brought to my mill. I will work on it.
|
|