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
66 changes: 66 additions & 0 deletions calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
puts "Welcome to the Calculator program! Which operator would you like to use?"
puts "1. add (+)
2. subtract (-)
3. multiply (*)
4. divide (/)
5. exponify (^)
6. find a remainder from a division (%)"

# Check if a valid operator is chosen
command = gets.chomp.downcase
until ["add", "+", "subtract", "-", "multiply", "*", "divide", "/", "remainder", "find a remainder", "%", "exponify", "^"].include?(command)
puts "Please tell me to add (+), subtract (-), multiply (*), divide (/), exponentiate (^) or find a remainder (%)!"
command = gets.chomp.downcase
end

# Check if user input is not a letter
def input_check(number)
until number.match(/\A[-]?\d+(\.\d+)?\z/)

Choose a reason for hiding this comment

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

nice use of regex :)

Copy link
Author

Choose a reason for hiding this comment

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

Thank you, Becca!

puts "Please enter a valid number "
number = gets.chomp
end
return number
end

puts "Please enter the first number "

Choose a reason for hiding this comment

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

Consider using a method or loop to get user input to DRY up your code.

Copy link
Author

Choose a reason for hiding this comment

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

Thank you for the code review and great comments. I'll do my best to dry up my code next time.

first_number = gets.chomp
checked_first = input_check(first_number)

puts "Please enter the second number "
second_number = gets.chomp
checked_second = input_check(second_number)

# Convert a string to a float or an integer depending on the presence of "."
def int_or_float(num)
return num.include?(".") ? num.to_f : num.to_i
end

checked_first = int_or_float(checked_first)
checked_second = int_or_float(checked_second)

puts "Here is the result of the calculation."

# Output the result depending on the chosen operation
case command
when "add", "+"
puts "#{checked_first} + #{checked_second} = #{checked_first + checked_second}"
when "subtract", "-"
puts "#{checked_first} - #{checked_second} = #{checked_first - checked_second}"
when "multiply", "*"
puts "#{checked_first} * #{checked_second} = #{checked_first * checked_second}"
when "divide", "/"
# Handle the division by zero case
if checked_second != 0 && (checked_first % checked_second > 0)
puts "#{checked_first} / #{checked_second} = #{checked_first.to_f / checked_second}"
elsif checked_second == 0
puts "Error! Division by zero"
end
when "exponify", "^"
# Show the process of exponentiation
print "#{checked_first} * " * (checked_second - 1)
puts "#{checked_first} = #{checked_first ** checked_second}"
when "remainder", "find a remainder", "%"
puts "#{checked_first} % #{checked_second} = #{checked_first % checked_second}"
else
puts "What do you want from me?!"
end