From 7964449c9ed39e3909fc2bab670610f3d954d806 Mon Sep 17 00:00:00 2001 From: Laura Robertson Date: Fri, 11 Aug 2017 08:15:03 -0700 Subject: [PATCH 1/5] Add files via upload --- calculator1.rb | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 calculator1.rb diff --git a/calculator1.rb b/calculator1.rb new file mode 100644 index 0000000..4f5c0a9 --- /dev/null +++ b/calculator1.rb @@ -0,0 +1,69 @@ +# ****************** +# Stage 1 Calculator +# ****************** + +# Method for calculator +def calculator(operation, num1, num2) + + # Variables created for each calculation type + result_add = num1 + num2 + result_subtract = num1 - num2 + result_multiply = num1 * num2 + result_divide = num1 / num2 + result_exponent = num1 ** num2 + result_module = num1 % num2 + + # Output based on operation type + if operation == "+" || operation == "add" + puts "#{num1} + #{num2} = #{result_add}" + elsif operation == "-" || operation == "subtract" + puts "#{num1} - #{num2} = #{result_subtract}" + elsif operation == "*" || operation == "multiply" + puts "#{num1} * #{num2} = #{result_multiply}" + elsif operation == "/" || operation == "divide" + # Divide by 0 output + if num1 == 0 || num2 == 0 + puts "Dividing by zero is undefined!" + else + puts "#{num1} / #{num2} = #{result_divide}" + end + elsif operation == "^" || operation == "exponent" + puts "#{num1} ^ #{num2} = #{result_exponent}" + elsif operation == "%" || operation == "module" + puts "#{num1} % #{num2} = #{result_module}" + else + # Error message, just in case + puts "Hey! How'd this glitch happen?" + end +end + +puts "------------------" +puts "Let's do some math" +puts "------------------\n\n" + +# Operation options stored in array for the until loop to scan over +operator_types = ["+", "add", "-", "subtract", "*", "multiply", "/", "divide", "^", "exponent", "%", "module"] + +x = "" + +# Until true, gets.chomp is a string that matches the array +until operator_types.include? x +puts "Would you like to add(+), subtract(-), multiply(*), divide(/), exponent(^), or module(%)?" +x = gets.chomp.downcase +user_operator = x +end + +# Checks if gets is a float (integers work) retries until it is +puts "Please enter the first number:" +user_num1 = Float(gets) rescue retry + +puts "Please enter the second number:" +user_num2 = Float(gets) rescue retry + +# Runs method with user inputs +puts "\n" +calculator(user_operator, user_num1, user_num2) + +puts "\n------------------" +puts "Calc-U-Later!" +puts "------------------" From 24e1488eb6f8e388ab17e0c389c06f7f12936080 Mon Sep 17 00:00:00 2001 From: Laura Robertson Date: Fri, 11 Aug 2017 08:18:08 -0700 Subject: [PATCH 2/5] Create calc1.rb --- calc1.rb | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 calc1.rb diff --git a/calc1.rb b/calc1.rb new file mode 100644 index 0000000..4f5c0a9 --- /dev/null +++ b/calc1.rb @@ -0,0 +1,69 @@ +# ****************** +# Stage 1 Calculator +# ****************** + +# Method for calculator +def calculator(operation, num1, num2) + + # Variables created for each calculation type + result_add = num1 + num2 + result_subtract = num1 - num2 + result_multiply = num1 * num2 + result_divide = num1 / num2 + result_exponent = num1 ** num2 + result_module = num1 % num2 + + # Output based on operation type + if operation == "+" || operation == "add" + puts "#{num1} + #{num2} = #{result_add}" + elsif operation == "-" || operation == "subtract" + puts "#{num1} - #{num2} = #{result_subtract}" + elsif operation == "*" || operation == "multiply" + puts "#{num1} * #{num2} = #{result_multiply}" + elsif operation == "/" || operation == "divide" + # Divide by 0 output + if num1 == 0 || num2 == 0 + puts "Dividing by zero is undefined!" + else + puts "#{num1} / #{num2} = #{result_divide}" + end + elsif operation == "^" || operation == "exponent" + puts "#{num1} ^ #{num2} = #{result_exponent}" + elsif operation == "%" || operation == "module" + puts "#{num1} % #{num2} = #{result_module}" + else + # Error message, just in case + puts "Hey! How'd this glitch happen?" + end +end + +puts "------------------" +puts "Let's do some math" +puts "------------------\n\n" + +# Operation options stored in array for the until loop to scan over +operator_types = ["+", "add", "-", "subtract", "*", "multiply", "/", "divide", "^", "exponent", "%", "module"] + +x = "" + +# Until true, gets.chomp is a string that matches the array +until operator_types.include? x +puts "Would you like to add(+), subtract(-), multiply(*), divide(/), exponent(^), or module(%)?" +x = gets.chomp.downcase +user_operator = x +end + +# Checks if gets is a float (integers work) retries until it is +puts "Please enter the first number:" +user_num1 = Float(gets) rescue retry + +puts "Please enter the second number:" +user_num2 = Float(gets) rescue retry + +# Runs method with user inputs +puts "\n" +calculator(user_operator, user_num1, user_num2) + +puts "\n------------------" +puts "Calc-U-Later!" +puts "------------------" From 9bbecf7fd6400d99d408dee581a8793b6545d7cb Mon Sep 17 00:00:00 2001 From: Laura Robertson Date: Fri, 11 Aug 2017 08:19:54 -0700 Subject: [PATCH 3/5] Delete calc1.rb --- calc1.rb | 69 -------------------------------------------------------- 1 file changed, 69 deletions(-) delete mode 100644 calc1.rb diff --git a/calc1.rb b/calc1.rb deleted file mode 100644 index 4f5c0a9..0000000 --- a/calc1.rb +++ /dev/null @@ -1,69 +0,0 @@ -# ****************** -# Stage 1 Calculator -# ****************** - -# Method for calculator -def calculator(operation, num1, num2) - - # Variables created for each calculation type - result_add = num1 + num2 - result_subtract = num1 - num2 - result_multiply = num1 * num2 - result_divide = num1 / num2 - result_exponent = num1 ** num2 - result_module = num1 % num2 - - # Output based on operation type - if operation == "+" || operation == "add" - puts "#{num1} + #{num2} = #{result_add}" - elsif operation == "-" || operation == "subtract" - puts "#{num1} - #{num2} = #{result_subtract}" - elsif operation == "*" || operation == "multiply" - puts "#{num1} * #{num2} = #{result_multiply}" - elsif operation == "/" || operation == "divide" - # Divide by 0 output - if num1 == 0 || num2 == 0 - puts "Dividing by zero is undefined!" - else - puts "#{num1} / #{num2} = #{result_divide}" - end - elsif operation == "^" || operation == "exponent" - puts "#{num1} ^ #{num2} = #{result_exponent}" - elsif operation == "%" || operation == "module" - puts "#{num1} % #{num2} = #{result_module}" - else - # Error message, just in case - puts "Hey! How'd this glitch happen?" - end -end - -puts "------------------" -puts "Let's do some math" -puts "------------------\n\n" - -# Operation options stored in array for the until loop to scan over -operator_types = ["+", "add", "-", "subtract", "*", "multiply", "/", "divide", "^", "exponent", "%", "module"] - -x = "" - -# Until true, gets.chomp is a string that matches the array -until operator_types.include? x -puts "Would you like to add(+), subtract(-), multiply(*), divide(/), exponent(^), or module(%)?" -x = gets.chomp.downcase -user_operator = x -end - -# Checks if gets is a float (integers work) retries until it is -puts "Please enter the first number:" -user_num1 = Float(gets) rescue retry - -puts "Please enter the second number:" -user_num2 = Float(gets) rescue retry - -# Runs method with user inputs -puts "\n" -calculator(user_operator, user_num1, user_num2) - -puts "\n------------------" -puts "Calc-U-Later!" -puts "------------------" From acd605dc9b3d33fc36c3b4b2076b753203306901 Mon Sep 17 00:00:00 2001 From: Laura Robertson Date: Fri, 11 Aug 2017 08:32:18 -0700 Subject: [PATCH 4/5] Delete calculator1.rb --- calculator1.rb | 69 -------------------------------------------------- 1 file changed, 69 deletions(-) delete mode 100644 calculator1.rb diff --git a/calculator1.rb b/calculator1.rb deleted file mode 100644 index 4f5c0a9..0000000 --- a/calculator1.rb +++ /dev/null @@ -1,69 +0,0 @@ -# ****************** -# Stage 1 Calculator -# ****************** - -# Method for calculator -def calculator(operation, num1, num2) - - # Variables created for each calculation type - result_add = num1 + num2 - result_subtract = num1 - num2 - result_multiply = num1 * num2 - result_divide = num1 / num2 - result_exponent = num1 ** num2 - result_module = num1 % num2 - - # Output based on operation type - if operation == "+" || operation == "add" - puts "#{num1} + #{num2} = #{result_add}" - elsif operation == "-" || operation == "subtract" - puts "#{num1} - #{num2} = #{result_subtract}" - elsif operation == "*" || operation == "multiply" - puts "#{num1} * #{num2} = #{result_multiply}" - elsif operation == "/" || operation == "divide" - # Divide by 0 output - if num1 == 0 || num2 == 0 - puts "Dividing by zero is undefined!" - else - puts "#{num1} / #{num2} = #{result_divide}" - end - elsif operation == "^" || operation == "exponent" - puts "#{num1} ^ #{num2} = #{result_exponent}" - elsif operation == "%" || operation == "module" - puts "#{num1} % #{num2} = #{result_module}" - else - # Error message, just in case - puts "Hey! How'd this glitch happen?" - end -end - -puts "------------------" -puts "Let's do some math" -puts "------------------\n\n" - -# Operation options stored in array for the until loop to scan over -operator_types = ["+", "add", "-", "subtract", "*", "multiply", "/", "divide", "^", "exponent", "%", "module"] - -x = "" - -# Until true, gets.chomp is a string that matches the array -until operator_types.include? x -puts "Would you like to add(+), subtract(-), multiply(*), divide(/), exponent(^), or module(%)?" -x = gets.chomp.downcase -user_operator = x -end - -# Checks if gets is a float (integers work) retries until it is -puts "Please enter the first number:" -user_num1 = Float(gets) rescue retry - -puts "Please enter the second number:" -user_num2 = Float(gets) rescue retry - -# Runs method with user inputs -puts "\n" -calculator(user_operator, user_num1, user_num2) - -puts "\n------------------" -puts "Calc-U-Later!" -puts "------------------" From 8c175b310e5a81889c2eb9f20f21e135da397a24 Mon Sep 17 00:00:00 2001 From: Laura Robertson Date: Fri, 11 Aug 2017 08:32:40 -0700 Subject: [PATCH 5/5] Add files via upload --- calculator1.rb | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 calculator1.rb diff --git a/calculator1.rb b/calculator1.rb new file mode 100644 index 0000000..1cfdeae --- /dev/null +++ b/calculator1.rb @@ -0,0 +1,79 @@ +# ****************** +# Stage 1 Calculator +# ****************** + +# Method for calculator +def calculator(operation, num1, num2) + + # Variables created for each calculation type + result_add = num1 + num2 + result_subtract = num1 - num2 + result_multiply = num1 * num2 + result_divide = num1 / num2 + result_exponent = num1 ** num2 + result_module = num1 % num2 + + # Output based on operation type + if operation == "+" || operation == "add" + puts "#{num1} + #{num2} = #{result_add}" + elsif operation == "-" || operation == "subtract" + puts "#{num1} - #{num2} = #{result_subtract}" + elsif operation == "*" || operation == "multiply" + puts "#{num1} * #{num2} = #{result_multiply}" + elsif operation == "/" || operation == "divide" + # Divide by 0 output + if num1 == 0 || num2 == 0 + puts "Dividing by zero is undefined!" + else + puts "#{num1} / #{num2} = #{result_divide}" + end + elsif operation == "^" || operation == "exponent" + puts "#{num1} ^ #{num2} = #{result_exponent}" + elsif operation == "%" || operation == "module" + puts "#{num1} % #{num2} = #{result_module}" + else + # Error message, just in case + puts "Hey! How'd this glitch happen?" + end +end + +puts "------------------" +puts "Let's do some math" +puts "------------------\n\n" + +# Operation options stored in array for the until loop to scan over +operator_types = ["+", "add", "-", "subtract", "*", "multiply", "/", "divide", "^", "exponent", "%", "module"] + +x = "" + +# Until true, gets.chomp is a string that matches the array +until operator_types.include? x +puts "Would you like to add(+), subtract(-), multiply(*), divide(/), exponent(^), or module(%)?" +x = gets.chomp.downcase +user_operator = x +end + +# Checks if gets is a float (integers work) retries until it is +puts "Please enter the first number:" +begin +user_num1 = Float(gets) +rescue ArgumentError + puts "Try again" +retry +end + +puts "Please enter the second number:" +begin +user_num2 = Float(gets) +rescue ArgumentError + puts "Try again" +retry +end + +# Runs method with user inputs +puts "\n" +calculator(user_operator, user_num1, user_num2) + +puts "\n------------------" +puts "Calc-U-Later!" +puts "------------------"