File tree Expand file tree Collapse file tree 1 file changed +12
-5
lines changed Expand file tree Collapse file tree 1 file changed +12
-5
lines changed Original file line number Diff line number Diff line change 11#include " Calculator.hpp"
22
3+ #include < QRegularExpression>
4+
35QString Calculator::calculate (const QString& expression) const {
46 int result = 0 ;
57 char operation = ' +' ;
6- QRegExp regexp (" (\\ d+)" );
7- int pos = 0 ;
8- while ((pos = regexp.indexIn (expression, pos)) != -1 ) {
9- int value = regexp.cap (1 ).toInt ();
8+ const QRegularExpression regexp (" (\\ d+)" );
9+ QRegularExpressionMatchIterator matches = regexp.globalMatch (expression);
10+
11+ while (matches.hasNext ()) {
12+ const auto match = matches.next ();
13+ const int value = match.captured (1 ).toInt ();
14+
1015 switch (operation) {
1116 case ' +' :
1217 result += value;
@@ -15,11 +20,13 @@ QString Calculator::calculate(const QString& expression) const {
1520 result -= value;
1621 break ;
1722 }
18- pos += regexp.matchedLength ();
23+
24+ const int pos = match.capturedEnd ();
1925 if (pos < expression.length ()) {
2026 operation = expression.at (pos).toLatin1 ();
2127 }
2228 }
29+
2330 return QString::number (result);
2431}
2532
You can’t perform that action at this time.
0 commit comments