[data structure] – infix expression evaluation

Time:2021-3-9

Title Requirements

Input infix expression from keyboard, build stack of operands and operators, calculate and output evaluation result of expression.
Basic requirements: implement +, -, *, / four binary operators and (); the range of operands is 0 to 9.
Improve the requirements: implement +, – two unary operators (that is, positive and negative signs); the operands can be any integer value (the program may not consider the calculation overflow).
If two integers are divided, only the integer quotient is retained (the remainder is discarded); the syntax error of the expression is not handled.

Knowledge points involved

Stack and queue

Data structure design

In each stack object, the element pointer is used to build an array of length N, n represents the maximum number of elements in the stack, and top represents the top pointer.

Algorithm description

① Infix expressions end with ‘#’, and ‘#’ characters are also treated as operators.
② ‘(‘, ”) are treated as operators, and the brackets need to be removed after the operation in brackets.
③ Two different types of stacks need to be built, the integer type operands stack opnd and the character type operators stack OPTR.
④ The main steps of the algorithm are as follows
A. initially, opnd is set to null, and ‘#’ is used as the bottom element of OPTR stack;
B. read each character in the expression in turn. If it is an operand, directly press it into the opnd stack;
C. if operator (denoted as θ), PTR stack top operator (denoted as λ) has priority;
When λ < θ, θ is pushed into the opt stack to continue to read the next character;
λ = θ, remove the bracket and continue to read the next character;
When λ > θ, λ operation is performed (λ is off the stack), and θ is outside the stack (the next character is not read), that is, θ continues to compare the priority with the top of the OPTR stack operator (repeat the above operation) until θ can enter the stack.
⑤ For unary operators (positive and negative signs), use ‘p’ for ‘+’, and ‘n’ for ‘-‘.

Program code

