forked from AdaGold/calculator
-
Notifications
You must be signed in to change notification settings - Fork 44
Ana Lisa Sutherland - Calculator- Octos #25
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
Open
The-Beez-Kneez
wants to merge
2
commits into
Ada-C9:master
Choose a base branch
from
The-Beez-Kneez: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
2 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 hidden or 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,93 @@ | ||
# Week#1: Calculator Build | ||
# Ana Lisa Sutherland - Octos C9 | ||
|
||
# method for addition using two numbers | ||
def addition (num_one, num_two) | ||
return num_one + num_two | ||
end | ||
#method for subtracting using two numbers | ||
def subtraction (num_one, num_two) | ||
return num_one - num_two | ||
end | ||
#method for multiplication using two numbers | ||
def multiply (num_one, num_two) | ||
return num_one * num_two | ||
end | ||
#method for division using two numbers | ||
def divide (num_one, num_two) | ||
return num_one / num_two | ||
end | ||
#OPTIONAL: method for exponents | ||
def exponent (num_one, num_two) | ||
return num_one ** num_two | ||
end | ||
#OPTIONAL: method for modulo | ||
def modulo (num_one, num_two) | ||
return num_one % num_two | ||
end | ||
#OPTIONAL: ENSURE IT IS ONLY NUMBERS THAT ARE ENTERED | ||
def req () | ||
num = gets.chomp | ||
until num =~ /^[1-9]\d*(\.\d+)?$/ | ||
if num =~ /^[1-9]\d*(\.\d+)?$/ | ||
return num | ||
else | ||
puts "Invalid number." | ||
num = gets.chomp | ||
end | ||
end | ||
if num =~ /\A\d+\z/ | ||
puts 'integer' | ||
return num.to_i | ||
elsif num =~ /\A\d+\.\d+\z/ | ||
puts 'float' | ||
return num.to_f | ||
else | ||
puts 'not integer or float' | ||
end | ||
end | ||
|
||
#collect first number from the user and run it through req method | ||
puts "Welcome, seems like you need a calculator.\n\Please Input a number." | ||
num_1 = req() | ||
puts "Enter a command." | ||
# make sure that only operators are accepted | ||
command_use = gets.chomp | ||
until ["add", "+", "subtract", "-","multiply", "*", "divide", "/", "**", "^", "modulo", "%"].include?(command_use) | ||
puts "Please put a valid command!" | ||
command_use = gets.chomp | ||
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. It might be good to let the user know what commands are valid. |
||
end | ||
|
||
#collect second number from the user and run it through req method | ||
puts "Enter a second number." | ||
num_2 = req() | ||
|
||
#method to utilize both written and modifying commands | ||
case command_use | ||
when "add", "+" | ||
puts "We're adding numbers" | ||
result = addition(num_1,num_2) | ||
when "subtract", "-" | ||
puts "We're subtracting numbers" | ||
result = subtraction(num_1,num_2) | ||
when "multiply", "*" | ||
puts "We're multiplying numbers" | ||
result = multiply(num_1,num_2) | ||
when "divide", "/" | ||
puts "We're dividing numbers" | ||
if num_2 != 0 | ||
result = divide(num_1,num_2) | ||
else | ||
puts "You cannot divide by 0." | ||
end | ||
when "^", "**" | ||
puts "We're exponentiating numbers" | ||
result = exponent(num_1,num_2) | ||
when "modulo", "%" | ||
puts "We are using a modulo for numbers." | ||
results = modulo(num_1,num_2) | ||
else | ||
puts "That makes no sense. Please tell me which operator you want to use." | ||
command = gets.chomp | ||
end | ||
puts "#{num_1} #{command_use} #{num_2} = #{result}" |
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.
Good use of a regex here to verify user input - way to go above and beyond.
One comment: I'm not so sure about the method name
req
. This doesn't really tell me what the method does, which will make code that calls it harder to read. I would prefer something more descriptive likeread_number
.