14 result= result.toDouble() - term.toDouble();
15 }
16 } else {
17 result = Invalid;
18 }
19 }
20 return result;
21 }
Во-первых, мы вызываем функцию
Мы продолжаем эту процедуру, пока не закончатся термы. Это даст правильный результат, потому что операции сложения и вычитания обладают свойством «ассоциативности слева» (left—associative), то есть «1—2—3» означает «(1—2)—3», а не «1—(2—3)».
01 QVariant Cell::evalTerm(const QString &str, int &pos) const
02 {
03 QVariant result = evalFactor(str, pos);
04 while (str[pos] != QChar::Null) {
05 QChar op = str[pos];
06 if (op != '*' && op != '/')
07 return result;
08 ++pos;
09 QVariant factor = evalFactor(str, pos);
10 if (result.type() == QVariant::Double &&
11 factor.type() == QVariant::Double) {
12 if (op == '*') {
13 result = result.toDouble() * factor.toDouble();
14 } else {
15 if (factor.toDouble() == 0.0) {
16 result = Invalid;
17 } else {
18 result = result.toDouble() / factor.toDouble();
19 }
20 }
21 } else {
22 result = Invalid;
23 }
24 }
25 return result;
26 }
Функция
01 QVariant Cell::evalFactor(const QString &str, int &pos) const
02 {
03 QVariant result;
04 bool negative = false;
05 if (str[pos] == '-') {
06 negative = true;
07 ++pos;
08 }
09 if (str[pos] == '(') {
10 ++pos;
11 result = evalExpression(str, pos);
12 if (str[pos] != ')')
13 result = Invalid;
14 ++pos;
15 } else {
16 QRegExp regExp("[A-Za-z][1-9][0-9]{0,2}");
17 QString token;
18 while (str[pos].isLetterOrNumber() || str[pos] == '.') {
19 token += str[pos];
20 ++pos;
21 }
22 if (regExp.exactMatch(token)) {
23 int column = token[0].toUpper().unicode() - 'A';