-
Notifications
You must be signed in to change notification settings - Fork 51
Leaves - Nicky #51
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?
Leaves - Nicky #51
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,101 @@ | ||
| # Methods for each arithmetic operator | ||
| def add(num_one, num_two) | ||
| puts "#{num_one} + #{num_two} = #{num_one + num_two}" | ||
| end | ||
|
|
||
| def subtract(num_one, num_two) | ||
| puts "#{num_one} - #{num_two} = #{num_one - num_two}" | ||
| end | ||
|
|
||
| def multiply(num_one, num_two) | ||
| puts "#{num_one} * #{num_two} = #{num_one * num_two}" | ||
| end | ||
|
|
||
| def divide(num_one, num_two) | ||
| puts "#{num_one} / #{num_two} = #{num_one / num_two}" | ||
| end | ||
|
|
||
| def modulo(num_one, num_two) | ||
| puts "#{num_one} % #{num_two} = #{num = num_one / num_two | ||
| total = num_two * num | ||
| remainder = num_one - total}" | ||
| end | ||
|
|
||
| def exponent(base_num, exp_num) | ||
| result = 1 | ||
| exp_num.times do | ||
| result *= base_num | ||
| end | ||
| puts "#{base_num}^#{exp_num} = #{result}" | ||
| end | ||
|
|
||
| # Welcome message to user | ||
| puts "Welcome to the Calculator program! Which operator would you like to use? | ||
| 1. add(+) | ||
| 2. subtract(-) | ||
| 3. multiply (*) | ||
| 4. divide (/) | ||
| 5. modulo (%) | ||
| 6. exponent (** or ^)" | ||
|
|
||
| puts "Please choose one operator(name or symbol):" | ||
|
|
||
| # Stores user's input as one of the operators or prints a message telling them to enter a valid operator | ||
| while users_operator = gets.chomp.upcase | ||
| case users_operator | ||
| when "ADD", "+" | ||
| break | ||
| when "SUBTRACT", "-" | ||
| break | ||
| when "MULTIPLY", "*" | ||
| break | ||
| when "DIVIDE", "/" | ||
| break | ||
| when "MODULO", "%" | ||
| break | ||
| when "EXPONENT", "**", "^" | ||
| break | ||
| else | ||
| puts "Invalid operator. Please enter a valid operator in order to continue: " | ||
| end | ||
| end | ||
|
|
||
| puts "Please enter a number:" | ||
|
|
||
| # Checks and validates user input. If input is not an integer, an exception is thrown and prompts the user to enter a valid number | ||
| begin | ||
| num_one = Integer(gets.chomp) # Question: Is there a way to accept both integers and floats from the user? Would this require conditional statements? | ||
|
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. Instead of Interger(gets.chomp) you could use Float(gets.chomp). Converting to Floats is a better option so that it performs division correctly (5/2 = 2.5 (as opposed to 2).) |
||
| rescue | ||
| puts "Invalid input. Please enter a valid number: " | ||
| retry | ||
| end | ||
|
|
||
| puts "Please enter another number:" | ||
|
|
||
| begin | ||
|
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 loop to DRY up your code. |
||
| num_two = Integer(gets.chomp) | ||
| rescue | ||
| puts "Invalid input. Please enter a valid number: " | ||
| retry | ||
| end | ||
|
|
||
| # Performs calculations based on user input | ||
|
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. The is a clever use of begin/rescue/retry. Nice research! |
||
| if users_operator == "ADD" || users_operator == "+" | ||
|
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 case/when block to simplify your code. |
||
| add(num_one, num_two) | ||
| elsif users_operator == "SUBTRACT" || users_operator == "-" | ||
| subtract(num_one, num_two) | ||
| elsif users_operator == "MULTIPLY" || users_operator == "*" | ||
| multiply(num_one, num_two) | ||
| elsif users_operator == "DIVIDE" || users_operator == "/" | ||
| if num_two == 0 # If user enters 0 as the second number, an error message is printed and reprompts the user for a greater input | ||
| puts "You cannot divide by 0! Please enter a number greater than 0: " | ||
| num_two = gets.chomp.to_i | ||
| divide(num_one, num_two) | ||
| else | ||
| divide(num_one, num_two) | ||
| end | ||
| elsif users_operator == "MODULO" || users_operator == "%" | ||
| modulo(num_one, num_two) | ||
| elsif users_operator == "EXPONENT" || users_operator == "**" || users_operator == "^" | ||
| exponent(num_one, num_two) | ||
| 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.
This is clever -- however an
includes?method would simplify this operation: