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
105 changes: 105 additions & 0 deletions calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@

# FUNCTIONS

# Boolean function on whether string input is a number
def is_numeric(num)

if num.to_f.to_s == num || num.to_i.to_s == num
return true
else
return false
end

end

# Function to reprompt user if input is not valid
def is_valid_numeric_input(order)

print "#{order} number >> "
num = gets.chomp

until is_numeric(num)
puts "Not valid number. Please try again."
print "#{order} number >> "
num = gets.chomp
end

return num
end

# Simplify to integer if possible
def simplify_integer(num)
if num.to_i == num then
return num.to_i
else
return num
end
end

puts "\n\nWelcome to the calculator! Which operator do you want to use?"
puts "\n\tadd (+)"
puts "\tsubtract (-)"
puts "\tmultiply (*)"
puts "\tdivide (/)"
puts "\texponify (^)"
puts "\tmodulo (%)"

print "\nPlease input name or symbol of operator >> "

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

operator = gets.chomp.downcase
until operators.include? operator
print "Not valid operation. Please try again. >> "
operator = gets.chomp
end


puts "What two numbers would you like to operate on respectively?"
first_num = is_valid_numeric_input("first").to_f
second_num = is_valid_numeric_input("second").to_f


case operator
when "add", "+"
answer = first_num + second_num
symbol = "+"
when "subtract", "-"
answer = first_num - second_num
symbol = "-"
when "multiply", "*"
answer = first_num * second_num
symbol = "*"
when "divide", "/"

# Handle cases when trying to divide by zero
if second_num == 0 then

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

you add in the then keyword to this... which is cool! I just wanted to call out that I don't see this syntax much, and I have no expectations for you to use it, unless it makes you happy.


until is_numeric(second_num) && second_num.to_f != 0
puts "Please enter valid second number. (Cannot be zero)"
print "second number >> "
second_num = gets.chomp
end
end

second_num = second_num.to_f

answer = first_num / second_num
symbol = "/"

when "exponify", "^"
answer = first_num ** second_num
symbol = "^"

when "modulo", "%"
answer = first_num % second_num
symbol = "%"

end

# Simplify floats to integers if possible
first = simplify_integer(first_num)
second = simplify_integer(second_num)
answer = simplify_integer(answer)

puts "#{first} #{symbol} #{second} = #{answer}"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

if i were to prioritize conciseness over clarity/readability, i may suggest the following refactorings:

  1. renaming simplify_integer to simply the name format, as if it were a method responsible for formatting the number to a human-readable string
  2. renaming first_num to first and second_num to second...
  3. by getting those variable names back by inlining everything:
puts "#{format(first)} #{symbol} #{format(second)} = #{answer}"