forked from AdaGold/calculator
-
Notifications
You must be signed in to change notification settings - Fork 48
Sammi-Jo's calculator.rb (nodes) #21
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
sjlee3157
wants to merge
1
commit into
Ada-C10:master
Choose a base branch
from
sjlee3157: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,150 @@ | ||
| # calculator.rb | ||
| # https://www.google.com/url?q=https%3A%2F%2Fgithub.com%2FAda-C10%2Fcalculator&sa=D&usd=2&usg=AFQjCNF8YsY1ij6pguWOvbMUgTJO86XQ7Q | ||
|
|
||
| $known_adds = ["+", "add", "plus"] | ||
| $known_mins = ["-", "sub", "minus"] | ||
| $known_mults = ["x", "*", "times", "mult"] | ||
| $known_divs = ["/", "÷", "div", "divided by"] | ||
| $known_mods = ["%", "mod", "modulo", "remainder"] | ||
| $known_powers = ["^", "**", "power", "to the power of"] | ||
| $known_operations = $known_adds + $known_mins + $known_mults + $known_divs + $known_mods + $known_powers | ||
|
|
||
| def header | ||
| puts ":" * 50, "\n", "CALCULATOR!", "\n" | ||
| puts "[Number 1] [Operation] [Number 2] = _______", "\n" | ||
| puts "You can type any of these operations:" | ||
| puts $known_adds.join("\t") | ||
| puts $known_mins.join("\t") | ||
| puts $known_mults.join("\t") | ||
| puts $known_divs.join("\t") | ||
| puts $known_mods.join("\t") | ||
| puts $known_powers.join("\t") | ||
| puts "\n" | ||
| end | ||
|
|
||
| def footer | ||
| puts "\n", ":" * 50, "\n" | ||
| end | ||
|
|
||
| def add(a,b) | ||
| $operator_symbol = "+" | ||
| $result = a+b | ||
| end | ||
|
|
||
| def subtract(a,b) | ||
| $operator_symbol = "-" | ||
| $result = a-b | ||
| end | ||
|
|
||
| def multiply(a,b) | ||
| $operator_symbol = "x" | ||
| $result = a*b | ||
| end | ||
|
|
||
| def divide(a,b) | ||
| $operator_symbol = "÷" | ||
| if b == 0.0 | ||
| $result = nil | ||
| else | ||
| $result = a/b | ||
| end | ||
| end | ||
|
|
||
| def remainder(a,b) | ||
| $operator_symbol = "%" | ||
| if b == 0.0 | ||
| $result = nil | ||
| else | ||
| $result = (a%b).to_i | ||
| end | ||
| end | ||
|
|
||
| def exponify(a,b) | ||
| $operator_symbol = "^" | ||
| $result = (a**b) | ||
| end | ||
|
|
||
| def reset_errors_hash | ||
| $errors_hash = Hash.new | ||
| end | ||
|
|
||
| def check_number(label) | ||
| print "Number #{label}\t" | ||
| number = gets.chomp | ||
| allowed_number = /\A-{0,1}\d{0,}(\.\d{0,})?\z/ | ||
|
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. Nice use of regex |
||
| if number !~ allowed_number || number == "" | ||
| $errors_hash["Number #{label}"] = 1 | ||
| else number = number.to_f | ||
| end | ||
| return number | ||
| end | ||
|
|
||
| def get_math | ||
| reset_errors_hash | ||
|
|
||
| $num1 = check_number(1) | ||
|
|
||
| print "Operation\t" | ||
| $operation = gets.chomp | ||
| unless $known_operations.include?($operation) | ||
| $errors_hash["THE OPERATION"] = 1 | ||
| end | ||
|
|
||
| $num2 = check_number(2) | ||
| end | ||
|
|
||
| def check_math | ||
| while $errors_hash.has_value?(1) | ||
| puts "\n", "Oops, try again. I can't accept your input for: #{$errors_hash.keys.join("; ")}.", "\n" | ||
| get_math | ||
| end | ||
| end | ||
|
|
||
| def do_math(a,b) | ||
| case | ||
| when $known_adds.include?($operation) | ||
| add(a,b) | ||
| when $known_mins.include?($operation) | ||
| subtract(a,b) | ||
| when $known_mults.include?($operation) | ||
| multiply(a,b) | ||
| when $known_divs.include?($operation) | ||
| divide(a,b) | ||
| when $known_mods.include?($operation) | ||
| remainder(a,b) | ||
| when $known_powers.include?($operation) | ||
| exponify(a,b) | ||
| else | ||
| quit("case $operation") | ||
| end | ||
| return $result | ||
| end | ||
|
|
||
| def print_math(a,b,c,d) | ||
| if ( c == "÷" || c == "%" ) && b == 0.0 | ||
| puts "\t\tCan't divide by zero! [nil]" | ||
| else | ||
| puts "Expression\t#{a} #{c} #{b} = #{$result.round(d)}\t\t(Rounded to up to #{d} decimal places.)" | ||
| end | ||
| end | ||
|
|
||
| def quit(why) | ||
| puts "Quitting. Something unexpected happened in: #{why}." | ||
| exit | ||
| end | ||
|
|
||
| # here's the program running | ||
|
|
||
| header | ||
| get_math | ||
| check_math | ||
| do_math($num1,$num2) | ||
| print_math($num1,$num2,$operator_symbol,5) | ||
| footer | ||
|
|
||
| # To do: | ||
| # put arrays in an array of hashes | ||
| # do I really need all of the dollar signs? | ||
| # parentheticals? refactor without asking 3 times. input expression. remove whitespace. split string by a known operator. string 1 = num1, operator = operator, string 2 = num2. | ||
| # square root | ||
| # repeat unless type quit to quit | ||
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.
We'll get into this as a class later, but never ever ever use global variables. It's a bad practice and can make code that's very hard to debug.
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.
Haha... well learning about scope and constants today was definitely a game changer for me!