Skip to content
Open
Show file tree
Hide file tree
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
52 changes: 52 additions & 0 deletions calculatorbasic.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
operators_array = %w[+ add - subtract * multiply / divide]
num1 = nil
num2 = nil
answer = nil

puts "Welcome to the Calculator program!\n"
puts "What would you like to do (add (+), subtract (-), multiply (*) or divide (/))?"
operator = gets.chomp.to_s.downcase

# def correct_operator
# if operators_array.include?(operator) == false
# puts "That is not a valid option, please try again."
# end
# end

until operators_array.include? (operator)
puts "That is not a valid option, please try again."
operator = gets.chomp.to_s
end


until num1 != nil && num2 != nil
puts "What is the first number?"
num1 = gets.chomp.to_f
puts "What is the second number?"
num2 = gets.chomp.to_f
if num1 == nil || num2 == nil
puts "Whoops, you entered something that isn't a number! Please try again."
end
end

def math(operator, num1, num2)
if operator == "+" || operator == "add"
return num1 + num2
elsif operator == "-" || operator == "subtract"
return num1 - num2
elsif operator == "*" || operator == "multiply"
return num1 * num2
else operator == "/" || operator == "divide"
if num2 == 0
puts "I'm sorry, I can't divide by 0. Please start over."
exit
else
return num1 / num2
end
end
end




puts "The answer is #{math(operator, num1, num2)}."
71 changes: 71 additions & 0 deletions calculatorwithoptionals.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@

operators_array = ["+", "add", "-", "subtract", "*", "multiply", "/", "divide", "^", "to the power of", "%", "modulo" ]
num1 = nil
num2 = nil
answer = nil
total_operators = operators_array.length - 1

puts "Welcome to the Calculator program!\n"
puts "What would you like to do:"

i = 0
total_operators.times do
print "#{operators_array[i]}, "
i+=1
end
print "or #{operators_array.last}\n"
operator = gets.chomp.to_s.downcase

until operators_array.include? (operator)
puts "#{operator} is not a valid option, please try again."
operator = gets.chomp.to_s
end


until num1 == "0" || num1.class == Integer
puts "What is the first number?"
num1 = gets.chomp
if num1 != "0" && num1.to_i == 0
puts "Whoops, #{num1} isn't a number! Please try again."
num1 = nil
else num1 = num1.to_i
end
end

until num2 == "0" || num2.class == Integer
puts "What is the second number?"
num2 = gets.chomp
if num2 != "0" && num2.to_i == 0
puts "Whoops, #{num1} isn't a number! Please try again."
num2 = nil
else num2 = num2.to_i
end
end


def math(operator, num1, num2)
if operator == "+" || operator == "add"
return num1 + num2
elsif operator == "-" || operator == "subtract"
return num1 - num2
elsif operator == "*" || operator == "multiply"
return num1 * num2
elsif operator == "/" || operator == "divide"
if num2 == 0
puts "I'm sorry, I can't divide by 0. Please start over."
exit
else
return (num1.to_f / num2.to_f).round(2)
end
elsif operator == "^" || operator == "the the power of"
return (num1.to_f / num2.to_f).round(2)
elsif operator == "%" || operator == "modulo"
return num1 % num2
end
end

answer = "#{math(operator, num1, num2)}"
equation = "#{num1} #{operator} #{num2} = #{answer}"


puts "The answer is #{equation}."