EvaluateExpression.cpp to add operators ^ for exponentiation and % for modulus.

EvaluateExpression.cpp to add operators ^ for exponentiation and % for modulus. For example, 3 ^ 2 is 9 and 3 % 2 is 1. The ^ operator has the highest precedence and the % operator has the same precedence as the * and / operators. For simplicity, assume the expression has at most one ^ operator. Sample Run: Enter an expression: (5 * 2 ^ 3 + 2 * 3 % 2) * 4 (5 * 2 ^ 3 + 2 * 3 % 2) * 4 = 160.

The Correct Answer and Explanation is:

✅ Modified EvaluateExpression.cpp

cppCopyEdit#include <iostream>
#include <string>
#include <stack>
#include <cmath>
using namespace std;

int precedence(char op) {
    if (op == '^') return 3;
    if (op == '*' || op == '/' || op == '%') return 2;
    if (op == '+' || op == '-') return 1;
    return 0;
}

int applyOp(int a, int b, char op) {
    switch (op) {
        case '+': return a + b;
        case '-': return a - b;
        case '*': return a * b;
        case '/': return a / b;
        case '%': return a % b;
        case '^': return pow(a, b);
    }
    return 0;
}

int evaluate(const string& tokens) {
    stack<int> values;
    stack<char> ops;
    for (size_t i = 0; i < tokens.length(); i++) {
        if (tokens[i] == ' ') continue;

        if (isdigit(tokens[i])) {
            int val = 0;
            while (i < tokens.length() && isdigit(tokens[i])) {
                val = (val * 10) + (tokens[i] - '0');
                i++;
            }
            values.push(val);
            i--; // adjust index after loop
        } else if (tokens[i] == '(') {
            ops.push(tokens[i]);
        } else if (tokens[i] == ')') {
            while (!ops.empty() && ops.top() != '(') {
                int val2 = values.top(); values.pop();
                int val1 = values.top(); values.pop();
                char op = ops.top(); ops.pop();
                values.push(applyOp(val1, val2, op));
            }
            ops.pop(); // remove '('
        } else {
            while (!ops.empty() && precedence(ops.top()) >= precedence(tokens[i])) {
                int val2 = values.top(); values.pop();
                int val1 = values.top(); values.pop();
                char op = ops.top(); ops.pop();
                values.push(applyOp(val1, val2, op));
            }
            ops.push(tokens[i]);
        }
    }

    while (!ops.empty()) {
        int val2 = values.top(); values.pop();
        int val1 = values.top(); values.pop();
        char op = ops.top(); ops.pop();
        values.push(applyOp(val1, val2, op));
    }

    return values.top();
}

int main() {
    string expr;
    cout << "Enter an expression: ";
    getline(cin, expr);
    cout << expr << " = " << evaluate(expr) << endl;
    return 0;
}

🧠 Explanation

This modified C++ program evaluates arithmetic expressions containing parentheses, addition, subtraction, multiplication, division, modulus, and exponentiation. The new functionality adds support for ^ (exponentiation) and % (modulus) operators.

In arithmetic, precedence defines the order in which operations are performed. In this program, the precedence function assigns values to each operator based on standard rules. Exponentiation ^ has the highest precedence, followed by multiplication *, division /, and modulus %, which share the same level. Addition + and subtraction - have the lowest.

The applyOp function takes two operands and an operator and returns the result. For exponentiation, it uses the standard pow function from the <cmath> library. Modulus uses the % operator directly since it is already supported for integers in C++.

To evaluate the expression, the program uses two stacks. One stack stores integer values, and the other stores operators. It reads the expression from left to right. When it finds a number, it builds and pushes the full number to the value stack. When it finds an operator, it compares its precedence with the operator on top of the stack. If the current operator has lower or equal precedence, it pops operators and values from the stacks and computes partial results before pushing the new operator.

Parentheses are handled specially to ensure correct grouping. After parsing, any remaining operators are applied in order using the stacks.

This code correctly evaluates an expression such as (5 * 2 ^ 3 + 2 * 3 % 2) * 4 as 160, showing that ^ and % are correctly implemented and interpreted with proper precedence.

Scroll to Top