Given an expression as a string str consisting of numbers and basic arithmetic operators(+, -, *, /), the task is to solve the expression. Note that the numbers used in this program are single-digit numbers and parentheses are not allowed.
Input: str = “3/3+4*6-9”
Since (3 / 3) = 1 and (4 * 6) = 24.
So the overall expression becomes (1 + 24 – 9) = 16
Input: str = “9*5-4*5+9”
Approach: A Stack class is created to store both numbers and operators (both as characters). The stack is a useful storage mechanism because, when parsing expressions, the last item stored needs to be accessed frequently; and a stack is a last-in-first-out (LIFO) container.
Besides the Stack class, a class called express(short for expression) is also created, representing an entire arithmetic expression. Member functions for this class allow the user to initialize an object with an expression in the form of a string, parse the expression, and return the resulting arithmetic value.
Here’s how an arithmetic expression is parsed.
A pointer is started at the left and is iterated to look at each character. It can be either a number(always a single-digit character between 0 and 9) or an operator (the characters +, -, *, and /).
If the character is a number, it is pushed onto the stack. The first operator encountered is also pushed into the stack. The trick is subsequent operators are handled. Note that the current operator can’t be executed because the number that follows it hasn’t been read yet. Finding an operator is merely the signal that we can execute the previous operator, which is stored on the stack. That is, if the sequence 2+3 is on the stack, we wait until we find another operator before carrying out the addition.
Thus, whenever the current character is an operator (except the first), the previous number (3 in the preceding example) and the previous operator (+) are popped off the stack, placing them in the variables lastval and lastop. Finally, the first number (2) is popped and the arithmetic operation is carried on the two numbers (obtaining 5).
However, when * and / which have higher precedence than + and – are encountered, the expression can’t be executed. In the expression 3+4/2, the + cant be executed until the division is performed. So, the 2 and the + are put back on the stack until the division is carried out.
On the other hand, if the current operator is a + or -, the previous operator can be executed. That is when the + is encountered in the expression 4-5+6, it’s all right to execute the -, and when the – is encountered in 6/2-3, it’s okay to do the division. Table 10.1 shows the four possibilities.
|Previous Operator||Current Operator||Example||Action|
|+ or –||* or /||3+4/||Push previous operator and previous number (+, 4)|
|* or /||* or /||9/3*||Execute previous operator, push result (3)|
|+ or –||+ or –||6+3+||Execute previous operator, push result (9)|
|* or /||+ or –||8/2-||Execute previous operator, push result (4)|
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.