MDAS calculator

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lyodmichael
    New Member
    • Jul 2012
    • 75

    MDAS calculator

    i made a simple calculator but i need a calculator that compute using MDAS, hmm. in a textbox i need to compute multiple number and multiple operation and it will echo the computation?
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    could you show what you have done already and explain a bit more what changes/requirements you want to have in it?

    Comment

    • lyodmichael
      New Member
      • Jul 2012
      • 75

      #3
      Code:
      <script language="javascript">
      var av,first,sc;
      function displayone()
      {
      var e;
      e=document.forms[0].one.value;
      document.forms[0].dis.value=document.forms[0].dis.value+e;
      }
      function displaytwo()
      {
      var e;
      e=document.forms[0].two.value;
      document.forms[0].dis.value=document.forms[0].dis.value+e;
      }
      function displaythree()
      {
      var e;
      e=document.forms[0].three.value;
      document.forms[0].dis.value=document.forms[0].dis.value+e;
      }
      function displayfour()
      {
      var e;
      e=document.forms[0].four.value;
      document.forms[0].dis.value=document.forms[0].dis.value+e;
      }
      function displayfive()
      {
      var e;
      e=document.forms[0].five.value;
      document.forms[0].dis.value=document.forms[0].dis.value+e;
      }
      function displaysix()
      {
      var e;
      e=document.forms[0].six.value;
      document.forms[0].dis.value=document.forms[0].dis.value+e;
      }
      
      function displayseven()
      {
      var e;
      e=document.forms[0].seven.value;
      document.forms[0].dis.value=document.forms[0].dis.value+e;
      }
      function displayeight()
      {
      var e;
      e=document.forms[0].eight.value;
      document.forms[0].dis.value=document.forms[0].dis.value+e;
      }
      function displaynine()
      {
      var e;
      e=document.forms[0].nine.value;
      document.forms[0].dis.value=document.forms[0].dis.value+e;
      }
      function displayzero()
      {
      
      e=document.forms[0].zero.value;
      document.forms[0].dis.value=document.forms[0].dis.value+e;
      }
      function add()
      {
      first=parseInt(document.forms[0].dis.value);
      document.forms[0].dis.value=""
      av=document.forms[0].plus.value;
      return av;
      return first;
      }
      function m()
      {
      first=parseInt(document.forms[0].dis.value);
      document.forms[0].dis.value=""
      av=document.forms[0].minus.value;
      return av;
      return first;
      }
      function mu()
      {
      first=parseInt(document.forms[0].dis.value);
      document.forms[0].dis.value=""
      av=document.forms[0].mult.value;
      return av;
      return first;
      }
      function di()
      {
      first=parseInt(document.forms[0].dis.value);
      document.forms[0].dis.value=""
      av=document.forms[0].div.value;
      return av;
      return first;
      }
      function cal()
      {
      if(av=="+"){
      	sc=parseInt(document.forms[0].dis.value);
      	document.forms[0].dis.value=first+sc;
      }
      if(av=="-"){
      	sc=parseInt(document.forms[0].dis.value);
      	document.forms[0].dis.value=first-sc;
      }
      if(av=="*"){
      	sc=parseInt(document.forms[0].dis.value);
      	document.forms[0].dis.value=first*sc;
      }
      if(av=="/"){
      	sc=parseInt(document.forms[0].dis.value);
      	document.forms[0].dis.value=first/sc;
      }
      }
      function ceo()
      {
      document.forms[0].dis.value="";
      }
      
      </script>
      this is my reference but i need to compute like this

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        as far as i can see from the code you posted, at the moment you have some form-elements that you use to claculate something. note that you cannot return multiple from a function like this:

        Code:
        function di() {
            first=parseInt(document.forms[0].dis.value);
            document.forms[0].dis.value=""
            av=document.forms[0].div.value;
            return av;
            return first;
        }
        this function never returns first since it already exits by returning av.

        btw. what is really needed? just computing the input like:

        Code:
        20-8+3*4/2
        or even display the steps from the calculation? getting the steps make it much more complex.

        Comment

        • lyodmichael
          New Member
          • Jul 2012
          • 75

          #5
          yes i just need to compute the inpute the number that inputted in the text box.

          Comment

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5390

            #6
            then its easy - you might use something like this:

            Code:
            <html>
            <head>
            <script type="text/javascript">
            
            function calc() {
                var term   = document.getElementById('term').value;
                var result = eval(term);
            
                document.getElementById('result').value = result;
            }
            
            </script>
            </head>
            <body>
                <input type="text" id="term"/>
                <input type="button" value="calculate" onclick="calc();"/>
                <br/>
                <input type="text" id="result"/>
            </body>
            </html>
            be aware that this will evaluate everything that you put into the first textfield (you could for example type in alert('foo')), so you probably want some input-validation so that only numbers and operators and may be brackets could be typed in.
            Last edited by gits; Jul 17 '12, 02:20 PM.

            Comment

            • lyodmichael
              New Member
              • Jul 2012
              • 75

              #7
              thanks, but its not follow the MDAS(multiply divide add subtract ) procedure, it's read the first operation you put and then read the followings i wander how to follow the procedure, btw sorry about my english .

              Comment

              • lyodmichael
                New Member
                • Jul 2012
                • 75

                #8
                sir thank i did it, thank for your help thank you so mach . im just a novice in this but i try my best to gadder information.

                Comment

                • gits
                  Recognized Expert Moderator Expert
                  • May 2007
                  • 5390

                  #9
                  thanks, but its not follow the MDAS(multiply divide add subtract ) procedure
                  it calculates the term mathematicly correct (since it has to - otherwise the language would be completly messed up). i'm not aware what MDAS should mean - from my point of view it just means that multiplication/division is to be calculated before addition/subtraction and may be only those operations are allowed at all. but i might be wrong here - since i don't know that abbreviation (MDAS) and cannot really find something that really defines it - the best explaination i found is mentioned above.
                  Last edited by gits; Jul 18 '12, 01:59 PM.

                  Comment

                  Working...