Skip to main content
gamedev.net
🔒 Locked

Strings for equation solution

Started by TheShadow Mar 19, 2003 at 4:36 PM 9 replies 500+ views
Original Post
TheShadow
TheShadow
I want to make a simple math function evaluator... i want to input a string like x+3 and then tell it the value of x and it evaluates the function... but i want it to be able to evaluate stuff like ((x*x)^3)/2 and i can''t come up with a procedure to achieve this. For all i know is that i got to parse the text and know when a parenthesis starts and all of that... But how do i make the computer follow the function and parenthesis. Please help me :D
Mecha Engineer (Making Real Humanoid Suits)
TheShadow
TheShadow
Im sorry it wont run in vc++ 6.0 and uhm one more thing... i want to know if you can help me with a small pseudo code or explanation so I can code it... i don''t want to copy anybody''s work or anything like that.
Mecha Engineer (Making Real Humanoid Suits)
Dwiel
Dwiel
is your problem that you are having trouble converting the string to your data structure or is it that you don''t know what data structure you should use?

Dwiel
Burning_Ice
Burning_Ice
well i only have VC++ 5, but usually it works with VC++6. what errors are you getting?

and i guess it's too complicated for a small piece of pseudocode..i tried my best to comment the code but i know it doesn't make things better


basically what i'm doing:
1) create a list of tokens out of the string; a token can either be a number, a binary op (+,*,...), a unary op (sin, cos,...), a variable (a,b,c,...) or a bracket.
while i'm doing this i already check for syntactic errors (unrecognized characters, combinations of letters that are neither variable nor unary op) and make sure the number of opening brackets is the same as the number of closing ones, and i remove whitespaces and convert , to . and all kinds of brackets to round brackets.

2) from right to left walk trough this list of tokens and build the syntax tree, which is a binary tree that will hold tokens.
best look at the code how i did it since its a bit complicated to explain. important is that every operator has a precedence (* is higher then + etc. ) and to adjust the depth-level whenever a bracket is encountered. the root of the tree is the op with lowest precedence and lowest depth (eg. in "1+2*(3+4)" the first + will be the root node. and then just work from there to higher precedence and depth levels.
now the 'compiling' phase is finished and we have a nice tree structure, meanwhile all variables are stored in a lookup table.

3) finally, when the function is executed the formula doesn't need to be interpreted anymore, all that happens is that the tree is traversed, with the ops connecting the leaves (all leaves are numbers or variables)

i hope that made some sense to you, but as i said it's complicated.

I realize this is just one possible method to do this, and i'm currently in the process of beginning to make my own scripting language in whitch i handle such things differently, but the current code works nice and very fast so don't be afraid to copy it




My Homepage

[edited by - Burning_Ice on March 19, 2003 7:13:25 PM]
TheShadow
TheShadow
I had some idea of several stuff you are showing me with your code. I''d really like to evaluate the function in real time since im not going to reuse it. There must be a way to evaluate it meanwhile you read it. I intend on using the code for an integral approximation. I have the aproximation code but it can''t work with user input. The algebraic function must be defined in code. For now i don''t intend on using any more than:

x
+
-
*
/
()
0123456789

So i think my code should look much more simpler than what you are doing there... since it really is long...
Mecha Engineer (Making Real Humanoid Suits)
Oluseyi
Oluseyi
Convert infix to prefix notation, evaluate LTR. That''s another way to do it.
TheShadow
TheShadow
what do you mean with prefix and infix notation and LTR?
Mecha Engineer (Making Real Humanoid Suits)
Extrarius
Extrarius
infix notation is where you put the function in the middle:
((5 + 3) * 7)
infix notation is where the function comes first:
(* (+ 5 3) 7)

LTR means Left to right, so you start with the * symbol, and it takes 2 numbers, so you need to get 2 numbers to multiply. You read the first thing, and its an expression. You get the first element of the list and it is a '+' and it takes two numbers. So you read a number, and it is 5, then 3, so you add them and you get 8, which is the first number for multiplication. You read the next number and it is 7. Then you mulitply 8 and 7 and get 56, which is the answer to both problems above.

Prefix notation is really just the text version of the syntax tree that Burning_Ice talks about in step 3.

If you are the main person that is going to be using your program, you might just want to make it take the input in prefix notation since that would mean less coding.

[edited by - Extrarius on March 20, 2003 7:07:52 PM]
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
TheShadow
TheShadow
Thanks all! I''m gonna have to study all this to get an answer i can''t still make a map in my head of how to do it... I''ll think more
Mecha Engineer (Making Real Humanoid Suits)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.