Skip to content

Instantly share code, notes, and snippets.

@livmaria7891
Created August 12, 2016 05:28
Show Gist options
  • Save livmaria7891/908234858c5937371386451ab3f8c329 to your computer and use it in GitHub Desktop.
Save livmaria7891/908234858c5937371386451ab3f8c329 to your computer and use it in GitHub Desktop.
puts "Hey! I'm a calculator!"
# Collects and checks for appropriate user input
print "I can add, subtract, divide, multiply, do exponents, and find a remainder. What would you like to do? "
operation = gets.chomp
possible_operations = ["+","add","addition","-","subtract","subtration","*","multiply","multiplication","/","divide","division","**","exponent","exponents","%","remainder","modulo"]
until possible_operations.include? operation
print "\n\nWomp Womp. Like I said, I can add, subtract, divide, multiply, do exponents, and find a remainder. What would you like to do? "
operation = gets.chomp
end
print "Cool, cool. Which two numbers?\n"
num1 = gets.chomp
num2 = gets.chomp
if num1.include?(".") == true || num2.include?(".") == true
num1 = num1.to_f
num2 = num2.to_f
else
num1 = num1.to_i
num2 = num2.to_i
end
puts "\n\nMy work here is done:"
case operation
when "+","add","addition"
ans = num1 + num2
puts "#{num1} + #{num2} = #{ans}"
when "-","subtract","subtraction"
ans = num1 - num2
puts "#{num1} - #{num2} = #{ans}"
when "*","multiply","multiplication"
puts "#{num1} * #{num2} = #{num1 * num2}"
when "/","divide","division"
puts "#{num1} / #{num2} = #{num1 / num2}"
when "**" , "exponents"
puts "#{num1} ** #{num2} = #{num1 ** num2}"
when "%", "remainder", "modulo"
puts "#{num1} % #{num2} = #{num1 % num2}"
end
@CheezItMan
Copy link

Simple and clearly done. How could you prevent incorrect input at the command line?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment