2

私は ActionScript で少し遊んでいます。私が欲しいのは、文字列から数学関数を表示できることです。

たとえば、私の作業中の python スクリプトでは、次のようなことを行います。

formula = 'x**2 + 3*x'
for x in range( 0, 100 ):
    y = eval( formula )
    graph.display( x, y )

これを ActionScript に移植したいのですが、バージョン 3 以降は eval がないようです。どうすれば関数の値を計算できますか?

4

5 に答える 5

3

あなたのケースでもうまくいくかもしれない何かは、代わりに Javascript eval を使用しています。次のようなものを使用できます。

var result = ExternalInterface.call(myEvalFunctionInJS,formula)

数学関数を評価します。

JavaScript は ActionScript に非常に近いため、これはやや簡単で便利な回避策です。

ループ内に ExternalInterface 呼び出しを配置すると、動作が遅くなる場合があります。これを回避するには、JavaScript でループを記述します。(実際の html ページに触れる必要がないように、javascript 全体を as3 内に記述することもできます。)

編集:これがそのリンクです。

http://www.actionscript.org/resources/articles/745/2/JavaScript-and-VBScript-Injection-in-ActionScript-3/Page2.html

于 2009-03-23T13:55:05.967 に答える
2

Just noticed this question and realized I answered almost the exact same thing here: https://stackoverflow.com/a/11460839/1449525

To paraphrase myself, you can definitely use D.eval, AS3Eval, or ExternalInterface (as seen in the currently chosen answer) assuming you're running in a web page. However, all it seems like you really need is something like this simple MathParser (More info about the MathParser)

Here's how you'd use the MathParser:

package {
    import bkde.as3.parsers.*;
    import flash.display.Sprite;
    public class MathTest extends Sprite {
        public function MathTest() {
            var parser:MathParser = new MathParser([]);
            var compiledObj:CompiledObject = parser.doCompile("(10/3)*4+10");
            var answer:Number = parser.doEval(compiledObj.PolishArray, []);

            var xyParser:MathParser = new MathParser(["x", "y"]);
            var xyCompiledObj:CompiledObject = xyParser.doCompile("(x/3)*y+10");
            var xyAnswer:Number = xyParser.doEval(xyCompiledObj.PolishArray, [10, 4]);
        }
    }
}

I'm sure ExternalInterface stuff works just fine, but I have personal reservations about the cross language communication (especially in terms of efficiency and security) as well as just the awkward nature of it. I feel like a wholly-contained, same-language solution is typically preferable in most situations.

于 2012-08-27T15:59:32.650 に答える
1

少し遅れましたが、参考までに、D.eval ライブラリはあなたが求めていることを行います。

http://www.riaone.com/products/deval/

それは無料で、私にとってはうまく機能しますが、ソースは付属していません。この質問は、組み込みまたはソースで利用可能な代替ソリューションを探しているのを見つけました。

于 2011-05-30T00:31:43.880 に答える
0

また、Tamarin を Flash 自体に移植するための一見放棄されたプロジェクトもあります。

http://eval.hurlant.com/

もっと進歩があれば素晴らしいのですが、今のところ好奇心に満ちているようです。

于 2009-04-23T19:24:38.650 に答える