forked from AdaGold/calculator
-
Notifications
You must be signed in to change notification settings - Fork 52
Branches - Paige #31
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
minipaige02
wants to merge
1
commit into
Ada-C12:master
Choose a base branch
from
minipaige02: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
Branches - Paige #31
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 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,102 @@ | ||
# Calculator Program | ||
|
||
# stores valid input options for math operations | ||
valid_maths = [ | ||
"add", | ||
"+", | ||
"subtract", | ||
"-", | ||
"multiply", | ||
"*", | ||
"divide", | ||
"/", | ||
"exponent", | ||
"^", | ||
"modulo", | ||
"%" | ||
] | ||
|
||
# method for text displaying available math options | ||
def maths_text | ||
puts " Add (+)" | ||
puts " Subtract (-)" | ||
puts " Multiply (*)" | ||
puts " Divide (/)" | ||
puts " Exponent (^)" | ||
puts " Modulo (%)" | ||
end | ||
|
||
# displays welcome message | ||
puts "Welcome to the Calculator Program!" | ||
|
||
# displays available math operations | ||
# asks user to input a math operation | ||
puts "Please select one of the following options: " | ||
maths_text | ||
print "Selection: " | ||
operation = gets.chomp.downcase | ||
|
||
until valid_maths.include?(operation) | ||
puts "I can't do that. I only know how to:" | ||
maths_text | ||
print "Which operation do you want me to perform? " | ||
operation = gets.chomp.downcase | ||
end | ||
|
||
# asks user to provide a number | ||
print "First number: " | ||
num_1 = gets.chomp | ||
until num_1.to_i.to_s == num_1 || num_1.to_f.to_s == num_1 | ||
puts "That's not a number. Try again." | ||
print "First number: " | ||
num_1 = gets.chomp | ||
end | ||
|
||
# asks user to provide a second number | ||
print "Second number: " | ||
num_2 = gets.chomp | ||
until num_2.to_i.to_s == num_2 || num_2.to_f.to_s == num_2 | ||
puts "That's not a number. Try again." | ||
print "Second number: " | ||
num_2 = gets.chomp | ||
end | ||
|
||
# asks user to state if result should be integer or float | ||
print "Do you want the result as an integer? " | ||
result_integer = gets.chomp.downcase | ||
|
||
case result_integer | ||
when "yes", "y" | ||
num_1 = num_1.to_i | ||
num_2 = num_2.to_i | ||
when "no", "n" | ||
num_1 = num_1.to_f | ||
num_2 = num_2.to_f | ||
end | ||
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. This is great! |
||
|
||
# executes and prints math operation | ||
puts "\n--CALCULATION--" | ||
case operation | ||
when "add", "+" | ||
result = num_1 + num_2 | ||
puts "#{num_1} + #{num_2} = #{result}" | ||
when "subtract", "-" | ||
result = num_1 - num_2 | ||
puts "#{num_1} - #{num_2} = #{result}" | ||
when "multiply", "*" | ||
result = num_1 * num_2 | ||
puts "#{num_1} * #{num_2} = #{result}" | ||
when "divide", "/" | ||
if num_2 == 0 | ||
puts "ERROR -- You can't divide by 0!" | ||
else | ||
result = num_1 / num_2 | ||
puts "#{num_1} / #{num_2} = #{result}" | ||
end | ||
when "exponent", "^" | ||
result = num_1 ** num_2.to_i | ||
puts "#{num_1} ^ #{num_2} = #{result}" | ||
when "modulo", "%" | ||
result = num_1 % num_2 | ||
puts "#{num_1} % #{num_2} = #{result}" | ||
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.
I love that this display functionality is pulled into a method! Keep doing things like this! Also, I like my method names to usually be actions or action-like. Consider maybe changing this method name to
display_maths_text
orshow_maths_text
?