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
101 changes: 101 additions & 0 deletions calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# Methods for each arithmetic operator
def add(num_one, num_two)
puts "#{num_one} + #{num_two} = #{num_one + num_two}"
end

def subtract(num_one, num_two)
puts "#{num_one} - #{num_two} = #{num_one - num_two}"
end

def multiply(num_one, num_two)
puts "#{num_one} * #{num_two} = #{num_one * num_two}"
end

def divide(num_one, num_two)
puts "#{num_one} / #{num_two} = #{num_one / num_two}"
end

def modulo(num_one, num_two)
puts "#{num_one} % #{num_two} = #{num = num_one / num_two
total = num_two * num
remainder = num_one - total}"
end

def exponent(base_num, exp_num)
result = 1
exp_num.times do
result *= base_num
end
puts "#{base_num}^#{exp_num} = #{result}"
end

# Welcome message to user
puts "Welcome to the Calculator program! Which operator would you like to use?
1. add(+)
2. subtract(-)
3. multiply (*)
4. divide (/)
5. modulo (%)
6. exponent (** or ^)"

puts "Please choose one operator(name or symbol):"

# Stores user's input as one of the operators or prints a message telling them to enter a valid operator
while users_operator = gets.chomp.upcase

Choose a reason for hiding this comment

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

This is clever -- however an includes? method would simplify this operation:

until [array of valid operations].includes?(users_operator)

case users_operator
when "ADD", "+"
break
when "SUBTRACT", "-"
break
when "MULTIPLY", "*"
break
when "DIVIDE", "/"
break
when "MODULO", "%"
break
when "EXPONENT", "**", "^"
break
else
puts "Invalid operator. Please enter a valid operator in order to continue: "
end
end

puts "Please enter a number:"

# Checks and validates user input. If input is not an integer, an exception is thrown and prompts the user to enter a valid number
begin
num_one = Integer(gets.chomp) # Question: Is there a way to accept both integers and floats from the user? Would this require conditional statements?

Choose a reason for hiding this comment

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

Instead of Interger(gets.chomp) you could use Float(gets.chomp). Converting to Floats is a better option so that it performs division correctly (5/2 = 2.5 (as opposed to 2).)

rescue
puts "Invalid input. Please enter a valid number: "
retry
end

puts "Please enter another number:"

begin

Choose a reason for hiding this comment

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

Consider using a loop to DRY up your code.

num_two = Integer(gets.chomp)
rescue
puts "Invalid input. Please enter a valid number: "
retry
end

# Performs calculations based on user input

Choose a reason for hiding this comment

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

The is a clever use of begin/rescue/retry. Nice research!

if users_operator == "ADD" || users_operator == "+"

Choose a reason for hiding this comment

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

Consider using a case/when block to simplify your code.

add(num_one, num_two)
elsif users_operator == "SUBTRACT" || users_operator == "-"
subtract(num_one, num_two)
elsif users_operator == "MULTIPLY" || users_operator == "*"
multiply(num_one, num_two)
elsif users_operator == "DIVIDE" || users_operator == "/"
if num_two == 0 # If user enters 0 as the second number, an error message is printed and reprompts the user for a greater input
puts "You cannot divide by 0! Please enter a number greater than 0: "
num_two = gets.chomp.to_i
divide(num_one, num_two)
else
divide(num_one, num_two)
end
elsif users_operator == "MODULO" || users_operator == "%"
modulo(num_one, num_two)
elsif users_operator == "EXPONENT" || users_operator == "**" || users_operator == "^"
exponent(num_one, num_two)
end