From 2ff5c568b5fe9f1f9f8ba51ffa8316e69647dadf Mon Sep 17 00:00:00 2001 From: skwiens Date: Fri, 11 Aug 2017 08:34:11 -0700 Subject: [PATCH 1/3] Shaunna Wiens --Carets --- calculator2.rb | 124 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 calculator2.rb diff --git a/calculator2.rb b/calculator2.rb new file mode 100644 index 0000000..aee56f1 --- /dev/null +++ b/calculator2.rb @@ -0,0 +1,124 @@ +def add(a, b) + return a + b +end + +def subtract(a, b) + return a - b +end + +def multiply(a, b) + return a * b +end + +def divide(a, b) + return a / b.to_f +end + +def power(a, b) + return a ** b.to_f +end + +def modulus(a, b) + return a % b +end + +def verify_number(number, which) + until number.to_f.to_s == number || number.to_i.to_s == number + print "Please enter a numerical value for your #{which} number: " + number = gets.chomp + end + return number.to_f +end + +def float_or_int(number) + if number.to_s[-1] == "0" + number = number.to_i + end + return number +end + +def parenth(number) + if number.to_s[0] == "(" && number.to_s[-1] == ")" + + else + end +end + +def calculate(num1, operation, num2) + case operation + when "+", "add", "plus" + operation = "+" + result = add(num1, num2) + when "-", "subtract", "minus" + operation = "-" + result = subtract(num1, num2) + when "/", "divide" + operation = "/" + if num2 == 0 + result = "Error. Cannot divide by zero." + else + result = divide(num1,num2) + end + when "*", "x", "multiply", "times" + operation = "*" + result = multiply(num1, num2) + when "power", "exponent", "^" + operation = "^" + result = power(num1, num2) + when "modulus", "remainder", "%" + operation = "%" + result = modulus(num1, num2) + else + result = "42. Really, this isn't possible. But since 42 is the answer to life, the universe, and everything, we'll just assume it is also the answer to this operation that you've asked." + end +end + +puts "Enter your mathematical expression using no spaces: " +computation = gets.chomp + +while computation == "" + puts "Enter your mathematical expression using no spaces: " + computation = gets.chomp +end + +math_hash = { + "plus" => "+", + "add" => "+", + "subtract" => "-", + "minus" => "-", + "multiply" => "*", + "times" => "*", + "x" => "*", + "divide" => "/", + "power" => "^", + "exponent" => "^", + "modulus" => "%", + "remainder" => "%" +} + +math_hash.each do |key, value| + computation.gsub!(key, value) +end + +math_regex = /-\*|\+|\^|\*|\/|%/ +operation_index = computation =~ math_regex + +num1 = computation[0...operation_index] +num1 = verify_number(num1, "first") +operation = computation[operation_index] +num2 = computation[operation_index+1..-1] +num2 = verify_number(num2, "second") + +result = calculate(num1, operation, num2) + +num1 = float_or_int(num1) +num2 = float_or_int(num2) +result = float_or_int(result) + +#Prints equation and result to the sceen. If the power symbol is used, the resulting printout also shows the supporting multiplication. +print "#{num1}#{operation}#{num2} = " +if operation == "^" + print "#{num1}*" *(num2-1) + print "#{num1} = " +end +puts "#{result}" From b73841af5fae9ecb44dd137b29e9d79ad076f1b0 Mon Sep 17 00:00:00 2001 From: skwiens Date: Fri, 11 Aug 2017 09:01:36 -0700 Subject: [PATCH 2/3] YAY! It subtracts! Forgot a pipe. Also added comments that should have been done as I was working... --- calculator2.rb | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/calculator2.rb b/calculator2.rb index aee56f1..3aef435 100644 --- a/calculator2.rb +++ b/calculator2.rb @@ -1,3 +1,4 @@ +#Define mathematical methods def add(a, b) return a + b end @@ -22,6 +23,7 @@ def modulus(a, b) return a % b end +#Verify that entry is a number def verify_number(number, which) until number.to_f.to_s == number || number.to_i.to_s == number print "Please enter a numerical value for your #{which} number: " @@ -30,6 +32,7 @@ def verify_number(number, which) return number.to_f end +#Determine whether to print a float or integer to the screen based on if it ends in zero (int) def float_or_int(number) if number.to_s[-1] == "0" number = number.to_i @@ -37,13 +40,19 @@ def float_or_int(number) return number end +#Wasn't used.... this is a work in progress....Will use gsub to replace the parenthesis with the number. def parenth(number) if number.to_s[0] == "(" && number.to_s[-1] == ")" - + parenth_computation = number[1..(number.length)] + find_numbers(parenth_computation) + number = calculate(num1, operation, num2) else + number = number end + return number end +#Calculates based on operation (Still need to delete extra words since they are replaced in line 108 def calculate(num1, operation, num2) case operation when "+", "add", "plus" @@ -73,14 +82,17 @@ def calculate(num1, operation, num2) end end +#Prompt user to enter math expression puts "Enter your mathematical expression using no spaces: " computation = gets.chomp +#Prompts user to enter math expression if they only pressed enter while computation == "" puts "Enter your mathematical expression using no spaces: " computation = gets.chomp end +#A hash that uses words as keys and the math symbols as values so that words can be replaced by symbols. math_hash = { "plus" => "+", "add" => "+", @@ -96,21 +108,26 @@ def calculate(num1, operation, num2) "remainder" => "%" } +#Replace math words with math symbols math_hash.each do |key, value| computation.gsub!(key, value) end -math_regex = /-\*|\+|\^|\*|\/|%/ +#Look for the index of the operator +math_regex = /-|\*|\+|\^|\*|\/|%/ operation_index = computation =~ math_regex +#Determine the first number, operation, and second number num1 = computation[0...operation_index] num1 = verify_number(num1, "first") operation = computation[operation_index] num2 = computation[operation_index+1..-1] num2 = verify_number(num2, "second") +#Calculate result result = calculate(num1, operation, num2) +#Determine if numbers should be printed as floats or integers. num1 = float_or_int(num1) num2 = float_or_int(num2) result = float_or_int(result) From a31aa175da91d4d185eed50fb9419494a9215900 Mon Sep 17 00:00:00 2001 From: skwiens Date: Fri, 11 Aug 2017 13:55:36 -0700 Subject: [PATCH 3/3] Update calculator2.rb Updated to prompt user to enter a valid math expression if they don't include an operator in their input --- calculator2.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/calculator2.rb b/calculator2.rb index 3aef435..3e8fe82 100644 --- a/calculator2.rb +++ b/calculator2.rb @@ -86,9 +86,9 @@ def calculate(num1, operation, num2) puts "Enter your mathematical expression using no spaces: " computation = gets.chomp -#Prompts user to enter math expression if they only pressed enter -while computation == "" - puts "Enter your mathematical expression using no spaces: " +#Prompts user to enter a valid math expression if they don't include an operator. +while math_hash.to_a.flatten & computation.split("") == [] + puts "Please enter a VALID mathematical expression" computation = gets.chomp end