From 52f441a99810a10ce184bf77e50e70f67d55fe5f Mon Sep 17 00:00:00 2001 From: SRBusiness <30781062+SRBusiness@users.noreply.github.com> Date: Fri, 11 Aug 2017 00:11:08 -0700 Subject: [PATCH 1/2] Create Pipes - Sarah Read-Brown - Calculator.rb --- Pipes - Sarah Read-Brown - Calculator.rb | 83 ++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 Pipes - Sarah Read-Brown - Calculator.rb diff --git a/Pipes - Sarah Read-Brown - Calculator.rb b/Pipes - Sarah Read-Brown - Calculator.rb new file mode 100644 index 0000000..e2244f6 --- /dev/null +++ b/Pipes - Sarah Read-Brown - Calculator.rb @@ -0,0 +1,83 @@ +# command line calculator +# array of operators for later use +op_arr = ["add", "+", "addition", "subtract", "subtraction", "-", "multiply", "*", "multiplication", "divide", "division", "/", "exponent", "^", "modulo", "remainder", "%"] + +# array to store user input numbers +number = [] + +# methods for addition, subtraction, multiplication, division, exponents, and modulus + +def add(a, b) + return a + b +end + +def sub(a, b) + return a - b +end + +def mult(a, b) + return a * b +end + +def div(a,b) + if b == 0 + return "undefined" + else + return a / b + end +end + +def mod(a,b) + return a % b +end + +def exponent(a, b) + return a ** b +end + +# method for calculating and printing out the equation and solution to the user +def calculator(op, num1, num2) + if op == "add" || op == "+" || op == "addition" + return num1 + " + " + num2 + " = #{add(num1.to_i, num2.to_i)}" + elsif op == "subtract" || op == "-" || op == "subtraction" + return num1 + " - " + num2 + " = #{sub(num1.to_i, num2.to_i)}" + elsif op == "multiply" || op == "*" || op == "multiplication" + return num1 + " * " + num2 + " = #{mult(num1.to_i, num2.to_i)}" + elsif op == "divide" || op == "/" || op == "division" + return num1 + " / " + num2 + " = #{div(num1.to_i, num2.to_i)}" + elsif op == "modulo" || op == "remainder" || op == "%" + return num1 + " % " + num2 + " = #{mod(num1.to_i, num2.to_i)}" + else op == "exponent" || op == "^" + return num1 + "^" + num2 + " = #{exponent(num1.to_i, num2.to_i)}" + end +end + +# method using regex to ensure that the user's input is a number AKA a float or integer +def number?(obj) + obj = obj.to_s unless obj.is_a? String + /\A[+-]?\d+(\.[\d]+)?\Z/.match obj +end + +puts "I am a calculator!" +puts "Please enter the type of math operation you want to perform" + +# loop to ensure the user input for the mathematical operator is valid +op = gets.chomp.to_s +until op_arr.include?(op) + puts "please enter a valid math operator" + op = gets.chomp.downcase +end + +# loop using the number? method - repeats until the user's inputs are numbers +2.times do |n| + puts "Pick a number:" + # number = nil + loop do + number[n] = gets.chomp + break if number?(number[n]) + puts "That is not a number! Try again." + end +end + +puts "---" +puts calculator(op, number[0], number[1]) From 9baeb77fbd4ec555e57dd71aaf64e71a71e7e962 Mon Sep 17 00:00:00 2001 From: SRBusiness <30781062+SRBusiness@users.noreply.github.com> Date: Fri, 11 Aug 2017 00:12:51 -0700 Subject: [PATCH 2/2] Rename Pipes - Sarah Read-Brown - Calculator.rb to Calculator.rb --- Pipes - Sarah Read-Brown - Calculator.rb => Calculator.rb | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Pipes - Sarah Read-Brown - Calculator.rb => Calculator.rb (100%) diff --git a/Pipes - Sarah Read-Brown - Calculator.rb b/Calculator.rb similarity index 100% rename from Pipes - Sarah Read-Brown - Calculator.rb rename to Calculator.rb