Adobe Animate Actionscript calculator storing numbers after a character as int / string

I need to somehow make the numbers after +, -, *, / in the variable "num2" Any ideas? I am new to actionscript and have not found a solution in a book or on the Internet: / A Java tag has been added because the flash section is almost dead, and I believe that the solution will be the same in both languages, if I'm wrong, tell me and I will remove the tag; )

enter image description here

    function equals(evt:MouseEvent) {
    if (action == plus) {
        text_field.text = text_field.text + "=" + (num1 + num2);
    }
    else if (action == minus) {
        text_field.text = text_field.text + "=" + (num1 - num2);
    }
    else if (action == divide) {
        text_field.text = text_field.text + "=" + (num1 / num2);
    }
    else if (action == multiply) {
        text_field.text = text_field.text + "=" + (num1 * num2);
    }
}
function plus(evt:MouseEvent) {
    action = plus;
    num1 = parseInt(text_field.text);
    text_field.text = text_field.text + '+';
}
+4
source share
1 answer

I suggest breaking input text with operators or even all non-numeric characters like me using a regular expression

// returns an array of values
function parse_values(inputString:String):Array {
    return inputString.split(/[^0-9]/);
}

eg:

var cinput:String = "333+663/2345-6554";
trace(parse_values(cinput));

and the result:

333,663,2345,6554

Edit:

, , , .. : RegularExpression

+2

Source: https://habr.com/ru/post/1661613/


All Articles