home *** CD-ROM | disk | FTP | other *** search
/ 600 Games / 600games.iso / Puzzle / calculadora_quebrada.swf / scripts / DefineSprite_226 / frame_1 / DoAction.as
Encoding:
Text File  |  2007-03-13  |  6.3 KB  |  341 lines

  1. function initCalc()
  2. {
  3.    binpressed = false;
  4.    state = 1;
  5.    tDisp = "0";
  6.    pendingOp = "";
  7.    stack = 0;
  8.    current = "0";
  9.    decimal = 0;
  10.    sign = 0;
  11.    maxD = 99999999;
  12.    minD = 1e-7;
  13. }
  14. function buttonPressed(k)
  15. {
  16.    var _loc1_ = k;
  17.    if(!locked)
  18.    {
  19.       if(_loc1_ >= "0" && _loc1_ <= "9")
  20.       {
  21.          digitKey(_loc1_);
  22.       }
  23.       else if(_loc1_ == ".")
  24.       {
  25.          decimalPoint();
  26.       }
  27.       else if(_loc1_ == "neg")
  28.       {
  29.          negate();
  30.       }
  31.       else if("├ù/-+".indexOf(_loc1_,0) > -1)
  32.       {
  33.          binaryOp(_loc1_);
  34.       }
  35.       else if("x^2sqrt".indexOf(_loc1_,0) > -1)
  36.       {
  37.          unaryOp(_loc1_);
  38.       }
  39.       else if(_loc1_.indexOf("M",0) > -1)
  40.       {
  41.          memoryOp(_loc1_);
  42.       }
  43.       else if(_loc1_ == "=")
  44.       {
  45.          equals();
  46.       }
  47.       else if(_loc1_ == "AC")
  48.       {
  49.          allClear();
  50.       }
  51.       else
  52.       {
  53.          setError("Key? " + _loc1_);
  54.       }
  55.       checkTargets();
  56.    }
  57.    else
  58.    {
  59.       tDisp = "-= travado =-";
  60.    }
  61. }
  62. function setError(errStr)
  63. {
  64.    state = 2;
  65.    tDisp = errStr;
  66.    stack = 0;
  67.    decimal = false;
  68. }
  69. function digitKey(k)
  70. {
  71.    var _loc1_ = k;
  72.    if(state == 0)
  73.    {
  74.       binpressed = false;
  75.       if(current == "0")
  76.       {
  77.          current = _loc1_;
  78.       }
  79.       else if(current.length < 8 + decimal + sign)
  80.       {
  81.          current = current.concat(_loc1_);
  82.          tDisp = current;
  83.       }
  84.    }
  85.    else if(state == 1)
  86.    {
  87.       current = _loc1_;
  88.       state = 0;
  89.       tDisp = _loc1_;
  90.       binpressed = false;
  91.    }
  92. }
  93. function allClear()
  94. {
  95.    initCalc();
  96. }
  97. function negate()
  98. {
  99.    if(state < 2 && current.charAt(0) != "-")
  100.    {
  101.       current = "-".concat(current);
  102.       sign = 1;
  103.       tDisp = current;
  104.       binpressed = false;
  105.    }
  106.    else if(state < 2 && current.charAt(0) == "-")
  107.    {
  108.       current = current.substr(1);
  109.       sign = 0;
  110.       tDisp = current;
  111.       binpressed = false;
  112.    }
  113. }
  114. function decimalPoint()
  115. {
  116.    if(state == 0 && current.length < 8 + sign && decimal == 0)
  117.    {
  118.       current = current.concat(".");
  119.       decimal = 1;
  120.       tDisp = current;
  121.       binpressed = false;
  122.    }
  123.    else if(state == 1)
  124.    {
  125.       current = "0.";
  126.       decimal = 1;
  127.       state = 0;
  128.       tDisp = current;
  129.       binpressed = false;
  130.    }
  131. }
  132. function unaryOp(k)
  133. {
  134.    if(state < 2)
  135.    {
  136.       binpressed = false;
  137.       if(k == "sqrt")
  138.       {
  139.          current = Math.sqrt(current);
  140.          setResult(current);
  141.       }
  142.       if(k == "x^2")
  143.       {
  144.          current *= current;
  145.          setResult(current);
  146.       }
  147.    }
  148. }
  149. function binaryOp(k)
  150. {
  151.    if(state < 2)
  152.    {
  153.       state = 1;
  154.    }
  155.    if(pendingOp != "" && state < 2 && !binpressed)
  156.    {
  157.       if(pendingOp == "+")
  158.       {
  159.          current = Number(stack) + Number(current);
  160.          setResult(current);
  161.       }
  162.       else if(pendingOp == "-")
  163.       {
  164.          current = stack - Number(current);
  165.          setResult(current);
  166.       }
  167.       else if(pendingOp == "├ù")
  168.       {
  169.          current = stack * Number(current);
  170.          setResult(current);
  171.       }
  172.       else if(pendingOp == "/")
  173.       {
  174.          current = stack / Number(current);
  175.          setResult(current);
  176.       }
  177.    }
  178.    if(state < 2)
  179.    {
  180.       if(!binpressed)
  181.       {
  182.          pendingOp = k;
  183.          stack = current;
  184.          state = 1;
  185.          binpressed = true;
  186.       }
  187.       else
  188.       {
  189.          pendingOp = k;
  190.       }
  191.    }
  192. }
  193. function equals()
  194. {
  195.    if(pendingOp != "" && state < 2)
  196.    {
  197.       if(pendingOp == "+")
  198.       {
  199.          current = Number(stack) + Number(current);
  200.          setResult(current);
  201.       }
  202.       else if(pendingOp == "-")
  203.       {
  204.          current = stack - Number(current);
  205.          setResult(current);
  206.       }
  207.       else if(pendingOp == "├ù")
  208.       {
  209.          current = stack * Number(current);
  210.          setResult(current);
  211.       }
  212.       else if(pendingOp == "/")
  213.       {
  214.          current = stack / Number(current);
  215.          setResult(current);
  216.       }
  217.       pendingOp = "";
  218.       state = 1;
  219.    }
  220.    else if(state < 2)
  221.    {
  222.       state = 1;
  223.    }
  224. }
  225. function memoryOp(k)
  226. {
  227.    var _loc1_ = k;
  228.    if(state < 2)
  229.    {
  230.       if(_loc1_ == "Min")
  231.       {
  232.          mem = Number(current);
  233.          state = 1;
  234.       }
  235.       else if(_loc1_ == "M+")
  236.       {
  237.          mem += Number(current);
  238.          state = 1;
  239.          checkMem();
  240.       }
  241.       else if(_loc1_ == "M-")
  242.       {
  243.          mem -= Number(current);
  244.          state = 1;
  245.          checkMem();
  246.       }
  247.       else if(_loc1_ == "MR")
  248.       {
  249.          current = mem;
  250.          setResult(current);
  251.          state = 1;
  252.       }
  253.       else if(_loc1_ == "MC")
  254.       {
  255.          mem = 0;
  256.       }
  257.    }
  258.    if(mem == 0)
  259.    {
  260.       memindicator.gotoAndStop(1);
  261.    }
  262.    else
  263.    {
  264.       memindicator.gotoAndStop(2);
  265.    }
  266. }
  267. function setResult(x)
  268. {
  269.    var _loc1_ = x;
  270.    if(isNan(_loc1_))
  271.    {
  272.       setError("Err: Illegal");
  273.    }
  274.    else if(Math.abs(_loc1_) > maxD)
  275.    {
  276.       setError("Err: Too big");
  277.    }
  278.    else if(Math.abs(_loc1_) < minD)
  279.    {
  280.       current = 0;
  281.       tDisp = "0";
  282.       state = 1;
  283.    }
  284.    else
  285.    {
  286.       var s = Math.abs(_loc1_) != _loc1_ ? 1 : 0;
  287.       var d = _loc1_.indexOf(".",0) <= -1 ? 0 : 1;
  288.       var _loc2_ = String(_loc1_);
  289.       var _loc3_ = Math.round(Math.log(_loc1_) / 2.302585092994046);
  290.       _loc3_ = Math.max(_loc3_,0);
  291.       if(_loc2_.length < 9 + d + s)
  292.       {
  293.          current = _loc1_;
  294.          tDisp = _loc2_;
  295.          state = 1;
  296.       }
  297.       else
  298.       {
  299.          var unit = Math.pow(10,8 - Math.abs(_loc3_));
  300.          _loc2_ = Math.round(_loc1_ * unit) / unit;
  301.          current = _loc1_;
  302.          tDisp = _loc2_;
  303.          state = 1;
  304.       }
  305.    }
  306. }
  307. function checkMem()
  308. {
  309.    if(isNan(mem))
  310.    {
  311.       setError("Err: Bad Mem");
  312.    }
  313.    else if(Math.abs(mem) > maxD)
  314.    {
  315.       setError("Mem too big");
  316.    }
  317.    else if(Math.abs(mem) < minD)
  318.    {
  319.       mem = 0;
  320.    }
  321.    memindicator.gotoAndStop(0);
  322. }
  323. function checkTargets()
  324. {
  325.    i = 0;
  326.    while(i < 8)
  327.    {
  328.       if(_root.targets[i] == Number(tDisp))
  329.       {
  330.          eval("_parent.target" + i).gotoAndStop(2);
  331.          _root.done[i] = 0;
  332.       }
  333.       i++;
  334.    }
  335. }
  336. stop();
  337. mem = 0;
  338. memindicator.gotoAndStop(1);
  339. initCalc();
  340. tDisp = "=Calculadora=";
  341.