From 02a0e8e9bd2ac18b9b49b6502f335c9da53ebfc2 Mon Sep 17 00:00:00 2001 From: analisasutherland Date: Wed, 7 Feb 2018 10:29:52 -0800 Subject: [PATCH 1/2] Create calculator.rb 02/07/2018: Almost finished with the calculator assignment for Week 1 --- calculator.rb | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 calculator.rb diff --git a/calculator.rb b/calculator.rb new file mode 100644 index 0000000..2e0da15 --- /dev/null +++ b/calculator.rb @@ -0,0 +1,97 @@ +# Week#1: Calculator Build +# Ana Lisa Sutherland - Octos C9 + +# method for addition using two numbers +def addition (num_one, num_two) + return num_one + num_two +end +#method for subtracting using two numbers +def subtraction (num_one, num_two) + return num_one - num_two +end +#method for multiplication using two numbers +def multiply (num_one, num_two) + return num_one * num_two +end +#method for division using two numbers +def divide (num_one, num_two) + return num_one / num_two +end +#OPTIONAL: ENSURE IT IS ONLY NUMBERS THAT ARE ENTERED +def req () + num = gets.chomp + until num =~ /^[1-9]\d*(\.\d+)?$/ + if num =~ /^[1-9]\d*(\.\d+)?$/ + return num + else + puts "Invalid number." + num = gets.chomp + end + end + if num =~ /\A\d+\z/ + puts 'integer' + return num.to_i + elsif num =~ /\A\d+\.\d+\z/ + puts 'float' + return num.to_f + else + puts 'not integer or float' + end +end +#OPTIONAL: method for exponents +# def exponent (num_one, num_two) +# return num_one ** num_two +# end + +#OPTIONAL: method for modulo +# def modulo (num_one, num_two) +# return num_one % num_two +# end + +#collect first number from the user and run it through req method +puts "Welcome, seems like you need a calculator.\n\Please Input a number." +num_1 = req() +puts "Enter a command." +# make sure that only operators are accepted +command_use = gets.chomp +until ["add", "+", "subtract", "-","multiply", "*", "divide", "/"].include?(command_use) + #OPTIONAL: For modulo and exponents + # until ["add", "+", "subtract", "-","multiply", "*", "divide", "/", "**", "^", "modulo", "%"].include?(command_use) + puts "Please put a valid command!" + command_use = gets.chomp +end + +#collect second number from the user and run it through req method +puts "Enter a second number." +num_2 = req() + +#method to utilize both written and modifying commands +case command_use +when "add", "+" + puts "We're adding numbers" + result = addition(num_1,num_2) +when "subtract", "-" + puts "We're subtracting numbers" + result = subtraction(num_1,num_2) +when "multiply", "*" + puts "We're multiplying numbers" + result = multiply(num_1,num_2) +when "divide", "/" + puts "We're dividing numbers" + if num_2 != 0 + result = divide(num_1,num_2) + else + puts "You cannot divide by 0." + end + #OPTIONAL: method for exponents and modulos + # when "^", "**" + # puts "We're exponentiating numbers" + # result = exponent(num_1,num_2) + #when "modulo", "%" + # puts "We are using a modulo for numbers." + # results = modulo(num_1,num_2) +else + puts "That makes no sense. Please tell me which operator you want to use." + command = gets.chomp +end +puts "#{num_1} #{command_use} #{num_2} = #{result}" From 4e97c067defafb80c58c456fdfb5ed74963b9908 Mon Sep 17 00:00:00 2001 From: analisasutherland Date: Wed, 7 Feb 2018 10:54:09 -0800 Subject: [PATCH 2/2] Update calculator.rb 02/07/18: Finished and submitting Calculator Assignment for Week 1. --- calculator.rb | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/calculator.rb b/calculator.rb index 2e0da15..3a44a17 100644 --- a/calculator.rb +++ b/calculator.rb @@ -17,6 +17,14 @@ def multiply (num_one, num_two) def divide (num_one, num_two) return num_one / num_two end +#OPTIONAL: method for exponents +def exponent (num_one, num_two) + return num_one ** num_two +end +#OPTIONAL: method for modulo +def modulo (num_one, num_two) + return num_one % num_two +end #OPTIONAL: ENSURE IT IS ONLY NUMBERS THAT ARE ENTERED def req () num = gets.chomp @@ -38,15 +46,6 @@ def req () puts 'not integer or float' end end -#OPTIONAL: method for exponents -# def exponent (num_one, num_two) -# return num_one ** num_two -# end - -#OPTIONAL: method for modulo -# def modulo (num_one, num_two) -# return num_one % num_two -# end #collect first number from the user and run it through req method puts "Welcome, seems like you need a calculator.\n\Please Input a number." @@ -54,9 +53,7 @@ def req () puts "Enter a command." # make sure that only operators are accepted command_use = gets.chomp -until ["add", "+", "subtract", "-","multiply", "*", "divide", "/"].include?(command_use) - #OPTIONAL: For modulo and exponents - # until ["add", "+", "subtract", "-","multiply", "*", "divide", "/", "**", "^", "modulo", "%"].include?(command_use) +until ["add", "+", "subtract", "-","multiply", "*", "divide", "/", "**", "^", "modulo", "%"].include?(command_use) puts "Please put a valid command!" command_use = gets.chomp end @@ -83,13 +80,12 @@ def req () else puts "You cannot divide by 0." end - #OPTIONAL: method for exponents and modulos - # when "^", "**" - # puts "We're exponentiating numbers" - # result = exponent(num_1,num_2) - #when "modulo", "%" - # puts "We are using a modulo for numbers." - # results = modulo(num_1,num_2) +when "^", "**" + puts "We're exponentiating numbers" + result = exponent(num_1,num_2) +when "modulo", "%" + puts "We are using a modulo for numbers." + results = modulo(num_1,num_2) else puts "That makes no sense. Please tell me which operator you want to use." command = gets.chomp