document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. <!--In the name of ALLAH
  2. Name : Muhammad Azri bin Jasni @ Abdul Rani
  3. ID   : 2009147897
  4. Group: CS1104A
  5. -->
  6. <!--5.  Write a HTML file using JavaScript to produce online calculator.-->
  7. <!--Reference :- http://www.anaesthetist.com/mnm/javascript/calc.htm-->
  8. <html>
  9.     <head>
  10.     <!--
  11.     Azri\'s Online Calculator
  12.     Features:
  13.         Have Digits buttons, 0-9
  14.         Calculate button, =
  15.         Result display area, result
  16.         Clear button, C
  17.         Exponent button, EXP
  18.         Decimal Point
  19.         Maximum digit allowed is 30, MAX
  20.         Error will be told in the result display area (Error!)
  21.     Known Bugs:
  22.         Didn\'t handle situation where operator buttons press more than once
  23.     -->
  24.     <script language="JavaScript">
  25.    
  26.     //the html comment tag below is used to hide javascript
  27.     //if the browser does not support JavaScript or
  28.     //it is disabled.
  29.    
  30.     <!-- hide from JavaScript impaired browsers
  31.    
  32.    Operation = 0; //operators (* / + -)
  33.     CurrentString = "0"; //current number or operator
  34.    MAX = 30; //maximum digits
  35.     RAM  = "0"; //previous number
  36.    
  37.     /*AddDigit()*/
  38.     function AddDigit(Digit)
  39.     {
  40.         if ( CurrentString.indexOf("!") == -1) //check error
  41.         //use indexOf just to look for that char/string
  42.         {  
  43.             if (CurrentString.length > MAX)
  44.             {
  45.                 alert("Error! - max 30 digits.");
  46.                 CurrentString = "Error! - max 30 digits."; //max digits
  47.             }
  48.             else
  49.             {
  50.                 if ( (eval(CurrentString) == 0) && (CurrentString.indexOf(".") == -1) )
  51.                 {
  52.                     CurrentString = Digit;
  53.                 }
  54.                 else
  55.                 {
  56.                     CurrentString = CurrentString + Digit;
  57.                 }
  58.             }
  59.         }
  60.         else
  61.         {
  62.             CurrentString = "Hint! Press \'C\'";
  63.         }
  64.         //if exp button was pressed
  65.         if (CurrentString.indexOf("e0") != -1)
  66.         {
  67.             var exp = CurrentString.indexOf("e");
  68.             CurrentString = CurrentString.substring(0,exp+1) + CurrentString.substring(exp+2);
  69.         }
  70.        
  71.         document.Calculator.answer.value = CurrentString;
  72.     }
  73.    
  74.     /*DecimalPoint()*/
  75.     function DecimalPoint()                  
  76.     {
  77.         if ( CurrentString.length != 0)
  78.         {
  79.             if ( ( CurrentString.indexOf(".") == -1) &&( CurrentString.indexOf("e") == -1) )
  80.             {
  81.                 CurrentString = CurrentString + ".";
  82.             }
  83.         }
  84.         else
  85.         {
  86.             CurrentString = "0.";
  87.         }
  88.        
  89.         document.Calculator.answer.value = CurrentString;
  90.     }
  91.    
  92.     /*DoExponent()*/
  93.     function DoExponent()
  94.     {
  95.         if ( CurrentString.indexOf("!") == -1)
  96.         {
  97.             if ( CurrentString.indexOf("e") == -1 )
  98.             {
  99.                 CurrentString = CurrentString + "e0";
  100.             }
  101.             else
  102.                 CurrentString = "Hint! Press \'C\'";  //Help out, if error present.
  103.         }
  104.         document.Calculator.answer.value = CurrentString;
  105.     }
  106.    
  107.     /*Clear()*/
  108.     function Clear()
  109.     {
  110.         CurrentString = "0";
  111.         Operation = 0;
  112.         RAM = "0";
  113.         document.Calculator.answer.value = CurrentString;
  114.     }
  115.    
  116.     /*Operate()*/
  117.     function Operate(op)            //STORE OPERATION e.g. + * / etc.
  118.     {
  119.         if ( CurrentString.indexOf("!") == -1) // check for error
  120.         {  
  121.    
  122.             if (Operation != 0) //do calculation
  123.             {
  124.                 Calculate();
  125.             }
  126.         if (op.indexOf("*") > -1)
  127.             Operation = 1;
  128.         if (op.indexOf("/") > -1)
  129.             Operation = 2;
  130.         if (op.indexOf("+") > -1)
  131.             Operation = 3;
  132.         if (op.indexOf("-") > -1)
  133.             Operation = 4;
  134.  
  135.         RAM = CurrentString;
  136.         CurrentString = "";
  137.         }
  138.         else
  139.             CurrentString = "Hint! Press \'C\'";  //Inform to press C to use calculator again
  140.        
  141.         document.Calculator.answer.value = CurrentString;
  142.     }
  143.    
  144.     /*Calculate()*/
  145.     function Calculate()  //Calculate
  146.     {
  147.         switch (Operation)
  148.         {
  149.         case 1:
  150.             CurrentString = eval(RAM) * eval(CurrentString);
  151.             break;
  152.         case 2:
  153.             if (eval(CurrentString) != 0)
  154.             {
  155.                 CurrentString = eval(RAM) / eval(CurrentString);
  156.             }
  157.             else
  158.             {
  159.             CurrentString = "Error! - Divide by zero";
  160.             }
  161.             break;
  162.         case 3:
  163.             CurrentString = eval(RAM) + eval(CurrentString);
  164.             break;
  165.         case 4:
  166.             CurrentString = eval(RAM) - eval(CurrentString);
  167.         default:
  168.         }
  169.        
  170.         //Clearing temporary variable for next operation
  171.         Operation = 0;
  172.         RAM = "0";
  173.         CurrentString = CurrentString + "";
  174.        
  175.         //Math Errors
  176.         if (CurrentString.indexOf("Infinity") != -1)
  177.         {
  178.             CurrentString = "Error! - Infinite";
  179.         }
  180.         if (CurrentString.indexOf("NaN") != -1)
  181.         {
  182.             CurrentString = "Error! - can\'t understand";
  183.         }
  184.         document.Calculator.answer.value = CurrentString;
  185.     }
  186.    
  187.     //done hiding-->
  188.     </script>
  189.    
  190.     <NOSCRIPT>
  191.     Please unblock script or update your browsers to use the JavaScript features in this page. :)
  192.     </NOSCRIPT>
  193.     </head>
  194.    
  195. <body bgcolor="teal">
  196.     <center>
  197.    
  198.     <form name="Calculator">
  199.     <table border=\'1\' bgcolor=\'aqua\'>
  200.     <tr><td colspan=\'4\' align="center" style="color:teal">Azri\'s Online Calculator
  201.             <br/> <input name=\'answer\' type=\'text\' value="0" readonly></td></tr>
  202.     <tr>
  203.         <td colspan=\'2\'><input name=\'C\' type=\'button\' value=\'C\' style=\'width:100%\' OnClick="Clear()"></td>
  204.         <td><input name=\'div\' type=\'button\' value=\'/\' style=\'width:100%\' OnClick="Operate(\'/\')"></td>
  205.         <td><input name=\'mul\' type=\'button\' value=\'*\' style=\'width:100%\' OnClick="Operate(\'*\')"></td>
  206.     </tr>
  207.     <tr>
  208.         <td><input name=\'7\' type=\'button\' value=\'7\' style=\'width:100%\' OnClick="AddDigit(\'7\')"></td>
  209.         <td><input name=\'8\' type=\'button\' value=\'8\' style=\'width:100%\' OnClick="AddDigit(\'8\')"></td>
  210.         <td><input name=\'9\' type=\'button\' value=\'9\' style=\'width:100%\' OnClick="AddDigit(\'9\')"></td>
  211.         <td><input name=\'-\' type=\'button\' value=\'-\' style=\'width:100%\' OnClick="Operate(\'-\')"></td>
  212.     </tr>
  213.     <tr>
  214.         <td><input name=\'4\' type=\'button\' value=\'4\' style=\'width:100%\' OnClick="AddDigit(\'4\')"></td>
  215.         <td><input name=\'5\' type=\'button\' value=\'5\' style=\'width:100%\' OnClick="AddDigit(\'5\')"></td>
  216.         <td><input name=\'6\' type=\'button\' value=\'6\' style=\'width:100%\' OnClick="AddDigit(\'6\')"></td>
  217.         <td><input name=\'+\' type=\'button\' value=\'+\' style=\'width:100%\' OnClick="Operate(\'+\')"></td>
  218.     </tr>
  219.     <tr>
  220.         <td><input name=\'1\' type=\'button\' value=\'1\' style=\'width:100%\' OnClick="AddDigit(\'1\')"></td>
  221.         <td><input name=\'2\' type=\'button\' value=\'2\' style=\'width:100%\' OnClick="AddDigit(\'2\')"></td>
  222.         <td><input name=\'3\' type=\'button\' value=\'3\' style=\'width:100%\' OnClick="AddDigit(\'3\')"></td>
  223.         <td><input name=\'EXP\' type=\'button\' value=\'EXP\' style=\'width:100%\' OnClick="DoExponent()"></td>
  224.     </tr>
  225.     <tr>
  226.         <td colspan=\'2\'><input name=\'0\' type=\'button\' value=\'0\' style=\'width:100%\' OnClick="AddDigit(\'0\')"></td>
  227.         <td><input name=\'.\' type=\'button\' value=\'.\' style=\'width:100%\' OnClick="DecimalPoint()"></td>
  228.         <td><input name=\'=\' type=\'button\' value="=" style="width:100%; height:100%" OnClick="Calculate()"></td>
  229.     </tr>
  230.     <tr>
  231.        
  232.     </tr>
  233.     </table>
  234.     </form>
  235.    
  236.     </center>
  237. </body>
  238. <footer><div align="center" style="font-size: 9px;">© 2011 Creed Production.</div></footer>
  239. </html>
');