Skip to main content

Evaluate postfix expression using stack in c++

C++ program for Evaluate postfix expression using stack. Here problem description and other solutions.

// Include header file
#include <iostream>
#include <string>
using namespace std;
/*
    C++ program for 
    Evaluate postfix expression
*/

// Stack Node
class StackNode
{
	public: 
    int operand;
	StackNode *next;
	StackNode(int operand, StackNode *top)
	{
		this->operand = operand;
		this->next = top;
	}
};
class MyStack
{
	public: StackNode *top;
	int count;
	MyStack()
	{
		this->top = nullptr;
		this->count = 0;
	}
	// Returns the number of element in stack
	int size()
	{
		return this->count;
	}
	bool isEmpty()
	{
		if (this->size() > 0)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	// Add a new element in stack
	void push(int value)
	{
		// Make a new stack node
		// And set as top
		this->top = new StackNode(value, this->top);
		// Increase node value
		this->count++;
	}
	// Remove a top element 
	int pop()
	{
		int value = this->peek();
		if (this->isEmpty() == false)
		{
          	StackNode *t = this->top;
			this->top = this->top->next;
			// Reduce the size
			this->count--;
          	delete t;
		}
		return value;
	}
	// Used to get top element of stack
	int peek()
	{
		if (!this->isEmpty())
		{
			return this->top->operand;
		}
		else
		{
			return 0;
		}
	}
};
class Execution
{
	public: void evaluate(string e)
	{
		// Get the length of expression
		int size = e.length();
		int a = 0;
		int b = 0;
		MyStack *s = new MyStack();
		bool isVaild = true;
		// work with (+,-,/,*,%) operator
		for (int i = 0; i < size && isVaild; i++)
		{
			if (e[i] >= '0' && e[i] <= '9')
			{
				a = e[i] - '0';
				s->push(a);
			}
			else if (s->size() > 1)
			{
				a = s->pop();
				b = s->pop();
				// Perform arithmetic operations between 2 operands
				if (e[i] == '+')
				{
					s->push(b + a);
				}
				else if (e[i] == '-')
				{
					s->push(b - a);
				}
				else if (e[i] == '*')
				{
					s->push(b *a);
				}
				else if (e[i] == '/')
				{
					s->push(b / a);
				}
				else if (e[i] == '%')
				{
					s->push(b % a);
				}
				else
				{
					// When use other operator
					isVaild = false;
				}
			}
			else if (s->size() == 1)
			{
				// Special case  
				// When use +, - at the beginning
				if (e[i] == '-')
				{
					a = s->pop();
					s->push(-a);
				}
				else if (e[i] != '+')
				{
					// When not use  +,-
					isVaild = false;
				}
			}
			else
			{
				isVaild = false;
			}
		}
		if (isVaild == false)
		{
			// Possible case use other operators
			// 1) When using special operators
			// 2) Or expression is invalid
			cout << e << " Invalid expression " << endl;
			return;
		}
		cout << e << " = " << s->pop() << endl;
	}
};
int main()
{
	Execution *task = new Execution();
	// Execute the following expression
	// (1+2)*(3+5)
	task->evaluate("12+35+*");
	// -((7*2) + (6/2) + 4)
	task->evaluate("72*62/+4+-");
	return 0;
}

Output

12+35+* = 24
72*62/+4+- = -21




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment