View Full Version : String to equation
neo_philiac
06-02-2008, 05:32 PM
Simple question!
I have a text box where I put "2+6*9". How do I get flash to convert this string to a function? So that it gives me the result 56 ??
I am trying to make a very simple calculator where I can see what I am typing and getting the results.
Thanks
Medyman
06-03-2008, 04:41 AM
How important is that exact functionality to what you're doing? What I recommend doing is splitting up the functionality like a calculator.
For example, first you would do 2+6 and then multiply that result by 9. In so doing, you can create a calculator fairly easily.
What you want to do -- take the "2-6*9" string input and then parse the operations isn't actually that simple. You would have to use regular expressions to split the expressions and operators into separate arrays. Then you would evaluate the operator symbols to decide which action Flash should run. Then you would have to actually have Flash perform the calculation. When you get into things like order of operations, this gets really tricky!!
With the solution I offer above. You would not need to worry about parsing the expression. You could just perform the operations as expected (one at a time, though).
For illustration purposes, I mean something like this (http://www.flzone.net/ShowDetail.asp?NewsId=5241). I would make the operations dynamic though, so you don't need to have four different fields. Also, if you're going to be doing higher level math, you might want to research some Math classes for AS2.0. There are many publicly available though I can't think of any sources off the top of my head. This will make your job a lot easier than coding each method by hand.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.