用Flex4做的计算器
这是界面图: 这个可以在design界面里拖动组件,修改组件属性值来实现。 然后是计算器的算法(仿照win下的计算器算法): 问题描述: 输入一个数,然后按某个运算符,再输入另一个数,按等号即可得出结果。只按照输入顺序计算结果,而非按照运算符优先级来得出结果,即,如果按顺序输入2+8*3=会得到30,而非26. 算法描述: 定义三个全局变量,分别是first(Number), second(Number), symbol(String) first代表二目运算中第一个数,second代表第二个数,symbol代表运算符(+-*/) (初始化之后flex对Number型默认值为NaN,String型默认值为null) 1.二目运算第一个数的输入数字阶段,对于小数点(.)按钮,如果输入一次之后enable属性设为false. 2.输入运算符(+-*/),将第一步输入的数字保存到first变量中,并将此运算符保存到symbol中,小数点(.)按钮enable属性为true. 3.二目运算第二个数字输入阶段,对于小数点(.)按钮,如果输入一次之后enable属性设为false. 4. ???? 4.1如果输入等号(=),将前面存储的first, second, symbol按照相应规则运算得出结果,这个结果存储到first中,second和symbol分别设为NaN和null。小数点按钮enable属性为true。然后可以转入步骤2. ???? 4.2 如果输入运算符(+-× /) ,将first, second, symbol按照规则运算出结果,结果保存到first中,second设为NaN, 将输入运算符保存到symbol中,小数点按钮enable属性为true。然后可以转入步骤3。 下面是整个计算器的代码及注释: <?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" ??? ??? ??? ?? xmlns:s="library://ns.adobe.com/flex/spark" ??? ??? ??? ?? xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"> ??? <fx:Style source="flexapp.css"/> ??? <fx:Script> ??? ??? <![CDATA[ ??? ??? ??? public var first:Number; ??? ??? ??? public var second:Number; ??? ??? ??? public var symbol:String; ??? ??? ??? public var display_content:String='0'; ??? ??? ??? ??? ??? ??? ?private function addText(str:String):void//输入数字时,显示内容 ??? ??? ??? { ??? ??? ??? ??? display_content+=str; //每点一个数字,显示内容增加进去 ??? ??? ??? ??? var myFloat:Number = parseFloat(display_content); ??? ??? ??? ??? //将显示内容转换为Number型 ??? ??? ??? ??? if(str!='.') ??? ??? ??? ??? { ??? ??? ??? ??? ??? txt_display.text = myFloat.toString(); ??? ??? ??? ??? ??? //将刚转换的Number型转换为String型,显示到input_Text中?? ??? ??? ??? ??? } ??? ??? ??? ??? else//处理小数点情况 ??? ??? ??? ??? { ??? ??? ??? ??? ??? txt_display.text = myFloat.toString()+"."; ??? ??? ??? ??? ??? bt_dot.enabled=false; ??? ??? ??? ??? } ??? ??? ??? ??? ??? ??? ??? } ??? ??? ??? //C按钮的功能 ??? ??? ??? private function c():void ??? ??? ??? { ??? ??? ??? ??? display_content ='0'; ??? ??? ??? ??? txt_display.text='0'; ??? ??? ??? ??? first=NaN; ??? ??? ??? ??? second=NaN; ??? ??? ??? ??? symbol=null; ??? ??? ??? ??? bt_dot.enabled=true; ??? ??? ??? } ??? ??? ??? //CE按钮功能 ??? ??? ??? private function ce():void ??? ??? ??? { ??? ??? ??? ??? display_content='0'; ??? ??? ??? ??? txt_display.text='0'; ??? ??? ??? ??? second=NaN; ??? ??? ??? ??? bt_dot.enabled=true; ??? ??? ??? } ??? ??? ??? //运算功能 ??? ??? ??? private function cal():void ??? ??? ??? { ??? ??? ??? ??? second=parseFloat(txt_display.text); ??? ??? ??? ??? switch (symbol) ??? ??? ??? ??? { ??? ??? ??? ??? ??? case "+": ??? ??? ??? ??? ??? ??? first=first+second; ??? ??? ??? ??? ??? ??? break; ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? case "-": ??? ??? ??? ??? ??? ??? first=first-second; ??? ??? ??? ??? ??? ??? break; ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? case "*": ??? ??? ??? ??? ??? ??? first=first*second; ??? ??? ??? ??? ??? ??? break; ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? case "/": ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? first=first/second; ??? ??? ??? ??? ??? ??? break; ??? ??? ??? ??? ??? ??? ??? ??? ??? ??? default: ??? ??? ??? ??? ??? ??? // do nothing ??? ??? ??? ??? } ??? ??? ??? ??? txt_display.text=first.toString(); ??? ??? ??? ??? display_content='0'; ??? ??? ??? ??? symbol=null; ??? ??? ??? ??? second=NaN; ??? ??? ??? ??? bt_dot.enabled=true; ??? ??? ??? } ??? ??? ??? //输入运算符(+-× /)符号处理 ??? ??? ??? private function process_symbol(str:String):void ??? ??? ??? { ??? ??? ??? ??? if(first.toString()=='NaN') ??? ??? ??? ??? { ??? ??? ??? ??? ??? first=parseFloat(txt_display.text); ??? ??? ??? ??? ??? if(str!="=") ??? ??? ??? ??? ??? { symbol=str; } ??? ??? ??? ??? ??? display_content='0'; ??? ??? ??? ??? ??? bt_dot.enabled=true; ??? ??? ??? ??? } ??? ??? ??? ??? else if(second.toString()=='NaN' ) ??? ??? ??? ??? { ??? ??? ??? ??? ??? symbol=str; ??? ??? ??? ??? } ??? ??? ??? ??? else ??? ??? ??? ??? { ??? ??? ??? ??? ??? cal(); ??? ??? ??? ??? } ??? ??? ??? } ??? ??? ??? ??? ??? ??? //输入等号时?????? ??? ??? ??? private function equals():void ??? ??? ??? { ??? ??? ??? ??? if(first.toString()=='NaN') ??? ??? ??? ??? { ??? ??? ??? ??? ??? first=parseFloat(txt_display.text); ??? ??? ??? ??? ??? display_content='0'; ??? ??? ??? ??? ??? bt_dot.enabled=true; ??? ??? ??? ??? } ??? ??? ??? ??? ??? ??? ??? ??? ??? else ??? ??? ??? ??? { ??? ??? ??? ??? ??? cal(); ??? ??? ??? ??? ??? ??? ??? ??? ??? } ??? ??? ??? } ??? ??? ]]> ??? </fx:Script> ??? <fx:Declarations> ??? ??? <!-- 将非可视元素(例如服务、值对象)放在此处 --> ??? </fx:Declarations> ??? <s:BorderContainer width="309" height="354" x="327" y="130"> ??? ??? <s:Panel width="273.5" height="334" title="calculator" horizontalCenter="center" verticalCenter="center" backgroundColor="#AFB6B9"> ??? ??? ??? ??? ??? <s:TextInput width="252" x="9" y="10" id="txt_display" textAlign="right" text="0"/> ??? ??? ??? <s:Button x="137" y="37" label="CE" width="58" height="31" color="#0B0C0F" id="bt_ce" click="ce()"/> ??? ??? ??? <s:Button x="204" y="37" label="C" width="58" height="31" color="#0B0C0F" id="bt_c" click="c()"/> ??? ??? ??? <s:Button x="10" y="81" label="7" width="36" height="31" color="#0B0C0F" id="bt_7" click="addText('7')"/> ??? ??? ??? <s:Button x="59" y="81" label="8" width="36" height="31" color="#0B0C0F" id="bt_8" click="addText('8')"/> ??? ??? ??? <s:Button x="108" y="81" label="9" width="36" height="31" color="#0B0C0F" click="addText('9')"/> ??? ??? ??? <s:Button x="10" y="120" label="4" width="36" height="31" color="#0B0C0F" id="bt_4" click="addText('4')"/> ??? ??? ??? <s:Button x="59" y="120" label="5" width="36" height="31" color="#0B0C0F" id="bt_5" click="addText('5')"/> ??? ??? ??? <s:Button x="108" y="120" label="6" width="36" height="31" color="#0B0C0F" click="addText('6')"/> ??? ??? ??? <s:Button x="10" y="159" label="1" width="36" height="31" color="#0B0C0F" id="bt_1" click="addText('1')"/> ??? ??? ??? <s:Button x="59" y="159" label="2" width="36" height="31" color="#0B0C0F" id="bt_2" click="addText('2')"/> ??? ??? ??? <s:Button x="108" y="159" label="3" width="36" height="31" color="#0B0C0F" click="addText('3')"/> ??? ??? ??? <s:Button x="159" y="81" label="+" width="36" height="31" color="#0B0C0F" id="bt_plus" click="process_symbol('+')"/> ??? ??? ??? <s:Button x="159" y="120" label="-" width="36" height="31" color="#0B0C0F" id="bt_minus" click="process_symbol('-')"/> ??? ??? ??? <s:Button x="159" y="159" label="*" width="36" height="31" color="#0B0C0F" id="bt_multiply" click="process_symbol('*')"/> ??? ??? ??? <s:Button x="160" y="201" label="/" width="36" height="31" color="#0B0C0F" id="bt_divide" click="process_symbol('/')"/> ??? ??? ??? <s:Button x="108" y="201" label="." width="36" height="31" color="#0B0C0F" id="bt_dot" click="addText('.')"/> ??? ??? ??? <s:Button x="10" y="201" label="0" width="85" height="31" color="#0B0C0F" click="addText('0')"/> ??? ??? ??? <s:Button x="213" y="83" label="=" height="148" width="49" color="#0B0C0F" id="bt_equal" click="equals()"/> ??? ??? ??? ??? ??? </s:Panel> ??? </s:BorderContainer> </s:Application> ? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |