forked from AdaGold/calculator
-
Notifications
You must be signed in to change notification settings - Fork 42
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
LauraAddams
wants to merge
5
commits into
Ada-C8:master
Choose a base branch
from
LauraAddams:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
# 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 "------------------" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This method could be broken up into 6 methods. It's trying to do too many things.