<?xml version="1.0"><mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml"></mx:Application>
<mx:Panel title="Calculator"></mx:Panel>
<mx:Label id="calcDisplay" width="150" textAlign="right"/>
<mx:Grid> <mx:GridRow> <mx:GridItem colSpan="2"></mx:GridItem> <mx:GridItem></mx:GridItem> <mx:GridItem></mx:GridItem> </mx:GridRow> <mx:GridRow> <mx:GridItem></mx:GridItem> <mx:GridItem></mx:GridItem> <mx:GridItem></mx:GridItem> <mx:GridItem></mx:GridItem> </mx:GridRow> <mx:GridRow> <mx:GridItem></mx:GridItem> <mx:GridItem></mx:GridItem> <mx:GridItem></mx:GridItem> <mx:GridItem></mx:GridItem> </mx:GridRow> <mx:GridRow> <mx:GridItem></mx:GridItem> <mx:GridItem></mx:GridItem> <mx:GridItem></mx:GridItem> <mx:GridItem></mx:GridItem> </mx:GridRow> <mx:GridRow> <mx:GridItem></mx:GridItem> <mx:GridItem ></mx:GridItem> <mx:GridItem colSpan="2"></mx:GridItem> </mx:GridRow> </mx:Grid>
width="70" label="Clear" click="calcController.clearAll()" width="30" label="C/E" click="calcController.clearEntry()" width="30" label="+" click="calcController.setOperation('add')"
width="30" label="1" click="calcController.addNumber('1')" width="30" label="2" click="calcController.addNumber('2')" width="30" label="3" click="calcController.addNumber('3')" width="30" label="-" click="calcController.setOperation('subtract')"
width="30" label="4" click="calcController.addNumber('4')" width="30" label="5" click="calcController.addNumber('5')" width="30" label="6" click="calcController.addNumber('6')" width="30" label="*" click="calcController.setOperation('multiply')"
width="30" label="7" click="calcController.addNumber('7')" width="30" label="8" click="calcController.addNumber('8')" width="30" label="9" click="calcController.addNumber('9')" width="30" label="/" click="calcController.setOperation('divide')"
width="30" label="0" click="calcController.addNumber('0')" width="30" label="." click="calcController.addNumber('.')" width="70" label="=" click="calcController.doOperation()"
class CalculatorController{}
public function CalculatorController(){}
public var calcView:Object; private var reg1:String=""; private var reg2:String=""; private var result:Number; private var currentRegister:String="reg1"; private var operation:String="none"; private var r1:Number; private var r2:Number;
public function doOperation():Void { r1=Number(reg1); r2=Number(reg2); switch (operation) { case "add": result=r1+r2; resetAfterOp(); break; case "subtract": result=r1-r2; resetAfterOp(); break; case "multiply": result=r1*r2; resetAfterOp(); break; case "divide": result=r1/r2; resetAfterOp(); break; default: //do nothing } }
public function addNumber(n:String):Void { if (operation=="none" && currentRegister=="reg2") { reg1=""; setCurrentRegister(); } this[currentRegister]+=n; setDisplay(currentRegister); }
public function clearEntry():Void { this[currentRegister]=""; setDisplay(currentRegister); }
public function clearAll():Void { reg1=""; reg2=""; setCurrentRegister(); setOperation("none"); setDisplay(currentRegister); }
public function setOperation(operation:String):Void { this.operation=operation; setCurrentRegister(); }
private function setDisplay(register:String):Void { calcView.calcDisplay.text = this[register]; }
private function setCurrentRegister():Void { if (reg1=="") { currentRegister="reg1"; } else { currentRegister="reg2"; } } private function resetAfterOp():Void { reg1=String(result); reg2=""; setDisplay("reg1"); setOperation("none"); }
<?xml version="1.0"?><mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml" xmlns="*"> <!-- calculator controller --> <CalculatorController id="calcController" calcView="{this}"/> <!-- calculator view --> <mx:Panel title="Calculator"> <!-- calculator display --> <mx:Label id="calcDisplay" width="150" textAlign="right"/> <!-- calculator controls --> <mx:Grid> <mx:GridRow> <mx:GridItem colSpan="2"> <mx:Button width="70" label="Clear" click="calcController.clearAll()"/> </mx:GridItem> <mx:GridItem> <mx:Button width="30" label="C/E" click="calcController.clearEntry()"/> </mx:GridItem> <mx:GridItem> <mx:Button width="30" label="+" click="calcController.setOperation('add')"/> </mx:GridItem> </mx:GridRow> <mx:GridRow> <mx:GridItem> <mx:Button width="30" label="1" click="calcController.addNumber('1')"/> </mx:GridItem> <mx:GridItem> <mx:Button width="30" label="2" click="calcController.addNumber('2')"/> </mx:GridItem> <mx:GridItem> <mx:Button width="30" label="3" click="calcController.addNumber('3')"/> </mx:GridItem> <mx:GridItem> <mx:Button width="30" label="-" click="calcController.setOperation('subtract')"/> </mx:GridItem> </mx:GridRow> <mx:GridRow> <mx:GridItem> <mx:Button width="30" label="4" click="calcController.addNumber('4')"/> </mx:GridItem> <mx:GridItem> <mx:Button width="30" label="5" click="calcController.addNumber('5')"/> </mx:GridItem> <mx:GridItem> <mx:Button width="30" label="6" click="calcController.addNumber('6')"/> </mx:GridItem> <mx:GridItem> <mx:Button width="30" label="*" click="calcController.setOperation('multiply')"/> </mx:GridItem> </mx:GridRow> <mx:GridRow> <mx:GridItem> <mx:Button width="30" label="7" click="calcController.addNumber('7')"/> </mx:GridItem> <mx:GridItem> <mx:Button width="30" label="8" click="calcController.addNumber('8')"/> </mx:GridItem> <mx:GridItem> <mx:Button width="30" label="9" click="calcController.addNumber('9')"/> </mx:GridItem> <mx:GridItem> <mx:Button width="30" label="/" click="calcController.setOperation('divide')"/> </mx:GridItem> </mx:GridRow> <mx:GridRow> <mx:GridItem> <mx:Button width="30" label="0" click="calcController.addNumber('0')"/> </mx:GridItem> <mx:GridItem > <mx:Button width="30" label="." click="calcController.addNumber('.')"/> </mx:GridItem> <mx:GridItem colSpan="2"> <mx:Button width="70" label="=" click="calcController.doOperation()"/> </mx:GridItem> </mx:GridRow> </mx:Grid> </mx:Panel></mx:Application>
/*Calculator Controller*/class CalculatorController{ // properties // object to hold a reference to the view object public var calcView:Object; // registers to hold temporary values pending operation private var reg1:String=""; private var reg2:String=""; // result of an operation private var result:Number; // the name of the register currently used private var currentRegister:String="reg1"; // the name of the next operation to be performed private var operation:String="none"; // for convenience, holder for numerical equivalents // of the register string values private var r1:Number; private var r2:Number; // constructor public function CalculatorController() {} // methods // perform the current operation on the 2 registers public function doOperation():Void { // cast the register values to numbers r1=Number(reg1); r2=Number(reg2); switch (operation) { case "add": result=r1+r2; resetAfterOp(); break; case "subtract": result=r1-r2; resetAfterOp(); break; case "multiply": result=r1*r2; resetAfterOp(); break; case "divide": result=r1/r2; resetAfterOp(); break; default: // do nothing } } // concatenate number to the value of the current register public function addNumber(n:String):Void { if (operation=="none" && currentRegister=="reg2") { reg1=""; setCurrentRegister(); } this[currentRegister]+=n; setDisplay(currentRegister); } // clear the current register public function clearEntry():Void { this[currentRegister]=""; setDisplay(currentRegister); } // clear both registers and the current operation public function clearAll():Void { reg1=""; reg2=""; setCurrentRegister(); setOperation("none"); setDisplay(currentRegister); } // set the current operation public function setOperation(operation:String):Void { this.operation=operation; setCurrentRegister(); } // set the value shown in the display private function setDisplay(register:String):Void { calcView.calcDisplay.text = this[register]; } // set which register is current private function setCurrentRegister():Void { if (reg1=="") { currentRegister="reg1"; } else { currentRegister="reg2"; } } // reset values after an operation private function resetAfterOp():Void { reg1=String(result); reg2=""; setDisplay("reg1"); setOperation("none"); }}
新闻热点
疑难解答