当前位置: 代码迷 >> Web前端 >> Calculator 计算器种
  详细解决方案

Calculator 计算器种

热度:215   发布时间:2012-11-22 00:16:41.0
Calculator 计算器类

我在FormulaPaster的基础上,写了Calculator 计算器类,提供了模拟一个科学计算器所需的函数.
/**
?*?Calculator?计算器类
?*?Verson?1.0
?*?2006.8.20
?*?Copyright?CYJB
*///
/*
?* 模拟科学计算器的功能
?* 1.数据输入及计算(加(+),减(-),乘(*),除(/),乘(^),取模(%)).
?* 2.支持正负数.
?* 3.支持括号.
?* 4.支持计算函数(平方,开方,幂运算,对数,三角函数).
?* 5.支持科学记数法的转换
?* 6.支持进制转换(2~36)
?* 7.支持数字分组
?* 8.支持数据暂存
?* 9.支持统计计算
?* 10.支持角度(degree),弧度(radian),梯度(grads)转换
*/
import?mx.transitions.BroadcasterMX;
class?Calculator?{
?private?var?_value:String?=?"0";
?private?var?isgroup:Boolean?=?false;
?private?var?data:Array;
?private?var?datam:Object;
?private?var?formula?=?[];
?private?var?isnew:Number?=?3;
?private?var?_memory:Array;
?private?var?_system:Number?=?10;
?private?var?units:Number?=?0;
?private?var?backets:Array;
?private?var?lastData:Object;
?private?var?_isE:Boolean?=?false;
?static?var?__initBroadcaster?=?BroadcasterMX.initialize(Calculator.prototype,?true);
?public?var?addListener:Function;
?public?var?removeListener:Function;
?public?var?broadcastMessage:Function;
?public?var?_listeners:Array;?//事件监听
?//构造函数
?
public?function?Calculator()?{
??this.backets?=?[];
??this._memory?=?[];
??this.lastData?=?{sign:"+",?value:0};
??this._listeners?=?[];
??this.addListener(this);
?}
?private?function?broadMessage(s:String)?{
??this.broadcastMessage("onKeyDown",?this,?s);
??this.broadcastMessage("onChange",?this,?"KeyChange");
?}?//基本功能
?public?function?C()?{??//清除键
??
this._value?=?"0";
??this.formula?=?[];
??this.backets?=?[];
??this.isnew?=?3;
??this.lastData?=?{sign:"+",?value:0};
??this.broadMessage("C");
?}
?public?function?CE()?{??//清空键
?
?this._value?=?"0";
??this.isnew?=?3;
??this.broadMessage("CE");
?}
?public?function?BackSpace()?{
??//BackSpace
??if?(this.isnew?==?0)?{
???this._value?=?this._value.slice(0,?-1);
???if?(this._value.length?==?0)?{
????this._value?=?"0";
???}
???this.broadMessage("BackSpace");
??}
?}
?public?function?numbers(n:String)?{
??//输入数字
n?=?n.toUpperCase();
??if?(this.isnew)?{
???this._value?=?"";
??}
??this.isnew?=?0;
??//判断输入的数字是否在数制范围内
??
var?nc?=?n.charCodeAt(0);
??if?(this._system=48?&&?nc10)?{
???if?((nc>=48?&&?nc=65?&&?nc
本文转自:http://www.5uflash.com/flashjiaocheng/Flashaschengxu/271.html