From 0923b98ede929d27530942dbe5c940b0f41c66b2 Mon Sep 17 00:00:00 2001 From: Steffnay Date: Wed, 7 Feb 2018 10:27:29 -0800 Subject: [PATCH 1/3] Steffany Calculator Assignment --- calculator.rb | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 calculator.rb diff --git a/calculator.rb b/calculator.rb new file mode 100644 index 0000000..9de7f1d --- /dev/null +++ b/calculator.rb @@ -0,0 +1,76 @@ + + +def add(num_one, num_two) + return num_one + num_two +end + +def subtract(num_one, num_two) + return num_one - num_two +end + +def multiply(num_one, num_two) + return num_one * num_two +end + +def divide(num_one, num_two) + return num_one / num_two +end + +def exponent(num_one, num_two) + return num_one ** num_two +end + +possible_operations = ["ADD", "SUBTRACT", "DIVIDE", "MULTIPLY", "EXPONENT", "+", "-","/", "*", "^"] + +puts "Which operation would you like to perform?" +print "(Add, Subtract, Multiply, Divide, Exponent) " +operation = gets.chomp.upcase + until possible_operations.include?(operation) + puts "That is an invalid operation. Try again" + operation = gets.chomp.upcase + end + +puts "Okay, please enter a number." + num_one = gets.chomp + while num_one.to_i.to_s != num_one + puts "That is not a valid number. Please try again." + num_one = gets.chomp + end + +num_one = num_one.to_i + +puts "Please enter another number." +num_two = gets.chomp + +while num_two.to_i.to_s != num_two + puts "That is not a valid number. Please try again." + num_two = gets.chomp +end + +num_two = num_two.to_i + +if operation == "ADD" || operation == "+" + sum = add(num_one, num_two) + puts "#{num_one} + #{num_two} = #{sum}" + return sum +elsif operation == "SUBTRACT" || operation == "-" + sum = subtract(num_one, num_two) + puts "#{num_one} - #{num_two} = #{sum}" + return sum +elsif operation == "MULTIPLY" || operation == "*" + sum = multiply(num_one, num_two) + puts "#{num_one} * #{num_two} = #{sum}" + return sum +elsif operation == "DIVIDE" || operation == "/" + sum = divide(num_one, num_two) + puts "#{num_one} / #{num_two} = #{sum}" + return sum +elsif operation == "EXPONENT" || operation == "^" + sum = exponent(num_one, num_two) + printing_exponent = num_two - 1 + print "#{num_one}^#{num_two} = " + print num_one + printing_exponent.times { print " * #{num_one}"} + print " = #{sum}\n" + return sum +end From 814c74650c73c10263fed51ed2571999600a89bd Mon Sep 17 00:00:00 2001 From: Steffany Brown <30247553+steffnay@users.noreply.github.com> Date: Wed, 7 Feb 2018 15:45:02 -0800 Subject: [PATCH 2/3] Update calculator.rb --- calculator.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/calculator.rb b/calculator.rb index 9de7f1d..f6a977c 100644 --- a/calculator.rb +++ b/calculator.rb @@ -1,4 +1,6 @@ +# This is a calculator program +# Define methods def add(num_one, num_two) return num_one + num_two From 52504b47be8ddc89708c469d146bbe14d7a718ca Mon Sep 17 00:00:00 2001 From: Steffany Brown <30247553+steffnay@users.noreply.github.com> Date: Wed, 7 Feb 2018 15:49:30 -0800 Subject: [PATCH 3/3] Updated with comments --- calculator.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/calculator.rb b/calculator.rb index f6a977c..882fafa 100644 --- a/calculator.rb +++ b/calculator.rb @@ -24,6 +24,8 @@ def exponent(num_one, num_two) possible_operations = ["ADD", "SUBTRACT", "DIVIDE", "MULTIPLY", "EXPONENT", "+", "-","/", "*", "^"] +# Get user input for the operation to perform +# Ask for user input until operation is valid puts "Which operation would you like to perform?" print "(Add, Subtract, Multiply, Divide, Exponent) " operation = gets.chomp.upcase @@ -32,6 +34,8 @@ def exponent(num_one, num_two) operation = gets.chomp.upcase end +# Get user input for numbers +# Ask for input until user enter integer puts "Okay, please enter a number." num_one = gets.chomp while num_one.to_i.to_s != num_one @@ -41,6 +45,7 @@ def exponent(num_one, num_two) num_one = num_one.to_i + puts "Please enter another number." num_two = gets.chomp @@ -51,6 +56,7 @@ def exponent(num_one, num_two) num_two = num_two.to_i +# Perform calculations if operation == "ADD" || operation == "+" sum = add(num_one, num_two) puts "#{num_one} + #{num_two} = #{sum}"