-
Notifications
You must be signed in to change notification settings - Fork 44
Jackie Onofre - Calculator - Amper #39
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| # Intro for user | ||
| puts "\nI'm a calculator.\n\nI can add, subtract, multiply and divide." | ||
|
|
||
| # Method to determine whether input is numeric | ||
| def numeric(num) | ||
| if num =~ /[-+]?([0-9]*\.[0-9]+|[0-9]+)/ | ||
| return true | ||
| else | ||
| return false | ||
| end | ||
| end | ||
|
|
||
| # User prompted to enter first number | ||
| print "\nGive me a number: " | ||
| num1 = gets.chomp | ||
|
|
||
| # User gets message to enter number again if it is not numerical | ||
| until numeric(num1) | ||
|
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. Good way to trap the user until they enter a valid number. |
||
| print "\nThat's not a number! Try again: " | ||
| num1 = gets.chomp | ||
| end | ||
|
|
||
| # numbers are converted to either integers or floats | ||
| if num1 =~ /\A\d+\z/ | ||
| num1 = num1.to_i | ||
| else | ||
| num1 = num1.to_f | ||
| end | ||
|
|
||
| # List of acceptable operations | ||
| operations = ["add", "+", "subtract", "-", "multiply", "*", "divide", "/"] | ||
|
|
||
| # Prompt user to enter operation | ||
| print "Enter an operation: " | ||
| operation = gets.chomp.downcase | ||
|
|
||
| # User prompted to enter valid operation if they enter something not acceptable. | ||
| until operations.include? operation | ||
| print "\nTry again! Enter valid operation (+,-,*,/): " | ||
| operation = gets.chomp | ||
| end | ||
|
|
||
| # Enter Number 2 | ||
| print "Give me another number: " | ||
| num2 = gets.chomp | ||
|
|
||
| until numeric(num2) | ||
| print "\nThat's not a number! Try again: " | ||
| num2 = gets.chomp | ||
| end | ||
|
|
||
| if num2 =~ /\A\d+\z/ | ||
| num2 = num2.to_i | ||
| else | ||
| num2 = num2.to_f | ||
| end | ||
|
|
||
| if operation == "divide" || operation == "/" | ||
| until num2 != 0.0 | ||
|
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. good catch |
||
| print "Error: Division by zero is undefined. Try another number: " | ||
| num2 = gets.chomp | ||
| until numeric(num2) | ||
|
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 looks like a similar operation to what you did before... hmm... this would make a good method (prompting the user for a number and reading it in). |
||
| print "That's not a number! Try again: " | ||
| num2 = gets.chomp | ||
| end | ||
| if num2 =~ /\A\d+\z/ | ||
| num2 = num2.to_i | ||
| else | ||
| num2 = num2.to_f | ||
| end | ||
| end | ||
| end | ||
|
|
||
| # create methods for each operation | ||
| def add(num_one, num_two) | ||
| return num_one + num_two | ||
| end | ||
|
|
||
| def subtract(num_one, num_two) | ||
| return num_one - num_two | ||
| end | ||
|
|
||
| def multiply(num_one, num_two) | ||
| return num_one * num_two | ||
| end | ||
|
|
||
| def divide(num_one, num_two) | ||
| return num_one / num_two | ||
| end | ||
|
|
||
| # the result value will be printed based on operation user input and two numbers input | ||
| if operation == "add" || operation == "+" | ||
| puts "\n#{num1} + #{num2} = #{add(num1, num2)}" | ||
| elsif operation == "subtract" || operation == "-" | ||
| puts "\n#{num1} - #{num2} = #{subtract(num1, num2)}" | ||
| elsif operation == "multiply" || operation == "*" | ||
| puts "\n#{num1} * #{num2} = #{multiply(num1, num2)}" | ||
| elsif operation == "divide" || operation == "/" | ||
| puts "\n#{num1} / #{num2} = #{divide(num1, num2)}" | ||
| else | ||
| puts "error" | ||
| end | ||
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.
nice regular expression! Good way to verify something's a number.