#include <string>
#include <stack>
#include <iostream>

using namespace std;

// Checks if string is an operator (as defined by assignment specifications).
bool isOperator(const string &input) {

    if (input == "*" || input == "/" || input == "%" || input == "+" || input == "-") {

        return true;

    } else {

        return false;

    }

}

// Calculates a postfix expression with integer operands using a stack
// The expression is represented using an array of strings
// Each string is either an operand or operator symbol
// Operator symbols:
// "+" addition
// "-" subtraction
// "*" multiplication
// "/" divison
// "%" remainder
// Example expression: "8", "5", "-" evaluates to a result of 3:
// 1: push 8, 2: push 5,
// 3: "-"-> pop top two values, earlier value is the left-side operand: 8 - 5
// The result of the operation (8-5) is 3, push 3 onto stack
// After evaluation of the expression, the final result should be
// the only value on the stack, return 0 if the stack is
// non-empty after popping the result
// Return 0 if expression is zero-length or if there are insufficient operands
// for an operation
// The STL Stack class can be used
// To easily convert a numeric string to an int you may use the stoi() function
// which takes a string as a parameter and returns a string
int calculatePostfixExpression(string expression[], int length) {
    
    // If the length of the expression is 0, return 0
    if (length == 0) {

        return 0;

    }

    int isValid = 0;

    // Loop through expression
    // Increment isValid for each operand, decrement isValid for each operator
    for (int pos = 0; pos < length; pos++) {

        if (!isOperator(expression[pos])) {

            isValid++;

        } else {

            isValid--;

        }

    }

    // If isValid is not equal to 1, return 0
    // Signifies not enough operands for valid postfix expression
    if (isValid != 1) {

        return 0;

    }

    // Create stack for evaluation
    stack<string> evaluationStack;

    // Loop through expression
    for (int pos = 0; pos < length; pos++) {

        // If value in expression is not an operator, push to stack
        if (!isOperator(expression[pos])) {

            evaluationStack.push(expression[pos]);

          // If value in expression is "+", pop top two values in stack, add them, then push result to stack
        } else if (expression[pos] == "+") {

            int value1 = stoi(evaluationStack.top());
            evaluationStack.pop();

            int value2 = stoi(evaluationStack.top());
            evaluationStack.pop();

            int total = value1 + value2;

            evaluationStack.push(to_string(total));

          // If value in expression is "-", pop top two values in stack, subtract them, then push result to stack
        } else if (expression[pos] == "-") {

            int value1 = stoi(evaluationStack.top());
            evaluationStack.pop();

            int value2 = stoi(evaluationStack.top());
            evaluationStack.pop();

            int total = value2 - value1;

            evaluationStack.push(to_string(total));

          // If value in expression is "*", pop top two values in stack, multiply them, then push result to stack
        } else if (expression[pos] == "*") {

            int value1 = stoi(evaluationStack.top());
            evaluationStack.pop();

            int value2 = stoi(evaluationStack.top());
            evaluationStack.pop();

            int total = value1 * value2;

            evaluationStack.push(to_string(total));

          // If value in expression is "/", pop top two values in stack, divide them, then push result to stack
        } else if (expression[pos] == "/") {

            int value1 = stoi(evaluationStack.top());
            evaluationStack.pop();

            int value2 = stoi(evaluationStack.top());
            evaluationStack.pop();

            int total = value2 / value1;

            evaluationStack.push(to_string(total));

          // If value in expression is "%", pop top two values in stack, calculate remainder, then push result to stack
        } else if (expression[pos] == "%") {

            int value1 = stoi(evaluationStack.top());
            evaluationStack.pop();

            int value2 = stoi(evaluationStack.top());
            evaluationStack.pop();

            int total = value2 % value1;

            evaluationStack.push(to_string(total));

        }

    }

    // Store top of stack as result, then pop the stack
    string result = evaluationStack.top();
    evaluationStack.pop();

    // If stack is not empty, return 0
    if (!evaluationStack.empty()) {

        return 0;

    }

    // Return result cast to integer
    return stoi(result);

}