forked from AdaGold/calculator
-
Notifications
You must be signed in to change notification settings - Fork 49
Time - Nataliya #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
npogodina
wants to merge
1
commit into
Ada-C13:master
Choose a base branch
from
npogodina: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
Time - Nataliya #25
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,106 @@ | ||
| # 2/4/2020 | ||
| # Ada Classwork by Nataliya Pogodina | ||
|
|
||
| def print_operation_instructions | ||
| puts "Welcome to Awesome Ruby Calculator" | ||
| puts "==================================" | ||
| puts "Please, choose a math operation:" | ||
| puts "1. add(+)" | ||
| puts "2. substract(-)" | ||
| puts "3. multiply(*)" | ||
| puts "4. divide(/)" | ||
| puts "(type a name or a symbol)" | ||
| end | ||
|
|
||
| def get_math_operation | ||
| operation_types = ["add", "+", "substract", "-", "multiply", "*", "divide", "/"] | ||
| math_operation = gets.chomp.downcase | ||
|
|
||
| until operation_types.include?(math_operation) | ||
| puts "Please, enter a valid operation!" | ||
| puts "(review the guide above!)" | ||
| math_operation = gets.chomp | ||
| end | ||
| return math_operation | ||
| end | ||
|
|
||
|
|
||
| def validate(input) | ||
| while input !~ /\A[+-]?\d+(\.\d+)?\z/ # regex | ||
| print "Please, enter a valid number: " | ||
| input = gets.chomp | ||
| end | ||
| return input.to_f | ||
| end | ||
|
|
||
| # Understanding the regex | ||
| # / start of the regex | ||
| # \A start of the string to be matched | ||
| # [+-]? zero or one of '+' or'-' | ||
| # \d+ one or more of digit | ||
| # (\.\d+)? zero or one of 'one dot and 'one or more of digit'' | ||
| # \z end of the string to be matched | ||
| # / end of the regex | ||
|
|
||
| # def check_if_int(input) | ||
| # if input.to_s.include? "." | ||
| # display_number = input.to_f | ||
| # elsif | ||
| # display_number = input.to_i | ||
| # end | ||
| # return display_number | ||
| # end | ||
|
|
||
| def compute(math_operation, num1, num2) | ||
| case math_operation | ||
| when "add", "+" | ||
| result = num1 + num2 | ||
| when "substract", "-" | ||
| result = num1 - num2 | ||
| when "multiply", "*" | ||
| result = num1 * num2 | ||
| when "divide", "/" | ||
|
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. Overall nice work handling division by 0. There's a very small bug introduced when you first try to divide by 0 as evidenced by this output. When you enter a new number, it still says |
||
| while num2 == 0 | ||
| puts "\nYou can't devide by zero!" | ||
| print "Enter a different number: " | ||
| num2 = validate(gets.chomp) | ||
| end | ||
| result = num1 / num2 | ||
| end | ||
| return result | ||
| end | ||
|
|
||
| def selector_symbol(math_operation) | ||
| case math_operation | ||
| when "add" | ||
| math_operation_display = "+" | ||
| when "substract" | ||
| math_operation_display = "-" | ||
| when "multiply" | ||
| math_operation_display = "*" | ||
| when "divide" | ||
| math_operation_display = "/" | ||
| else | ||
| math_operation_display = math_operation | ||
| end | ||
| return math_operation_display | ||
| end | ||
|
|
||
| def calculator | ||
| print_operation_instructions | ||
| math_operation = get_math_operation | ||
|
|
||
| print "Please, enter the first number: " | ||
| num1 = validate(gets.chomp) | ||
|
|
||
| print "Please, enter the second number: " | ||
| num2 = validate(gets.chomp) | ||
|
|
||
| result = compute(math_operation, num1, num2) | ||
|
|
||
| math_operation_display = selector_symbol(math_operation) | ||
| puts "\nHere you go!" | ||
| puts "#{num1} #{math_operation_display} #{num2} = #{result}" | ||
| end | ||
|
|
||
| calculator | ||
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.
Minor note: you should remove unused code from your finished project. Consider creating a file for yourself to store code logic that you'd like to save and might use in the future.