home *** CD-ROM | disk | FTP | other *** search
- function initCalc()
- {
- binpressed = false;
- state = 1;
- tDisp = "0";
- pendingOp = "";
- stack = 0;
- current = "0";
- decimal = 0;
- sign = 0;
- maxD = 99999999;
- minD = 1e-7;
- }
- function buttonPressed(k)
- {
- var _loc1_ = k;
- if(!locked)
- {
- if(_loc1_ >= "0" && _loc1_ <= "9")
- {
- digitKey(_loc1_);
- }
- else if(_loc1_ == ".")
- {
- decimalPoint();
- }
- else if(_loc1_ == "neg")
- {
- negate();
- }
- else if("×/-+".indexOf(_loc1_,0) > -1)
- {
- binaryOp(_loc1_);
- }
- else if("x^2sqrt".indexOf(_loc1_,0) > -1)
- {
- unaryOp(_loc1_);
- }
- else if(_loc1_.indexOf("M",0) > -1)
- {
- memoryOp(_loc1_);
- }
- else if(_loc1_ == "=")
- {
- equals();
- }
- else if(_loc1_ == "AC")
- {
- allClear();
- }
- else
- {
- setError("Key? " + _loc1_);
- }
- checkTargets();
- }
- else
- {
- tDisp = "-= travado =-";
- }
- }
- function setError(errStr)
- {
- state = 2;
- tDisp = errStr;
- stack = 0;
- decimal = false;
- }
- function digitKey(k)
- {
- var _loc1_ = k;
- if(state == 0)
- {
- binpressed = false;
- if(current == "0")
- {
- current = _loc1_;
- }
- else if(current.length < 8 + decimal + sign)
- {
- current = current.concat(_loc1_);
- tDisp = current;
- }
- }
- else if(state == 1)
- {
- current = _loc1_;
- state = 0;
- tDisp = _loc1_;
- binpressed = false;
- }
- }
- function allClear()
- {
- initCalc();
- }
- function negate()
- {
- if(state < 2 && current.charAt(0) != "-")
- {
- current = "-".concat(current);
- sign = 1;
- tDisp = current;
- binpressed = false;
- }
- else if(state < 2 && current.charAt(0) == "-")
- {
- current = current.substr(1);
- sign = 0;
- tDisp = current;
- binpressed = false;
- }
- }
- function decimalPoint()
- {
- if(state == 0 && current.length < 8 + sign && decimal == 0)
- {
- current = current.concat(".");
- decimal = 1;
- tDisp = current;
- binpressed = false;
- }
- else if(state == 1)
- {
- current = "0.";
- decimal = 1;
- state = 0;
- tDisp = current;
- binpressed = false;
- }
- }
- function unaryOp(k)
- {
- if(state < 2)
- {
- binpressed = false;
- if(k == "sqrt")
- {
- current = Math.sqrt(current);
- setResult(current);
- }
- if(k == "x^2")
- {
- current *= current;
- setResult(current);
- }
- }
- }
- function binaryOp(k)
- {
- if(state < 2)
- {
- state = 1;
- }
- if(pendingOp != "" && state < 2 && !binpressed)
- {
- if(pendingOp == "+")
- {
- current = Number(stack) + Number(current);
- setResult(current);
- }
- else if(pendingOp == "-")
- {
- current = stack - Number(current);
- setResult(current);
- }
- else if(pendingOp == "×")
- {
- current = stack * Number(current);
- setResult(current);
- }
- else if(pendingOp == "/")
- {
- current = stack / Number(current);
- setResult(current);
- }
- }
- if(state < 2)
- {
- if(!binpressed)
- {
- pendingOp = k;
- stack = current;
- state = 1;
- binpressed = true;
- }
- else
- {
- pendingOp = k;
- }
- }
- }
- function equals()
- {
- if(pendingOp != "" && state < 2)
- {
- if(pendingOp == "+")
- {
- current = Number(stack) + Number(current);
- setResult(current);
- }
- else if(pendingOp == "-")
- {
- current = stack - Number(current);
- setResult(current);
- }
- else if(pendingOp == "×")
- {
- current = stack * Number(current);
- setResult(current);
- }
- else if(pendingOp == "/")
- {
- current = stack / Number(current);
- setResult(current);
- }
- pendingOp = "";
- state = 1;
- }
- else if(state < 2)
- {
- state = 1;
- }
- }
- function memoryOp(k)
- {
- var _loc1_ = k;
- if(state < 2)
- {
- if(_loc1_ == "Min")
- {
- mem = Number(current);
- state = 1;
- }
- else if(_loc1_ == "M+")
- {
- mem += Number(current);
- state = 1;
- checkMem();
- }
- else if(_loc1_ == "M-")
- {
- mem -= Number(current);
- state = 1;
- checkMem();
- }
- else if(_loc1_ == "MR")
- {
- current = mem;
- setResult(current);
- state = 1;
- }
- else if(_loc1_ == "MC")
- {
- mem = 0;
- }
- }
- if(mem == 0)
- {
- memindicator.gotoAndStop(1);
- }
- else
- {
- memindicator.gotoAndStop(2);
- }
- }
- function setResult(x)
- {
- var _loc1_ = x;
- if(isNan(_loc1_))
- {
- setError("Err: Illegal");
- }
- else if(Math.abs(_loc1_) > maxD)
- {
- setError("Err: Too big");
- }
- else if(Math.abs(_loc1_) < minD)
- {
- current = 0;
- tDisp = "0";
- state = 1;
- }
- else
- {
- var s = Math.abs(_loc1_) != _loc1_ ? 1 : 0;
- var d = _loc1_.indexOf(".",0) <= -1 ? 0 : 1;
- var _loc2_ = String(_loc1_);
- var _loc3_ = Math.round(Math.log(_loc1_) / 2.302585092994046);
- _loc3_ = Math.max(_loc3_,0);
- if(_loc2_.length < 9 + d + s)
- {
- current = _loc1_;
- tDisp = _loc2_;
- state = 1;
- }
- else
- {
- var unit = Math.pow(10,8 - Math.abs(_loc3_));
- _loc2_ = Math.round(_loc1_ * unit) / unit;
- current = _loc1_;
- tDisp = _loc2_;
- state = 1;
- }
- }
- }
- function checkMem()
- {
- if(isNan(mem))
- {
- setError("Err: Bad Mem");
- }
- else if(Math.abs(mem) > maxD)
- {
- setError("Mem too big");
- }
- else if(Math.abs(mem) < minD)
- {
- mem = 0;
- }
- memindicator.gotoAndStop(0);
- }
- function checkTargets()
- {
- i = 0;
- while(i < 8)
- {
- if(_root.targets[i] == Number(tDisp))
- {
- eval("_parent.target" + i).gotoAndStop(2);
- _root.done[i] = 0;
- }
- i++;
- }
- }
- stop();
- mem = 0;
- memindicator.gotoAndStop(1);
- initCalc();
- tDisp = "=Calculadora=";
-