Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Date: July 21, 2017
- // Purpose: To reduce a given fraction by finding the greatest common denominator (GCD or GCF)
- // This line makes the button, btnCalculate wait for a mouse click
- // When the button is clicked, the calculateReduced function is called
- btnCalculate.addEventListener(MouseEvent.CLICK, calculateReduced);
- // These lines make the TextInput wait for a mouse click
- // When any of these components are clicked, the clearLabels function is called
- txtinNumerator.addEventListener(MouseEvent.CLICK, clearLabels);
- txtinDenominator.addEventListener(MouseEvent.CLICK, clearLabels);
- // Declare Global Variables
- var Numerator:Number; // The numerator of the fraction
- var Denominator:Number; // The denominator of the fraction
- var Factor:Number; // The greatest common denominator of the numerator and the denominator
- // This is the reduce function
- // e:MouseEvent is the click event experienced by the button
- // void indicates that the function does not return a value
- function calculateReduced(e:MouseEvent):void
- {
- getData();
- lblResult.text = Numerator + "/" + Denominator + " can be reduced to " + (Numerator / gcd(Numerator,Denominator)) + "/" + (Denominator / gcd(Numerator,Denominator))
- }
- // This is function gcd
- // It uses the Eculidian Algorithm for calculating the GCD of two numbers
- function gcd(a:Number,b:Number):Number
- {
- var tmp:Number;
- //Swap the numbers so a >= b
- if(a < b)
- {
- tmp = a;
- a = b;
- b = tmp;
- }
- //Find the gcd
- while(b != 0)
- {
- tmp = a % b;
- a = b;
- b = tmp;
- }
- return a;
- }
- // This is the getData function
- // It retrieves the numerator and the denominator from their respective TextInput boxes
- function getData()
- {
- Numerator = Number(txtinNumerator.text);
- Denominator = Number(txtinDenominator.text);
- }
- // This is the clearLabels function
- // e:MouseEvent is the click event experienced by the textInput
- // void indicates that the function does not return a value
- function clearLabels(e:MouseEvent):void
- {
- lblResult.text = "";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement