·您当前的位置:首页 > 技术教程 > AS2与AS3技术 >

[AS3]as3.0写的一个普通计算器源代码

时间:2012-10-31 15:21bbs.9ria.com
本人认真学AS3还没一个月吧,之前断断续续的学了些,后来看了cookbook那本书和黑羽大哥的殿堂之路,慢慢的可以看懂一些as3的源码了呵呵,不过天地会上的兄弟感觉都是高人啊,很多源码我现在还看不懂

  本人认真学AS3还没一个月吧,之前断断续续的学了些,后来看了cookbook那本书和黑羽大哥的殿堂之路,慢慢的可以看懂一些as3的源码了呵呵,不过天地会上的兄弟感觉都是高人啊,很多源码我现在还看不懂。
贴上自己的第一个as3程序,不怕大家笑话,希望能给我提些学习的建议,好让我进步更快,先谢过!

  算法很简单,也是网上看到的:

  主要处理运算符的优先,优先计算优先级高的,比如当输入一个运算符,计算器先检查前一步输入的表达式,如果这个运算符比之前的高,则暂不计算,等待继续输入,如果这个运算符的优先级小于或与之前输入的相同,则将前面输入的表达式进行运算,得出一个临时结果。 直到最后按下=号。
比如8+8*8+6=
输入8+8 未计算
输入* 未计算,因为优先级高于前面的+号
输入8 未计算
输入+ 计算,优先级低于前面一个,计算8*8,结果存入一个临时变量,之后判断后面的运算符与第一个的优先级,发现是同级的,则进行计算,即计算8+64,结果存入操作数A(第一个操作数)中
输入6 未计算,等待下个运算符
输入= 计算结果

里面还有一个显示时间的部分,占用了一点代码呵呵,其实没这么多。
 

  1. package {  
  2.         import flash.display.*;  
  3.         import flash.events.*;  
  4.         import flash.text.*;  
  5.         import flash.utils.Timer;  
  6.         import flash.filters.DropShadowFilter;  
  7.  
  8.         public class Calculator extends Sprite {                  
  9.                 private var _bg_mc:Sprite;  
  10.                 private var _displayDateText:TextField;  
  11.                 private var _displayTimeText:TextField;  
  12.                 private var _time:Timer;  
  13.                 private var buttonTextArray:Array;  
  14.                 private var _result:TextField;  
  15.                 private var _displayOpe:TextField;  
  16.                 private var _variableA:String;  
  17.                 private var _variableB:String;  
  18.                 private var _operatorA:String;  
  19.                 private var _operatorB:String;  
  20.                 private var _variableTemp:String;  
  21.                 private var tag:int;  
  22.                 private var operatorTag:int;  
  23.                 private var tempVar:String;  
  24.                 private var _expression:String;  
  25.                 private var _currentVarTag:int;  
  26.                 private var _isEqualOrResetTag:int;  
  27.                   
  28.                 public function Calculator() {  
  29.                         _variableA="";  
  30.                         _variableB="";  
  31.                         _operatorA="";  
  32.                         _operatorB="";  
  33.                         _variableTemp="";  
  34.                         _expression="";  
  35.                         operatorTag=0;  
  36.                         _currentVarTag=0;  
  37.                         _isEqualOrResetTag=0;  
  38.                         tag=0;  
  39.                         tempVar=new String("");  
  40.                         set_stage();  
  41.                         displayDateTime();  
  42.                         _time=new Timer(1000);  
  43.                         _time.addEventListener(TimerEvent.TIMER,getDateTime);  
  44.                         _time.start();  
  45.                           
  46.                           
  47.                   
  48.                 }  
  49.             private function set_stage():void{  
  50.                 _bg_mc=new Sprite();  
  51.                         _bg_mc.x=50;  
  52.                         _bg_mc.y=50;  
  53.                         _bg_mc.graphics.lineStyle(1,0x000000);  
  54.                         _bg_mc.filters=[new DropShadowFilter(2)];  
  55.                         _bg_mc.graphics.beginFill(0xFF0000);  
  56.                         _bg_mc.graphics.drawRoundRect(0,0,350,300,8);                          
  57.                         _bg_mc.graphics.endFill();  
  58.                         addChild(_bg_mc);          
  59.                         _displayDateText=writeTextField(250,22,50,25);                          
  60.                         _bg_mc.addChild(_displayDateText);  
  61.                         _displayTimeText=writeTextField(250,22,50,48);                          
  62.                         _bg_mc.addChild(_displayTimeText);  
  63.                         _result=writeTextField(250,22,50,90);                          
  64.                         _bg_mc.addChild(_result);  
  65.                         _displayOpe=writeTextField(150,22,50,115);  
  66.                         _bg_mc.addChild(_displayOpe);  
  67.                         addCalculator();  
  68.             }  
  69.                 private function displayDateTime():void{  
  70.                         var tempDate:Date=new Date();  
  71.                         var tempArray:Array=new Array("日","一","二","三","四","五","六");  
  72.                         var _year:uint;  
  73.                         var _month:uint;  
  74.                         var _day:uint;  
  75.                         var _weekDay:uint;  
  76.                         var _hour:uint;  
  77.                         var _minute:uint;  
  78.                         var _second:uint;  
  79.                         _year=tempDate.fullYear;  
  80.                         _month=tempDate.month+1;  
  81.                         _day=tempDate.date;  
  82.                         _weekDay=tempDate.day;  
  83.                         _hour=tempDate.hours;  
  84.                         _minute=tempDate.minutes;  
  85.                         _second=tempDate.seconds;  
  86.                         _displayDateText.htmlText="<p align='center'><font size='16' color='#ff0000'>日期:"+_year+"年"+_month+"月"+_day+"日  "+"星期"+tempArray[_weekDay]+"</font></p>";  
  87.                         _displayTimeText.htmlText="<p align='center'><font size='16'>时间:"+_hour+":"+_minute+"  "+_second+"</font></p>";  
  88.                 }  
  89.                 private function getDateTime(e:TimerEvent):void{  
  90.                         displayDateTime();  
  91.                 }  
  92.                 private function writeTextField(textWidth,textHeight,textX,textY):TextField{  
  93.                         var tempText=new TextField();  
  94.                         tempText.border=true;  
  95.                         tempText.borderColor=0xffffff;  
  96.                         tempText.background=true;  
  97.                         tempText.backgroundColor=0xcccccc;  
  98.                         tempText.width=textWidth;  
  99.                         tempText.height=textHeight;  
  100.                         tempText.x=textX;  
  101.                         tempText.y=textY;  
  102.                         return tempText;  
  103.                 }  
  104.                 private function setState(color:uint,radius:int,topX:int,topY:int,rightX:int,heightY:int,buttonText:String):Sprite{  
  105.                         var textBtn:TextField=new TextField();  
  106.                         textBtn.x=topX+6;  
  107.                         textBtn.y=topY+2;                          
  108.                         textBtn.width=rightX-10;  
  109.                         textBtn.height=heightY;  
  110.                         var roundRect:Sprite=new Sprite();  
  111.                         roundRect.graphics.lineStyle(1,0x000000);  
  112.                         roundRect.graphics.beginFill(color);  
  113.                         roundRect.graphics.drawRoundRect(topX,topY,rightX,heightY,radius);                          
  114.                         roundRect.graphics.endFill();  
  115.                         roundRect.filters=[new DropShadowFilter(1)];  
  116.                         textBtn.htmlText="<p align='center'><font color='#FFFFFF'>"+buttonText+"</font></p>";  
  117.                         roundRect.addChild(textBtn);  
  118.                         return roundRect  
  119.                 }  
  120.                 private function addCalculator():void{  
  121.                         buttonTextArray=new Array("7","8","9","/","4","5","6","*","1","2","3","-","0","+/-",".","+")  
  122.                         var tempX:uint=50;  
  123.                         var tempY:uint=120;  
  124.                         var temp:uint=1 
  125.                         var buttonArray:Array=new Array();  
  126.                         for(var i:uint=0;i<16;i++){  
  127.                                 if(i%4==0){  
  128.                                         if(i!=0){  
  129.                                         temp++;}  
  130.                                         tempX=50;  
  131.                                         tempY=120+25*temp;  
  132.                                 }  
  133.                                 buttonArray[i]=new SimpleButton();  
  134.                                 buttonArray[i].upState=setState(0x009900,2,tempX,tempY,25,20,buttonTextArray[i]);  
  135.                                 buttonArray[i].overState=setState(0x00CC00,2,tempX,tempY,25,20,buttonTextArray[i]);  
  136.                                 buttonArray[i].downState=setState(0x006600,2,tempX,tempY,25,20,buttonTextArray[i]);  
  137.                                 buttonArray[i].hitTestState=buttonArray[i].upState;  
  138.                                 buttonArray[i].addEventListener(MouseEvent.CLICK,toCalculate(buttonTextArray[i]));  
  139.                                 tempXtempX=tempX+30;  
  140.                                 _bg_mc.addChild(buttonArray[i]);  
  141.                         }  
  142.                         buttonArray[17]=new SimpleButton();  
  143.                         buttonArray[17].upState=setState(0x009900,2,180,180,100,25,"计算结果");  
  144.                         buttonArray[17].overState=setState(0x00CC00,2,180,180,100,25,"计算结果");  
  145.                         buttonArray[17].downState=setState(0x006600,2,180,180,100,25,"计算结果");  
  146.                         buttonArray[17].hitTestState=buttonArray[17].upState;  
  147.                         buttonArray[17].addEventListener(MouseEvent.CLICK,toCalculate("="));  
  148.                         _bg_mc.addChild(buttonArray[17]);  
  149.                         buttonArray[18]=new SimpleButton();  
  150.                         buttonArray[18].upState=setState(0x009900,2,180,210,100,25,"重置");  
  151.                         buttonArray[18].overState=setState(0x00CC00,2,180,210,100,25,"重置");  
  152.                         buttonArray[18].downState=setState(0x006600,2,180,210,100,25,"重置");  
  153.                         buttonArray[18].hitTestState=buttonArray[18].upState;  
  154.                         buttonArray[18].addEventListener(MouseEvent.CLICK,toCalculate("r"));  
  155.                         _bg_mc.addChild(buttonArray[18]);  
  156.                 }  
  157.                  

 

热门文章推荐

请稍候...

保利威视云平台-轻松实现点播直播视频应用

酷播云数据统计分析跨平台播放器