-
Notifications
You must be signed in to change notification settings - Fork 51
Create calculator.rb #53
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,81 @@ | ||
| puts "Hello user! Which operator would you like to use? | ||
| 1. add or + | ||
| 2. subtract or - | ||
| 3. multiply or * | ||
| 4. divide or / | ||
| 5. exponents or ** | ||
| 6. modulo or % | ||
| Please choose an operator (name or symbol)" | ||
| operator = gets.chomp.downcase.to_s | ||
|
|
||
| until operator == "add" || operator == "+" || operator == "subtract" || operator == "-" || operator == "multiply" || operator == "*" || operator == "divide" || operator == "/" || operator == "exponents" || operator == "**" || operator == "modulo" || operator == "%" | ||
| puts "That's not a valid operator! Try again." | ||
| operator = gets.chomp.downcase.to_s | ||
| end | ||
|
|
||
| puts "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 loop here to DRY up your code. Also, your validation for the first number is commented out, and as such that program doesn't appropriately deal with invalid user input for the first number. |
||
| num_one = gets.chomp.to_s | ||
| # case num_one | ||
| # when /\D/, "" | ||
| # puts "Invalid input. Please put an integer or a float:" | ||
| # num_one = gets.chomp | ||
| # end | ||
|
|
||
| puts "Enter the second number: " | ||
| num_two = gets.chomp.to_s | ||
| case num_two | ||
| when /\D/, "" | ||
| puts "Invalid input. Please put an integer or a float:" | ||
| num_two = gets.chomp | ||
| end | ||
|
|
||
| num_one = num_one.to_i | ||
|
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. You should convert your numbers to floats rather than ints so that the divide operator gives you the correct results. For example 5/6 should not equal 0. |
||
| num_two = num_two.to_i | ||
|
|
||
| if operator == "divide" || operator == "/" && num_one == 0 || num_two == 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. It is ok for num_one to equal 0. 0/5 = 0, where as 5/0 is invalid. |
||
| puts "Invalid. You can't divide by 0!" | ||
| until num_two != 0 | ||
| puts "Put another second number:" | ||
| num_two = gets.chomp.to_i | ||
| end | ||
| end | ||
|
|
||
| def addition(num_one, num_two) | ||
|
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 use of methods to encapsulate functionality of each operation. Consider returning the answer in each of these methods in case you want to use it elsewhere. |
||
| answer = num_one + num_two | ||
| puts "Great! #{num_one} + #{num_two} = #{answer}" | ||
| end | ||
| def subtraction(num_one, num_two) | ||
| answer = num_one - num_two | ||
| puts "Great! #{num_one} - #{num_two} = #{answer}" | ||
| end | ||
| def multiplication(num_one, num_two) | ||
| answer = num_one * num_two | ||
| puts "Great! #{num_one} * #{num_two} = #{answer}" | ||
| end | ||
| def division(num_one, num_two) | ||
| answer = num_one / num_two | ||
| puts "Great! #{num_one} / #{num_two} = #{answer}" | ||
| end | ||
| def exponent(num_one, num_two) | ||
| answer = num_one ** num_two | ||
| puts "Great! #{num_one} ** #{num_two} = #{answer}" | ||
| end | ||
| def modulos(num_one, num_two) | ||
| answer = num_one % num_two | ||
| puts "Great! #{num_one} % #{num_two} = #{answer}" | ||
| end | ||
|
|
||
| case 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. Nice use of case/when to simplify your code. Minor note: you should tab in each line of code under |
||
| when "add", "+" | ||
| addition(num_one, num_two) | ||
| when "subtract", "-" | ||
| subtraction(num_one, num_two) | ||
| when "multiply", "*" | ||
| multiplication(num_one, num_two) | ||
| when "divide", "/" | ||
| division(num_one, num_two) | ||
| when "exponents", "**" | ||
| exponent(num_one, num_two) | ||
| when "modulo", "%" | ||
| modulos(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.
consider using the method inlclude? here to simplify your code, for example: