Commits

Petar Stanev committed 1ac1b09

calc

Comments (0)

Files changed (1)

+class Array
+    alias :peek :last
+end
+
+
+
+
+class Objects
+
+stack = Array.new
+
+while true
+
+    
+
+    new = gets.chomp
+
+    while new.size > 0
+
+        
+        if new =~ /^\s*([-+]?[0-9]*\.?[0-9]+)\s*/
+            
+			
+            stack.push $1.to_f
+        else
+			if new =~ /^\s*([\+\-\*\/])\s*/ then
+            
+				operator = $1
+
+            
+				operand_second = stack.pop
+				operand_first = stack.pop
+
+            
+			
+				stack.push  case operator 
+					when '+'
+						operand_first + operand_second
+					when '-'
+						operand_first - operand_second
+					when '*'
+						operand_first * operand_second
+					when '/'
+						operand_first / operand_second
+					end
+			end		
+        end
+
+        
+        new.sub!($&, "")
+    end
+
+   
+    puts "  #{stack.peek}"
+end
+end