forked from AdaGold/calculator
-
Notifications
You must be signed in to change notification settings - Fork 42
carets Julia Meier calculator #23
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
julmeier
wants to merge
1
commit into
Ada-C8:master
Choose a base branch
from
julmeier: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,116 @@ | ||
|
|
||
| # create methods to define 4 operators | ||
|
|
||
| def add(x,y) | ||
| return x+y | ||
| end | ||
|
|
||
| def subtract(x,y) | ||
| return x-y | ||
| end | ||
|
|
||
| def multiply(x,y) | ||
| return x*y | ||
| end | ||
|
|
||
| def divide(x,y) | ||
| return x/y | ||
| end | ||
|
|
||
| def exponent(x,y) | ||
| return x**y | ||
| end | ||
|
|
||
| def modulo(x,y) | ||
| return x%y | ||
| end | ||
|
|
||
| def valid_numeric_input(x) | ||
| input_to_integer = x.to_i | ||
| integer_back_to_string = input_to_integer.to_s | ||
| if x == "0" || x == integer_back_to_string | ||
| return true | ||
| elsif x == "" || x != integer_back_to_string | ||
| return false | ||
| else | ||
| return "error" | ||
| end | ||
| end | ||
|
|
||
| # asks user for the type of operation | ||
| valid_operator = false | ||
|
|
||
| while valid_operator == false | ||
| puts "What type of calculation do you want to do? Please enter one of the following options: | ||
| add or + | ||
| subtract or - | ||
| multiply or x or * | ||
| divide or / | ||
| exponent or ** | ||
| modulo or %" | ||
|
|
||
| operation = gets.chomp.downcase | ||
|
|
||
| if operation == "add" || operation == "+" || operation == "subtract" || operation == "-" || operation == "multiply" || operation == "x" || operation == "*" || operation == "divide" || operation == "/" || operation == "modulo" || operation == "%" | ||
| valid_operator = true | ||
| puts "You have entered a valid operation: #{operation}" | ||
| else | ||
| puts "You did not enter a valid operator!" | ||
| end | ||
| end | ||
|
|
||
| # receives user inputs for 2 numbers and makes sure that, if division is being used, that the denominator (second number) is not equal to 0. | ||
|
|
||
| valid_first_number = false | ||
| while valid_first_number == false | ||
| puts "Please enter the first number:" | ||
| first_number = gets.chomp | ||
| if valid_numeric_input(first_number) == false | ||
| puts "That was not a valid input. Only numbers please!" | ||
| else | ||
| valid_first_number = true | ||
| end | ||
| end | ||
|
|
||
| puts "you entered a valid first number: #{first_number}" | ||
|
|
||
| valid_second_number = false | ||
| while valid_second_number == false | ||
| puts "Please enter the second number:" | ||
| second_number = gets.chomp | ||
|
|
||
| if (operation == "divide" || operation == "/" || operation == "modulo" || operation == "%") && second_number.to_f == 0 | ||
| valid_second_number = false | ||
| puts "You cannot enter a zero value as a denominator." | ||
|
|
||
| elsif valid_numeric_input(second_number) == false | ||
| puts "That was not a valid input. Only numbers please!" | ||
|
|
||
| else | ||
| valid_second_number = true | ||
| end | ||
| end | ||
|
|
||
| first_number = first_number.to_f | ||
| second_number = second_number.to_f | ||
|
|
||
| #interprets user input of the operation to apply the appropriate method | ||
| if operation == "add" || operation == "+" | ||
| answer = add(first_number, second_number) | ||
| puts "#{first_number} + #{second_number} = #{answer}" | ||
| elsif operation == "subtract" || operation == "-" | ||
| answer = subtract(first_number, second_number) | ||
| puts "#{first_number} - #{second_number} = #{answer}" | ||
| elsif operation == "multiply" || operation == "x" || operation == "*" | ||
| answer = multiply(first_number, second_number) | ||
| puts "#{first_number} * #{second_number} = #{answer}" | ||
| elsif operation == "divide" || operation == "/" | ||
| answer = divide(first_number, second_number) | ||
| puts "#{first_number} / #{second_number} = #{answer}" | ||
| elsif operation == "exponent" || operation == "**" | ||
| answer = exponent(first_number, second_number) | ||
| puts "#{first_number} / #{second_number} = #{answer}" | ||
| elsif operation == "modulo" || operation == "%" | ||
| answer = modulo(first_number, second_number) | ||
| puts "#{first_number} / #{second_number} = #{answer}" | ||
|
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. Not a big deal, but you're printing here the equation for a division instead of modulo ;) (same with line 112) |
||
| 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.
doesn't include exponent or **! Which was an optional requirement, not a big deal, but your intro says that it accepts those operators