- 
                Notifications
    
You must be signed in to change notification settings  - Fork 42
 
Pipes - <Lindsey> - <Calculator> #27
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,63 @@ | ||
| #combine all if blocks into one method | ||
| def process_operator(operation, numbers) | ||
| if operation == "add" || operation == "+" | ||
| return do_math("+", numbers[0], numbers[1]) | ||
| elsif operation == "subtract" || operation == "-" | ||
| return do_math("-", numbers[0], numbers[1]) | ||
| elsif operation == "multiply" || operation == "*" | ||
| return do_math("*", numbers[0], numbers[1]) | ||
| elsif operation == "divide" || operation == "/" | ||
| return do_math("/", numbers[0], numbers[1]) | ||
| elsif operation == "modulo" || operation == "%" | ||
| return do_math("%", numbers[0], numbers[1]) | ||
| end | ||
| end | ||
| 
     | 
||
| 
     | 
||
| def get_number(operation) | ||
| begin | ||
| puts "What's your 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. I like making getting a number from the user its own method - good organization.  | 
||
| num1 = Integer(gets.chomp) | ||
| 
     | 
||
| puts "What's your second number?" | ||
| num2 = Integer(gets.chomp) | ||
| 
     | 
||
| if operation == '/' && num2 == 0 | ||
| puts "You can't divide by zero! Try again." | ||
| puts | ||
| get_number(operation) | ||
| else | ||
| return num1, num2 | ||
| end | ||
| 
     | 
||
| rescue | ||
| puts "Try again, you clown. Enter a valid number." | ||
| puts | ||
| get_number(operation) | ||
| end | ||
| end | ||
| 
     | 
||
| #combine these into one method | ||
| def do_math(operation,num1, num2) | ||
| if operation == "+" | ||
| puts "#{num1} + #{num2} = #{num1+num2}" | ||
| elsif operation == "-" | ||
| 
         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 big   | 
||
| puts "#{num1} - #{num2} = #{num1-num2}" | ||
| elsif operation == '*' | ||
| puts "#{num1} * #{num2} = #{num1*num2}" | ||
| elsif operation == '/' | ||
| puts "#{num1} / #{num2} = #{num1.to_f/num2.to_f}" | ||
| else | ||
| puts "#{num1} % #{num2} = #{num1%num2}" | ||
| end | ||
| end | ||
| 
     | 
||
| 
     | 
||
| puts "Welcome to the command line calculator!" | ||
| puts "Would you like to add, subtract, multiply, divide, or find a modulo?" | ||
| 
     | 
||
| operation = gets.chomp | ||
| numbers = get_number(operation) | ||
| process_operator(operation, numbers) | ||
| 
     | 
||
| puts "Thanks for using the command line calculator!" | ||
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.
If the user inputs a bogus operator, your program quits silently. Instead, it would be polite to let them know what went wrong.