[AS3]as3.0写的一个普通计算器源代码
本人认真学AS3还没一个月吧,之前断断续续的学了些,后来看了cookbook那本书和黑羽大哥的殿堂之路,慢慢的可以看懂一些as3的源码了呵呵,不过天地会上的兄弟感觉都是高人啊,很多源码我现在还看不懂。
贴上自己的第一个as3程序,不怕大家笑话,希望能给我提些学习的建议,好让我进步更快,先谢过!
算法很简单,也是网上看到的:
主要处理运算符的优先,优先计算优先级高的,比如当输入一个运算符,计算器先检查前一步输入的表达式,如果这个运算符比之前的高,则暂不计算,等待继续输入,如果这个运算符的优先级小于或与之前输入的相同,则将前面输入的表达式进行运算,得出一个临时结果。 直到最后按下=号。
比如8+8*8+6=
输入8+8 未计算
输入* 未计算,因为优先级高于前面的+号
输入8 未计算
输入+ 计算,优先级低于前面一个,计算8*8,结果存入一个临时变量,之后判断后面的运算符与第一个的优先级,发现是同级的,则进行计算,即计算8+64,结果存入操作数A(第一个操作数)中
输入6 未计算,等待下个运算符
输入= 计算结果
里面还有一个显示时间的部分,占用了一点代码呵呵,其实没这么多。
- package {
- import flash.display.*;
- import flash.events.*;
- import flash.text.*;
- import flash.utils.Timer;
- import flash.filters.DropShadowFilter;
- public class Calculator extends Sprite {
- private var _bg_mc:Sprite;
- private var _displayDateText:TextField;
- private var _displayTimeText:TextField;
- private var _time:Timer;
- private var buttonTextArray:Array;
- private var _result:TextField;
- private var _displayOpe:TextField;
- private var _variableA:String;
- private var _variableB:String;
- private var _operatorA:String;
- private var _operatorB:String;
- private var _variableTemp:String;
- private var tag:int;
- private var operatorTag:int;
- private var tempVar:String;
- private var _expression:String;
- private var _currentVarTag:int;
- private var _isEqualOrResetTag:int;
- public function Calculator() {
- _variableA="";
- _variableB="";
- _operatorA="";
- _operatorB="";
- _variableTemp="";
- _expression="";
- operatorTag=0;
- _currentVarTag=0;
- _isEqualOrResetTag=0;
- tag=0;
- tempVar=new String("");
- set_stage();
- displayDateTime();
- _time=new Timer(1000);
- _time.addEventListener(TimerEvent.TIMER,getDateTime);
- _time.start();
- }
- private function set_stage():void{
- _bg_mc=new Sprite();
- _bg_mc.x=50;
- _bg_mc.y=50;
- _bg_mc.graphics.lineStyle(1,0x000000);
- _bg_mc.filters=[new DropShadowFilter(2)];
- _bg_mc.graphics.beginFill(0xFF0000);
- _bg_mc.graphics.drawRoundRect(0,0,350,300,8);
- _bg_mc.graphics.endFill();
- addChild(_bg_mc);
- _displayDateText=writeTextField(250,22,50,25);
- _bg_mc.addChild(_displayDateText);
- _displayTimeText=writeTextField(250,22,50,48);
- _bg_mc.addChild(_displayTimeText);
- _result=writeTextField(250,22,50,90);
- _bg_mc.addChild(_result);
- _displayOpe=writeTextField(150,22,50,115);
- _bg_mc.addChild(_displayOpe);
- addCalculator();
- }
- private function displayDateTime():void{
- var tempDate:Date=new Date();
- var tempArray:Array=new Array("日","一","二","三","四","五","六");
- var _year:uint;
- var _month:uint;
- var _day:uint;
- var _weekDay:uint;
- var _hour:uint;
- var _minute:uint;
- var _second:uint;
- _year=tempDate.fullYear;
- _month=tempDate.month+1;
- _day=tempDate.date;
- _weekDay=tempDate.day;
- _hour=tempDate.hours;
- _minute=tempDate.minutes;
- _second=tempDate.seconds;
- _displayDateText.htmlText="<p align='center'><font size='16' color='#ff0000'>日期:"+_year+"年"+_month+"月"+_day+"日 "+"星期"+tempArray[_weekDay]+"</font></p>";
- _displayTimeText.htmlText="<p align='center'><font size='16'>时间:"+_hour+":"+_minute+" "+_second+"</font></p>";
- }
- private function getDateTime(e:TimerEvent):void{
- displayDateTime();
- }
- private function writeTextField(textWidth,textHeight,textX,textY):TextField{
- var tempText=new TextField();
- tempText.border=true;
- tempText.borderColor=0xffffff;
- tempText.background=true;
- tempText.backgroundColor=0xcccccc;
- tempText.width=textWidth;
- tempText.height=textHeight;
- tempText.x=textX;
- tempText.y=textY;
- return tempText;
- }
- private function setState(color:uint,radius:int,topX:int,topY:int,rightX:int,heightY:int,buttonText:String):Sprite{
- var textBtn:TextField=new TextField();
- textBtn.x=topX+6;
- textBtn.y=topY+2;
- textBtn.width=rightX-10;
- textBtn.height=heightY;
- var roundRect:Sprite=new Sprite();
- roundRect.graphics.lineStyle(1,0x000000);
- roundRect.graphics.beginFill(color);
- roundRect.graphics.drawRoundRect(topX,topY,rightX,heightY,radius);
- roundRect.graphics.endFill();
- roundRect.filters=[new DropShadowFilter(1)];
- textBtn.htmlText="<p align='center'><font color='#FFFFFF'>"+buttonText+"</font></p>";
- roundRect.addChild(textBtn);
- return roundRect
- }
- private function addCalculator():void{
- buttonTextArray=new Array("7","8","9","/","4","5","6","*","1","2","3","-","0","+/-",".","+")
- var tempX:uint=50;
- var tempY:uint=120;
- var temp:uint=1
- var buttonArray:Array=new Array();
- for(var i:uint=0;i<16;i++){
- if(i%4==0){
- if(i!=0){
- temp++;}
- tempX=50;
- tempY=120+25*temp;
- }
- buttonArray[i]=new SimpleButton();
- buttonArray[i].upState=setState(0x009900,2,tempX,tempY,25,20,buttonTextArray[i]);
- buttonArray[i].overState=setState(0x00CC00,2,tempX,tempY,25,20,buttonTextArray[i]);
- buttonArray[i].downState=setState(0x006600,2,tempX,tempY,25,20,buttonTextArray[i]);
- buttonArray[i].hitTestState=buttonArray[i].upState;
- buttonArray[i].addEventListener(MouseEvent.CLICK,toCalculate(buttonTextArray[i]));
- tempXtempX=tempX+30;
- _bg_mc.addChild(buttonArray[i]);
- }
- buttonArray[17]=new SimpleButton();
- buttonArray[17].upState=setState(0x009900,2,180,180,100,25,"计算结果");
- buttonArray[17].overState=setState(0x00CC00,2,180,180,100,25,"计算结果");
- buttonArray[17].downState=setState(0x006600,2,180,180,100,25,"计算结果");
- buttonArray[17].hitTestState=buttonArray[17].upState;
- buttonArray[17].addEventListener(MouseEvent.CLICK,toCalculate("="));
- _bg_mc.addChild(buttonArray[17]);
- buttonArray[18]=new SimpleButton();
- buttonArray[18].upState=setState(0x009900,2,180,210,100,25,"重置");
- buttonArray[18].overState=setState(0x00CC00,2,180,210,100,25,"重置");
- buttonArray[18].downState=setState(0x006600,2,180,210,100,25,"重置");
- buttonArray[18].hitTestState=buttonArray[18].upState;
- buttonArray[18].addEventListener(MouseEvent.CLICK,toCalculate("r"));
- _bg_mc.addChild(buttonArray[18]);
- }
热门文章推荐
- [HLS]做自己的m3u8点播系统使用HTTP Live Streaming(HLS技术)
- [FMS]FMS流媒体服务器配置与使用相关的介绍
- [AS3]什么是M3U8,与HTML5的区别是什么
- AS2.0 让flash自适应全屏,并且不自动缩放
- [AS3]as3.0的sound类常用技巧整理
- [AS3]as3与ByteArray详解、ByteArray介绍、ByteArray用法
- 关于RTMP,RTMPT,RTMPS,RTMPE,RTMPTE协议的介绍
- [JS]分享浏览器弹出窗口不被拦截JS示例