A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: 2 subjects in one (calculator)

  1. #1
    Senior Member
    Join Date
    Apr 2004
    Posts
    325

    2 subjects in one

    for one thing check out the scale calculator I made to do my homework.http://davidw.da.funpic.org/embed.ph...le&w=550&h=400

    and.
    I Plan to start making flash calculators and I dont know how I make flash reconize the strings and do the calculations.

    like user imput:
    5^2
    and return 25
    thanks.

  2. #2
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    Unlike some other languages (like Perl), actionscript does not provide a function for parsing and executing arbitrary actionscript code. In other words, this does not work:

    result = eval("2 * 3");

    It *is* possible to write your own parser, but it requires a bit of work. For example, to convert the string "2 * 3" into separate symbols, you could use the split function.

    str = "2 * 3";
    symbols = str.split(' ');

    Then you could walk thru the symbols, and examine what they are, and do the appropriate things. Writing parsers is not for the faint-hearted though.

    It is somewhat easier if you use either prefix or postfix notation, such as like this:

    2 2 + 3 *

    Which is how HP calculators used to work.

  3. #3
    Senior Member
    Join Date
    Apr 2004
    Posts
    325
    I want to start making calcs but i Dont want to write the parser.

    Can I find the code done for me somewhere?

  4. #4
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    Aww c'mon, writing parsers is fun!

    Here's a quick & dirty one I just wrote for you.

    This one uses reverse polish notation, and accepts formulas such as

    2 2 +

    2 2 * 4 +

    I added support for 5 operators:

    + (addition)
    - (subtraction)
    * (multiplication)
    / (division)
    ^ (power)

    Be sure to separate the numbers and operators with spaces.

    To find other examples, search the actionscript thread for 'calculator' -- this is a pretty common question.
    Attached Files Attached Files

  5. #5
    Senior Member
    Join Date
    Apr 2004
    Posts
    325
    Nice, sorry I dident reply, I was to busy working on this.

    It now excepts strings such as
    sin 45
    cos 45
    tan 45
    2 + 8 + 9
    2 + 8 - 9 / 2

    It still has some problems and I want to implement Parenthesis
    Attached Files Attached Files

  6. #6
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    Cool! Glad you were able to get your hands dirty

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center