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
102 changes: 102 additions & 0 deletions calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# Intro for user
puts "\nI'm a calculator.\n\nI can add, subtract, multiply and divide."

# Method to determine whether input is numeric
def numeric(num)
if num =~ /[-+]?([0-9]*\.[0-9]+|[0-9]+)/

Choose a reason for hiding this comment

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

nice regular expression! Good way to verify something's a number.

return true
else
return false
end
end

# User prompted to enter first number
print "\nGive me a number: "
num1 = gets.chomp

# User gets message to enter number again if it is not numerical
until numeric(num1)

Choose a reason for hiding this comment

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

Good way to trap the user until they enter a valid number.

print "\nThat's not a number! Try again: "
num1 = gets.chomp
end

# numbers are converted to either integers or floats
if num1 =~ /\A\d+\z/
num1 = num1.to_i
else
num1 = num1.to_f
end

# List of acceptable operations
operations = ["add", "+", "subtract", "-", "multiply", "*", "divide", "/"]

# Prompt user to enter operation
print "Enter an operation: "
operation = gets.chomp.downcase

# User prompted to enter valid operation if they enter something not acceptable.
until operations.include? operation
print "\nTry again! Enter valid operation (+,-,*,/): "
operation = gets.chomp
end

# Enter Number 2
print "Give me another number: "
num2 = gets.chomp

until numeric(num2)
print "\nThat's not a number! Try again: "
num2 = gets.chomp
end

if num2 =~ /\A\d+\z/
num2 = num2.to_i
else
num2 = num2.to_f
end

if operation == "divide" || operation == "/"
until num2 != 0.0

Choose a reason for hiding this comment

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

good catch

print "Error: Division by zero is undefined. Try another number: "
num2 = gets.chomp
until numeric(num2)

Choose a reason for hiding this comment

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

This looks like a similar operation to what you did before... hmm... this would make a good method (prompting the user for a number and reading it in).

print "That's not a number! Try again: "
num2 = gets.chomp
end
if num2 =~ /\A\d+\z/
num2 = num2.to_i
else
num2 = num2.to_f
end
end
end

# create methods for each operation
def add(num_one, num_two)
return num_one + num_two
end

def subtract(num_one, num_two)
return num_one - num_two
end

def multiply(num_one, num_two)
return num_one * num_two
end

def divide(num_one, num_two)
return num_one / num_two
end

# the result value will be printed based on operation user input and two numbers input
if operation == "add" || operation == "+"
puts "\n#{num1} + #{num2} = #{add(num1, num2)}"
elsif operation == "subtract" || operation == "-"
puts "\n#{num1} - #{num2} = #{subtract(num1, num2)}"
elsif operation == "multiply" || operation == "*"
puts "\n#{num1} * #{num2} = #{multiply(num1, num2)}"
elsif operation == "divide" || operation == "/"
puts "\n#{num1} / #{num2} = #{divide(num1, num2)}"
else
puts "error"
end