forked from AdaGold/calculator
-
Notifications
You must be signed in to change notification settings - Fork 48
Aurea - Calculator - Edges #29
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
Aurea-Li
wants to merge
1
commit into
Ada-C10:master
Choose a base branch
from
Aurea-Li: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 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,105 @@ | ||
|
|
||
| # FUNCTIONS | ||
|
|
||
| # Boolean function on whether string input is a number | ||
| def is_numeric(num) | ||
|
|
||
| if num.to_f.to_s == num || num.to_i.to_s == num | ||
| return true | ||
| else | ||
| return false | ||
| end | ||
|
|
||
| end | ||
|
|
||
| # Function to reprompt user if input is not valid | ||
| def is_valid_numeric_input(order) | ||
|
|
||
| print "#{order} number >> " | ||
| num = gets.chomp | ||
|
|
||
| until is_numeric(num) | ||
| puts "Not valid number. Please try again." | ||
| print "#{order} number >> " | ||
| num = gets.chomp | ||
| end | ||
|
|
||
| return num | ||
| end | ||
|
|
||
| # Simplify to integer if possible | ||
| def simplify_integer(num) | ||
| if num.to_i == num then | ||
| return num.to_i | ||
| else | ||
| return num | ||
| end | ||
| end | ||
|
|
||
| puts "\n\nWelcome to the calculator! Which operator do you want to use?" | ||
| puts "\n\tadd (+)" | ||
| puts "\tsubtract (-)" | ||
| puts "\tmultiply (*)" | ||
| puts "\tdivide (/)" | ||
| puts "\texponify (^)" | ||
| puts "\tmodulo (%)" | ||
|
|
||
| print "\nPlease input name or symbol of operator >> " | ||
|
|
||
| operators = ["add", "+", "subtract", "-", "multiply", "*", "divide", "/", "exponify", "^", "modulo", "%"] | ||
|
|
||
| operator = gets.chomp.downcase | ||
| until operators.include? operator | ||
| print "Not valid operation. Please try again. >> " | ||
| operator = gets.chomp | ||
| end | ||
|
|
||
|
|
||
| puts "What two numbers would you like to operate on respectively?" | ||
| first_num = is_valid_numeric_input("first").to_f | ||
| second_num = is_valid_numeric_input("second").to_f | ||
|
|
||
|
|
||
| case operator | ||
| when "add", "+" | ||
| answer = first_num + second_num | ||
| symbol = "+" | ||
| when "subtract", "-" | ||
| answer = first_num - second_num | ||
| symbol = "-" | ||
| when "multiply", "*" | ||
| answer = first_num * second_num | ||
| symbol = "*" | ||
| when "divide", "/" | ||
|
|
||
| # Handle cases when trying to divide by zero | ||
| if second_num == 0 then | ||
|
|
||
| until is_numeric(second_num) && second_num.to_f != 0 | ||
| puts "Please enter valid second number. (Cannot be zero)" | ||
| print "second number >> " | ||
| second_num = gets.chomp | ||
| end | ||
| end | ||
|
|
||
| second_num = second_num.to_f | ||
|
|
||
| answer = first_num / second_num | ||
| symbol = "/" | ||
|
|
||
| when "exponify", "^" | ||
| answer = first_num ** second_num | ||
| symbol = "^" | ||
|
|
||
| when "modulo", "%" | ||
| answer = first_num % second_num | ||
| symbol = "%" | ||
|
|
||
| end | ||
|
|
||
| # Simplify floats to integers if possible | ||
| first = simplify_integer(first_num) | ||
| second = simplify_integer(second_num) | ||
| answer = simplify_integer(answer) | ||
|
|
||
| puts "#{first} #{symbol} #{second} = #{answer}" | ||
|
Collaborator
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. if i were to prioritize conciseness over clarity/readability, i may suggest the following refactorings:
puts "#{format(first)} #{symbol} #{format(second)} = #{answer}" |
||
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.
you add in the
thenkeyword to this... which is cool! I just wanted to call out that I don't see this syntax much, and I have no expectations for you to use it, unless it makes you happy.