这是界面图:
这个可以在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。
下面是整个计算器的代码及注释:
这个可以在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"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="horizontal">
<mx:Script>
<![CDATA[
public var first:Number; //定义二目运算第一个数的存储变量
public var second:Number; //第二个数的存储变量
public var symbol:String; //运算符
public var display_content:String='0' //input_Text显示内容
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();
}
}
]]>
</mx:Script>
//组件代码:
<mx:Canvas width="309" height="354">
<mx:Panel y="10" width="273.5" height="334" layout="absolute" title="calculator" horizontalAlign="center" verticalAlign="middle" backgroundColor="#AFB6B9" horizontalCenter="-2">
<mx:TextInput x="10.75" y="29" width="231" id="txt_display" text="0" textAlign="right"/>
<mx:Button x="13" y="98" label="7" width="36" height="31" color="#0B0C0F" id="bt_7" click="addText('7')"/>
<mx:Button x="57" y="98" label="8" width="36" height="31" id="bt_8" click="addText('8')"/>
<mx:Button x="101" y="98" label="9" width="36" height="31" id="bt_9" click="addText('9')"/>
<mx:Button x="145" y="98" label="+" width="36" height="31" color="#0DC5ED" id="bt_plus" click="process_symbol('+')"/>
<mx:Button x="145" y="137" label="-" width="36" height="31" color="#089D5C" id="bt_minus" click="process_symbol('-')"/>
<mx:Button x="145" y="176" label="*" width="36" height="31" color="#F018CA" id="bt_multiply" click="process_symbol('*')"/>
<mx:Button x="145" y="215" label="/" width="36" height="31" color="#67B0BF" id="bt_divide" click="process_symbol('/')"/>
<mx:Button x="180" y="59" label="C" width="58" height="31" color="#FB2407" id="bt_c" click="c()"/>
<mx:Button x="189" y="98" label="=" width="49" height="148" id="bt_equal" click="equals()"/>
<mx:Button x="103.75" y="59" label="CE" width="58" height="31" id="bt_ce" click="ce()"/>
<mx:Button x="13" y="137" label="4" width="36" height="31" id="bt_4" click="addText('4')"/>
<mx:Button x="57" y="137" label="5" width="36" height="31" id="bt_5" click="addText('5')"/>
<mx:Button x="101" y="137" label="6" width="36" height="31" id="bt_6" click="addText('6')"/>
<mx:Button x="13" y="176" label="1" width="36" height="31" id="bt_1" click="addText('1')"/>
<mx:Button x="57" y="176" label="2" width="36" height="31" id="bt_2" click="addText('2')"/>
<mx:Button x="101" y="176" label="3" width="36" height="31" id="bt_3" click="addText('3')"/>
<mx:Button x="101" y="215" label="." width="36" height="31" id="bt_dot" click="addText('.')"/>
<mx:Button x="13" y="215" label="0" width="80" height="31" id="bt_0" click="addText('0')"/>
</mx:Panel>
</mx:Canvas>
</mx:Application>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="horizontal">
<mx:Script>
<![CDATA[
public var first:Number; //定义二目运算第一个数的存储变量
public var second:Number; //第二个数的存储变量
public var symbol:String; //运算符
public var display_content:String='0' //input_Text显示内容
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();
}
}
]]>
</mx:Script>
//组件代码:
<mx:Canvas width="309" height="354">
<mx:Panel y="10" width="273.5" height="334" layout="absolute" title="calculator" horizontalAlign="center" verticalAlign="middle" backgroundColor="#AFB6B9" horizontalCenter="-2">
<mx:TextInput x="10.75" y="29" width="231" id="txt_display" text="0" textAlign="right"/>
<mx:Button x="13" y="98" label="7" width="36" height="31" color="#0B0C0F" id="bt_7" click="addText('7')"/>
<mx:Button x="57" y="98" label="8" width="36" height="31" id="bt_8" click="addText('8')"/>
<mx:Button x="101" y="98" label="9" width="36" height="31" id="bt_9" click="addText('9')"/>
<mx:Button x="145" y="98" label="+" width="36" height="31" color="#0DC5ED" id="bt_plus" click="process_symbol('+')"/>
<mx:Button x="145" y="137" label="-" width="36" height="31" color="#089D5C" id="bt_minus" click="process_symbol('-')"/>
<mx:Button x="145" y="176" label="*" width="36" height="31" color="#F018CA" id="bt_multiply" click="process_symbol('*')"/>
<mx:Button x="145" y="215" label="/" width="36" height="31" color="#67B0BF" id="bt_divide" click="process_symbol('/')"/>
<mx:Button x="180" y="59" label="C" width="58" height="31" color="#FB2407" id="bt_c" click="c()"/>
<mx:Button x="189" y="98" label="=" width="49" height="148" id="bt_equal" click="equals()"/>
<mx:Button x="103.75" y="59" label="CE" width="58" height="31" id="bt_ce" click="ce()"/>
<mx:Button x="13" y="137" label="4" width="36" height="31" id="bt_4" click="addText('4')"/>
<mx:Button x="57" y="137" label="5" width="36" height="31" id="bt_5" click="addText('5')"/>
<mx:Button x="101" y="137" label="6" width="36" height="31" id="bt_6" click="addText('6')"/>
<mx:Button x="13" y="176" label="1" width="36" height="31" id="bt_1" click="addText('1')"/>
<mx:Button x="57" y="176" label="2" width="36" height="31" id="bt_2" click="addText('2')"/>
<mx:Button x="101" y="176" label="3" width="36" height="31" id="bt_3" click="addText('3')"/>
<mx:Button x="101" y="215" label="." width="36" height="31" id="bt_dot" click="addText('.')"/>
<mx:Button x="13" y="215" label="0" width="80" height="31" id="bt_0" click="addText('0')"/>
</mx:Panel>
</mx:Canvas>
</mx:Application>
转载于:https://blog.51cto.com/fsjoy/222100