Skip to content
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
74 changes: 74 additions & 0 deletions calc.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
class Object
def is_number?
self.to_f.to_s == self.to_s || self.to_i.to_s == self.to_s
end
end

puts "Welcome to the okayest calculator ever!"

puts "Are we using floats or integers today?"
num_class = gets.chomp.downcase

num_options = ["integer", "integers", "float", "floats"]

until num_options.include?(num_class)
print "Was that a float or integer? "
num_class = gets.chomp.downcase
end

print "Please enter an operation: "
user_operation = gets.chomp.downcase

operations = ["add", "+", "subtract", "-", "multiply", "*", "divide", "/",
"^", "exponent", "**", "%", "modulo"]

until operations.include?(user_operation)
print "Please enter valid operation, remember-this calculator is only okay: "
user_operator = gets.chomp.downcase
Copy link

@tildeee tildeee Feb 12, 2018

Choose a reason for hiding this comment

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

You probably want to assign this value to the variable user_operation instead of user_operator. If you put in an erroneous operation and get into this until loop, you end up never being able to escape, hehehe

end

print "Please enter the first number of your problem: "
num1 = gets.chomp
until num1.is_number?
print "Please enter a number, remember-this calculator is only okay: "
num1 = gets.chomp
end

if num1.include?(".")
num1 = num1.to_f
else
num1 = num1.to_i
end

print "Please enter the second number of your problem: "
num2 = gets.chomp
until num2.is_number?
print "Please enter a number, remember-this calculator is only okay: "
num2 = gets.chomp
end

if num2.include?(".")
num2 = num2.to_f
else
num2 = num2.to_i
end


case user_operation
when "add" , "+"
puts "#{num1} + #{num2} = #{num1 + num2}"
when "subtract" , "-"
puts "#{num1} - #{num2} = #{num1 - num2}"
when "multiply" , "*"
puts "#{num1} * #{num2} = #{num1 * num2}"
when "divide" , "/"
if num2 == 0
puts "Cannot divide by 0"
else
puts "#{num1} / #{num2} = #{num1 / num2}"
end
when "^" , "exponent" , "**"
puts "#{num1} ^ #{num2} = #{num1 ** num2}"
when "%" , "modulo"
puts "#{num1} % #{num2} = #{num1 % num2}"
end