#include <iostream>#include <stack>#include <string>using namespace std;
int main() {
stack<int> nums;
stack<char> ops;
string expression;
cin >> expression;
cout << "Step 1: Expression entered: " << expression << endl;
for (int i = 0; i < expression.size(); i++) {
if (isdigit(expression[i])) {
int num = 0;
while (i < expression.size() && isdigit(expression[i])) {
num = num * 10 + (expression[i] - '0');
i++;
}
nums.push(num);
i--; // Backtrack to the end of the number
} else if (expression[i] == '+' || expression[i] == '-' || expression[i] == '*' || expression[i] == '/') {
if (!ops.empty() && (ops.top() == '*' || ops.top() == '/')) {
int num2 = nums.top();
nums.pop();
int num1 = nums.top();
nums.pop();
if (ops.top() == '*') {
nums.push(num1 * num2);
} else if (ops.top() == '/') {
nums.push(num1 / num2);
}
ops.pop();
cout << "Step 2: Result: " << nums.top() << endl;
} else {
ops.push(expression[i]);
cout << "Step 3: Operator added: " << expression[i] << endl;
}
} else {
cout << "Step 4: Invalid character encountered: " << expression[i] << endl;
}
}
while (!ops.empty()) {
char op = ops.top();
ops.pop();
int num2 = nums.top();
nums.pop();
int num1 = nums.top();
nums.pop();
if (op == '+') {
nums.push(num1 + num2);
} else if (op == '-') {
nums.push(num1 - num2);
} else if (op == '*') {
nums.push(num1 * num2);
} else if (op == '/') {
nums.push(num1 / num2);
} else {
cout << "Step 5: Invalid operator encountered: " << op << endl;
}
cout << "Step 6: Result: " << nums.top() << endl;
}
cout << "Step 7: Final result: " << nums.top() << endl;
return 0;
}