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

Kayla Ecker - Carets #43

Open
wants to merge 1 commit 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
62 changes: 62 additions & 0 deletions Calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
def add(num1, num2)
return num1 + num2
end

def subtract(num1, num2)
return num1 - num2
end

def multiply(num1, num2)
return num1 * num2
end

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


def convert_to_num(num)
until num.to_f.to_s == num || num.to_i.to_s == num
print "I'm sorry, I did not understand. Please input a number (ex: 1): "
num = gets.chomp
end

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


puts "What operation would you like to use? \n\nYour options are to add (+), subtract (-), multiply (*) or divide (/): "
operation = gets.chomp
possible_operators = ["add", "+", "subtract", "-", "multiply", "*", "divide", "/"]
until possible_operators.include?(operation)
print "I'm sorry, the only available options are to add (+), subtract (-), multiply (*) or divide (/): "
operation = gets.chomp
end


puts "What is your first number? "
num1 = convert_to_num(gets.chomp)

puts "What is your second number? "
num2 = convert_to_num(gets.chomp)


case operation
when "add", "+"
puts add(num1, num2)
when "subtract", "-"
puts subtract(num1, num2)
when "multiply", "*"
puts multiply(num1, num2)
when "divide", "/"
puts divide(num1, num2)

end