From 70f735987ef4df6a7fe21ea131adaf7794a1b660 Mon Sep 17 00:00:00 2001 From: Rob Freiburger Date: Thu, 11 Apr 2013 20:04:07 -0700 Subject: [PATCH 1/8] Adds -, *, and / operations. --- calculator.rb | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/calculator.rb b/calculator.rb index e7e20c6..bf3c728 100644 --- a/calculator.rb +++ b/calculator.rb @@ -47,6 +47,32 @@ def run else puts "Error: Not Enough Operands" end + when '-' + if @memory.length >= 2 + op1 = @memory.pop + op2 = @memory.pop + @memory.push(op1 - op2) + puts "= #{@memory.last}" + else + puts "Error: Not Enough Operands" + end + when '*' + if @memory.length >= 2 + op1 = @memory.pop + op2 = @memory.pop + @memory.push(op1 * op2) + puts "= #{@memory.last}" + else + puts "Error: Not Enough Operands" + end + when '/' + if @memory.length >= 2 + op1 = @memory.pop + op2 = @memory.pop + @memory.push(op1 / op2) + puts "= #{@memory.last}" + else + puts "Error: Not Enough Operands" else puts "Error: Unsupported Operator: #{input}" unless input.empty? end From c6fb1041158f6d76bb215e1afad07fc85f532dea Mon Sep 17 00:00:00 2001 From: Rob Freiburger Date: Thu, 11 Apr 2013 20:22:32 -0700 Subject: [PATCH 2/8] Adds paper tape function. --- calculator.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/calculator.rb b/calculator.rb index bf3c728..02d0f78 100644 --- a/calculator.rb +++ b/calculator.rb @@ -6,6 +6,7 @@ class CalculatorEngine def initialize @memory = [] + @history = [] end @@ -18,6 +19,7 @@ def run if input input = input.chomp + @history.push(input) if input == 'q' done = true @@ -38,6 +40,13 @@ def run puts "\t#{position}: #{m}" position = position + 1 end + when 'p' + puts "Paper Tape:" + position = 0 + @history.each do |m| + puts "\t#{position}: #{m}" + position = position + 1 + end when '+' if @memory.length >= 2 op1 = @memory.pop From 7040005af439d09596b721279d478b5ec7a495db Mon Sep 17 00:00:00 2001 From: Rob Freiburger Date: Thu, 11 Apr 2013 20:25:39 -0700 Subject: [PATCH 3/8] Adds clear all function. --- calculator.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/calculator.rb b/calculator.rb index 02d0f78..fa17a90 100644 --- a/calculator.rb +++ b/calculator.rb @@ -33,6 +33,10 @@ def run when 'c' @memory = [] puts "Memory Cleared" + when 'ca' + @memory = [] + @history = [] + puts "Memory and History Cleared" when 'm' puts "Memory:" position = 0 From b9adc42b58b0c9c9501b5674b2df2006559df483 Mon Sep 17 00:00:00 2001 From: Rob Freiburger Date: Thu, 11 Apr 2013 20:31:33 -0700 Subject: [PATCH 4/8] Adds pi command. --- calculator.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/calculator.rb b/calculator.rb index fa17a90..ae68637 100644 --- a/calculator.rb +++ b/calculator.rb @@ -51,6 +51,9 @@ def run puts "\t#{position}: #{m}" position = position + 1 end + when 'pi' + @memory.push(3.14) + puts @memory.last when '+' if @memory.length >= 2 op1 = @memory.pop From 25a4f910ce4cae91be5fae318ef867ceba01d959 Mon Sep 17 00:00:00 2001 From: Rob Freiburger Date: Thu, 18 Apr 2013 19:17:44 -0700 Subject: [PATCH 5/8] Fixes missing end to if conditional. --- calculator.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/calculator.rb b/calculator.rb index ae68637..e211653 100644 --- a/calculator.rb +++ b/calculator.rb @@ -89,6 +89,7 @@ def run puts "= #{@memory.last}" else puts "Error: Not Enough Operands" + end else puts "Error: Unsupported Operator: #{input}" unless input.empty? end From a150b581fb03a7fb68b790e64b1f1712a7c5b942 Mon Sep 17 00:00:00 2001 From: Rob Freiburger Date: Wed, 8 May 2013 15:07:26 -0700 Subject: [PATCH 6/8] Adds sqrt. --- calculator.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/calculator.rb b/calculator.rb index e211653..08b885d 100644 --- a/calculator.rb +++ b/calculator.rb @@ -54,6 +54,14 @@ def run when 'pi' @memory.push(3.14) puts @memory.last + when 'sqrt' + if @memory.length >= 2 + num = @memory.pop + @memory.push(num * num) + puts "= #{@memory.last}" + else + puts "Error: Not Enough Operands" + end when '+' if @memory.length >= 2 op1 = @memory.pop From ea71df4c1b299cc991cf37f56b0b957615a48db8 Mon Sep 17 00:00:00 2001 From: Rob Freiburger Date: Wed, 8 May 2013 15:30:22 -0700 Subject: [PATCH 7/8] Adds floating number support. --- calculator.rb | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/calculator.rb b/calculator.rb index 08b885d..1636739 100644 --- a/calculator.rb +++ b/calculator.rb @@ -26,10 +26,6 @@ def run else # operators will work on the stack, numbers will be added to stack case input - when '0','1','2','3','4','5','6','7','8','9' - # ['0','1'].include?, %w(0 1).include? - @memory.push(input.to_f) - puts @memory.last when 'c' @memory = [] puts "Memory Cleared" @@ -98,8 +94,17 @@ def run else puts "Error: Not Enough Operands" end + when '0','1','2','3','4','5','6','7','8','9' + # ['0','1'].include?, %w(0 1).include? + @memory.push(input.to_f) + puts @memory.last else - puts "Error: Unsupported Operator: #{input}" unless input.empty? + if input =~ /^\d+\.\d*$/ + @memory.push(input.to_f) + puts @memory.last + else + puts "Error: Unsupported Operator: #{input}" unless input.empty? + end end end From e54f0ec796ef55c6c335dfef7e64c06b0fb14115 Mon Sep 17 00:00:00 2001 From: Rob Freiburger Date: Wed, 8 May 2013 16:01:58 -0700 Subject: [PATCH 8/8] Fixes sqrt to perform actual square root rather than inverse. --- calculator.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/calculator.rb b/calculator.rb index 1636739..8fc5989 100644 --- a/calculator.rb +++ b/calculator.rb @@ -51,9 +51,9 @@ def run @memory.push(3.14) puts @memory.last when 'sqrt' - if @memory.length >= 2 + if @memory.length >= 1 num = @memory.pop - @memory.push(num * num) + @memory.push(Math.sqrt(num)) puts "= #{@memory.last}" else puts "Error: Not Enough Operands"