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.

**Examples:**

Input:str = “3/3+4*6-9”

Output:16

Since (3 / 3) = 1 and (4 * 6) = 24.

So the overall expression becomes (1 + 24 – 9) = 16

Input:str = “9*5-4*5+9”

Output:16

**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:

`// C++ implementation of the approach ` `#include <cstring> ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `// Length of expressions in characters ` `const` `int` `LEN = 80; ` ` ` `// Size of the stack ` `const` `int` `MAX = 40; ` ` ` `class` `Stack { ` `private` `: ` ` ` `// Stack: array of characters ` ` ` `char` `st[MAX]; ` ` ` ` ` `// Number at top of the stack ` ` ` `int` `top; ` ` ` `public` `: ` ` ` `Stack() ` ` ` `{ ` ` ` `top = 0; ` ` ` `} ` ` ` ` ` `// Function to put a character in stack ` ` ` `void` `push(` `char` `var) ` ` ` `{ ` ` ` `st[++top] = var; ` ` ` `} ` ` ` ` ` `// Function to return a character off stack ` ` ` `char` `pop() ` ` ` `{ ` ` ` `return` `st[top--]; ` ` ` `} ` ` ` ` ` `// Function to get the top of the stack ` ` ` `int` `gettop() ` ` ` `{ ` ` ` `return` `top; ` ` ` `} ` `}; ` ` ` `// Expression class ` `class` `Express { ` `private` `: ` ` ` `// Stack for analysis ` ` ` `Stack s; ` ` ` ` ` `// Pointer to input string ` ` ` `char` `* pStr; ` ` ` ` ` `// Length of the input string ` ` ` `int` `len; ` ` ` `public` `: ` ` ` `Express(` `char` `* ptr) ` ` ` `{ ` ` ` `pStr = ptr; ` ` ` `len = ` `strlen` `(pStr); ` ` ` `} ` ` ` ` ` `// Parse the input string ` ` ` `void` `parse(); ` ` ` ` ` `// Evaluate the stack ` ` ` `int` `solve(); ` `}; ` ` ` `void` `Express::parse() ` `{ ` ` ` ` ` `// Character from the input string ` ` ` `char` `ch; ` ` ` ` ` `// Last value ` ` ` `char` `lastval; ` ` ` ` ` `// Last operator ` ` ` `char` `lastop; ` ` ` ` ` `// For each input character ` ` ` `for` `(` `int` `j = 0; j < len; j++) { ` ` ` `ch = pStr[j]; ` ` ` ` ` `// If it's a digit then save ` ` ` `// the numerical value ` ` ` `if` `(ch >= ` `'0'` `&& ch <= ` `'9'` `) ` ` ` `s.push(ch - ` `'0'` `); ` ` ` ` ` `// If it's an operator ` ` ` `else` `if` `(ch == ` `'+'` `|| ch == ` `'-'` ` ` `|| ch == ` `'*'` `|| ch == ` `'/'` `) { ` ` ` ` ` `// If it is the first operator ` ` ` `// then put it in the stack ` ` ` `if` `(s.gettop() == 1) ` ` ` ` ` `s.push(ch); ` ` ` ` ` `// Not the first operator ` ` ` `else` `{ ` ` ` `lastval = s.pop(); ` ` ` `lastop = s.pop(); ` ` ` ` ` `// If it is either '*' or '/' and the ` ` ` `// last operator was either '+' or '-' ` ` ` `if` `((ch == ` `'*'` `|| ch == ` `'/'` `) ` ` ` `&& (lastop == ` `'+'` `|| lastop == ` `'-'` `)) { ` ` ` ` ` `// Restore the last two pops ` ` ` `s.push(lastop); ` ` ` `s.push(lastval); ` ` ` `} ` ` ` ` ` `// In all the other cases ` ` ` `else` `{ ` ` ` ` ` `// Perform the last operation ` ` ` `switch` `(lastop) { ` ` ` ` ` `// Push the result in the stack ` ` ` `case` `'+'` `: ` ` ` `s.push(s.pop() + lastval); ` ` ` `break` `; ` ` ` `case` `'-'` `: ` ` ` `s.push(s.pop() - lastval); ` ` ` `break` `; ` ` ` `case` `'*'` `: ` ` ` `s.push(s.pop() * lastval); ` ` ` `break` `; ` ` ` `case` `'/'` `: ` ` ` `s.push(s.pop() / lastval); ` ` ` `break` `; ` ` ` `default` `: ` ` ` `cout << ` `"\nUnknown oper"` `; ` ` ` `exit` `(1); ` ` ` `} ` ` ` `} ` ` ` `s.push(ch); ` ` ` `} ` ` ` `} ` ` ` `else` `{ ` ` ` `cout << ` `"\nUnknown input character"` `; ` ` ` `exit` `(1); ` ` ` `} ` ` ` `} ` `} ` ` ` `int` `Express::solve() ` `{ ` ` ` `// Remove the items from stack ` ` ` `char` `lastval; ` ` ` `while` `(s.gettop() > 1) { ` ` ` `lastval = s.pop(); ` ` ` `switch` `(s.pop()) { ` ` ` ` ` `// Perform operation, push answer ` ` ` `case` `'+'` `: ` ` ` `s.push(s.pop() + lastval); ` ` ` `break` `; ` ` ` `case` `'-'` `: ` ` ` `s.push(s.pop() - lastval); ` ` ` `break` `; ` ` ` `case` `'*'` `: ` ` ` `s.push(s.pop() * lastval); ` ` ` `break` `; ` ` ` `case` `'/'` `: ` ` ` `s.push(s.pop() / lastval); ` ` ` `break` `; ` ` ` `default` `: ` ` ` `cout << ` `"\nUnknown operator"` `; ` ` ` `exit` `(1); ` ` ` `} ` ` ` `} ` ` ` `return` `int` `(s.pop()); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` ` ` `char` `string[LEN] = ` `"2+3*4/3-2"` `; ` ` ` ` ` `// Make expression ` ` ` `Express* eptr = ` `new` `Express(string); ` ` ` ` ` `// Parse it ` ` ` `eptr->parse(); ` ` ` ` ` `// Solve the expression ` ` ` `cout << eptr->solve(); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

4

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.

## Recommended Posts:

- How to find index of any Currency Symbols in a given string
- Split numeric, alphabetic and special symbols from a String
- Solve the Logical Expression given by string
- Evaluate a boolean expression represented as string
- How to check string is alphanumeric or not using Regular Expression
- Find all the patterns of "1(0+)1" in a given string | SET 2(Regular Expression Approach)
- Check if a binary string has a 0 between 1s or not | Set 2 (Regular Expression Approach)
- Check if a string contains only alphabets in Java using Lambda expression
- Check if a given string is a valid number (Integer or Floating Point) in Java | SET 2 (Regular Expression approach)
- Building Expression tree from Prefix Expression
- Parsing Apache access log in Java
- WildCard pattern matching having three symbols ( * , + , ? )
- Expression Evaluation
- Expression contains redundant bracket or not
- Arithmetic Expression Evalution
- Balanced expression with replacement
- Find if an expression has duplicate parenthesis or not
- Stack | Set 4 (Evaluation of Postfix Expression)
- How to validate CVV number using Regular Expression
- Check if expression contains redundant bracket or not | Set 2

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.