23-02-2024

This code is designed to evaluate mathematical expressions that are given in Reverse Polish Notation (RPN). RPN is a mathematical notation where every operator follows all of its operands. For example, the expression 3 4 + in RPN is equivalent to 3 + 4 in standard notation.


#include <iostream>
#include <stack> // Includes stack data structure. Is used to handle operand storage and manipulation.
#include <sstream> // String manipulation and parsing.
#include <cmath> // This includes Mathematical functions.
#include <cctype> // Character handling functions.

double evaluateRPN(std::string expression) {
    std::stack<double> numbers; // Stack to store operands.
    std::string token; // Token to represent individual elements (operands or operators) extracted from the input expression.
    
    // Iterate over the expression.
    for (int i = 0; i < expression.size(); i++) { //If the character is a digit or a negative sign followed by a digit, it's an operand.
        // If the character is a digit or a negative sign followed by a digit, it's an operand.
        if (isdigit(expression[i]) || (expression[i] == '-' && isdigit(expression[i+1]))) {
            // Build the operand.
            while (i < expression.size() && (isdigit(expression[i]) || expression[i] == '.')) {
                token += expression[i++];
            }
            // Push the operand to the stack.
            numbers.push(std::stod(token));
            token.clear();
        }
        // If the character is an operator, perform the operation.
        if (i < expression.size()) {
            char op = expression[i];
            if (op == '+' || op == '-' || op == '*' || op == '/' || op == '^') {
                double operand2 = numbers.top();
                numbers.pop();
                double operand1 = numbers.top();
                numbers.pop();
                switch(op) {
                    case '+':
                        numbers.push(operand1 + operand2);
                        break;
                    case '-':
                        numbers.push(operand1 - operand2);
                        break;
                    case '*':
                        numbers.push(operand1 * operand2);
                        break;
                    case '/':
                        numbers.push(operand1 / operand2);
                        break;
                    case '^':
                        numbers.push(std::pow(operand1, operand2));
                        break;
                    default:
                        std::cerr << "Invalid operator: " << op << std::endl;
                        return 0;
                }
            }
        }
    }
    // If the stack is not empty, the top of the stack is the result.
    if (!numbers.empty()) {
        double result = numbers.top();
        numbers.pop();
        return result;
    } else {
        std::cerr << "Invalid expression" << std::endl;
        return 0;
    }
}

int main() {
    std::string expression;
    std::cout << "Enter:";
    std::getline(std::cin, expression);
    double result = evaluateRPN(expression);
    std::cout << "Result: " << result << std::endl;
    return 0;
}

//i is just a counter going through each letter in the math problem.