Skip to content
Open
Show file tree
Hide file tree
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
58 changes: 58 additions & 0 deletions Queues - Cara Comfort- Calculator
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
def add(num_one, num_two)
return num_one + num_two
end

def subtract(num_one, num_two)
return num_one - num_two
end

def multiply(num_one, num_two)
return num_one * num_two
end

def divide(num_one, num_two)
if num_two == 0
return "#{num_one} is not divisible by 0"
else
return num_one / num_two
end
end

def convert_to_num(num)
until num.to_f.to_s == num || num.to_i.to_s == num
print "Please input a number: "
num = gets.chomp
end

if num.include? "."
return num.to_f
else
return num.to_i
end
end


print "\nWhat operation would you like to perform? "
operation = gets.chomp
possible_operators = ["add", "+", "subtract", "-", "multiply", "*", "divide", "/"]
until possible_operators.include?(operation)
print "Please tell me to add (+), subtract (-), multiply (*) or divide (/): "
operation = gets.chomp
end

print "\nWhat is your first number? "
num_one = convert_to_num(gets.chomp)

print "\nWhat is your second number? "
num_two = convert_to_num(gets.chomp)

case operation
when "add", "+"
puts add(num_one, num_two)
when "subtract", "-"
puts subtract(num_one, num_two)
when "multiply", "*"
puts multiply(num_one, num_two)
when "divide", "/"
puts divide(num_one, num_two)
end
58 changes: 58 additions & 0 deletions calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
def add(num_one, num_two)
return num_one + num_two
end

def subtract(num_one, num_two)
return num_one - num_two
end

def multiply(num_one, num_two)
return num_one * num_two
end

def divide(num_one, num_two)
if num_two == 0
return "#{num_one} is not divisible by 0"
else
return num_one / num_two
end
end

def convert_to_num(num)
until num.to_f.to_s == num || num.to_i.to_s == num
print "Please input a number: "
num = gets.chomp
end

if num.include? "."
return num.to_f
else
return num.to_i
end
end


print "\nWhat operation would you like to perform? "
operation = gets.chomp
possible_operators = ["add", "+", "subtract", "-", "multiply", "*", "divide", "/"]
until possible_operators.include?(operation)
print "Please tell me to add (+), subtract (-), multiply (*) or divide (/): "
operation = gets.chomp
end

print "\nWhat is your first number? "
num_one = convert_to_num(gets.chomp)

print "\nWhat is your second number? "
num_two = convert_to_num(gets.chomp)

case operation
when "add", "+"
puts add(num_one, num_two)
when "subtract", "-"
puts subtract(num_one, num_two)
when "multiply", "*"
puts multiply(num_one, num_two)
when "divide", "/"
puts divide(num_one, num_two)
end