Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    How can i calculate Qstring data ?

    General and Desktop
    6
    13
    10564
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • W
      webadana last edited by

      i have a Qstring data like "43+40/2"
      i want to calculate it each other.. when i debug it seem "43+40/2" but i want to return it 53..

      and sometimes data could be "43+sin(30)". how can i calculate it.
      Thanks..

      1 Reply Last reply Reply Quote 0
      • F
        Franzk last edited by

        [quote author="webadana" date="1299997503"]i have a Qstring data like "43+40/2"
        i want to calculate it each other.. when i debug it seem "43+40/2" but i want to return it 53..[/quote]

        Seems to me you need to start working on simpler problems first. QString isn't a calculator. You need to parse the string and translate it to math operations. Get the order of execution right. Lots of stuff to think about.

        Also, 43 + 40 / 2 = 63, but you probably made a typo there.

        "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

        http://www.catb.org/~esr/faqs/smart-questions.html

        1 Reply Last reply Reply Quote 0
        • W
          webadana last edited by

          lol:
          43 + 40 / 2 = 63 thanks.. its very helpfull for me..:D

          i know Qstring isn't calculater and i asked how to calculate.. how can i parse ?? and order..

          may be there is a funksion to do it.. ?? is there..?

          1 Reply Last reply Reply Quote 0
          • Z
            ZapB last edited by

            I would approach this using the QtScript module. Put your expression to be evaluated into a QString, add some wrapper code to create a QScriptValue from it (ie add in the function signature and curly braces). Then evaluate the resulting QScriptObject using a QScriptEngine instance.

            Something along these lines...

            @
            QScriptEngine engine;
            QString function( myFunction() { return 43 + 40 / 2; } );

            // Tell engine about the function
            engine.evaluate( function );

            // Get a "function pointer" type object
            QScriptValue scriptFunction = engine.globalObject().property( "myFunction" );

            // Call the function
            double result = scriptFunction.call().toNumber();
            @

            Note that with this approach you can also pass arguments into the function too. See the QtScript docs for more details.

            Nokia Certified Qt Specialist
            Interested in hearing about Qt related work

            1 Reply Last reply Reply Quote 0
            • A
              andre last edited by

              Indeed: QtScript is your friend. You do not want to build your own parser, I think.

              Note that the example above is missing some quotation marks on line 2...

              1 Reply Last reply Reply Quote 0
              • Z
                ZapB last edited by

                Oops my bad. Thanks for pointing that out Andre.

                Nokia Certified Qt Specialist
                Interested in hearing about Qt related work

                1 Reply Last reply Reply Quote 0
                • D
                  deimos last edited by

                  if you don't think about portability, in linux you can use QProcess to execute "bc" and send to it functionString+enter
                  but I am not sure if it can handle sin and cos functions

                  1 Reply Last reply Reply Quote 0
                  • W
                    webadana last edited by

                    thank you for your answers..
                    @
                    QString x = "30.5+40/2";
                    QScriptEngine myEngine;
                    QScriptValue x2 = myEngine.evaluate(x);
                    qDebug() << x2.toNumber();
                    @
                    i use this code and its work but i cant calculate sin, cos.. how can i calculate sin cos..
                    i try to @ZapB 's code but it didn't calculate too.. (or i couldn't :))
                    thank you very much..:)

                    1 Reply Last reply Reply Quote 0
                    • Z
                      ZapB last edited by

                      @Math.sin( x )@

                      Nokia Certified Qt Specialist
                      Interested in hearing about Qt related work

                      1 Reply Last reply Reply Quote 0
                      • W
                        webadana last edited by

                        thank you but i want to calculate the x when x became like this

                        @QString x = "30.5+40/2+sin(30)";@

                        how can i apply on this code..

                        1 Reply Last reply Reply Quote 0
                        • A
                          andre last edited by

                          A hack that comes to mind is some regular expression magic. Why not simply insert the "Math." at the appropriate place in your string?

                          Alternatively, you can define the functions you want to have available in your scripting environment yourself, so you don't need to modify the input string.

                          1 Reply Last reply Reply Quote 0
                          • Z
                            ZapB last edited by

                            Either of the approaches suggested by Andre will work fine. Experiment and have fun.

                            Nokia Certified Qt Specialist
                            Interested in hearing about Qt related work

                            1 Reply Last reply Reply Quote 0
                            • S
                              sabrimev last edited by

                              one of the biggest problem I accross people dont give header when they answer to question. I spend much time to find header too. Please do not do this. Give header too in your answer. Its not Java that imports header automaticly. Its QT and C++ .

                              1 Reply Last reply Reply Quote 0
                              • First post
                                Last post