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
106 changes: 106 additions & 0 deletions calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# 2/4/2020
# Ada Classwork by Nataliya Pogodina

def print_operation_instructions
puts "Welcome to Awesome Ruby Calculator"
puts "=================================="
puts "Please, choose a math operation:"
puts "1. add(+)"
puts "2. substract(-)"
puts "3. multiply(*)"
puts "4. divide(/)"
puts "(type a name or a symbol)"
end

def get_math_operation
operation_types = ["add", "+", "substract", "-", "multiply", "*", "divide", "/"]
math_operation = gets.chomp.downcase

until operation_types.include?(math_operation)
puts "Please, enter a valid operation!"
puts "(review the guide above!)"
math_operation = gets.chomp
end
return math_operation
end


def validate(input)
while input !~ /\A[+-]?\d+(\.\d+)?\z/ # regex
print "Please, enter a valid number: "
input = gets.chomp
end
return input.to_f
end

# Understanding the regex
# / start of the regex
# \A start of the string to be matched
# [+-]? zero or one of '+' or'-'
# \d+ one or more of digit
# (\.\d+)? zero or one of 'one dot and 'one or more of digit''
# \z end of the string to be matched
# / end of the regex

# def check_if_int(input)

Choose a reason for hiding this comment

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

Minor note: you should remove unused code from your finished project. Consider creating a file for yourself to store code logic that you'd like to save and might use in the future.

# if input.to_s.include? "."
# display_number = input.to_f
# elsif
# display_number = input.to_i
# end
# return display_number
# end

def compute(math_operation, num1, num2)
case math_operation
when "add", "+"
result = num1 + num2
when "substract", "-"
result = num1 - num2
when "multiply", "*"
result = num1 * num2
when "divide", "/"

Choose a reason for hiding this comment

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

Overall nice work handling division by 0. There's a very small bug introduced when you first try to divide by 0 as evidenced by this output. When you enter a new number, it still says 5.0 / 0.0 = ....

You can't devide by zero!
Enter a different number: ^[[A
Please, enter a valid number: 6.7

Here you go!
5.0 / 0.0 = 0.7462686567164178

while num2 == 0
puts "\nYou can't devide by zero!"
print "Enter a different number: "
num2 = validate(gets.chomp)
end
result = num1 / num2
end
return result
end

def selector_symbol(math_operation)
case math_operation
when "add"
math_operation_display = "+"
when "substract"
math_operation_display = "-"
when "multiply"
math_operation_display = "*"
when "divide"
math_operation_display = "/"
else
math_operation_display = math_operation
end
return math_operation_display
end

def calculator
print_operation_instructions
math_operation = get_math_operation

print "Please, enter the first number: "
num1 = validate(gets.chomp)

print "Please, enter the second number: "
num2 = validate(gets.chomp)

result = compute(math_operation, num1, num2)

math_operation_display = selector_symbol(math_operation)
puts "\nHere you go!"
puts "#{num1} #{math_operation_display} #{num2} = #{result}"
end

calculator