Advance Calculator in JavaScript


<!DOCTYPE html>
<html>
<body>
<script language="JavaScript">

    Memory  = "0";      // initialise memory variable
    Current = "0";      //   and value of Display ("current" value)
    Operation = 0;      // Records code for eg * / etc.
    MAXLENGTH = 30;     // maximum number of digits before decimal!

function AddDigit(dig)          //ADD A DIGIT TO DISPLAY (keep as 'Current')
 { if (Current.indexOf("!") == -1)  //if not already an error
    { if (    (eval(Current) == 0)
              && (Current.indexOf(".") == -1)
         ) { Current = dig;
           } else
           { Current = Current + dig;
           };
      Current = Current.toLowerCase(); //FORCE LOWER CASE
    } else
    { Current = "Hint! Press 'AC'";  //Help out, if error present.
    };
   if (Current.indexOf("e0") != -1)
     { var epos = Current.indexOf("e");
       Current = Current.substring(0,epos+1) + Current.substring(epos+2);
     };
  if (Current.length > MAXLENGTH)
     { Current = "Aargh! Too long"; //don't allow over MAXLENGTH digits before "." ???
     };
   document.Calculator.Display.value = Current;
 }

function Dot()                  //PUT IN "." if appropriate.
 {
  if ( Current.length == 0)     //no leading ".", use "0."
    { Current = "0.";
    } else
    {  if (   ( Current.indexOf(".") == -1)
            &&( Current.indexOf("e") == -1)
          )
         { Current = Current + ".";
    };   };
  document.Calculator.Display.value = Current;
 }

function DoExponent()
 {
  if ( Current.indexOf("e") == -1 )
       { Current = Current + "e0";
         document.Calculator.Display.value = Current;
       };
 }

function PlusMinus()
 {
  if  (Current.indexOf("e") != -1)
    { var epos = Current.indexOf("e-");
      if (epos != -1)
         { Current = Current.substring(0,1+epos) + Current.substring(2+epos); //clip out -ve exponent
         } else
         { epos = Current.indexOf("e");
           Current = Current.substring(0,1+epos) + "-" + Current.substring(1+epos); //insert -ve exponent
         };
    } else
    {  if ( Current.indexOf("-") == 0 )
         { Current = Current.substring(1);
         } else
         { Current = "-" + Current;
         };
       if (    (eval(Current) == 0)
            && (Current.indexOf(".") == -1 )
          ) { Current = "0"; };
    };
  document.Calculator.Display.value = Current;
 }

function Clear()                //CLEAR ENTRY
 { Current = "0";
   document.Calculator.Display.value = Current;
 }

function AllClear()             //Clear ALL entries!
 { Current = "0";
   Operation = 0;                //clear operation
   Memory = "0";                  //clear memory
   document.Calculator.Display.value = Current;
 }

function Operate(op)            //STORE OPERATION e.g. + * / etc.
 {
 if (Operation != 0) { Calculate(); }; //'Press "=" if pending operation!
 // note that design is not good for showing *intermediate* results.

  if (op.indexOf("*") > -1) { Operation = 1; };       //codes for *
  if (op.indexOf("/") > -1) { Operation = 2; };       // slash (divide)
  if (op.indexOf("+") > -1) { Operation = 3; };       // sum
  if (op.indexOf("-") > -1) { Operation = 4; };       // difference

  Memory = Current;                 //store value
  // note how e.g. Current.value gives neither error nor value! ***
  Current = "";
  document.Calculator.Display.value = Current;
 }

function Calculate()            //PERFORM CALCULATION 😊 button)
 {
  if (Operation == 1) { Current = eval(Memory) * eval(Current); };
  if (Operation == 2)
    { if (eval(Current) != 0)
      { Current = eval(Memory) / eval(Current)
      } else
      { Current = "Aargh! Divide by zero"; //don't allow over MAXLENGTH digits before "." ???
      }
    };
  if (Operation == 3) { Current = eval(Memory) + eval(Current); };
  if (Operation == 4) { Current = eval(Memory) - eval(Current); };
  Operation = 0;                //clear operation
  Memory = "0";                  //clear memory
  Current = Current + "";       //FORCE A STRING!
  if (Current.indexOf("Infinity") != -1)        //eg "1e320" * 1
    { Current = "Aargh! Value too big";
    };
  if (Current.indexOf("NaN") != -1)        //eg "1e320" / "1e320"
    { Current = "Aargh! I don't understand";
    };
  document.Calculator.Display.value = Current;
  // NOTE: if no operation, nothing changes, Current is left the same!
 }

function FixCurrent()
 {
  Current = document.Calculator.Display.value;
  Current = "" + parseFloat(Current);
  if (Current.indexOf("NaN") != -1)
    { Current = "Aargh! I don't understand";
    };
  document.Calculator.Display.value = Current;
 }

  //--------------------------------------------------------------->
  </script>

<div align="center">
<form name="Calculator">
<table width="45%" border="4" bgcolor="yellow"><tbody><tr>      <!--OUTER MARGIN OF CALCULATOR-->
<td colspan="2" align="center">

A Simple JavaScript Calculator
Made By Noman Khan MCS Student<br>
<font face="Courier" size="5">
<input type="text" maxlength="40" size="25" name="Display" onchange="FixCurrent()">
</font></b>

</td></tr>
<tr><td width="65%" align="center">                   <!--left panel------>

<table><tbody><tr>
  <td><input type="button" name="seven" value="   7    " onclick="AddDigit('7')"></td>
  <td><input type="button" name="eight" value="   8    " onclick="AddDigit('8')"></td>
  <td><input type="button" name="nine" value="   9    " onclick="AddDigit('9')"></td>
</tr><tr>
  <td><input type="button" name="four" value="   4    " onclick="AddDigit('4')"></td>
  <td><input type="button" name="five" value="   5    " onclick="AddDigit('5')"></td>
  <td><input type="button" name="six" value="   6    " onclick="AddDigit('6')"></td>
</tr><tr>
  <td><input type="button" name="one" value="   1    " onclick="AddDigit('1')"></td>
  <td><input type="button" name="two" value="   2    " onclick="AddDigit('2')"></td>
  <td><input type="button" name="three" value="   3    " onclick="AddDigit('3')"></td>
</tr><tr>
  <td><input type="button" name="plusmin" value="  +/-  " onclick="PlusMinus()"></td>
  <td><input type="button" name="one" value="   0    " onclick="AddDigit('0')"></td>
  <td><input type="button" name="two" value="    .    " onclick="Dot()"></td>
</tr>
</tbody></table>


</td>                                   <!--end left panel-->
<td width="35%" align="center">                     <!--right panel----->

<table><tbody><tr>
  <td><input type="button" name="clear" value="    C     " onclick="Clear()"></td>
  <td><input type="button" name="AC" value="   AC    " onclick="AllClear()"></td>
</tr><tr>
  <td><input type="button" name="mul" value="     *     " onclick="Operate('*')"></td>
  <td><input type="button" name="div" value="     /      " onclick="Operate('/')"></td>
</tr><tr>
  <td><input type="button" name="add" value="    +     " onclick="Operate('+')"></td>
  <td><input type="button" name="sub" value="     -      " onclick="Operate('-')"></td>
</tr><tr>
  <td><input type="button" name="result" value="     =    " onclick="Calculate()"></td>
  <td align="right"><input type="button" name="exp" value="  EXP  " onclick="DoExponent()"></td>
</tr></tbody></table>


</td>                                   <!--end right panel-->
</tr></tbody></table>                          <!--END OUTER MARGIN CALC------->
</form></div>





</body>
</html>



Comments

Popular posts from this blog

Linear Search in C++