-
Notifications
You must be signed in to change notification settings - Fork 49
Time - Olga Patrakova #30
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,66 @@ | ||
| puts "Welcome to the Calculator program! Which operator would you like to use?" | ||
| puts "1. add (+) | ||
| 2. subtract (-) | ||
| 3. multiply (*) | ||
| 4. divide (/) | ||
| 5. exponify (^) | ||
| 6. find a remainder from a division (%)" | ||
|
|
||
| # Check if a valid operator is chosen | ||
| command = gets.chomp.downcase | ||
| until ["add", "+", "subtract", "-", "multiply", "*", "divide", "/", "remainder", "find a remainder", "%", "exponify", "^"].include?(command) | ||
| puts "Please tell me to add (+), subtract (-), multiply (*), divide (/), exponentiate (^) or find a remainder (%)!" | ||
| command = gets.chomp.downcase | ||
| end | ||
|
|
||
| # Check if user input is not a letter | ||
| def input_check(number) | ||
| until number.match(/\A[-]?\d+(\.\d+)?\z/) | ||
| puts "Please enter a valid number " | ||
| number = gets.chomp | ||
| end | ||
| return number | ||
| end | ||
|
|
||
| puts "Please enter the first number " | ||
|
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. Consider using a method or loop to get user input to DRY up your code.
Author
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. Thank you for the code review and great comments. I'll do my best to dry up my code next time. |
||
| first_number = gets.chomp | ||
| checked_first = input_check(first_number) | ||
|
|
||
| puts "Please enter the second number " | ||
| second_number = gets.chomp | ||
| checked_second = input_check(second_number) | ||
|
|
||
| # Convert a string to a float or an integer depending on the presence of "." | ||
| def int_or_float(num) | ||
| return num.include?(".") ? num.to_f : num.to_i | ||
| end | ||
|
|
||
| checked_first = int_or_float(checked_first) | ||
| checked_second = int_or_float(checked_second) | ||
|
|
||
| puts "Here is the result of the calculation." | ||
|
|
||
| # Output the result depending on the chosen operation | ||
| case command | ||
| when "add", "+" | ||
| puts "#{checked_first} + #{checked_second} = #{checked_first + checked_second}" | ||
| when "subtract", "-" | ||
| puts "#{checked_first} - #{checked_second} = #{checked_first - checked_second}" | ||
| when "multiply", "*" | ||
| puts "#{checked_first} * #{checked_second} = #{checked_first * checked_second}" | ||
| when "divide", "/" | ||
| # Handle the division by zero case | ||
| if checked_second != 0 && (checked_first % checked_second > 0) | ||
| puts "#{checked_first} / #{checked_second} = #{checked_first.to_f / checked_second}" | ||
| elsif checked_second == 0 | ||
| puts "Error! Division by zero" | ||
| end | ||
| when "exponify", "^" | ||
| # Show the process of exponentiation | ||
| print "#{checked_first} * " * (checked_second - 1) | ||
| puts "#{checked_first} = #{checked_first ** checked_second}" | ||
| when "remainder", "find a remainder", "%" | ||
| puts "#{checked_first} % #{checked_second} = #{checked_first % checked_second}" | ||
| else | ||
| puts "What do you want from me?!" | ||
| 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 use of regex :)
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.
Thank you, Becca!