Skip to content
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

Laura Robertson -- Carets #29

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
79 changes: 79 additions & 0 deletions calculator1.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# ******************
# Stage 1 Calculator
# ******************

# Method for calculator
def calculator(operation, num1, num2)

Choose a reason for hiding this comment

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

This method could be broken up into 6 methods. It's trying to do too many things.


# Variables created for each calculation type
result_add = num1 + num2
result_subtract = num1 - num2
result_multiply = num1 * num2
result_divide = num1 / num2
result_exponent = num1 ** num2
result_module = num1 % num2

# Output based on operation type
if operation == "+" || operation == "add"
puts "#{num1} + #{num2} = #{result_add}"
elsif operation == "-" || operation == "subtract"
puts "#{num1} - #{num2} = #{result_subtract}"
elsif operation == "*" || operation == "multiply"
puts "#{num1} * #{num2} = #{result_multiply}"
elsif operation == "/" || operation == "divide"
# Divide by 0 output
if num1 == 0 || num2 == 0
puts "Dividing by zero is undefined!"
else
puts "#{num1} / #{num2} = #{result_divide}"
end
elsif operation == "^" || operation == "exponent"
puts "#{num1} ^ #{num2} = #{result_exponent}"
elsif operation == "%" || operation == "module"
puts "#{num1} % #{num2} = #{result_module}"
else
# Error message, just in case
puts "Hey! How'd this glitch happen?"
end
end

puts "------------------"
puts "Let's do some math"
puts "------------------\n\n"

# Operation options stored in array for the until loop to scan over
operator_types = ["+", "add", "-", "subtract", "*", "multiply", "/", "divide", "^", "exponent", "%", "module"]

x = ""

# Until true, gets.chomp is a string that matches the array
until operator_types.include? x
puts "Would you like to add(+), subtract(-), multiply(*), divide(/), exponent(^), or module(%)?"
x = gets.chomp.downcase
user_operator = x
end

# Checks if gets is a float (integers work) retries until it is
puts "Please enter the first number:"
begin
user_num1 = Float(gets)
rescue ArgumentError
puts "Try again"
retry
end

puts "Please enter the second number:"
begin
user_num2 = Float(gets)
rescue ArgumentError
puts "Try again"
retry
end

# Runs method with user inputs
puts "\n"
calculator(user_operator, user_num1, user_num2)

puts "\n------------------"
puts "Calc-U-Later!"
puts "------------------"