From 1fce96157af074c5e9443386a0b786a79ca6e825 Mon Sep 17 00:00:00 2001 From: murog Date: Fri, 11 Aug 2017 08:52:34 -0700 Subject: [PATCH 1/2] Add files via upload --- method_calculator.rb | 90 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 method_calculator.rb diff --git a/method_calculator.rb b/method_calculator.rb new file mode 100644 index 0000000..5dacb1d --- /dev/null +++ b/method_calculator.rb @@ -0,0 +1,90 @@ + +# Method to check if a string is numeric +class String + def is_i? + /\A[-+]?\d+\z/ === self + end +end + +# Method that returns calculator operations +# If argument attempts to divide zero it returns "Cannot divide by zero" +# If argument attempt to perform operation other than +,-,*,/,%,^ it returns "Operator is not supported" +def calculator(num1,operator,num2) + if ["add", "+", "plus"].include? operator + return num1 + num2 + elsif ["subtract", "-", "minus"].include? operator + return num1 - num2 + elsif ["multiply", "*", "times"].include? operator + return num1 * num2 + elsif ["power", "^"].include? operator + return num1**num2 + elsif ["%", "modulo"].include? operator + return num1 % num2 + elsif ["divide", "/"].include? operator + if num2 == 0 + return "Cannot divide by zero" + else + return num1 / num2 + end + else + return "Operator is not currently supported" + end +end +# Method that turns operations into appropriate math symbol +def equation(num1,operator,num2) + if ["add", "+", "plus"].include? operator + return "#{num1} + #{num2}" + elsif ["subtract", "-", "minus"].include? operator + return "#{num1} - #{num2}" + elsif ["multiply", "*", "times"].include? operator + return "#{num1} * #{num2}" + elsif ["power", "^"].include? operator + return "#{num1} ^ #{num2}" + elsif ["%", "modulo"].include? operator + return "#{num1} % #{num2}" + elsif ["divide", "/"].include? operator + return "#{num1} / #{num2}" + else + return "#{num1} #{operator} #{num2}" + end +end + +# Prompts user to enter a number, operation and another number +puts "Hey, I'm a calculator. I take in arguments as number operator number. " +input = gets.chomp + +# Splits input into array named calculation +calculation = input.split + +# If user inputs more than 3 things, they will be prompted to re-enter input +# If user does not enter numbers, they will be prompted to re-enter inputs +i = 0 +while i <= 1 + if calculation.length > 3 || calculation.length < 3 + puts "Please only enter 3 things! Number Operator Number" + input = gets.chomp + calculation = input.split + puts calculation + elsif (calculation[0].is_i? != true) || (calculation[2].is_i? != true) + puts "I can only calculate numbers! Make sure you put in number operation number." + input = gets.chomp + calculation = input.split + else + i += 1 + end +end + +# Converts the first and last elements of user's input to floats and assigns them to variables +num1 = calculation[0].to_f +num2 = calculation[2].to_f +# Assigns the second element of user's input to operator variable +operator = calculation[1] +# Invokes calculator method with numbers and operator +result = calculator(num1, operator, num2) +# If the result ends with a .0, the result is displayed as an integer +if "#{result}".slice(-2..-1) == ".0" + result = "#{result}".slice(0..-3) +else +end +eq = equation("#{calculation[0]}", "#{calculation[1]}", "#{calculation[2]}" ) +puts "#{eq} = #{result}" From 3642641d4142251e4048daf780de7f57e3335d95 Mon Sep 17 00:00:00 2001 From: murog Date: Fri, 11 Aug 2017 11:38:37 -0700 Subject: [PATCH 2/2] Add files via upload Updates 1. Method that checked if input was numeric without using regex 2. Updated if/else statements in methods to use "case" instead 3. Added calculator with limited characters allowed on screen for result --- method_calculator.rb | 84 ++++++++++++++++++++++++++++++++------------ 1 file changed, 61 insertions(+), 23 deletions(-) diff --git a/method_calculator.rb b/method_calculator.rb index 5dacb1d..13661c7 100644 --- a/method_calculator.rb +++ b/method_calculator.rb @@ -1,26 +1,30 @@ - # Method to check if a string is numeric -class String - def is_i? - /\A[-+]?\d+\z/ === self - end +def is_num(number) + if number.to_s == number.to_f.to_s + return true + elsif number.to_s == number.to_i.to_s + return true + else + return false + end end # Method that returns calculator operations # If argument attempts to divide zero it returns "Cannot divide by zero" # If argument attempt to perform operation other than +,-,*,/,%,^ it returns "Operator is not supported" def calculator(num1,operator,num2) - if ["add", "+", "plus"].include? operator + case operator + when "add", "+", "plus" return num1 + num2 - elsif ["subtract", "-", "minus"].include? operator + when "subtract", "-", "minus" return num1 - num2 - elsif ["multiply", "*", "times"].include? operator + when "multiply", "*", "times" return num1 * num2 - elsif ["power", "^"].include? operator + when "power", "^" return num1**num2 - elsif ["%", "modulo"].include? operator + when "%", "modulo" return num1 % num2 - elsif ["divide", "/"].include? operator + when "divide", "/" if num2 == 0 return "Cannot divide by zero" else @@ -30,27 +34,31 @@ def calculator(num1,operator,num2) return "Operator is not currently supported" end end + # Method that turns operations into appropriate math symbol def equation(num1,operator,num2) - if ["add", "+", "plus"].include? operator + case operator + when "add", "+", "plus" return "#{num1} + #{num2}" - elsif ["subtract", "-", "minus"].include? operator + when "subtract", "-", "minus" return "#{num1} - #{num2}" - elsif ["multiply", "*", "times"].include? operator + when "multiply", "*", "times" return "#{num1} * #{num2}" - elsif ["power", "^"].include? operator + when "power", "^" return "#{num1} ^ #{num2}" - elsif ["%", "modulo"].include? operator + when "%", "modulo" return "#{num1} % #{num2}" - elsif ["divide", "/"].include? operator + when "divide", "/" return "#{num1} / #{num2}" else return "#{num1} #{operator} #{num2}" end end + + # Prompts user to enter a number, operation and another number -puts "Hey, I'm a calculator. I take in arguments as number operator number. " +puts " I take in arguments as number operator number. " input = gets.chomp # Splits input into array named calculation @@ -60,13 +68,12 @@ def equation(num1,operator,num2) # If user does not enter numbers, they will be prompted to re-enter inputs i = 0 while i <= 1 - if calculation.length > 3 || calculation.length < 3 + if [-1, 1].include? calculation.length <=> 3 puts "Please only enter 3 things! Number Operator Number" input = gets.chomp calculation = input.split - puts calculation - elsif (calculation[0].is_i? != true) || (calculation[2].is_i? != true) - puts "I can only calculate numbers! Make sure you put in number operation number." + elsif (is_num(calculation[0]) != true) || (is_num(calculation[2]) != true) + puts "\nI can only calculate numbers! Make sure you put in number operation number." input = gets.chomp calculation = input.split else @@ -77,14 +84,45 @@ def equation(num1,operator,num2) # Converts the first and last elements of user's input to floats and assigns them to variables num1 = calculation[0].to_f num2 = calculation[2].to_f + # Assigns the second element of user's input to operator variable operator = calculation[1] + # Invokes calculator method with numbers and operator result = calculator(num1, operator, num2) + # If the result ends with a .0, the result is displayed as an integer if "#{result}".slice(-2..-1) == ".0" result = "#{result}".slice(0..-3) else end + +# Invoked equation method to convert operator into appropriate mathematical symbol +# If operation is not supported by calculator, it will not change the operator eq = equation("#{calculation[0]}", "#{calculation[1]}", "#{calculation[2]}" ) -puts "#{eq} = #{result}" + +if "#{eq} = #{result}".length > 15 + short_result = "#{result}".slice(0..5) + "..." + screen = "#{eq} = #{short_result}" +else + screen = "#{eq} = #{result}" +end + +# Prints results to calculator screen +puts " _____________________" +puts "| _________________ |" +puts " RESULT..." +puts "" +puts " #{screen} " +puts "" +puts "| _________________ |" +puts "| ___ ___ ___ ___ |" +puts "| | 7 | 8 | 9 | | + | |" +puts "| |___|___|___| |___| |" +puts "| | 4 | 5 | 6 | | - | |" +puts "| |___|___|___| |___| |" +puts "| | 1 | 2 | 3 | | x | |" +puts "| |___|___|___| |___| |" +puts "| | . | 0 | = | | / | |" +puts "| |___|___|___| |___| |" +puts "|_____________________|"