1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
| #include "calculator.h" #include <QStack> #include <QString> #include <QChar> #include <cmath>
int getPriority(QChar op) { if (op == '+' || op == '-') return 1; if (op == '*' || op == '/') return 2; return 0; }
double applyOperation(double a, double b, QChar op) { switch (op.unicode()) { case '+': return a + b; case '-': return a - b; case '*': return a * b; case '/': if (b == 0) throw "Division by zero"; return a / b; default: return 0; } }
QString calculate(QString expression) { try { expression = expression.replace(" ", "");
QStack<double> values; QStack<QChar> operators;
for (int i = 0; i < expression.length(); i++) { QChar c = expression[i];
if (c.isDigit() || c == '.') { QString numStr; while (i < expression.length() && (expression[i].isDigit() || expression[i] == '.')) { numStr += expression[i]; i++; } i--;
bool ok; double num = numStr.toDouble(&ok); if (!ok) throw "Invalid number format"; values.push(num); } else if (c == '(') { operators.push(c); } else if (c == ')') { while (!operators.empty() && operators.top() != '(') { if (values.size() < 2) throw "Invalid expression";
double b = values.top(); values.pop(); double a = values.top(); values.pop(); QChar op = operators.top(); operators.pop();
values.push(applyOperation(a, b, op)); }
if (operators.empty()) throw "Mismatched parentheses"; operators.pop(); } else if (c == '+' || c == '-' || c == '*' || c == '/') { if (c == '-' && (i == 0 || expression[i-1] == '(' || expression[i-1] == '+' || expression[i-1] == '-' || expression[i-1] == '*' || expression[i-1] == '/')) { QString numStr = "-"; i++; while (i < expression.length() && (expression[i].isDigit() || expression[i] == '.')) { numStr += expression[i]; i++; } i--;
bool ok; double num = numStr.toDouble(&ok); if (!ok) throw "Invalid number format"; values.push(num); continue; }
while (!operators.empty() && getPriority(operators.top()) >= getPriority(c)) { if (values.size() < 2) throw "Invalid expression";
double b = values.top(); values.pop(); double a = values.top(); values.pop(); QChar op = operators.top(); operators.pop();
values.push(applyOperation(a, b, op)); } operators.push(c); } else { throw "Invalid character in expression"; } }
while (!operators.empty()) { if (values.size() < 2) throw "Invalid expression";
double b = values.top(); values.pop(); double a = values.top(); values.pop(); QChar op = operators.top(); operators.pop();
values.push(applyOperation(a, b, op)); }
if (values.size() != 1 || !operators.empty()) { throw "Invalid expression"; }
double result = values.top(); if (std::abs(result - std::round(result)) < 1e-10) { return QString::number(static_cast<long long>(std::round(result))); } else { return QString::number(result, 'g', 15); } } catch (const char* error) { return QString("Error: ") + error; } catch (...) { return "Error: Unknown error"; } }
|