通行证│用户名: 密码: 验证码: 验证码,看不清楚?请点击刷新验证码 电信网通铁通移动   在线
资源搜索:
热门搜索:Linux VB C语言 PhotoShop Flash TCP/IP
   首页 | 文章 | 软件 | 动画 | 资源 | 励志 | 骗术 | 论坛 | 邮箱 | 会员中心 | 军事 | 科技 | 博客 | 图片 | 商城 | 最新更新 | 800g资源 | 爱心黑客
您现在的位置: 爱国者黑客 >> 资源 >> 媒体动画 >> Flash >> Action Script >> 文章正文
用as设计计算器,按照运算法则运算
责任编辑:admin   更新日期:2005-8-6


这是我做的一个,模仿Windows自带的计算器,支持快捷键,进制转换。尤其是按运算法则运算(到现在,我也没见过这么好的Flash计算器,支持一下啦!

我的计算器类:

dynamic class Counter {
//输出的文字,是格式化了的,例如分组,科学记数法...
var output:String = "0";
//计算的文字,纯数字
var value:String = "0";
//是否分组
var grouping:Boolean = false;
//监听器用
var _listeners:Array = [];
//统计用
var data:Array = undefined;
var datas:Object = {Ave:undefined, Ave2:undefined, Sum:undefined, Sum2:undefined, S:undefined, S2:undefined};
//算式储存
private var count:Array = [];
//是否重新输入数字
private var isnew:Boolean = false;
//储存数字
private var remember:Number = undefined;
//暂时数字储存
private var nrem:String;
//暂时运算符储存
private var nsign:String = undefined;
//数制
private var systems:Number = 10;
//单位
private var units:Number = 0;
//括号中的
private var inbracket:Array = [];
//构造函数
function Counter(key:Boolean) {
if (key == undefined) {
key = true;
}
if (key) {
Key.addListener(this);
}
}
//清除键
function C() {
//清除所有数据
value = "0";
count = [];
isnew = true;
nsign = undefined;
CounterMessage("C");
}
//清空键
function CE() {
value = "0";
isnew = true;
CounterMessage("CE");
}
//BackSpace
function BackSpace() {
value = value.slice(0, -1);
if (value.length == 0) {
value = "0";
}
CounterMessage("BackSpace");
}
//输入数字
function numbers(n) {
n = n.toString().toUpperCase();
if (isnew) {
value = "0";
}
isnew = false;
//判断输入的数字是否在数制范围内
if (systems<=10) {
if (n.charCodeAt(0)<=47+systems) {
if (value != "0") {
value += n;
} else {
value = n;
}
CounterMessage(n);
}
} else if (systems>10 && systems<=36) {
if (n.charCodeAt(0)<=54+systems) {
if (value != "0") {
value += n;
} else {
value = n;
}
CounterMessage(n);
}
}
}
//小数点
function dot() {
if (systems == 10) {
if (isnew) {
value = output="0";
}
isnew = false;
if (value.indexOf(".") == -1) {
value += ".";
}
CounterMessage(".");
}
}
//正负号
function minuss() {
if (systems == 10) {
if (isnew) {
value = "0";
}
isnew = false;
if (value != "0") {
if (value.indexOf("-") == -1) {
value = "-"+value;
} else {
value = value.substr(1);
}
}
CounterMessage("+/-");
}
}
//+
function plus() {
duseless();
if (count[count.length-1] == "+" || count[count.length-1] == "-" || count[count.length-1] == "%") {
if (systems == 10) {
count.push(value);
} else {
count.push(parseInt(value, systems).toString());
}
} else if (count.length == 0) {
if (systems == 10) {
count.push(value);
} else {
count.push(parseInt(value, systems).toString());
}
} else {
//if(count[count.length-1].substr(-1,1)=="*"||count[count.length-1].substr(-1,1)=="/"){
//count[count.length-1]=count[count.length-1].slice(0,-1)
//}
if (systems == 10) {
count[count.length-1] += value;
} else {
count[count.length-1] += parseInt(value, systems).toString();
}
}
count.push("+");
nsign = "+";
value = calculate();
isnew = true;
CounterMessage("+");
}
//-
function minus() {
duseless();
if (count[count.length-1] == "+" || count[count.length-1] == "-" || count[count.length-1] == "%") {
if (systems == 10) {
count.push(value);
} else {
count.push(parseInt(value, systems).toString());
}
} else if (count.length == 0) {
if (systems == 10) {
count.push(value);
} else {
count.push(parseInt(value, systems).toString());
}
} else {
//if(count[count.length-1].substr(-1,1)=="*"||count[count.length-1].substr(-1,1)=="/"){
//count[count.length-1]=count[count.length-1].slice(0,-1)
//}
if (systems == 10) {
count[count.length-1] += value;
} else {
count[count.length-1] += parseInt(value, systems).toString();
}
}
count.push("-");
nsign = "-";
value = calculate();
isnew = true;
CounterMessage("-");
}
//*
function multiply() {
duseless();
if (count[count.length-1] == "+" || count[count.length-1] == "-" || count[count.length-1] == "%") {
if (systems == 10) {
count.push(value);
} else {
count.push(parseInt(value, systems).toString());
}
} else if (count.length == 0) {
if (systems == 10) {
count.push(value);
} else {
count.push(parseInt(value, systems).toString());
}
} else {
//if(count[count.length-1].substr(-1,1)=="*"||count[count.length-1].substr(-1,1)=="/"){
//count[count.length-1]=count[count.length-1].slice(0,-1)
//}
if (systems == 10) {
count[count.length-1] += value;
} else {
count[count.length-1] += (parseInt(value, systems).toString());
}
}
count[count.length-1] += "*";
nsign = "*";
value = calculate();
isnew = true;
CounterMessage("*");
}
// /
function except() {
duseless();
if (count[count.length-1] == "+" || count[count.length-1] == "-" || count[count.length-1] == "%") {
if (systems == 10) {
count.push(value);
} else {
count.push(parseInt(value, systems).toString());
}
} else if (count.length == 0) {
if (systems == 10) {
count.push(value);
} else {
count.push(parseInt(value, systems).toString());
}
} else {
//if(count[count.length-1].substr(-1,1)=="*"||count[count.length-1].substr(-1,1)=="/"){
//count[count.length-1]=count[count.length-1].slice(0,-1)
//}
if (systems == 10) {
count[count.length-1] += value;
} else {
count[count.length-1] += (parseInt(value, systems).toString());
}
}
count[count.length-1] += "/";
nsign = "/";
value = calculate();
isnew = true;
CounterMessage("/");
}
// =
function equal() {
duseless();
if (count[count.length-1] == "+" || count[count.length-1] == "-" || count[count.length-1] == "%") {
if (systems == 10) {
count.push(value);
} else {
count.push(parseInt(value, systems).toString());
}
nrem = value;
} else if (count.length == 0) {
if (systems == 10) {
count.push(value);
} else {
count.push(parseInt(value, systems).toString());
}
nrem = value;
} else {
if (count[count.length-1].substr(-1, 1) == "*" || count[count.length-1].substr(-1, 1) == "/" || count[count.length-1].substr(-1, 1) == "^") {
if (systems == 10) {
count[count.length-1] += value;
} else {
count[count.length-1] += parseInt(value, systems).toString();
}
nrem = value;
} else {
//if (count[count.length-1] == "+" || count[count.length-1] == "-" || count[count.length-1] == "*" || count[count.length-1] == "/" || count[count.length-1] == "^") {
//count.pop();
//}
if (nsign == "+") {
count.push("+");
count.push(parseInt2(nrem, systems, 10));
} else if (nsign == "-") {
count.push("-");
count.push(parseInt2(nrem, systems, 10));
} else if (nsign == "%") {
count.push("%");
count.push(parseInt2(nrem, systems, 10));
} else if (nsign == "*") {
count[count.length-1] += ("*"+parseInt2(nrem, systems, 10));
} else if (nsign == "+") {
count[count.length-1] += ("/"+parseInt2(nrem, systems, 10));
} else if (nsign == "^") {
count[count.length-1] += ("^"+parseInt2(nrem, systems, 10));
}
}
}
value = calculate();
isnew = true;
CounterMessage("=");
}
//sqrt
function sqrt() {
duseless();
if (systems == 10) {
value = Math.sqrt(Number(value)).toString();
} else {
value = Math.sqrt(parseInt(value, systems)).toString(systems).toUpperCase();
}
isnew = true;
CounterMessage("sqrt");
}
//取模
function Mod() {
duseless();
if (count[count.length-1] == "+" || count[count.length-1] == "-" || count[count.length-1] == "%") {
if (systems == 10) {
count.push(value);
} else {
count.push(parseInt(value, systems).toString());
}
} else if (count.length == 0) {
if (systems == 10) {
count.push(value);
} else {
count.push(parseInt(value, systems).toString());
}
} else {
//if(count[count.length-1].substr(-1,1)=="*"||count[count.length-1].substr(-1,1)=="/"){
//count[count.length-1]=count[count.length-1].slice(0,-1)
//}
if (systems == 10) {
count[count.length-1] += value;
} else {
count[count.length-1] += parseInt(value, systems).toString();
}
}
count.push("%");
nsign = "%";
value = calculate();
isnew = true;
CounterMessage("Mod");
}
//x^y
function pow() {
duseless();
if (count[count.length-1] == "+" || count[count.length-1] == "-" || count[count.length-1] == "%") {
if (systems == 10) {
count.push(value);
} else {
count.push(parseInt(value, systems).toString());
}
} else if (count.length == 0) {
if (systems == 10) {
count.push(value);
} else {
count.push(parseInt(value, systems).toString());
}
} else {
if (systems == 10) {
count[count.length-1] += value;
} else {
count[count.length-1] += (parseInt(value, systems).toString());
}
}
count[count.length-1] += "^";
nsign = "^";
value = calculate();
isnew = true;
CounterMessage("x^y");
}
//x^3
function pow3() {
duseless();
if (systems == 10) {
value =

[1] [2] [3] [4] 下一页

  • 上一篇文章:
  • 下一篇文章:
  • 热门文章
    Olldbg常见问题
    汇编语言的艺术(组合语言的艺术)--观
    汇编语言的艺术(组合语言的艺术)--准
    汇编语言的艺术(组合语言的艺术)--基
    汇编语言的艺术(组合语言的艺术)--基
    汇编语言---程式设计 (4)
    虚拟8086模式
    SYS命令使用说明
    javascript + CSS 实现动态菜单显
    推荐文章
    自制Windows XP SP2自动安装光盘
    SQLServer注入工具改进版 v1.02
    使用photoshop CS进行自然美肤
    Photoshop绘制诺基亚手机
    PHOTOSHOP制作秋日之梦
    PHOTOSHOP鼠绘名模王爱萍
    Photoshop制作晶莹飞溅的水珠
    教你用PHOTOSHOP做放大镜
    鼠绘美女及服装修画全过程