Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

simple calculator AS3

Contributor ,
Jul 29, 2014 Jul 29, 2014

Hi there!

I have been searching for a reliable simple calculator code in AS3. have found many tutorials and calculators, each have a problem.

Anyone please can help me find a robust reliable working code?! Very simple functions like this:

Untitled.png

This is a working sample code but with many problems:

var numbers:Array = [num0_btn,num1_btn,num2_btn,num3_btn,num4_btn,num5_btn,num6_btn,num7_btn,num8_btn,num9_btn];

var operators:Array = [divide_btn,add_btn,subtract_btn,multiply_btn,equal_btn];

var op:String;

var num1:Number;

var num2:Number;

function addListeners():void {

  for (var i:uint = 0; i < numbers.length; i++) {

  numbers.addEventListener(MouseEvent.CLICK, pressNumber);

  }

  for (i = 0; i < operators.length; i++) {

  operators.addEventListener(MouseEvent.CLICK, pressOperator);

  }

  clear_btn.addEventListener(MouseEvent.CLICK, clearAll);

  dot_btn.addEventListener(MouseEvent.CLICK, addDot);

  back_btn.addEventListener(MouseEvent.CLICK, backSpace);

}

function pressNumber(event:MouseEvent):void {

  var instanceName:String = event.target.name;

  var numPushed:String = instanceName.charAt(3);

  if (window_txt.text == "0" || num1 == Number(window_txt.text)) {

  window_txt.text = "";

  }

  window_txt.appendText(numPushed);

}

function pressOperator(event:MouseEvent):void {

  var instanceName:String = event.target.name;

  var currentOp:String = instanceName.slice(0,instanceName.indexOf("_"));

  if (! num1) {

  num1 = Number(window_txt.text);

  op = currentOp;

  } else if (!num2) {

  num2 = Number(window_txt.text);

  showCalculation();

  op = currentOp;

  }

}

function clearAll(event:MouseEvent):void {

  window_txt.text = "0";

  num1 = NaN;

  num2 = NaN;

}

function addDot(event:MouseEvent):void {

  if (num1 == Number(window_txt.text)) {

  window_txt.text = "0";

  }

  if (window_txt.text.indexOf(".") == -1) {

  window_txt.appendText(".");

  }

}

function backSpace(event:MouseEvent):void {

  window_txt.text = window_txt.text.substring(0,window_txt.text.length - 1);

}

function showCalculation():void {

  var dividedZero:Boolean;

  switch (op) {

  case "multiply" :

  num1 *=  num2;

  break;

  case "divide" :

  if (num2 != 0) {

  num1 /=  num2;

  } else {

  window_txt.text = "Err";

  dividedZero = true;

  }

  break;

  case "add" :

  num1 +=  num2;

  break;

  case "subtract" :

  num1 -=  num2;

  break;

  default :

  break;

  }

  if (! dividedZero) {

  window_txt.text = String(num1);

  }

  num2 = NaN;

}

addListeners();

All the best and thanks a lot for your time.

Ali

TOPICS
ActionScript
3.6K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 29, 2014 Jul 29, 2014

what's the problem?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 29, 2014 Jul 29, 2014

While someone might offer another example you can choose to try, it will be much better if you take one and work on it to make it work the way you want.  For all of the code you just showed you didn't indicate what was wrong with it - it is not likely anyone is going to search thru it to try to figure out what you think is wrong with it.   What you should do is take that same code and break out the portions of it that do not work the way you wish them to and seek help resolving them.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jul 29, 2014 Jul 29, 2014

Thanks Kglad and Ned Murphy for your complete reply. you are so right if I want to "re-invent" the wheel, that is the right process. My main request was to find out if there is a ready code for a calculator like Windows Simple calculator (in AS3) or if you know a place I can search for something like a calculator snippet etc.

I suppose this should be available and there are MANY small details I should take care of (rounding numbers, decimals, divide by zero, infinity, number of digits, digit separator, and so on!).

again thanks a lot for your kind reply and I should keep searching online.

All the best,

    Ali

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 30, 2014 Jul 30, 2014
LATEST

you're welcome.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines