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
99 changes: 99 additions & 0 deletions calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# This is a calculator program
# the user should ask what they would like to calculate.

operations = ["multiply", "divide", "subtract", "exponify",
"modulate", "-", "*", "/", "**", "+", "%"]

puts "\nWelcome to your Calculator Program!"
puts "Enter two numbers and an operation."
puts "Would you like to continue? (yes/no) "
answer = gets.chomp.downcase

while answer == "yes" do
# first number
print "\n#1 Number: "
until (num_1 = gets.chomp) =~ /^\d*\.?\d+$/
#starting of line, any digit(zero or more), including periods, following any digit(zero or more), ending of string.
puts "\nInvalid input. Please enter a number: "
end
num_1 = num_1.to_f
# second number
print "\n#2 Number: "
until (num_2 = gets.chomp) =~ /^\d*\.?\d+$/
puts "\nInvalid input. Please enter a number: "
end
num_2 = num_2.to_f
# operation to perform
print "\nOperation: "
op = gets.chomp.downcase
until operations.include?("#{op}")
print "\nPlease enter an operation
(add, subtract, multiply, divide, exponify, or modulate): "
op = gets.chomp.downcase
end

# divide
def divide(num1, num2)
if num2 != 0

Choose a reason for hiding this comment

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

This divide method is defined inside your while loop! While this is technically valid Ruby (and certainly works), it's a little odd to me as a reader. A better option might be to define all your methods at the very top of the file, and have your main control loop below that.

print "#{num1} / #{num2} = "
(num1 / num2).round(2)
else

puts "Can't divide by zero!"
end
end

# multiply
def multiply(num1, num2)

print "#{num1} * #{num2} = "
(num1 * num2).round(2)

Choose a reason for hiding this comment

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

Printing out the equation inside the method is one way to solve this optional, but it's not always ideal, because it means your method is doing two different things: doing some math problem, and printing an equation. This takes control away from whoever's calling your method. There's no way to do multiplication without also putsing something to the screen.

In general, you should try to make your methods do only one thing.

end

# subtract
def subtract(num1, num2)

print "#{num1} - #{num2} = "
(num1 - num2).round(2)
end

# addition
def add(num1, num2)

print "#{num1} + #{num2} = "
(num1 + num2).round(2)
end

# exponify
def exponify(num1, num2)

print "#{num1} ^ #{num2} = "
(num1**num2).round(2)
end

# modulo
def modulate(num1, num2)

print "#{num1} % #{num2} = "
(num1%num2).round(2)
end

# print the result and round numbers by two decimals
case op
when "add", "+"
print add(num_1, num_2)
when "subtract", "-"
print subtract(num_1, num_2)
when "multiply", "*"
print multiply(num_1, num_2)
when "divide", "/"
print divide(num_1, num_2)
when "exponify", "**"
print exponify(num_1, num_2)
when "modulate", "%"
print modulate(num_1, num_2)
end
print "\nWould you like to continue? (yes/no) "
answer = gets.chomp.downcase
end