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.
std::stack
std::string token;: This line creates a string called token to build up each number as it’s read from the expression.
for (int i = 0; i < expression.size(); i++) {: This line starts a loop that goes through each character in the expression.
if (isdigit(expression[i]) | (expression[i] == ‘-‘ && isdigit(expression[i+1]))) {: This line checks if the current character is a digit or a negative sign followed by a digit, indicating it’s part of a number. |
while (i < expression.size() && (isdigit(expression[i]) | expression[i] == ‘.’)) {: This line starts a loop that adds each digit to token until it encounters a character that’s not part of the number. |
token += expression[i++];: This line adds the current character to token.
numbers.push(std::stod(token));: Once the entire number has been read, this line converts it to a double and pushes it onto the numbers stack.
token.clear();: This line clears token to prepare for the next number.
if (i < expression.size()) {: This line checks if there are still characters left in the expression to process.
char op = expression[i];: This line gets the current character and stores it in op.
if (op == ‘+’ | op == ‘-‘ | op == ‘*’ | op == ‘/’ | op == ‘^’) {: This line checks if the current character is an operator. |
double operand2 = numbers.top(); numbers.pop();: This line gets the top number from the stack, stores it in operand2, and removes it from the stack.
double operand1 = numbers.top(); numbers.pop();: This line does the same for the next number on the stack, storing it in operand1.
switch(op) {: This line starts a switch statement that performs the operation corresponding to the operator.
numbers.push(operand1 + operand2);: If the operator is ‘+’, this line adds the two operands and pushes the result onto the stack.
std::cerr « “Invalid operator: “ « op « std::endl;: If the operator is not recognized, this line prints an error message.
if (!numbers.empty()) {: After all characters in the expression have been processed, this line checks if the numbers stack is empty.
double result = numbers.top(); numbers.pop();: If the stack is not empty, this line gets the top number from the stack (the final result of the expression), stores it in result, and removes it from the stack.
#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.