From f1a8d29385ffba277a88f789477295aaf37d424a Mon Sep 17 00:00:00 2001 From: denisseai <26889152+denisseai@users.noreply.github.com> Date: Wed, 5 Feb 2020 10:16:22 -0800 Subject: [PATCH 1/3] Create calculator.rb --- calculator.rb | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 calculator.rb diff --git a/calculator.rb b/calculator.rb new file mode 100644 index 0000000..a971913 --- /dev/null +++ b/calculator.rb @@ -0,0 +1,80 @@ +# Program that acts like a calculator + +# Display welcome message and instructions +puts "Welcome to the calculator program! Which operation would you like to use?" +puts "\n1. add(+) \n2. substraction(-) \n3. multiply(*) \n4. divide(/)" +puts "\nPlease choose one operator(name or symbol):" + +acceptable_operation = %w(add + subtract - multiply * divide /) + +# Methods for operations +def addition (x, y) + result = (x + y) +end + +def substraction (x, y) + result = (x - y) +end + +def multiplication (x, y) + result = (x * y) +end + +def division (x, y) + result = (x / y) +end + +# Methods for verification +def verify_operation(data_to_verify, data_acceptable) + until (data_acceptable.include? data_to_verify) == true + puts "Option not accepted. Please enter the symbol or name of the operation." + data_to_verify = gets.chomp + end + data_to_verify +end + +def verify_number(data_to_verify) + until (Integer(data_to_verify) rescue nil) != nil + puts "Input not accepted, please enter a number:" + data_to_verify = gets.chomp + end + data_to_verify.to_f +end + +# Prompt user to select an operation, two numbers and verify the input +puts "What operation would you like to do?" +operation = gets.chomp.downcase +operation = verify_operation(operation, acceptable_operation) + +puts "Enter in your first number" +first_number = gets.chomp +first_number = verify_number(first_number) + +puts "Enter in your second number" +second_number = gets.chomp +second_number = verify_number(second_number) + +# Calculate operation user requested +case operation + when "add", "+", "1" + puts "You have chosen to add " "#{first_number} with #{second_number}" + result = addition(first_number, second_number) + when "substraction", "-", "2" + puts "You have chosen to substract " "#{first_number} with #{second_number}" + result = substraction(first_number, second_number) + when "divide", "/", "3" + puts "You have chosen to divide " "#{first_number} by #{second_number}" + if second_number == 0 + result = 0 + else + result = division(first_number, second_number) + end + when "multiply", "*", "4" + puts "You have chosen to multiply " "#{first_number} with #{second_number}" + result = multiplication(first_number, second_number) + else + puts "You have made an invalid choice" +end + +# Display operation result with two decimals +puts result.round(2) From d5c7443eb689d50757c48b4229b5af9957080e92 Mon Sep 17 00:00:00 2001 From: denisseai <26889152+denisseai@users.noreply.github.com> Date: Wed, 5 Feb 2020 10:47:33 -0800 Subject: [PATCH 2/3] Update calculator.rb --- calculator.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/calculator.rb b/calculator.rb index a971913..a384928 100644 --- a/calculator.rb +++ b/calculator.rb @@ -3,9 +3,9 @@ # Display welcome message and instructions puts "Welcome to the calculator program! Which operation would you like to use?" puts "\n1. add(+) \n2. substraction(-) \n3. multiply(*) \n4. divide(/)" -puts "\nPlease choose one operator(name or symbol):" +puts "\nPlease choose one operator(number, name or symbol for the operation):" -acceptable_operation = %w(add + subtract - multiply * divide /) +acceptable_operation = %w(1 add + 2 subtract - 3 multiply * 4 divide /) # Methods for operations def addition (x, y) From 8805fc81064846623e208be6c00389c3661d1ed4 Mon Sep 17 00:00:00 2001 From: denisseai <26889152+denisseai@users.noreply.github.com> Date: Thu, 6 Feb 2020 21:26:02 -0800 Subject: [PATCH 3/3] Add files via upload --- calculator.rb | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/calculator.rb b/calculator.rb index a384928..1140c29 100644 --- a/calculator.rb +++ b/calculator.rb @@ -3,7 +3,7 @@ # Display welcome message and instructions puts "Welcome to the calculator program! Which operation would you like to use?" puts "\n1. add(+) \n2. substraction(-) \n3. multiply(*) \n4. divide(/)" -puts "\nPlease choose one operator(number, name or symbol for the operation):" +puts "\nPlease choose one operator(name or symbol):" acceptable_operation = %w(1 add + 2 subtract - 3 multiply * 4 divide /) @@ -57,24 +57,26 @@ def verify_number(data_to_verify) # Calculate operation user requested case operation when "add", "+", "1" - puts "You have chosen to add " "#{first_number} with #{second_number}" - result = addition(first_number, second_number) + puts "You have chosen to add " "#{first_number} with #{second_number}" + result = addition(first_number, second_number) when "substraction", "-", "2" - puts "You have chosen to substract " "#{first_number} with #{second_number}" - result = substraction(first_number, second_number) + puts "You have chosen to substract " "#{first_number} with #{second_number}" + result = substraction(first_number, second_number) when "divide", "/", "3" - puts "You have chosen to divide " "#{first_number} by #{second_number}" - if second_number == 0 - result = 0 - else - result = division(first_number, second_number) - end + puts "You have chosen to divide " "#{first_number} by #{second_number}" + if second_number == 0 + puts = "Cannot divide by zero" + elsif first_number == 0 + result = 0 + else + result = division(first_number, second_number) + end when "multiply", "*", "4" - puts "You have chosen to multiply " "#{first_number} with #{second_number}" - result = multiplication(first_number, second_number) + puts "You have chosen to multiply " "#{first_number} with #{second_number}" + result = multiplication(first_number, second_number) else puts "You have made an invalid choice" end # Display operation result with two decimals -puts result.round(2) +puts result.round(2) \ No newline at end of file