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 to save the split string ?
Forum Updated to NodeBB v4.3 + New Features

How to save the split string ?

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 980 Views 2 Watching
  • 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.
  • sonichyS Offline
    sonichyS Offline
    sonichy
    wrote on last edited by
    #1

    How to get SL1 ?

    QString s = ui->lineEdit->text();
    // s = "1+2-3×4÷5" ;
    QStringList SL = s.split(QRegExp("+|-|×|÷"));
    // QStringList SL1 = ["+", "-", "×", "÷"]
    float result = 0;
    for(int i=0; i<SL.length(); i++){
        result += SL.at(i).toFloat();
        qDebug() << SL.at(i) << SL.at(i).toFloat();
    }
    ui->label->setText(QString::number(result));
    

    https://github.com/sonichy

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #2

      If you just want the easy way you can do:

      QString s = ui->lineEdit->text();
      
      QRegularExpression expr1("[+-×÷]");
      QRegularExpression expr2("[^+-×÷]");
      
      QStringList SL = s.split(expr1, QString::SkipEmptyParts);
      QStringList SL1 = s.split(expr2, QString::SkipEmptyParts);
      
      qDebug() << SL;
      qDebug() << SL1;
      
      float sum = std::accumulate(SL.cbegin(), SL.cend(), .0f, [](float val, const QString& str) { return val + str.toFloat(); });
      ui->label->setText(QString::number(sum));
      

      If you care about performance you can replace the regular expression with std::find_any_of in a loop until you reach the end.

      Btw. if you don't need to save those sting lists you should prefer splitRef() instead of split() to avoid unnecessary copies.

      sonichyS 1 Reply Last reply
      6
      • Chris KawaC Chris Kawa

        If you just want the easy way you can do:

        QString s = ui->lineEdit->text();
        
        QRegularExpression expr1("[+-×÷]");
        QRegularExpression expr2("[^+-×÷]");
        
        QStringList SL = s.split(expr1, QString::SkipEmptyParts);
        QStringList SL1 = s.split(expr2, QString::SkipEmptyParts);
        
        qDebug() << SL;
        qDebug() << SL1;
        
        float sum = std::accumulate(SL.cbegin(), SL.cend(), .0f, [](float val, const QString& str) { return val + str.toFloat(); });
        ui->label->setText(QString::number(sum));
        

        If you care about performance you can replace the regular expression with std::find_any_of in a loop until you reach the end.

        Btw. if you don't need to save those sting lists you should prefer splitRef() instead of split() to avoid unnecessary copies.

        sonichyS Offline
        sonichyS Offline
        sonichy
        wrote on last edited by
        #3

        @Chris-Kawa

        QString s = ui->lineEdit->text();
        QStringList SL = s.split(QRegExp("[+-×÷]"));
        QStringList SL1 = s.split(QRegExp("[^+-×÷]"), QString::SkipEmptyParts);
        float result = SL.at(0).toFloat();
        for(int i=1; i<SL.length(); i++){
            if(SL1.at(i-1) == "+"){
                result += SL.at(i).toFloat();
            }else if(SL1.at(i-1) == "-"){
                result -= SL.at(i).toFloat();
            }else if(SL1.at(i-1) == "×"){
                 result *= SL.at(i).toFloat();
            }else if(SL1.at(i-1) == "÷"){
                 result /= SL.at(i).toFloat();
            }
        }
        ui->label->setText(QString::number(result));
        

        It can calculate now, but can only calculate from left to right, can not calculate ×÷ before +-.

        https://github.com/sonichy

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by Chris Kawa
          #4

          If you want to have a fully working expression evaluator a simple if/else is not gonna be enough.

          To process it left to right you would have to turn your infix notation into the Reverse Polish notation, i.e. arguments followed by the operator

          Another way is to build an Abstract Syntax Tree and process that.

          To turn infix notation into RPN or AST you can use one of the popular algorithms, e.g. Shunting-yard algorithm.

          In any case you're kinda re-inventing a wheel. If you want scripting like that maybe you should look into some existing expression parsers.

          sonichyS 1 Reply Last reply
          7
          • Chris KawaC Chris Kawa

            If you want to have a fully working expression evaluator a simple if/else is not gonna be enough.

            To process it left to right you would have to turn your infix notation into the Reverse Polish notation, i.e. arguments followed by the operator

            Another way is to build an Abstract Syntax Tree and process that.

            To turn infix notation into RPN or AST you can use one of the popular algorithms, e.g. Shunting-yard algorithm.

            In any case you're kinda re-inventing a wheel. If you want scripting like that maybe you should look into some existing expression parsers.

            sonichyS Offline
            sonichyS Offline
            sonichy
            wrote on last edited by
            #5

            @Chris-Kawa

            QStringList SL1 = s.split(QRegExp("[^+-×÷]"), QString::SkipEmptyParts);
            

            Can not split 1+-2

            https://github.com/sonichy

            Chris KawaC 1 Reply Last reply
            0
            • sonichyS sonichy

              @Chris-Kawa

              QStringList SL1 = s.split(QRegExp("[^+-×÷]"), QString::SkipEmptyParts);
              

              Can not split 1+-2

              Chris KawaC Offline
              Chris KawaC Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @sonichy It does split it, but to parse it correctly you'd need to do another pass and detect that special case. You will also need that for parenthesis and other stuff. Like I said, a full expression parsing is a lot more involved than simple split + for loop.

              1 Reply Last reply
              2

              • Login

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