Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How can i calculate Qstring data ?

How can i calculate Qstring data ?

Scheduled Pinned Locked Moved General and Desktop
13 Posts 6 Posters 11.3k Views
  • 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 Offline
    W Offline
    webadana
    wrote on last edited by
    #1

    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
    0
    • F Offline
      F Offline
      Franzk
      wrote on last edited by
      #2

      [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
      0
      • W Offline
        W Offline
        webadana
        wrote on last edited by
        #3

        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
        0
        • Z Offline
          Z Offline
          ZapB
          wrote on last edited by
          #4

          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
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #5

            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
            0
            • Z Offline
              Z Offline
              ZapB
              wrote on last edited by
              #6

              Oops my bad. Thanks for pointing that out Andre.

              Nokia Certified Qt Specialist
              Interested in hearing about Qt related work

              1 Reply Last reply
              0
              • D Offline
                D Offline
                deimos
                wrote on last edited by
                #7

                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
                0
                • W Offline
                  W Offline
                  webadana
                  wrote on last edited by
                  #8

                  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
                  0
                  • Z Offline
                    Z Offline
                    ZapB
                    wrote on last edited by
                    #9

                    @Math.sin( x )@

                    Nokia Certified Qt Specialist
                    Interested in hearing about Qt related work

                    1 Reply Last reply
                    0
                    • W Offline
                      W Offline
                      webadana
                      wrote on last edited by
                      #10

                      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
                      0
                      • A Offline
                        A Offline
                        andre
                        wrote on last edited by
                        #11

                        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
                        0
                        • Z Offline
                          Z Offline
                          ZapB
                          wrote on last edited by
                          #12

                          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
                          0
                          • S Offline
                            S Offline
                            sabrimev
                            wrote on last edited by
                            #13

                            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
                            0

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved