From 3db5dbae469927019885090856fbadebdaaa2484 Mon Sep 17 00:00:00 2001 From: sharonkeikei <54300556+sharonkeikei@users.noreply.github.com> Date: Wed, 5 Feb 2020 10:06:56 -0800 Subject: [PATCH 1/2] Create calculator.rb --- calculator.rb | 1 + 1 file changed, 1 insertion(+) create mode 100644 calculator.rb diff --git a/calculator.rb b/calculator.rb new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/calculator.rb @@ -0,0 +1 @@ + From 715bd5cbcf1b03eb26dac021594ce5c826418777 Mon Sep 17 00:00:00 2001 From: sharonkeikei <54300556+sharonkeikei@users.noreply.github.com> Date: Wed, 5 Feb 2020 13:24:54 -0800 Subject: [PATCH 2/2] Update calculator.rb --- calculator.rb | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/calculator.rb b/calculator.rb index 8b13789..7a1f156 100644 --- a/calculator.rb +++ b/calculator.rb @@ -1 +1,67 @@ +# create a hash to store the operation and welcoming user +puts "Welcome to Sharon's calculator Program! Please see below a list of operators you can choose from" +accepted_operation = { "add" => "+", "subtract" => "-" , "multiply" => "*", "divide" => "/", "exponents" => "^" , "modulo" => "%" } +# loop thru the operator hash in order to show user what the choices are +i = 1 +accepted_operation.each do |key, value| + puts "#{i}. #{value} (#{key})" + i += 1 +end + +# ask user for a math operation +puts "Please chose one operator (either name or symbol)" +operation = gets.chomp.downcase + +# test the operation that user input is valid or not +until accepted_operation.values.include?(operation) || accepted_operation.keys.include?(operation.downcase) + puts "Invalid input! Please try again!" + operation = gets.chomp.downcase +end + +# create method for checking if the user has input a valid integer number for the formular +def num_check(user_input) + while (user_input != '0') && (user_input.to_i.to_s != user_input.strip && user_input.to_f.to_s != user_input.strip) do + puts "Invalid input, please enter an valid number!" + user_input = gets.chomp + end + return user_input.to_f +end + +# ask user for the first number and check if it's valid +puts "Now, please enter the first number for the calculation" + +num1 = gets.chomp +# to return the new user input from the method +num1 = num_check(num1) + +# ask user for the second number and check if it's vaild +puts "Now, please enter the second number for the calculation" +num2 = gets.chomp +# to return the new user input from the method +num2 = num_check(num2) + +# converting the opertaion user inputed from word to symbol +if accepted_operation.keys.include?(operation.downcase) + symbol = accepted_operation[operation] +else + symbol = operation +end + +# showing the formula and answer depending on user's inputs +puts "Here is your formula: #{num1} #{symbol} #{num2}" +print "Your answer is: " +case operation + when "add","+" + puts num1 + num2 + when "subtract", "-" + puts num1- num2 + when "multiply", "*" + puts num1 * num2 + when "divide", "/" + puts num1 / num2 + when "exponents", "**" + puts num1 ** num2 + when "modulo","%" + puts num1 % num2 +end