10 Replies Latest reply on Jan 15, 2007 5:14 PM by kglad

    Calculations in input text fields

    Beatie3
      Hello,
      I'm making an engineering game. Students need to input force equations and to check the equation is right it has to add up to zero. There are two input text fields, one the student sees with the force values expressed in letters like HA, VA, and MA, and one hidden field where the values are expressed as numbers. They also input operators as part of the equation. I need to make it so when they click a button to check if their equation is correct, the contents of the text field are calculated to see if the equation adds up to zero. I know you can express the contents as a number with say Number (this.my_hidden_text.text), but how do I get the operators in the equation, which are +, -, *, /, (, ), and ^ to mean something and do what they're supposed to?

      Any assistance would be most welcome.
      Thank you for your time,
      Beatie3
        • 1. Re: Calculations in input text fields
          kglad Adobe Community Professional & MVP
          you use the flash string methods to find the operator strings and then translate them into the corresponding flash operator.
          • 2. Re: Calculations in input text fields
            Beatie3 Level 1
            Thank you for your quick response. Would you mind explaining more how to use the strings method to find the operators, please? It's a bit messy but my code currently looks like this:

            stop();
            //hide stuff
            this.tickb._visible = false;
            this.crossb._visible = false;
            //declare the variables
            var check1:Number = parseInt(display_secret.text);
            var display1 = "";
            var display_secret = "";
            //function for the calculation
            function onCalculate() {
            if(this.display_secret = 0){
            this._parent.tickb._visible = true;
            this._parent.crossb._visible = false;
            } else {
            this._parent.crossb._visible = true;
            this._parent.tickb._visible = false;
            }
            }
            check02_btn.onRelease = function():Void {
            trace(display1);
            trace(check1);
            if(check1 == 0){
            this._parent.tickb._visible = true;
            this._parent.crossb._visible = false;
            } else {
            this._parent.crossb._visible = true;
            this._parent.tickb._visible = false;
            }
            };
            //load external images
            pic04_container.loadMovie("fig_3_1a.gif");
            //takes you back to the previous scene
            back02_btn.onRelease = function() {
            gotoAndStop("Scene 2", 1);
            };
            //Takes you home
            home02_btn.onRelease = function() {
            gotoAndStop("Scene 1", 1);
            };
            //moves to the next scene
            next02_btn.onRelease = function() {
            gotoAndStop("Scene 3a", 1);
            };

            //calculator buttons workings
            left_btn.onRelease = function() {
            display1 = display1+ "(";
            display_secret = display_secret + "(";
            };
            plus_btn.onRelease = function() {
            display1 = display1+"+";
            display_secret = display_secret +"+";
            };
            times_btn.onRelease = function() {
            display1 = display1+"*";
            display_secret = display_secret +"*";
            };
            right_btn.onRelease = function() {
            display1 = display1+")";
            display_secret = display_secret +")";
            };
            minus_btn.onRelease = function() {
            display1 = display1+"-";
            display_secret = display_secret +"-";
            };
            peek_btn.onRelease = function() {
            display1 = display1+"^";
            display_secret = display_secret +"^";
            };
            slash_btn.onRelease = function() {
            display1 = display1+"/";
            display_secret = display_secret +"/";
            };
            ha_btn.onRelease = function() {
            display1 = display1+"HA";
            display_secret = display_secret +"108";
            };
            va_btn.onRelease = function() {
            display1 = display1+"VA";
            display_secret = display_secret +"0";

            };
            ma_btn.onRelease = function() {
            display1 = display1+"MA";
            display_secret = display_secret +"1296";
            };
            c_btn.onRelease = function() {
            display1 = "";
            display_secret = "";
            };
            vd_btn.onRelease = function() {
            display1 = display1+"VD";
            display_secret = display_secret +"0";
            };
            w_btn.onRelease = function() {
            display1 = display1+"w";
            display_secret = display_secret +"12";
            };
            a_btn.onRelease = function() {
            display1 = display1+"a";
            display_secret = display_secret +"18";
            };
            b_btn.onRelease = function() {
            display1 = display1+"b";
            display_secret = display_secret +"9";
            };

            thank you very much for your help,
            Beatie3
            • 3. Re: Calculations in input text fields
              kglad Adobe Community Professional & MVP
              here's an example where you pass a string to calculateF() that contains two numbers with one of the operators between them. it becomes much more complicated if you have several operators in a string and must deduce the order in which to perform the operations:

              • 4. Re: Calculations in input text fields
                Beatie3 Level 1
                That's fabulous, thank you. The bad news is they can put any number of operators and operands in the string they like. I assume it will mean adding if statements to the function for each operator to look for what's in front or behind it and set the order accordingly, or is there a simpler way?

                You're a star, thank you very much,
                Beatie3
                • 5. Re: Calculations in input text fields
                  kglad Adobe Community Professional & MVP
                  i don't think there is a simple way.

                  to handle a generally well-defined mathematical statement, you're going to need to go through several steps.

                  first, you like for all substrings within parantheses and evaluate these according to standard rules:

                  exponentials first, multiplication and division next and finally addition and subtraction.

                  when you're left with no paranthetical expressions you will evaluate according to the same standard rules.

                  but i think this is a significant undertaking.
                  • 6. Re: Calculations in input text fields
                    Beatie3 Level 1
                    Yes, I've had the feeling it was for several weeks now, fortunately I have three and a half left and when I crack this and make it work the guts of the learning object will be all ready to go, it'll just be applied to different diagrams.

                    So I'll need functions for the operators in the parentetical statements as well, but they should go first. Thank you for your patience, but could I just clarify some syntax with you before I stop bothering you, please?

                    Would the function for a parenthetical statement look like this:

                    function operatorA0("(", s1,s2, ")"){
                    return s1+s2;
                    }

                    or like this:

                    function operatorA0((, s1,s2, )){
                    return (s1+s2);
                    }

                    And the for statement will need to loop through calculating all the mathematical statements, running all the operations in the appropriate order, and then it will return a value that can be checked against an if statement that says finally, if all this adds up to 0 give them a tick? Does that sound right?

                    I know it's a big job and I'm not a flash guru, but you have to earn your gurudom somehow. Thank you again for all your help, great kglad guru,
                    Beatie

                    • 7. Re: Calculations in input text fields
                      kglad Adobe Community Professional & MVP
                      the basic functions above won't change. they were all given above.

                      what will change is the coding to parse the input textfield's text property.

                      i would break this into several parts. first i would parse the text property looking for an inner most parantheses. that expression would be evaluated using the above rules (ie, exponentiation first, then mult/div etc).

                      i would then search for the next inner most parantheses and evaluate using the above rules.

                      i would repeat until there are no more parantheses.

                      the remaining expression would be evaluated by the above rules.

                      you are done.

                      • 8. Re: Calculations in input text fields
                        Beatie3 Level 1
                        Thank you very very very very very very much, kglad. I'll let you know how I go and come back with the resulting code.

                        That's been a tremendous help, thank you again,
                        Beatie
                        • 9. Re: Calculations in input text fields
                          Beatie3 Level 1
                          O.K. look hard enough and you'll usually find someone has already achieved what you need and I found this little beauty here: Flashgizmo. The code is on the site along with a working example. It's very easy to recreate and it parses equations beautifully. So Michael Kantor is my other guru next to kglad.

                          Of course now I have to make it work in the context of my gui, but I should have to do some of the work.

                          Thanks again,
                          Beatie
                          • 10. Re: Calculations in input text fields
                            kglad Adobe Community Professional & MVP
                            you're welcome.