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
Jocelyn Gonzalez --Carets #28
Open
jocegonz
wants to merge
1
commit into
Ada-C8:master
Choose a base branch
from
jocegonz: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
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,108 @@ | ||
|
||
#ask the user for an operation and two numbers | ||
puts "Choose your operator: \nOptions \n+\n-\n*\n/\nadd\nsubtract\nmultiply\ndivide\n" | ||
operator = gets.chomp.downcase | ||
|
||
#array of valid operators | ||
valid_operators = ["+", "-", "*", "/", "add", "subtract", "multiply", "divide"] | ||
|
||
#check if chosen operator is valid | ||
until valid_operators.include? operator | ||
puts "Please enter a valid operator:" | ||
operator = gets.chomp.downcase | ||
end | ||
|
||
#method that checks if numeric values are valid | ||
class String | ||
def valid_float? | ||
Float(self) rescue false | ||
end | ||
end | ||
|
||
#input for 2 numeric values | ||
puts "Please enter your first numeric value:" | ||
value1 = gets.chomp | ||
until value1.valid_float? | ||
puts "Please enter a valid numeric value:" | ||
value1 = gets.chomp | ||
end | ||
value1 = value1.to_f | ||
|
||
puts "Please enter your second numeric value:" | ||
value2 = gets.chomp | ||
until value2.valid_float? | ||
puts "Please enter a valid numeric value:" | ||
value2 = gets.chomp | ||
end | ||
value2 = value2.to_f | ||
|
||
#chooses operation based on operator | ||
case operator | ||
when "+", "add" | ||
answer = value1 + value2 | ||
when "-", "subtract" | ||
answer = value1 - value2 | ||
when "*", "multiply" | ||
answer = value1 * value2 | ||
when "/", "divide" | ||
#checks for zeros as second value | ||
#note: this doesn't also check if it's a valid float :( | ||
until value2 != 0 | ||
puts "Please enter a valid value that isn't 0:" | ||
value2 = gets.chomp.to_f | ||
end | ||
answer = value1 / value2 | ||
else puts "This means there's a break in my operators." | ||
end | ||
|
||
puts answer | ||
|
||
|
||
|
||
|
||
=begin | ||
class String | ||
def valid_float? | ||
# The double negation turns this into an actual boolean true - if you're | ||
# okay with "truthy" values (like 0.0), you can remove it. | ||
!!Float(self) rescue false | ||
end | ||
end | ||
|
||
|
||
def calculator(val1, val2) | ||
print operator_action | ||
val1.send operator_action, val2 | ||
end | ||
|
||
print calculator(value1, value2) | ||
|
||
#check if operator is not a string containing options | ||
while operator != "+" && operator != "add" && | ||
operator != "-" && operator != "subtract" && | ||
operator != "*" && operator != "multiply" && | ||
operator != "/" && operator != "divide" | ||
puts "Please enter a valid operation:" | ||
operator = gets.chomp.downcase | ||
end | ||
|
||
case operator | ||
when "+", "add" | ||
operator = "+" | ||
when "-", "subtract" | ||
operator = "-" | ||
when "*", "multiply" | ||
operator = "*" | ||
when "/", "divide" | ||
operator = "" | ||
else puts "::shrug::" | ||
end | ||
|
||
while operator == ask_operator | ||
puts "Please enter a valid operation:" | ||
operator = gets.chomp.downcase | ||
end | ||
|
||
#check if val1 and val2 are valid | ||
#if operator is divide, second value CANNOT be 0 | ||
=end |
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.
Just a small note... in this flow, your code doesn't check if the new number that replaces 0 in "divide" operations using your
valid_float?
method. It doesn't matter too much in the end because.to_f
actually handles it pretty well, but I just wanted to let you know!