最近老师让学Flex,于是跟着教学视频做了一个计算器的小程序。刚学,有很多不懂的,请大家多多指教~
【界面】
功能:可以实现加减乘除四种运算方法,以及开方、乘方、取反数等功能。
【代码】
声明变量
private var hasClickFloat:Boolean=false;
private var num:Number;
private var hasOver:Boolean=false;
private var cal:String;
函数部分
- 初始化函数
//init
protected function application1_creationCompleteHandler():void{
result.text="0";
hasClickFloat=false;
hasOver=false;
}
- addNumber函数
//append new number
private function addNumber(str:String):void{
//if not a number
if(result.text=="NaN"){
result.text=str;
}
//if result is 0,that str
else if(result.text=="0"){
result.text=str;
//if over,replace
}else if(hasOver==true){
result.text=str;
//if digits greater than 9
}else{
if(result.text.length>=9){
return;
}else{
result.text=result.text+str;
}
}
}
- saveNumber函数
//when input calculator,save the previous number
private function saveNumber(str:String):void{
//let can be input minus
if(str=="-" && result.text=="0"){
result.text="-";
return;
}
//when input calculator,let the value begin from 0
cal=str;
num=Number(result.text);
result.text="0";
hasOver=true;
}
- addFloat函数
//append a float
private function addFloat():void{
if(hasClickFloat==true){
return;
}else{
result.text=result.text+".";
hasClickFloat=true;
}
}
- 开方函数
//extract
private function pingFang():void{
result.text=String(Math.sqrt(Number(result.text)))
}
- 平方函数
//square
private function chengFang():void{
result.text=String((Number(result.text))*(Number(result.text)));
}
- 取反数函数
//inverse number
private function fanShu():void{
if(result.text=="0"){
result.text="NaN";
}else{
result.text=String(1/Number(result.text));
}
}
这里要判断下0没有反数哦。
- 输入运算符函数
private function doCal():void{
if(cal=="+"){
result.text=String(num+Number(result.text));
}else if(cal=="-"){
result.text=String(num-Number(result.text));
}else if(cal=="*"){
result.text=String(num*Number(result.text));
}else if(cal=="/"){
if(result.text=="0"){
result.text=="NaN";
}else {
result.text=String(num/Number(result.text));
}
}
}
- 布局部分代码
<s:HGroup width="100%" height="30" paddingLeft="5" paddingRight="5" verticalAlign="middle" horizontalAlign="center">
<s:Button label="S" width="40" click="pingFang();"/>
<s:Button label="G" width="40" click="chengFang();"/>
<s:Button label="N" width="40" click="fanShu();"/>
<s:Button label="C" width="40" click="application1_creationCompleteHandler()"/>
</s:HGroup>
<s:HGroup width="100%" height="30">
<s:HGroup width="100%" height="30" paddingLeft="5" paddingRight="5" verticalAlign="middle" horizontalAlign="center">
<s:Button label="1" width="40" click="addNumber('1')"/>
<s:Button label="2" width="40" click="addNumber('2')"/>
<s:Button label="3" width="40" click="addNumber('3')"/>
<s:Button label="=" width="40" click="doCal();"/>
</s:HGroup>
</s:HGroup>
<s:HGroup width="100%" height="30">
<s:HGroup width="100%" height="30" paddingLeft="5" paddingRight="5" verticalAlign="middle" horizontalAlign="center">
<s:Button label="4" width="40" click="addNumber('4')"/>
<s:Button label="5" width="40" click="addNumber('5')"/>
<s:Button label="6" width="40" click="addNumber('6')"/>
<s:Button label="+" width="40" click="saveNumber('+');"/>
</s:HGroup>
</s:HGroup>
<s:HGroup width="100%" height="30">
<s:HGroup width="100%" height="30" paddingLeft="5" paddingRight="5" verticalAlign="middle" horizontalAlign="center">
<s:Button label="7" width="40" click="addNumber('7')"/>
<s:Button label="8" width="40" click="addNumber('8')"/>
<s:Button label="9" width="40" click="addNumber('9')"/>
<s:Button label="-" width="40" click="saveNumber('-');"/>
</s:HGroup>
</s:HGroup>
<s:HGroup width="100%" height="30">
<s:HGroup width="100%" height="30" paddingLeft="5" paddingRight="5" verticalAlign="middle" horizontalAlign="center">
<s:Button label="0" width="40" click="addNumber('0')"/>
<s:Button label="." width="40" click="addFloat();"/>
<s:Button label="/" width="40" click="saveNumber('/');"/>
<s:Button label="*" width="40" click="saveNumber('*');"/>
</s:HGroup>