Results 1 to 2 of 2

Thread: QString - problem with adding strings

  1. #1
    Join Date
    Jan 2009
    Posts
    3
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default QString - problem with adding strings

    Qt Code:
    1. void Calculator::calculation(QString &operation, QString &text_display)
    2. {
    3. QString a_string = "", b_string = "", result_string = "", temp_string = "";
    4. double a, b, result;
    5. int position, i, j;
    6.  
    7.  
    8. position = operation.indexOf("*", 0) ;
    9.  
    10. for( i = position - 1; i >= 0 ; i-- )
    11. {
    12. if( operation.at( i ) == '*' || operation.at( i ) == '/' || operation.at( i ) == '+' || operation.at( i ) == '-' )
    13. break;
    14. temp_string = a_string;
    15. a_string = operation.at( i ) + temp_string;
    16. }
    17. ui.display->setText( a_string ); //displays a_string on QLineEdit
    18.  
    19. }
    To copy to clipboard, switch view to plain text mode 

    Basically this function has to extract a number placed before '*' operator.
    When operation = "3+56*35" a_string = "56" and it works correctly. But when operation="56*35" (without any operator before "56" - that is importanat) it multiplies extraced 56 three times and a_string = "565656". I've been thinking for a few hours about that and the problem still exists.

    Thanks for help.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QString - problem with adding strings

    Build upon this:
    Qt Code:
    1. QRegExp multiplication("(.*)\*(.*)");
    2. if(multiplication.exactMatch(someString)){
    3. QString firstOperand = multiplication.cap(1);
    4. QString secondOperand = multiplication.cap(2);
    5. // search firstOperand for +,-,/
    6. // search secondOperand for +,-,*,-
    7. } else {
    8. // no multiplication found
    9. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. plugin loading problem
    By naresh in forum Qt Programming
    Replies: 6
    Last Post: 9th June 2007, 20:05
  2. Problem when adding XML support
    By Shawn in forum Qt Programming
    Replies: 9
    Last Post: 22nd May 2007, 14:27
  3. Problem in converting QString to QChar array?
    By KaKa in forum Qt Programming
    Replies: 2
    Last Post: 19th March 2007, 01:38
  4. QString problem
    By vermarajeev in forum Qt Programming
    Replies: 9
    Last Post: 26th October 2006, 20:10
  5. Problem with custom widget
    By jnana in forum Qt Programming
    Replies: 3
    Last Post: 15th March 2006, 12:55

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.