A Flash Developer Resource Site

# Thread: 2 subjects in one (calculator)

1. ## 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. 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. I want to start making calcs but i Dont want to write the parser.

Can I find the code done for me somewhere?

4. 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:

- (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.

5. 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

6. 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
•

 » Home » Movies » Tutorials » Submissions » Board » Links » Reviews » Feedback » Gallery » Fonts » The Lounge » Sound Loops » Sound FX » About FK » Sitemap

Click Here to Expand Forum to Full Width