Skip to main content

Prefix to postfix conversion using stack in ruby

Ruby program for Prefix to postfix conversion using stack. Here problem description and other solutions.

#  Ruby program for
#  Prefix to postfix conversion

#  Stack Node
class StackNode 
	# Define the accessor and reader of class StackNode
	attr_reader :data, :next
	attr_accessor :data, :next
	def initialize(data, top) 
		self.data = data
		self.next = top
	end

end

class MyStack 
	# Define the accessor and reader of class MyStack
	attr_reader :top, :count
	attr_accessor :top, :count
	def initialize() 
		self.top = nil
		self.count = 0
	end

	#  Returns the number of element in stack
	def size() 
		return self.count
	end

	def isEmpty() 
		if (self.size() > 0) 
			return false
		else
 
			return true
		end

	end

	#  Add a new element in stack
	def push(data) 
		#  Make a new stack node
		#  And set as top
		self.top = StackNode.new(data, self.top)
		#  Increase node value
		self.count += 1
	end

	#  Add a top element in stack
	def pop() 
		temp = ""
		if (self.isEmpty() == false) 
			#  Get remove top value
			temp = self.top.data
			self.top = self.top.next
			#  Reduce size
			self.count -= 1
		end

		return temp
	end

	#  Used to get top element of stack
	def peek() 
		if (!self.isEmpty()) 
			return self.top.data
		else
 
			return ""
		end

	end

end

class Conversion 
	#  Check operator
	def isOperator(text) 
		if (text == '+' || text == '-' || 
            text == '*' || text == '/' || 
            text == '^' || text == '%') 
			return true
		end

		return false
	end

	#  Check operands
	def isOperands(text) 
		if ((text >= '0' && text <= '9') || 
            (text >= 'a' && text <= 'z') || 
            (text >= 'A' && text <= 'Z')) 
			return true
		end

		return false
	end

	#  Converting the given prefix expression to 
	#  postfix expression
	def prefixToPostfix(prefix) 
		#  Get the size
		size = prefix.length
		#  Create stack object
		s = MyStack.new()
		#  Some useful variables which is using 
		#  of to storing current result
		auxiliary = ""
		op1 = ""
		op2 = ""
		isValid = true
		i = size - 1
		while (i >= 0) 
			#  Check whether given prefix location
			#  at [i] is an operator or not
			if (self.isOperator(prefix[i])) 
				#  When operator exist
				#  Check that two operands exist or not
				if (s.size() > 1) 
					op1 = s.pop()
					op2 = s.pop()
					auxiliary = op1 + op2 + prefix[i]
					s.push(auxiliary)
				else
 
					isValid = false
				end

			elsif(self.isOperands(prefix[i])) 
				#  When get valid operands
				s.push(prefix[i])
			else
 
				#  Invalid operands or operator
				isValid = false
			end

			i -= 1
		end

		if (isValid == false) 
			#  When have something wrong
			print("Invalid Prefix : ", prefix, "\n")
		else
 
			#  Display calculated result
			print(" Prefix : ", prefix, "\n")
			print(" Postfix  : ", s.pop(), "\n")
		end

	end

end

def main() 
	task = Conversion.new()
	prefix = "+*ab^cd"
	task.prefixToPostfix(prefix)
	prefix = "-+*^%adcex*y^ab"
	task.prefixToPostfix(prefix)
end

main()

Output

 Prefix : +*ab^cd
 Postfix  : ab*cd^+
 Prefix : -+*^%adcex*y^ab
 Postfix  : ad%c^e*x+yab^*-




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