#include<iostream>
using namespace std;
template <typename T>
Class stack // template class: stack
{
public:
    Stack(); // default constructor
    Stack (int n); // the constructor calls the function createstack (int n) to create a stack of length n
    ~Stack(); // fictitious function
    Int createstack (int n); // create a stack of length n
    Int empty(); // judge whether the stack is empty
    Int full(); // judge whether the stack is full
    Int push (t e); // push the element E onto the stack
    Int pop (T & E); // the element is out of the stack and saved in E
    T get_ Top(); // get the top of stack element
    Friend int operator (char & E); // judge whether character e is an operator
    Friend int ISP (char & E); // returns the priority of the operators in the stack
    Friend int ICP (char & E); // returns the priority of the off stack operator
    Friend int compute (int x, char a, int y); // evaluation function
private:
    T * elem; // create an array of length n
    Int n; // the maximum number of elements in the stack
    Int top; // top of stack pointer
};
template<typename T>
Stack<T>::Stack()
{
    top = -1;
}
template<typename T>
Stack<T>::Stack(int n)
{
    createStack(n);
}
template<typename T>
Stack<T>::~Stack()
{
    n = 0;
    top = -1;
    delete[]elem;
}
template<typename T>
int Stack<T>::createStack(int n)
{
    if (n <= 0)
        return 0;
    this->n = n;
    top = -1;
    elem = new T[n];
    if (!elem)
        return 0;
    return 1;
}
template<typename T>
int Stack<T>::empty()
{
    return top == -1;
}
template<typename T>
int Stack<T>::full()
{
    return top >= n - 1;
}
template<typename T>
int Stack<T>::push(T e)
{
    if (top >= n - 1)
        return 0;
    elem[++top] = e;
    return 1;
}
template<typename T>
int Stack<T>::pop(T & e)
{
    if (top == -1)
        return 0;
    e = elem[top--];
    return 1;
}
template<typename T>
T Stack<T>::get_top()
{
    return elem[top];
};
Int operator (char & E) // judge whether it is an operator
{
    if (e == '+' || e == '-' || e == '*' || e == '/' || e == '(' || e == ')' || e == '#' || e == 'P' || e == 'N')
        Return 1; // is the operator that returns 1
    else
        Return 0; // not an operator, return 0
}
Int ISP (char & E) // returns the priority of the operators in the stack
{
    switch (e)
    {
    case '#':
        return 0; break;
    case '(':
        return 1; break;
    case '+':
    case '-':
        return 2; break;
    case '*':
    case '/':
        return 3; break;
    case 'P':
    case 'N':
        return 4; break;
    case ')':
        return 5; break;
    default:
        return -1; break;
    }
}
Int ICP (char & E) // returns the priority of the off stack operator
{
    switch (e)
    {
    case '#':
        return 0; break;
    case ')':
        return 1; break;
    case '+':
    case '-':
        return 2; break;
    case '*':
    case '/':
        return 3; break;
    case 'P':
    case 'N':
        return 4; break;
    case '(':
        return 5; break;
    default:
        return -1; break;
    }
}
int compute(int x, char a, int y)
{
    switch (a)
    {
    Case '+': // calculate addition
        return x + y; break;
    Case '-': // calculate subtraction
        return x - y; break;
    Case '*': // calculate multiplication
        return x * y; break;
    Case '/': // calculate Division
        return x / y; break;
    default:
        return -1; break;
    }
}
int g1()
{
    char a, b, c;
    int i, j, f, value, firstOpnd, secondOpnd, m;
    Stack < char > opt R (max); // build operator stack
    Stack < int > opnd (max); // create the stack of operands
    OPTR.push ('#'; // '#' stack
    Cout < "please input infix expression":;
    a = getchar();
    while (a != '#' || OPTR.get_top() != '#')
    {
        If (! Isoperator (a)) // is not an operator, it is an operand, and the operands are put on the stack
            OPND.push (a - 48); // convert character type to integer number
        Else // is an operator that compares the priority with the top of stack operator
        {
            b =  OPTR.get_ Top(); // get the top of stack element
            I = ISP (b); // priority of top of stack operator
            J = ICP (a); // priority of off stack operators
            If (I < J) // operators out of stack have higher priority and operators in stack
                OPTR.push(a);
            else
            {
                OPTR.pop(b);                    
                if (b != '('&&i == j || i > j)
                {
                    c = OPTR.get_top();
                    If ((C = = '(' || C = = '#') & (b = ='p '|| B = ='n')) / * C is a unitary operation
                                                                            Operator: Sign*/
                    {
                        OPND.pop (firstopnd); // get the operands
                        switch (b)
                        {
                        Case 'p': // positive sign
                            f = firstOpnd * 1;
                            break;
                        Case 'n': // negative sign
                            f = firstOpnd * (-1);
                            break;
                        }
                    }
                    Else // C is a binary operator
                    {
                        OPND.pop (secondopnd); // get the second operand
                        OPND.pop (firstopnd); // get the first operand
                        F = compute (firstopnd, B, secondopnd); // calculate and evaluate
                    }
                    OPND.push (f) ; // stack the evaluation results
                    continue;
                }
            }
        }
        c = a;
        A = getchar(); // continue reading characters
        While (! Isolator (a) & &! Isolator (c)) / * if the characters read continuously are all numbers, multiply by bit weight
                                                  Get multiple bits*/
        {
            OPND.pop(m);
            m = m * 10 + a - 48;
            OPND.push(m);
            c = a;
            a = getchar();
        }
        
    }
    OPND.pop(value);
    Return value; // returns the result of the expression
}
int main()
{
    int a;
    a = g1();
    Cout < "the operation result is: < a < endl;
    return 0;
}

Examples

(1)Program input:3+5*(9-5)/10#
Program output:5
[data structure] - infix expression evaluation

(2)Program input:P9+10*(N2*8/4)-10#
Program output:-41
[data structure] - infix expression evaluation

(3)Program input:20-3*25+(N41/3)*100#
Program output:-1355
[data structure] - infix expression evaluation