我在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("=");
}
private function calculate(isbacket:Boolean) { //计算数据
if (isbacket == undefined) {
while (getPriority(this.formula[this.formula.length-1])<=getPriority(this.formula[this.formula.length-3])) {
var s2 = this.formula.pop();
var n2 = this.formula.pop();
var s1 = this.formula.pop();
var n1 = this.formula.pop();
this.formula.push(this.evals(n1, s1, n2));
this.formula.push(s2);
}
if (this._system == 10) {
this._value = this.formula[this.formula.length-2];
} else {
this._value = this.parseInt2(this.formula[this.formula.length-2], 10, this._system);
}
} else {
while (this.formula.length>1) {
var n2 = this.formula.pop();
var s = this.formula.pop();
var n1 = this.formula.pop();
this.formula.push(this.evals(n1, s, n2));
}
if (this._system == 10) {
this._value = this.formula[0];
} else {
this._value = this.parseInt2(this.formula[0], 10, this._system);
}
if (isbacket) {
while (this.backets.length>=1) {
this.formula = this.backets.pop();
this.formula.push(this._value);
while (this.formula.length>1) {
var n2 = this.formula.pop();
var s = this.formula.pop();
var n1 = this.formula.pop();
this.formula.push(this.evals(n1, s, n2));
if (this._system == 10) {
this._value = this.formula[0];
} else {
this._value = this.parseInt2(this.formula[0], 10, this._system);
}
}
}
}
this.formula = [];
}
}
private function getPriority(s:String):Number { //计算权值
if (s == "+" || s == "-") {
return 1;
} else if (s == "*" || s == "/" || s == "%") {
return 2;
} else if (s == "^") {
return 3;
} else {
return 0;
}
}
private function evals(n1:String, s:String, n2:String):String { //计算数据
switch (s) {
case "+" :
return String(Number(n1)+Number(n2));
case "-" :
return String(Number(n1)-Number(n2));
case "*" :
return String(Number(n1)*Number(n2));
case "/" :
return String(Number(n1)/Number(n2));
case "%" :
return String(Number(n1)%Number(n2));
case "^" :
return String(Math.pow(Number(n1), Number(n2)));
}
}
private function parseInt2(s:String, from:Number, to:Number):String { //进制转换
if (from == undefined) {
from = this._system;
}
if (to == undefined) {
to = 10;
}
return parseInt(s, from).toString(to).toUpperCase();
} //其他功能
public function percent() { //百分比
if (this._system == 10) {
this.formula.push((Number(_value)/100).toString());
this.isnew = 1;
this.broadMessage("percent");
}
}
public function int() { //取整
if (_system == 10) {
_value = Math.floor(Number(_value)).toString();
this.broadMessage("int");
}
}
public function aint() { //显示小数部分
if (_system == 10) {
_value = (Number(_value)-Math.floor(Number(_value))).toString();
this.broadMessage("aint");
}
}
public function sqrt() { //开方
if (this._system == 10) {
this._value = Math.sqrt(Number(this._value)).toString();
} else {
this._value = Math.sqrt(parseInt(this._value, this._system)).toString(this._system).toUpperCase();
}
this.isnew = 1;
this.broadMessage("sqrt");
}
public function pow3() {
//x^3
if (this._system == 10) {
this._value = Math.pow(Number(this._value), 3).toString().toUpperCase();
} else {
this._value = Math.pow(parseInt(this._value, this._system), 3).toString(this._system).toUpperCase();
}
this.isnew = 1;
this.broadMessage("x^3");
}
public function pow2() {
//x^2
if (this._system == 10) {
this._value = Math.pow(Number(this._value), 2).toString().toUpperCase();
} else {
this._value = Math.pow(parseInt(this._value, this._system), 2).toString(this._system).toUpperCase();
}
this.isnew = 1;
this.broadMessage("x^2");
}
public function reciprocal() {
// 1/x
if (this._system == 10) {
this._value = (1/Number(this._value)).toString().toUpperCase();
}
this.isnew = 1;
this.broadMessage("1/x");
}
public function factorial() { // 阶乘
var num:Number = 1;
var max = parseInt2(this._value);
for (var i = 1; i<=max; i++) {
num *= i;
}
this._value = num.toString(this._system);
this.isnew = 1;
this.broadMessage("n!");
} //三角函数
public function sin() {
if (_system == 10) {
if (units == 1) {
this._value = this.dtr(Number(this._value));
} else if (units == 2) {
this._value = this.dtr(Number(this._value)*9/10);
}
this._value = Math.sin(Number(this._value)).toString();
this.isnew = 1;
this.broadMessage("sin");
}
}
public function cos() {
if (_system == 10) {
if (units == 1) {
this._value = this.dtr(Number(this._value));
} else if (units == 2) {
this._value = this.dtr(Number(this._value)*9/10);
}
this._value = Math.cos(Number(this._value)).toString();
this.isnew = 1;
this.broadMessage("cos");
}
}
public function tan() {
if (_system == 10) {
if (units == 1) {
this._value = this.dtr(Number(this._value));
} else if (units == 2) {
this._value = this.dtr(Number(this._value)*9/10);
}
this._value = Math.tan(Number(this._value)).toString();
this.isnew = 1;
this.broadMessage("tan");
}
} //反三角函数
public function asin() {
if (_system == 10) {
if (units == 1) {
this._value = this.dtr(Number(this._value));
} else if (units == 2) {
this._value = this.dtr(Number(this._value)*9/10);
}
this._value = Math.asin(Number(this._value)).toString();
this.isnew = 1;
this.broadMessage("asin");
}
}
public function acos() {
if (_system == 10) {
if (units == 1) {
this._value = this.dtr(Number(this._value));
} else if (units == 2) {
this._value = this.dtr(Number(this._value)*9/10);
}
this._value = Math.acos(Number(this._value)).toString();
this.isnew = 1;
this.broadMessage("acos");
}
}
public function atan() {
if (_system == 10) {
if (units == 1) {
this._value = this.dtr(Number(this._value));
} else if (units == 2) {
this._value = this.dtr(Number(this._value)*9/10);
}
this._value = Math.atan(Number(this._value)).toString();
this.isnew = 1;
this.broadMessage("atan");
}
} //角度转化为弧度
private function dtr(angle:Number):String {
return String(angle*(Math.PI/180));
} //弧度转化为角度
private function rtd(angle:Number):String {
return String(angle*(180/Math.PI));
}
public function exp() { //计算e的x次方
if (this._system == 10) {
this._value = Math.exp(Number(this._value)).toString();
} else {
this._value = Math.exp(parseInt(this._value, this._system)).toString(this._system);
}
this.broadMessage("exp");
}
public function Exp() { //计算10的x次方
if (_system == 10) {
this._value = Math.pow(10, Number(this._value)).toString();
} else {
this._value = Math.pow(10, Number(parseInt2(this._value, this._system, 10))).toString(_system);
}
this.broadMessage("Exp");
}
public function ln() { //计算自然对数
if (this._system == 10) {
this._value = Math.log(Number(this._value)).toString();
} else {
this._value = Math.log(parseInt(this._value, this._system)).toString(this._system);
}
this.broadMessage("ln");
}
public function log() { //计算常用对数
if (this._system == 10) {
this._value = (Math.log(Number(this._value))/Math.LN10).toString();
} else {
this._value = (Math.log(parseInt(this._value, this._system))/Math.LN10).toString(this._system);
}
this.broadMessage("log");
}
public function PI() { //显示PI
if (_system == 10) {
this._value = String(Math.PI);
this.isnew = 1;
this.broadMessage("PI");
}
}
public function E() { //显示E
if (_system == 10) {
this._value = String(Math.E);
this.isnew = 1;
this.broadMessage("E");
}
}
public function dms() { //将数字化为度-分-秒
/*if (this._system == 10) {
var st = this._value.indexOf(".");
if (st != -1) {
var ss = (Number(this._value.substr(st+1))*60);
if (ss.length>2) {
ss = ss.substr(0, 2)+(Number(ss.substr(2))*60).toString();
}
this._value = this._value.slice(0, st)+"."+ss;
this.broadMessage("dms");
}
}*/
}
public function adms() { //将数字化为度
/*if (_system == 10) {
if ((st=_value.indexOf(".")) != -1) {
var s:Number = 0;
if (_value.length>=st+4) {
s = Number(_value.substr(st+3))/60;
while (s>10) {
s /= 10;
}
}
var f:Number = (Number(_value.substr(st+1, 2))+s)/60;
_value = (Math.floor(Number(_value))+f).toString();
//_value=Rtrim(_value,"0")
this.broadMessage("adms");
}
}*/
}
public function ChangetoE(s:String) {
return s; //化成科学记数法
/*if (n>=1) {
if (String(n).length<16) {
var nz = Math.floor(n);
var nx = String(n).slice(String(nz).length+1);
nx = Rtrim(nx, "0");
var ns = String(nz).split("");
var nsp = ns[0]+".";
for (var i = 1; i
if (nx == 0) {
if (ns[i] == 0) {
var del = true;
for (var j = i; j
if (!ns[i] == 0) {
del = false;
break;
}
}
if (del) {
break;
}
} else {
nsp += ns[i];
}
} else {
nsp += ns[i];
}
}
nsp = String(nsp);
nsp = Rtrim(nsp, "0");
if (nx == "") {
if (String(nsp).substr(-1, 1) == ".") {
nsp = String(nsp).slice(0, -1);
}
} else {
nx = String(nx);
nsp += Rtrim(nx, "0");
}
return String(nsp+"e+"+(ns.length-1));
} else {
return n;
}
} else {
if (n == 0) {
return "0e+0";
} else if (n>0.00001) {
var ns = String(n).split("");
var nt = 0;
for (var i = 2; i
if (Number(ns[i]) == 0) {
nt++;
} else {
break;
}
}
var nz = String(n).substr(nt+2, 1);
var nx = String(n).substr(nt+3);
if (nx != "") {
return String(nz+"."+nx+"e-"+(nt+1));
} else {
return String(nz+"e-"+(nt+1));
}
} else {
return n;
}
}*/
}
public function ChangetoF(s:String) {
return s; //化成普通数
/*var ns = String(n);
if (ns.indexOf("e") == -1) {
return n;
} else {
var nss = ns.split("e");
var nz = nss[0];
var nx = nss[1];
if (nz.indexOf(".") == -1) {
for (var i = 0; i
nz += "0";
}
} else {
var nzs = nz.split(".");
var nzz = nzs[0];
if (nzs[1].length<=Number(nx)) {
for (var i = 0; i<(Number(nx)-Number(nzs[1])); i++) {
nzz += "0";
}
nz = nzz;
}
return String(nz);
}
}*/
}
private function Rtrim(n:String, d:String) {
//ChangetoF和ChangetoE函数支持,删除字符串右边的指定字符
/*if (d == undefined) {
d = " ";
}
if (n.indexOf(d) != -1) {
var ss = n.split("");
var ssl = ss.length;
for (var i = ssl; i>=0; i--) {
if (ss[i] == d) {
if (i+1 == ss.length) {
ss.pop();
}
}
}
return ss.join("");
} else {
return n;
}*/
}
public function set group(b:Boolean) {
this.isgroup = b;
this.broadcastMessage("onChange", this, "GroupChange");
}
public function get group():Boolean {
return this.isgroup;
}
public function set FtoE(b:Boolean) {
this._isE = b;
this.broadcastMessage("onChange", this, "FtoEChange");
}
public function get FtoE():Boolean {
return this._isE;
}
public function get value():String {
return this._value;
}
public function get result():String { //数字输出
var re = this._value;
if (this._isE) {
if (this._system == 10) {
re = ChangetoE(re);
}
} else {
re = ChangetoF(re);
if (this.isgroup) {
if (_system == 10) {
var s = ",";
var n:Number = 3;
} else {
var s = " ";
var n:Number = 4;
}
if (re != "0") {
if (re.indexOf(".") != -1) {
var num = re.indexOf(".");
} else {
var num = re.length;
}
var i = num-n;
while (i>0) {
if (re.slice(0, i) != "-") {
re = re.slice(0, i).concat(s, re.slice(i));
}
i -= n;
}
}
}
}
return re;
} //数据暂存
public function MC(index:Number) { // 清除存储区的数字
if (index == undefined) {
this._memory = [];
} else {
this._memory.splice(index, 1);
}
this.isnew = 1;
this.broadMessage("MC");
}
public function MR(index:Number) { // 输出存储区的所有数字
if (_memory == undefined) {
_value = "0";
} else {
if (index == undefined) {
index = 0;
}
this._value = this._memory[index].toString(this._system);
}
isnew = 1;
this.broadMessage("MR");
}
public function MS(index:Number) { // 将数字储存在存储区
if (_value != "0") {
if (index == undefined) {
index = 0;
}
if (_system == 10) {
this._memory[index] = Number(this._value);
} else {
this._memory[index] = parseInt(this._value, this._system);
}
isnew = 1;
this.broadMessage("MS");
}
}
public function MP(index:Number) { // 将存储区的数字与以有数字相加
if (_value != "0") {
if (index == undefined) {
index = 0;
}
if (_system == 10) {
this._memory[index] += Number(this._value);
} else {
this._memory[index] += parseInt(this._value, this._system);
}
isnew = 1;
this.broadMessage("M+");
}
}
public function get memory():Array {
return this._memory;
}
public function get hasMemory():Boolean {
return (this._memory.length != 0);
}
public function set system(n:Number) {
if (n>=2 && n<=36) {
this._system = n;
this.broadcastMessage("onChange", this, "SystemChange");
}
}
public function get system():Number {
return this._system;
}
public function set unit(n:Number) {
if (n == 1) {
this.units = 1;
} else if (n == 0) {
this.units = 0;
} else if (n == 2) {
this.units = 2;
}
this.broadcastMessage("onChange", this, "UnitChange");
}
public function get unit():Number {
return this.units;
} //统计计算
public function Sta() { //进入,退出统计模式
if (data == undefined) {
this.data = [];
} else {
this.data = undefined;
}
this.datam = {Ave:undefined, Ave2:undefined, Sum:undefined, Sum2:undefined, S:undefined, S2:undefined};
this.isnew = 1;
this.broadMessage("Sta");
}
private function getSum() { //求数据和
//Sum=x1+x2+x3+...+xn
var num:Number = 0;
for (var i in this.data) {
num += this.data[i];
}
this.datam.Sum = num;
}
private function getSum2() { //求数据平方和
//Sum=x1*x1+x2*x2+x3*x3+...+xn*xn
var num:Number = 0;
for (var i in this.data) {
num += this.data[i]*this.data[i];
}
this.datam.Sum2 = num;
}
public function Ave() { //求平均值
//Ave=(x1+x2+x3+...+xn)/xn
if (data != undefined) {
if (this.datam.Ave != undefined) {
this._value = this.datam.Ave.toString(this._system);
} else {
if (this.datam.Sum == undefined) {
this.getSum();
}
this.datam.Ave = this.datam.Sum/this.data.length;
this._value = this.datam.Ave.toString(this._system);
}
this.isnew = 1;
this.broadMessage("Ave");
}
}
public function Ave2() { //求均方值
//Ave2=Sum2/n
if (this.data != undefined) {
if (this.datam.Ave2 != undefined) {
this._value = this.datam.Ave2.toString(this._system);
} else {
if (this.datam.Sum2 == undefined) {
this.getSum2();
}
this.datam.Ave2 = this.datam.Sum2/this.data.length;
this._value = this.datam.Ave2.toString(this._system);
}
this.isnew = 1;
this.broadMessage("Ave2");
}
}
public function Sum() { //求数据和
//Sum=x1+x2+x3+...+xn
if (data != undefined) {
if (this.datam.Sum != undefined) {
this._value = this.datam.Sum.toString(this._system);
} else {
this.getSum();
this._value = this.datam.Sum.toString(this._system);
}
this.isnew = 1;
this.broadMessage("Sum");
}
}
public function Sum2() { //求数据平方和
//Sum2=x1*x1+x2*x2+x3*x3+...+xn*xn
if (data != undefined) {
if (this.datam.Sum2 != undefined) {
this._value = this.datam.Sum2.toString(this._system);
} else {
this.getSum2();
this._value = this.datam.Sum2.toString(this._system);
}
this.isnew = 1;
this.broadMessage("Sum2");
}
}
public function S() { //求总体参数为n的标准差
//S=Math.sqrt((Sum2-n*Ave*Ave)/n)
if (data != undefined) {
if (this.datam.S != undefined) {
this._value = this.datam.S.toString(this._system);
} else {
if (this.datam.Ave == undefined) {
//计算Ave
if (this.datam.Sum == undefined) {
this.getSum();
}
this.datam.Ave = this.datam.Sum/this.data.length;
}
//计算Sum2
if (datam.Sum2 == undefined) {
this.getSum2();
}
//计算S
this.datam.S = Math.sqrt((this.datam.Sum2-this.data.length*this.datam.Ave*this.datam.Ave)/this.data.length);
this._value = this.datam.S.toString(this._system);
}
this.isnew = 1;
this.broadMessage("S");
}
}
public function S2() { //求总体参数为n-1的标准差
//S=Math.sqrt((Sum2-n*Ave*Ave)/(n-1))
if (data != undefined) {
if (this.datam.S2 != undefined) {
this._value = this.datam.S2.toString(this._system);
} else {
if (this.datam.Ave == undefined) {
//计算Ave
if (this.datam.Sum == undefined) {
this.getSum();
}
this.datam.Ave = this.datam.Sum/this.data.length;
}
//计算Sum2
if (datam.Sum2 == undefined) {
this.getSum2();
}
//计算S
this.datam.S2 = Math.sqrt((this.datam.Sum2-this.data.length*this.datam.Ave*this.datam.Ave)/(this.data.length-1));
this._value = this.datam.S2.toString(this._system);
}
this.isnew = 1;
this.broadMessage("S2");
}
}
public function Data() { //添加数据
if (this.data != undefined) {
if (this._system == 10) {
this.data.push(Number(_value));
} else {
this.data.push(parseInt(_value, _system));
}
this.datam = {Ave:undefined, Ave2:undefined, Sum:undefined, Sum2:undefined, S:undefined, S2:undefined};
this.isnew = 1;
this.broadMessage("Data");
}
}
public function load(n:Number) { //将数据显示在显示框中
if (this.data != undefined) {
this._value = this.data[n].toString(this._system);
this.isnew = 1;
this.broadMessage("load");
}
}
public function clear(n:Number) {
//清除数据 if (this.data != undefined) {
if (n == undefined) {
this.data.splice(n, 1);
} else {
this.data = [];
}
this.isnew = 1;
this.broadMessage("clear");
}
}
public function get hasData():Boolean {
return (this.data == undefined);
}
public function get n():Number {
return this.data.length;
}
}
Word教程网 | Excel教程网 | Dreamweaver教程网 | Fireworks教程网 | PPT教程网 | FLASH教程网 | PS教程网 |
HTML教程网 | DIV CSS教程网 | FLASH AS教程网 | ACCESS教程网 | SQL SERVER教程网 | C语言教程网 | JAVASCRIPT教程网 |
ASP教程网 | ASP.NET教程网 | CorelDraw教程网 |