Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pipes - Sarah Read-Brown - Calculator #41

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions Calculator.rb
Original file line number Diff line number Diff line change
@@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that you've split these out into separate methods! It may seem a little pedantic for things like + and -, but for more complicated methods (for example checking for zero before doing division) it results in much cleaner code.


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"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like using until op_arr.include? to control this loop. Very easy to follow what's going on.

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])