10 Oct 2019

  • October 10, 2019
  • Amitraj
A Java script calculator


Dear learners,


Copy and paste this code in a notepad and save as a .html or .htm then open this file with any browser to run calculator program.


<!--This is my JavaScript Calculator, Sharing complete source code-->


<html>


 <head>


  <title>

   My Calcy

  </title>

  <script language="javascript">

   var temp1=0,temp2=0,temp=0;

   var flag1=0,flag2=0,flag3=0,flag4=0;

   var last_input=0,cur_input=0;

   var last_operation=0,cur_operation=0;

   var percent=0;

   setInterval

   (

    function setvalueoftextbox()

    {

     if(document.getElementById("display").value.length==0)

      document.getElementById("display").value=0;

     document.getElementById("button=").focus();

    },1

   );

   function cleardisplay()

   {

     document.getElementById("display").value=0;

   }

   function clearall()

   {

    cleardisplay();

    temp1=0;

    temp2=0;

    flag1=0;

    flag2=0;

    flag3=0

    flag4=0;

    cur_input=0;

    last_input=0;

    last_operation=0;

    cur_operation=0;

    percent=0;

   }

   function getvalue(n)

   {

    cur_input="button"+n;

    if(last_input=="button+"||last_input=="button-"||last_input=="button*"||last_input=="button/"||last_input=="buttonsqrt"||last_input=="button1/x"||last_input=="button="||last_input=="button%")

     cleardisplay();

    if(n>0&&n<=9&&document.getElementById("display").value[0]==0)

    {

     if(flag1==0)

      temp1=document.getElementById("display").value=parseFloat(document.getElementById("display").value+n);

     else if(flag2==0)

      temp2=document.getElementById("display").value=parseFloat(document.getElementById("display").value+n);

    }

    else

    {

     if(flag1==0)

     {

      if(flag3==1&&n=='.')

       temp1=parseFloat(document.getElementById("display").value=document.getElementById("display").value);

      else

       temp1=parseFloat(document.getElementById("display").value=document.getElementById("display").value+n);

     }

     else if(flag2==0)

     {

      if(flag4==1&&n=='.')

       temp2=parseFloat(document.getElementById("display").value=document.getElementById("display").value);

      else

       temp2=parseFloat(document.getElementById("display").value=document.getElementById("display").value+n);

     }

    }

    if(n=='.' && flag1==0)

      flag3=1;

    else if(n=='.' && flag2==0)

      flag4=1;

    last_input=cur_input;

   }

   function operation(cur_operation)

   {

    if(cur_operation==last_input || (cur_operation=="button=" && temp2==0))

     return 0;

    if(flag1==0)

    {

     flag1=1;

     cleardisplay();

     if(cur_operation=="buttonsqrt"||cur_operation=="button1/x")

     switch(cur_operation)

     {

      case "buttonsqrt":

       temp1=document.getElementById("display").value=Math.sqrt(temp1);

       break;

      case "button1/x":

       temp1=document.getElementById("display").value=1/temp1;

     }

    }

    else if(flag2==0)

    {

     flag2=1;

     cleardisplay();

    }

    if(flag1==1&&flag2==1)

    {

     //document.write("temp1 = "+temp1+" temp2 = "+temp2+" percent "+percent);

     if(cur_operation=="button%")

      temp2=temp1*temp2/100;

     switch(last_operation)

     {

      case "button+":

       temp1=document.getElementById("display").value=temp1+temp2;

       break;

      case "button-":

       temp1=document.getElementById("display").value=temp1-temp2;

       break;

      case "button*":

       temp1=document.getElementById("display").value=temp1*temp2;

       break;

      case "button/":

       temp1=document.getElementById("display").value=temp1/temp2;

       break;

     }

     flag2=0;

     temp2=0;

     flag4=0;

     switch(cur_operation)

     {

      case "buttonsqrt":

       temp1=document.getElementById("display").value=Math.sqrt(temp1);

       break;

      case "button1/x":

       temp1=document.getElementById("display").value=1/temp1;

     }

    }

    last_input=cur_operation;

    last_operation=cur_operation;

   }

  </script>

 </head>

 <body>

  <table align="center" border="2" bgcolor="#aaaaff" bordercolor="#aa99ff" >

<tr>

    <td>

     <table align="center" border="0" width="100%" height="30%">

<tr>

       <td>

        <hr>

   

        <hr width="50%">

     

        <hr>

     

        <hr width="50%">

     

        <input type="text" placeholder="0123456789" maxlength="10" value="0" id="display" size="31"/>

        <hr>

       </td>

      </tr>

</table>

<table align="center" border="2" bordercolor="#aa88ff" width="100%">

<tr>

       <td>

        <table align="center" border="0">

<tr>

          <td>

           <hr>

   

           <input type="button" id="button1" value="1  " onclick="getvalue('1')"/>

           <input type="button" id="button2" value="2  " onclick="getvalue('2')"/>

           <input type="button" id="button3" value="3  " onclick="getvalue('3')"/>

           <input type="button" id="button/" value="/   " onclick="operation('button/')"/>

           <input type="button" id="buttonsqrt" value="sqrt" onclick="operation('buttonsqrt')"/>

          </td>

         </tr>

<tr>

          <td>

           <input type="button" id="button4" value="4  " onclick="getvalue('4')"/>

           <input type="button" id="button5" value="5  " onclick="getvalue('5')"/>

           <input type="button" id="button6" value="6  " onclick="getvalue('6')"/>

           <input type="button" id="button*" value="*   " onclick="operation('button*')"/>

           <input type="button" id="button%" value="%   " onclick="operation('button%')"/>

          </td>

         </tr>

<tr>

          <td>

           <input type="button" id="button7" value="7  " onclick="getvalue('7')"/>

           <input type="button" id="button8" value="8  " onclick="getvalue('8')"/>

           <input type="button" id="button9" value="9  " onclick="getvalue('9')"/>

           <input type="button" id="button-" value="-   " onclick="operation('button-')"/>

           <input type="button" id="button1/x" value="1/x  " onclick="operation('button1/x')"/>

          </td>

         </tr>

<tr>

          <td>

           <input type="button" id="buttonc" value="c  " onclick="clearall()"/>

           <input type="button" id="button0" value="0  " onclick="getvalue('0')"/>

           <input type="button" id="button." value=".   " onclick="getvalue('.')"/>

           <input type="button" id="button+" value="+  " onclick="operation('button+')"/>

           <input type="button" id="button=" value="=    "onclick="operation('button=')"/>

           <hr>

   

          </td>

         </tr>

</table>

</td>

      </tr>

</table>

</td>

   </tr>

</table>

</body>

</html>





                                                 Output of code will be like this






Translate

Popular Posts