-
Notifications
You must be signed in to change notification settings - Fork 44
Ari Herman - Calculator - Octos #24
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,99 @@ | ||
| # This is a calculator program | ||
| # the user should ask what they would like to calculate. | ||
|
|
||
| operations = ["multiply", "divide", "subtract", "exponify", | ||
| "modulate", "-", "*", "/", "**", "+", "%"] | ||
|
|
||
| puts "\nWelcome to your Calculator Program!" | ||
| puts "Enter two numbers and an operation." | ||
| puts "Would you like to continue? (yes/no) " | ||
| answer = gets.chomp.downcase | ||
|
|
||
| while answer == "yes" do | ||
| # first number | ||
| print "\n#1 Number: " | ||
| until (num_1 = gets.chomp) =~ /^\d*\.?\d+$/ | ||
| #starting of line, any digit(zero or more), including periods, following any digit(zero or more), ending of string. | ||
| puts "\nInvalid input. Please enter a number: " | ||
| end | ||
| num_1 = num_1.to_f | ||
| # second number | ||
| print "\n#2 Number: " | ||
| until (num_2 = gets.chomp) =~ /^\d*\.?\d+$/ | ||
| puts "\nInvalid input. Please enter a number: " | ||
| end | ||
| num_2 = num_2.to_f | ||
| # operation to perform | ||
| print "\nOperation: " | ||
| op = gets.chomp.downcase | ||
| until operations.include?("#{op}") | ||
| print "\nPlease enter an operation | ||
| (add, subtract, multiply, divide, exponify, or modulate): " | ||
| op = gets.chomp.downcase | ||
| end | ||
|
|
||
| # divide | ||
| def divide(num1, num2) | ||
| if num2 != 0 | ||
|
|
||
| print "#{num1} / #{num2} = " | ||
| (num1 / num2).round(2) | ||
| else | ||
|
|
||
| puts "Can't divide by zero!" | ||
| end | ||
| end | ||
|
|
||
| # multiply | ||
| def multiply(num1, num2) | ||
|
|
||
| print "#{num1} * #{num2} = " | ||
| (num1 * num2).round(2) | ||
|
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. Printing out the equation inside the method is one way to solve this optional, but it's not always ideal, because it means your method is doing two different things: doing some math problem, and printing an equation. This takes control away from whoever's calling your method. There's no way to do multiplication without also In general, you should try to make your methods do only one thing. |
||
| end | ||
|
|
||
| # subtract | ||
| def subtract(num1, num2) | ||
|
|
||
| print "#{num1} - #{num2} = " | ||
| (num1 - num2).round(2) | ||
| end | ||
|
|
||
| # addition | ||
| def add(num1, num2) | ||
|
|
||
| print "#{num1} + #{num2} = " | ||
| (num1 + num2).round(2) | ||
| end | ||
|
|
||
| # exponify | ||
| def exponify(num1, num2) | ||
|
|
||
| print "#{num1} ^ #{num2} = " | ||
| (num1**num2).round(2) | ||
| end | ||
|
|
||
| # modulo | ||
| def modulate(num1, num2) | ||
|
|
||
| print "#{num1} % #{num2} = " | ||
| (num1%num2).round(2) | ||
| end | ||
|
|
||
| # print the result and round numbers by two decimals | ||
| case op | ||
| when "add", "+" | ||
| print add(num_1, num_2) | ||
| when "subtract", "-" | ||
| print subtract(num_1, num_2) | ||
| when "multiply", "*" | ||
| print multiply(num_1, num_2) | ||
| when "divide", "/" | ||
| print divide(num_1, num_2) | ||
| when "exponify", "**" | ||
| print exponify(num_1, num_2) | ||
| when "modulate", "%" | ||
| print modulate(num_1, num_2) | ||
| end | ||
| print "\nWould you like to continue? (yes/no) " | ||
| answer = gets.chomp.downcase | ||
| 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
dividemethod is defined inside your while loop! While this is technically valid Ruby (and certainly works), it's a little odd to me as a reader. A better option might be to define all your methods at the very top of the file, and have your main control loop below that.