From 43acff8e7dc14d95312494a7e4b332f10aad3a4a Mon Sep 17 00:00:00 2001 From: "Maddie (Mads)" Date: Wed, 7 Feb 2018 23:26:19 -0800 Subject: [PATCH 1/2] Create calculator.rb --- calculator.rb | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 calculator.rb diff --git a/calculator.rb b/calculator.rb new file mode 100644 index 0000000..99cfb77 --- /dev/null +++ b/calculator.rb @@ -0,0 +1,68 @@ +# If number is integer, calculate with integer. If number has decimal, calculate with floats + +# If division has no remainder, print integer, otherwise print float +# p calculate_division + +# p calculate_exponential + + +calculator_options = ["add", "+", "subtract", "-", "multiply", "*", "divide", "/", "exponent", "**"] +def addition(num_one, num_two) + return num_one + num_two +end + +def subtraction(num_one, num_two) + return num_one - num_two +end + +def multiplication(num_one, num_two) + return num_one * num_two +end + +def division(num_one, num_two) + return num_one / num_two +end + +def exponential(num_one, num_two) + return num_one ** num_two +end + +puts "" +puts "\nWhat would you like to do?" +option = gets.chomp.downcase + +until calculator_options.include?(option) + puts "\nInvalid entry. Try again.\n" + print "\nWhat would you like to do?" + option = gets.chomp.downcase +end + +puts "\nWhat is the first number?" +num_one = gets.chomp + +puts "\nWhat is the second number?" +num_two = gets.chomp + +puts "" + +case option + when "add", "+" + calculate_addition_f = addition(num_one.to_f, num_two.to_f) + p calculate_addition_f + + when "subtract", "-" + calculate_subtraction_f = subtraction(num_one.to_f, num_two.to_f) + p calculate_subtraction_f + + when "multiply", "*" + calculate_multiplication_f = multiplication(num_one.to_f, num_two.to_f) + p calculate_multiplication_f + + when "divide", "/" + calculate_division_f = division(num_one.to_f, num_two.to_f) + p calculate_division_f + + when "exponent", "**" + calculate_exponential_f = exponential(num_one.to_f, num_two.to_f) + p calculate_exponential_f +end From b5a680b7db6a1af9fb771be69186449d86307e78 Mon Sep 17 00:00:00 2001 From: "Maddie (Mads)" Date: Thu, 8 Feb 2018 10:16:17 -0800 Subject: [PATCH 2/2] Update calculator.rb --- calculator.rb | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/calculator.rb b/calculator.rb index 99cfb77..fef72d0 100644 --- a/calculator.rb +++ b/calculator.rb @@ -4,9 +4,12 @@ # p calculate_division # p calculate_exponential +# first_number = [] +# second_number = [] +calculator_options = %w[add + subtract - multiply * divide / exponent **] -calculator_options = ["add", "+", "subtract", "-", "multiply", "*", "divide", "/", "exponent", "**"] +# calculator_options = ["add", "+", "subtract", "-", "multiply", "*", "divide", "/", "exponent", "**"] def addition(num_one, num_two) return num_one + num_two end @@ -43,7 +46,13 @@ def exponential(num_one, num_two) puts "\nWhat is the second number?" num_two = gets.chomp +# first_number << num_one +# second_number << num_two + puts "" +# Integer check + +# while num_one.integer? && num_two.integer? case option when "add", "+" @@ -59,10 +68,15 @@ def exponential(num_one, num_two) p calculate_multiplication_f when "divide", "/" + if num_two == "0" + puts "Does Not Exist" + else calculate_division_f = division(num_one.to_f, num_two.to_f) p calculate_division_f + end when "exponent", "**" calculate_exponential_f = exponential(num_one.to_f, num_two.to_f) p calculate_exponential_f end +# end