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

Calculator类 模拟科学计算器函数
http://flash.QQ.com   2006年 08月 29日 11:15   闪吧  
1 2 3 4
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");

}

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




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