Skip to content
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
56 changes: 56 additions & 0 deletions calculator_final.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#Add,Subtract,Multiply,Divide Methode
def operation(num_one, math_operation, num_two)
if math_operation == "Add" || math_operation == "+"

Choose a reason for hiding this comment

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

I suggest capitalizing math_operation, before starting these if-elsif comparisons.

result = ((num_one.to_f) + (num_two.to_f)).round(2)
elsif math_operation == "Subtract" || math_operation == "-"
result = ((num_one.to_f) - (num_two.to_f)).round(2)
elsif math_operation == "Multiply" || math_operation == "*"
result = ((num_one.to_f) * (num_two.to_f)).round(2)
elsif math_operation == "Divide" || math_operation == "/"
result = ((num_one.to_f) / (num_two.to_f)).round(2)
end

Choose a reason for hiding this comment

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

indentation

Suggested change
end
end

return result
end

#Validate if input is an math operator
def ValidOperator (math_operation)
until ["Add","+", "Subtract","-", "Multiply", "*", "Divide", "/"].include? math_operation
puts "Enter a valid selection"
math_operation = gets.chomp
end
return math_operation
end
Comment on lines +16 to +22

Choose a reason for hiding this comment

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

indentation issues here.


#Validate if the input is Number
def ValidNumber (number)
until (Integer(number) rescue nil) !=nil || (Float(number) rescue nil) !=nil #Returns nil if it is not integer or Float else returns number
puts "Enter a valid number"
number = gets.chomp.to_i
end
Comment on lines +27 to +29

Choose a reason for hiding this comment

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

More messy indentation.

return number.to_i
end

#Program Start
puts "Welcome to the Calcuator Program."

operator_array = ["Add(+)", "Subtract(-)", "Multiply(*)", "Divide(/)"]
puts operator_array

#Accept operator input
puts"Please enter the operator would you like to use?"
math_operation = gets.chomp
math_operation = ValidOperator(math_operation)

#Accept first number
puts "Enter your first number."
num_one = gets.chomp.to_i
num_one = ValidNumber(num_one)

#Accept second number
puts "Enter your second number."
num_two = gets.chomp.to_i
num_two = ValidNumber(num_two)


final_result = operation(num_one, math_operation, num_two)
puts "Result is #{final_result}"