From 3233a623d0c5dadb14ea336e68cfc0dffec771bd Mon Sep 17 00:00:00 2001 From: mgraonic Date: Wed, 7 Feb 2018 10:29:13 -0800 Subject: [PATCH 1/2] Create calculator.rb --- calculator.rb | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 calculator.rb diff --git a/calculator.rb b/calculator.rb new file mode 100644 index 0000000..934f55b --- /dev/null +++ b/calculator.rb @@ -0,0 +1,79 @@ + + +def add(num1, num2) + return num1 + num2 +end + +def subtract(num1, num2) + return num1 - num2 +end + +def multiply(num1, num2) + return num1 * num2 +end + +def divide(num1, num2) + if num2 != 0 + return num1 / num2 + else + puts "Operation not possible. Division by zero is not allowed." + end +end + +def modulo(num1, num2) + return num1 % num2 +end + +def exponify(num1, num2) + return num1 ** num2 +end + +puts "Enter the operation you would like:" +operation = gets.chomp + +until ["add", "+", "subtract", "-", "multiply", "*", "divide", "/", "exponify", "**", "modulo", "%"].include?(operation) + puts "Please tell me to add (+), subtract (-), multiply (*), divide (/), exponify (**), or modulo (%)!" + operation = gets.chomp +end + +puts "Enter the first number:" +num1 = gets.chomp + +#regex to check for a numeral +# lines 44-59 should be condensed into a method somehow +while num1 !~ /^-?[0-9]+$/ + puts "Please enter a valid number:" + num1 = gets.chomp +end + +num1 = num1.to_f + +puts "Enter the second number:" +num2 = gets.chomp + +while num2 !~ /^-?[0-9]+$/ + puts "Please enter a valid number:" + num2 = gets.chomp +end + +num2 = num2.to_f + +# case statements only accept strings (?) +case operation + when "add", "+" + answer = add(num1,num2) + when "subtract", "-" + answer = subtract(num1,num2) + when "multiply", "*" + answer = multiply(num1,num2) + when "divide", "/" + answer = divide(num1,num2) + when "exponify", "**" + answer = exponify(num1,num2) + when "modulo", "%" + answer = modulo(num1,num2) + else + puts "Please enter a valid operation." +end + +puts "Answer: #{answer}" From ce2887baa20fd4680e43c645f42032903c6ca75a Mon Sep 17 00:00:00 2001 From: mgraonic Date: Wed, 7 Feb 2018 10:31:28 -0800 Subject: [PATCH 2/2] Update calculator.rb --- calculator.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/calculator.rb b/calculator.rb index 934f55b..3253282 100644 --- a/calculator.rb +++ b/calculator.rb @@ -1,5 +1,3 @@ - - def add(num1, num2) return num1 + num2 end