您所在的位置:QQ首页 > 动画频道 > 高级应用> 正文

Calculator类 模拟科学计算器函数
http://flash.QQ.com   2006年 08月 29日 11:15   闪吧  
第 1 2 3 4

下一个更精彩:AS3-通过Stage类动态改变影片帧速

我在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<=10) {

if (nc>=48 && nc<=47+this._system) {

this._value += n;

this.broadMessage(n);

}

} else if (this._system>10) {

if ((nc>=48 && nc<=57) || (nc>=65 && nc<=54+this._system)) {

this._value += n;

this.broadMessage(n);

}

}

}

public function dot() { //小数点

if (this._system == 10) {

if (this.isnew) {

_value = "0";

}

this.isnew = 0;

if (this._value.indexOf(".") == -1) {

this._value += ".";

}

this.broadMessage(".");

}

}

public function minuss() { //正负号

if (this._system == 10) {

if (this.isnew) {

this._value = "";

}

this.isnew = 0;

if (this._value.indexOf("-") == -1) {

this._value = "-"+this._value;

} else {

this._value = this._value.substr(1);

}

this.broadMessage("+/-");

}

}

public function operation(s:String) { //输入运算符

if (s == "+" || s == "-" || s == "*" || s == "/" || s == "%" || s == "^") {

if (this.isnew == 2) {

if (this.formula.length != 0) {

this.formula[this.formula.length-1] = s;

this.calculate();

}

} else {

if (this._system == 10) {

this.formula.push(this._value);

} else {

this.formula.push(this.parseInt2(this._value));

}

this.formula.push(s);

this.calculate();

}

this.lastData = {sign:s, value:"0"};

this.isnew = 2;

this.broadMessage(s);

}

}

public function leftBracket() { //左括弧

if (this.isnew != 3) {

if (this.isnew != 2) {

if (this._value != "0") {

this.formula.push(this._value);

}

this.formula.push("*");

}

this.backets.push(this.formula);

}

this.formula = [];

this._value = "0";

this.lastData = {sign:"+", value:"0"};

this.isnew = 2;

this.broadMessage("(");

}

public function rightBracket() { //右括弧

if (this.isnew == 2) {

this.formula.pop();

} else if (this.isnew != 3) {

this.formula.push(this._value);

}

this.calculate(false);

if (this.backets.length != 0) {

this.formula = this.backets.pop();

this.formula.push(this._value);

this.formula.push("*");

}

this.lastData = {sign:"+", value:"0"};

this.isnew = 2;

this.broadMessage(")");

}

public function get hasBacket():Boolean {

return (this.backets.length != 0);

}

public function get backetNum():Number {

return this.backets.length;

}

public function equal() { // =

if (this.lastData.value != "0") {

if (this._system == 10) {

this.formula.push(this._value);

} else {

this.formula.push(this.parseInt2(this._value));

}

this.formula.push(this.lastData.sign);

this.formula.push(this.lastData.value);

this.calculate(false);

} else {

this.lastData.value = this._value;

if (this.isnew == 2) {

if (this.formula.length != 0) {

this.formula.pop();

this.calculate(true);

}

} else {

if (this._system == 10) {

this.formula.push(this._value);

} else {

this.formula.push(this.parseInt2(this._value));

}

this.calculate(true);

}

}

this.isnew = 3;

this.broadMessage("=");

}

下一页
第 [1] [2] [3] [4]
免费订阅】【发表评论】【动画论坛】【  】【关闭
发表评论
 QQ号码:
 QQ密码:
 验证码: 匿名发表
* 请各位网友遵纪守法并注意语言文明。
*《互联网电子公告服务管理规定》
*《全国人大常委会关于维护互联网安全的规定》




关于腾讯 | About Tencent | 服务条款 | 广告服务 | 腾讯招聘 | 腾讯公益 | 客服中心 | 网站导航
Copyright © 1998 - 2008 Tencent Inc. All Rights Reserved
腾讯公司 版权所有