Results 1 to 2 of 2

Thread: String to equation

  1. #1
    Join Date
    Jul 2006
    Posts
    142
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default String to equation

    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

  2. #2
    Join Date
    Mar 2007
    Location
    Currently: New York/Philadelphia
    Posts
    2,735
    Thanks
    3
    Thanked 519 Times in 507 Posts

    Default

    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. 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.
    Last edited by Medyman; 06-03-2008 at 04:47 AM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •