File tree 1 file changed +12
-5
lines changed
1 file changed +12
-5
lines changed Original file line number Diff line number Diff line change 1
1
#include " Calculator.hpp"
2
2
3
+ #include < QRegularExpression>
4
+
3
5
QString Calculator::calculate (const QString& expression) const {
4
6
int result = 0 ;
5
7
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
+
10
15
switch (operation) {
11
16
case ' +' :
12
17
result += value;
@@ -15,11 +20,13 @@ QString Calculator::calculate(const QString& expression) const {
15
20
result -= value;
16
21
break ;
17
22
}
18
- pos += regexp.matchedLength ();
23
+
24
+ const int pos = match.capturedEnd ();
19
25
if (pos < expression.length ()) {
20
26
operation = expression.at (pos).toLatin1 ();
21
27
}
22
28
}
29
+
23
30
return QString::number (result);
24
31
}
25
32
You can’t perform that action at this time.
0 commit comments