-
Notifications
You must be signed in to change notification settings - Fork 44
Brenda Rios - Calculator - Octos #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
# definition of methods | ||
def add(num1,num2) | ||
return num1 + num2 | ||
end | ||
|
||
def subtract(num1,num2) | ||
return num1 - num2 | ||
end | ||
|
||
def multiply(num1,num2) | ||
return num1 * num2 | ||
end | ||
|
||
def divide(num1,num2) | ||
return num1 / num2 | ||
end | ||
|
||
def numberic?(string) | ||
/[%\+\*\d]/.match(string) | ||
end | ||
|
||
def empty?(input) | ||
if input.empty? | ||
puts "You did not enter anything!" | ||
end | ||
end | ||
|
||
def type?(input) | ||
if /\A\d+\z/.match(input) | ||
return "integer" | ||
elsif /\A\d+\.\d+\z/.match(input) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I really like that you've split all these off into helper methods - good organization! |
||
return "float" | ||
end | ||
end | ||
|
||
|
||
puts "Welcome to your calculator!\n" | ||
|
||
puts "I can add, subtract, multiply or divide\n" | ||
|
||
puts "Please enter the operator you want to use : add, + , subtract, - , multiply, * , divide , / " | ||
user_input = gets.chomp.downcase | ||
|
||
empty?(user_input) | ||
|
||
# to make sure the user input is a valid operator | ||
arr_operators = ["add","+", "subtract", "-", "multiply", "*" , "divide", "/"] | ||
|
||
attempt = 0 | ||
while !arr_operators.include?(user_input) | ||
if attempt < 3 | ||
attempt += 1 | ||
puts "Please enter a valid operator. You have #{(-attempt + 3) + 1} more attempts.\n" | ||
puts "Operator options: add, subtract, multiply, divide, +, - , *, /" | ||
user_input = gets.chomp | ||
else | ||
puts "Too many attempts..start the program again" | ||
exit | ||
end | ||
end | ||
|
||
print "Please enter value 1: " | ||
num1 = gets.chomp | ||
|
||
empty?(num1) | ||
|
||
until numberic? num1 do | ||
puts "Please enter a numeric expression for value 1: " | ||
num1 = gets.chomp | ||
end | ||
|
||
print "\nPlease enter value 2: " | ||
num2 = gets.chomp | ||
|
||
empty?(num1) | ||
|
||
until numberic? num2 do | ||
puts "Please enter a numeric expression for value 2: " | ||
end | ||
|
||
# To handle when user attempts to divide by zero. | ||
if user_input == "divide" || user_input == "/" | ||
while num2 == 0 | ||
puts "Cannot divide by 0. Please enter again the divisor" | ||
num2 = gets.chomp | ||
end | ||
end | ||
|
||
case user_input | ||
when "add", "+" | ||
puts "\nAnswer: #{results = add(eval(num1), eval(num2))}" | ||
puts "#{num1} + #{num2} = #{results}" | ||
when "subtract", "-" | ||
puts "\nAnswer: #{results = subtract(eval(num1),eval(num2))}" | ||
puts "#{num1} - #{num2} = #{results}" | ||
when "multiply", "*" | ||
puts "\nAnswer: #{results = multiply(eval(num1),eval(num2))}" | ||
puts "#{num1} * #{num2} = #{results}" | ||
when "divide", "/" | ||
puts "\nAnswer: #{results = divide(eval(num1),eval(num2))}" | ||
puts "#{num1} / #{num2} = #{results}" | ||
end | ||
|
||
# this will allow the program to know if the result should be printed as a float or an integer | ||
if type?(num1) == "float" | ||
results.to_f | ||
elsif type?(num1) == "integer" && type?(num2) == "integer" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This conditional could be simplified a bit: if type?(num1) == "integer" && type?(num2) == "integer"
results.to_i
else
results.to_f
end Also, you're turning |
||
results.to_i | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the spelling you're looking for here is
numeric?
.