在Textfield中输入一个公式并处理它? [动作3.0] [英] Inputting a Equation in Textfield and Processing it? [actionscript 3.0]
问题描述
我想知道他们是否可以在这里输入文本字段,例如5 * 5。
然后输入文件将采取数据库,并能够返回25。我很困惑,因为您输入到文本字段的内容必须是 String
,当你使用 Number()
转换器时,系统会感到困惑。
你需要打破你的字符串在运算符(使用正则表达式),然后保持这些运算符。
没有耐心做到这一点,但你需要优先考虑某些操作(加法之前的乘法等)。
我已经做了一些代码来获得你开始 - 注意,它只适用于目前形式的真正简单的算法:
// Finds +, - and *。
var操作符:RegExp = / \ + | \- | \ * / g;
var numbers:RegExp = / \ d + / g;
/ **
*尝试使用以字符串形式提供的数学公式。
* @param str主题字符串。
函数work(str:String):Number
{
var ops:Array = str.match(operators); //按照顺序排列的运算符列表。
var nums:Array = str.match(numbers); //按顺序列出数字。
var command:Array = [];
var cmbLen:int = Math.max(ops.length,nums.length);
$ b $ for(var i:int = 0; i< cmbLen; i ++)
{
if(nums [i])command.push(nums [i]);
if(ops [i])command.push(ops [i]);
}
//开始执行命令。
var val:Number = 0;
var queuedOperator:String =;
(i = 0; i {
//应用初始值;
if(i == 0)
val = Number(command [i]);
//工作。
if(i%2 == 0)
{
var num:Number = Number(command [i]);
//如果需要,操作。
if(queuedOperator.length> 0)
{
switch(queuedOperator)
{
case+:val + = num;打破;
case - :val - = num;打破;
case*:val * = num;打破;
}
}
queuedOperator = command [i + 1] as String ;
}
}
return val;
}
还有一些测试:
trace(work(7 + 10)); // 17
trace(work(5 * 5)); // 25
trace(work(12 - 4)); // 8
trace(work(10 + 5 + 1)); // 16
随意构建这个包含更多的运算符(只需将它们添加到 operators
)并按优先级顺序排序命令
。添加()
括号将开始变得有趣,但是随着时间的推移,我相信可以正确地工作。
I would like to know if theirs a way where you can type in a text field something like "5*5".
And then on enter, the document would take the database and be able to return 25.
I am quite confused due to the fact that content that you input into a textfield must be a String
and when you use the Number()
converter the system gets confused.
You'll need to break your string apart at operators (using regex) and then keep those operators.
I don't have the patience to do this myself but you'll need to prioritise certain operations as well (multiplication before addition, etc).
I've done some code to get you started - note that it only works with really simple algorithms in its current form:
// Finds +, - and *.
var operators:RegExp = /\+|\-|\*/g;
var numbers:RegExp = /\d+/g;
/**
* Attempts to work with a mathematical formula provided as string.
* @param str Subject string.
*/
function work(str:String):Number
{
var ops:Array = str.match(operators); // List of operators in order.
var nums:Array = str.match(numbers); // List of numbers in order.
var command:Array = [];
var cmbLen:int = Math.max(ops.length, nums.length);
for(var i:int = 0; i < cmbLen; i++)
{
if(nums[i]) command.push(nums[i]);
if(ops[i]) command.push(ops[i]);
}
// Begin performing the command.
var val:Number = 0;
var queuedOperator:String = "";
for(i = 0; i < command.length; i++)
{
// Apply initial value;
if(i == 0)
val = Number(command[i]);
// Work.
if(i % 2 == 0)
{
var num:Number = Number(command[i]);
// Operate if required.
if(queuedOperator.length > 0)
{
switch(queuedOperator)
{
case "+": val += num; break;
case "-": val -= num; break;
case "*": val *= num; break;
}
}
queuedOperator = command[i + 1] as String || "";
}
}
return val;
}
And some testing:
trace(work("7 + 10")); // 17
trace(work("5 * 5")); // 25
trace(work("12 - 4")); // 8
trace(work("10 + 5 + 1")); // 16
Feel free to build on this to include more operators (just add them into operators
) and sort the command
in order of priority. Adding ()
parenthesis will start to get interesting but with time I'm sure it's possible to get working right.
这篇关于在Textfield中输入一个公式并处理它? [动作3.0]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!