From d60821f33430e46a1c407a128db649774bd730ef Mon Sep 17 00:00:00 2001 From: npeters5 Date: Wed, 7 Feb 2018 10:25:52 -0800 Subject: [PATCH 1/3] Adding my calculator.rb file --- calculator.rb | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 calculator.rb diff --git a/calculator.rb b/calculator.rb new file mode 100644 index 0000000..cf1cec3 --- /dev/null +++ b/calculator.rb @@ -0,0 +1,81 @@ +# Baseline +# The program should ask the user for an operation (string or numeric symbol) and two numbers. + +# Primary Requirements +# The program should use the input operation and two numbers to provide the result of applying the operation to the two numbers. +# The program should have support for these four operations: addition, subtraction, multiplication, and division. +# The program should accept both the name (`add`) and the symbol (`+`) for each possible operation. + +def addition(x, y) + x + y +end + +def subtraction(x, y) + x - y +end + +def multiplication(x, y) + x * y +end + +def division(x, y) + x / y +end + +puts "Welcome to Calculator!" +puts "I'm going to ask you for two numbers, and an operator. What is your first number?: " +num_1 = gets.chomp + +puts num_1.Class + +until num_1.is_a?(Integer) + puts "Invalid input. Try again: " + num_1 = gets.chomp +end + +puts "Great! What is your second number? (Must be greater than zero)" +num_2 = gets.chomp + +until (num_2 > 0) && (num_2.is_a? Integer) + print "Invalid input. Try again: " + num_2 = gets.chomp +end + +puts "Awesome. Now, what would you like to do to these numbers? You can type add (+), subtract (-), multiply (*), or divide (/)" +operator = gets.chomp + +case operator +when "add", "+" + puts addition(num_1, num_2) +when "subtract", "-" + puts subtraction(num_1, num_2) +when "multiply", "*" + puts multiplication(num_1, num_2) +when "divide", "/" + puts division(num_1, num_2) +end + + +# puts num_1 +# puts num_2 +# puts operator + +# def calculate(x, y) +# +# case operator +# when "add" || "+" +# result = x + y +# return "The total of your numbers added together is: #{result}." +# when "subtract" || "-" +# result = x - y +# return "The total of your second number subtracted from your first number is: #{result}." +# when "multiply" || "*" +# result = x * y +# return "The total of your numbers multiplied together is: #{result}." +# when "divide" || "/" +# result = x / y +# return "The total of your first number divided by your second number is: #{result}." +# end +# end + +# puts calculate (num_1, num_2) From f70808e76aa30fc3b66c4af1dbfe43d78aca1e48 Mon Sep 17 00:00:00 2001 From: npeters5 Date: Wed, 7 Feb 2018 16:51:41 -0800 Subject: [PATCH 2/3] tests and handles erroneous input --- calculator.rb | 70 ++++++++++++++++++++++----------------------------- 1 file changed, 30 insertions(+), 40 deletions(-) diff --git a/calculator.rb b/calculator.rb index cf1cec3..28baece 100644 --- a/calculator.rb +++ b/calculator.rb @@ -6,6 +6,7 @@ # The program should have support for these four operations: addition, subtraction, multiplication, and division. # The program should accept both the name (`add`) and the symbol (`+`) for each possible operation. +# define methods for adding, subtracting, multiplying, and dividing def addition(x, y) x + y end @@ -22,60 +23,49 @@ def division(x, y) x / y end +# Welcome user and test for/handle erroneous input + puts "Welcome to Calculator!" puts "I'm going to ask you for two numbers, and an operator. What is your first number?: " num_1 = gets.chomp -puts num_1.Class - -until num_1.is_a?(Integer) - puts "Invalid input. Try again: " +until num_1 =~ /\d/ + puts "That's not a number, try again: " num_1 = gets.chomp end puts "Great! What is your second number? (Must be greater than zero)" num_2 = gets.chomp -until (num_2 > 0) && (num_2.is_a? Integer) - print "Invalid input. Try again: " +until (num_2 =~ /\d/) + puts "That's not a number. Try again: " + num_2 = gets.chomp +end + +if num_2.to_i <= 0 + puts "Number must be greater than zero, try again: " num_2 = gets.chomp end puts "Awesome. Now, what would you like to do to these numbers? You can type add (+), subtract (-), multiply (*), or divide (/)" operator = gets.chomp -case operator -when "add", "+" - puts addition(num_1, num_2) -when "subtract", "-" - puts subtraction(num_1, num_2) -when "multiply", "*" - puts multiplication(num_1, num_2) -when "divide", "/" - puts division(num_1, num_2) +# Case statement that tests the value of the operator variable +# The program also needs to handle erroneous operators. + +loop do + case operator + when "add", "+" + puts addition(num_1.to_f, num_2.to_f) + when "subtract", "-" + puts subtraction(num_1.to_f, num_2.to_f) + when "multiply", "*" + puts multiplication(num_1.to_f, num_2.to_f) + when "divide", "/" + puts division(num_1.to_f, num_2.to_f) + break + else + puts "Invalid input, try again: " + operator = gets.chomp + end end - - -# puts num_1 -# puts num_2 -# puts operator - -# def calculate(x, y) -# -# case operator -# when "add" || "+" -# result = x + y -# return "The total of your numbers added together is: #{result}." -# when "subtract" || "-" -# result = x - y -# return "The total of your second number subtracted from your first number is: #{result}." -# when "multiply" || "*" -# result = x * y -# return "The total of your numbers multiplied together is: #{result}." -# when "divide" || "/" -# result = x / y -# return "The total of your first number divided by your second number is: #{result}." -# end -# end - -# puts calculate (num_1, num_2) From f082eac7ac7d566a0a17d5bf68c90fccc905f44b Mon Sep 17 00:00:00 2001 From: Nora Peters Date: Thu, 8 Feb 2018 09:23:39 -0800 Subject: [PATCH 3/3] adds breaks to loop w/operator case statement --- calculator.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/calculator.rb b/calculator.rb index 28baece..9a47f6a 100644 --- a/calculator.rb +++ b/calculator.rb @@ -57,10 +57,13 @@ def division(x, y) case operator when "add", "+" puts addition(num_1.to_f, num_2.to_f) + break when "subtract", "-" puts subtraction(num_1.to_f, num_2.to_f) + break when "multiply", "*" puts multiplication(num_1.to_f, num_2.to_f) + break when "divide", "/" puts division(num_1.to_f, num_2.to_f) break