Skip to main content

Evaluate postfix expression using stack in scala

Scala program for Evaluate postfix expression using stack. Here more information.

import scala.collection.mutable._;
/* 
    Scala program for 
    Evaluate postfix expression
*/
// Stack Node
class StackNode(var operand: Int,
	var next: StackNode);

class MyStack(var top: StackNode,
	var count: Int)
{
	def this()
	{
		this(null, 0);
	}
	// Returns the number of element in stack
	def isSize(): Int = {
		return count;
	}
	def isEmpty(): Boolean = {
		if (this.isSize() > 0)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	// Add a new element in stack
	def push(value: Int): Unit = {
		// Make a new stack node
		// And set as top
		this.top = new StackNode(value, this.top);
		// Increase node value
		this.count += 1;
	}
	// Remove a top element 
	def pop(): Int = {
		var value: Int = this.peek();
		if (this.isEmpty() == false)
		{
			this.top = this.top.next;
			// Reduce the size
			this.count -= 1;
		}
		return value;
	}
	// Used to get top element of stack
	def peek(): Int = {
		if (!this.isEmpty())
		{
			return this.top.operand;
		}
		else
		{
			return 0;
		}
	}
}
class Execution()
{
	def evaluate(e: String): Unit = {
		// Get the length of expression
		var size: Int = e.length();
		var a: Int = 0;
		var b: Int = 0;
		var s: MyStack = new MyStack();
		var isVaild: Boolean = true;
		var i: Int = 0;
		// work with (+,-,/,*,%) operator
		while (i < size && isVaild)
		{
			if (e.charAt(i) >= '0' && e.charAt(i) <= '9')
			{
				a = e.charAt(i).toInt - '0'.toInt;
				s.push(a);
			}
			else if (s.isSize() > 1)
			{
				a = s.pop();
				b = s.pop();
				// Perform arithmetic operations between 2 operands
				if (e.charAt(i) == '+')
				{
					s.push(b + a);
				}
				else if (e.charAt(i) == '-')
				{
					s.push(b - a);
				}
				else if (e.charAt(i) == '*')
				{
					s.push(b * a);
				}
				else if (e.charAt(i) == '/')
				{
					s.push(b / a);
				}
				else if (e.charAt(i) == '%')
				{
					s.push(b % a);
				}
				else
				{
					// When use other operator
					isVaild = false;
				}
			}
			else if (s.isSize() == 1)
			{
				// Special case  
				// When use +, - at the beginning
				if (e.charAt(i) == '-')
				{
					a = s.pop();
					s.push(-a);
				}
				else if (e.charAt(i) != '+')
				{
					// When not use  +,-
					isVaild = false;
				}
			}
			else
			{
				isVaild = false;
			}
			i += 1;
		}
		if (isVaild == false)
		{
			// Possible case use other operators
			// 1) When using special operators
			// 2) Or expression is invalid
			println(e + " Invalid expression ");
			return;
		}
		println(e + " = " + s.pop());
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Execution = new Execution();
		// Execute the following expression
		// (1+2)*(3+5)
		task.evaluate("12+35+*");
		// -((7*2) + (6/2) + 4)
		task.evaluate("72*62/+4+-");
	}
}

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