package { import by.flastar.utils.Stack; import flash.display.Sprite; import flash.events.Event; /** * ... * @author Sergey Gonchar */ public class StackExample extends Sprite { public function StackExample():void { if (stage) init(); else addEventListener(Event.ADDED_TO_STAGE, init); } private function init(e:Event = null):void { removeEventListener(Event.ADDED_TO_STAGE, init); // entry point var stc:Stack = new Stack(); var math:String = "{(x+y-[a+b])*c-[(d+e)]}/(h-(j-(k-[1-n])))"; var right:Boolean = true; newfor:for (var i:int = 0; i < math.length; i++ ) { if (math.charAt(i) == "{" || math.charAt(i) == "[" || math.charAt(i) == "(") { stc.addItem(math.charAt(i)); } else if (math.charAt(i) == "}") { if (stc.lastItem == "{") { stc.removeLastItem(); } else { right = false; break newfor; } } else if (math.charAt(i) == ")") { if (stc.lastItem == "(") { stc.removeLastItem(); } else { right = false; break newfor; } } else if (math.charAt(i) == "]") { if (stc.lastItem == "[") { stc.removeLastItem(); } else { right = false; break newfor; } } } if (right) Debug.log("Answer true") else Debug.log("Answer false"); } } }