Skip to content
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

Create calculator.rb #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# prints title and operation options
puts "Are you ready to Calculate!!!"
puts "Operations:\nadd(+)\nsubtract(-)\nmultiply(*)\ndivide(/)\nexponent(^)\nmodulo(%)\n"
# collects operation type from user
print "Choose an operation: "
operation = gets.chop.downcase
# list of acceptible operations
operators = ["+", "add", "-", "subtract", "*", "multiply", "/", "divide", "exponent", "^", "modulo", "%", "()"]

# confirms if user entered a valid operation
until operators.include?(operation)
puts "Please enter a valid operation"
operation = gets.chop.downcase
end

# collects two numbers for calculation

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think this method block could be turned into a reusable method?

puts "What are the two numbers you want to use?"
print "Enter a number: "
value_1 = gets.chomp
# confirms user has entered a valid number
until value_1.to_s == value_1.to_i.to_s || value_1.to_f.to_s == value_1.to_s
print "Please enter a valid number: "
value_1 = gets.chomp
end

print "Enter a second number: "
value_2 = gets.chomp
# confirms user has entered a valid number
until value_2.to_s == value_2.to_i.to_s || value_2.to_f.to_s == value_2.to_s
print "Please enter a valid number: "
value_2 = gets.chomp
end

# methods used
def add(num1, num2)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is more of a style thing. It's a little odd putting the method definitions here in the middle of the program flow. I suggest moving method definitions to the top of the file.

num1 + num2
end

def subtract(num1, num2)
num1 - num2
end

def multiplying(num1, num2)
num1 * num2
end

def dividing(num1, num2)
num1 / num2
end

def exponential(num1, num2)
num1 ** num2
end

def mod(num1, num2)
num1 % num2
end

# checks for floats
if value_1.split('').include?('.') || value_2.split('').include?('.')
value_1 = value_1.to_f
value_2 = value_2.to_f
else
value_1 = value_1.to_i
value_2 = value_2.to_i
end

# calculate and print the result for user
if operation == "+" || operation == "add"
solution = add(value_1, value_2)
puts "#{value_1} + #{value_2} = #{solution}"

elsif operation == "-" || operation == "subtract"
solution = subtract(value_1, value_2)
puts "#{value_1} - #{value_2} = #{solution}"

elsif operation == "*" || operation == "multiply"
solution = multiplying(value_1, value_2)
puts "#{value_1} * #{value_2} = #{solution}"

elsif operation == "/" || operation == "solution"
if value_1 == 0 || value_2 == 0

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dividing something by zero is not zero. It's undefined.

solution = 0
else
solution = dividing(value_1, value_2)
end
puts "#{value_1} / #{value_2} = #{solution}"

elsif operation == "^" || operation == "exponent"
solution = exponential(value_1, value_2)
puts "#{value_1}^#{value_2} = #{solution}"

else operation == "%" || operation == "modulo"
solution = mod(value_1, value_2)
puts "#{value_1} % #{value_2} = #{solution}"
end