2 Replies Latest reply on Jul 21, 2007 6:54 AM by ScuffyScouty

    Creating calculator using Array

    ScuffyScouty
      Hi,

      I have programmed my calculator but I have some problem with it. Sorry, I am quite new to actionscript, so hope people up there can help me. I am only allow to use arrays, no switch statement..etcs. The code is as follows,

      var myArray:Array = new Array();

      button1.onRelease = function()
      {
      displaybox.text += Number(1);
      }

      button2.onRelease = function()
      {
      displaybox.text += Number(2)
      }

      button3.onRelease = function()
      {
      displaybox.text += Number(3)
      }

      button4.onRelease = function()
      {
      displaybox.text += Number(4)
      }

      button5.onRelease = function()
      {
      displaybox.text += Number(5)
      }

      button6.onRelease = function()
      {
      displaybox.text += Number(6)
      }

      button7.onRelease = function()
      {
      displaybox.text += Number(7)
      }

      button8.onRelease = function()
      {
      displaybox.text += Number(8)
      }

      button9.onRelease = function()
      {
      displaybox.text += Number(9)
      }

      button0.onRelease = function()
      {
      displaybox.text += Number(0)
      }

      buttondot.onRelease = function()
      {
      displaybox.text += "."
      }

      // buttons above are values

      plusbutton.onRelease = function() //button add function, pushes the value at the display to the array and the sign "+"
      {
      myArray.push(String(displaybox.text));
      displaybox.text = "+"
      myArray.push(String(displaybox.text))
      displaybox.text = []
      }

      minusbutton.onRelease = function() //button add function, pushes the value at the display to the array and the sign "-"
      {
      myArray.push(displaybox.text);
      displaybox.text = "-"
      myArray.push(String(displaybox.text))
      displaybox.text = []
      }

      dividebutton.onRelease = function() //button add function, pushes the value at the display to the array and the sign "/"
      {
      myArray.push(displaybox.text);
      displaybox.text = "/"
      myArray.push(String(displaybox.text))
      displaybox.text = []
      }

      multiplybutton.onRelease = function() //button add function, pushes the value at the display to the array and the sign "*"
      {
      myArray.push(displaybox.text);
      displaybox.text = "*"
      myArray.push(String(displaybox.text))
      displaybox.text = []
      }

      equalbutton.onRelease = function() //calculate the whole sum
      {
      myArray.push(Number(displaybox.text));
      var ans:Number = 0
      trace(myArray);
      if (myArray[1] = "+")
      {
      ans = (Number(myArray[0])) + (Number(myArray[2]))
      displaybox.text = ans
      }
      if (myArray[1] = "-")
      {
      ans = (Number(myArray[0])) - (Number(myArray[2]))
      displaybox.text = ans
      }
      if (myArray[1] = "*")
      {
      ans = (Number(myArray[0])) * (Number(myArray[2]))
      displaybox.text = ans
      }
      if (myArray[1] = "/")
      {
      ans = (Number(myArray[0])) / (Number(myArray[2]))
      displaybox.text = ans
      }
      for (var i:Number = 0; i < myArray.length; i ++)
      {
      ans += myArray ;
      }
      return ans;
      }

      When I wan to add some value and click on the add function, it will automatically be divided. Is there any other method that I can do a calculator using arrays or make changes to my script?
        • 1. Re: Creating calculator using Array
          Level 7
          >> When I wan to add some value and click on the add function, it will
          >> automatically be divided.

          I don't quite understand your question but you have a few oddities in your
          code:

          String(displaybox.text));

          the .text property is already a string - you don't need to cast it to a
          string - same with:

          >>displaybox.text += Number(9)

          9 is already a number... doesn't make too much sense to cast it to a
          number... and then you are telling a String (displaybox.text) to add in a
          number... more like you'd want displaybox.text += String(9)... though you
          don't need that - it'll cast automagically: displaybox.text += 9; is fine.

          >>displaybox.text = []

          hmmm... not sure what that would do, but it's certainly not proper syntax.
          An empty array is not a string... if you want to clear out a text field use
          "" - an empty string.



          --
          Dave -
          Head Developer
          http://www.blurredistinction.com
          Adobe Community Expert
          http://www.adobe.com/communities/experts/


          • 2. Re: Creating calculator using Array
            ScuffyScouty Level 1
            Hi

            Thanks for your reply

            Im quite new to actionscript and ain really sure about the function.

            Can you roughly tell me the pipeline of creating a calculator using arrays?