Saturday, March 20, 2010

Expression Handling (2nd approach)

After the first implementation, I have made a few more improvements and now, after submitting my assignment I think it's safe to post the final version of the classes here. Source code is available here (MediaFire). You may want to read the documentation here (Google Docs, in Vietnamese). The script is now accept natural expression input from console. The most interesting feature is each variable can also be enter as an expression (and it will ask for it's variables also). What does this mean?

For example, you input an expression: 5+x+y*z. Obviously, it will be calculated at 5 + x + ( y * z)
But because you used 3 variables, it will ask your for x, y, z. I will assign x with value of 20, y with 10.
Now is the fun stuff: I input "a-b" for z. Uhhhh. What do you think the script will do now?
Ah ha, it will ask for z.a (which means the 'a' variable of 'z' expression), 100 to go
And then z.b: 1000 to go
The result is -17985 (I'm not really sure with this result but you can check yourself)

That example is from the documentation. Feel free to leave comment