-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculator.rb
46 lines (43 loc) · 1.11 KB
/
calculator.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
def calculate(first, second, operator)
first_number = first.to_f
second_number = second.to_f
# case operator
# when "+"
# puts first_number + second_number
# when "-"
# puts first_number - second_number
# when "*"
# puts first_number * second_number
# when "/"
# puts first_number / second_number
# when "%"
# puts first_number % second_number
# else
# puts "You gave me #{operator} -- I have no idea what to do with that."
# end
if operator == "+"
first_number + second_number
elsif operator == "-"
first_number - second_number
elsif operator == "*"
first_number * second_number
elsif operator == "/"
first_number / second_number
elsif operator == "%"
first_number % second_number
else
"You gave me '#{operator}' - I have no idea what to do with that."
end
end
puts "Simple calculator"
25.times { print "-" }
puts
puts "Enter 1st number:"
num_1 = gets.chomp
puts "Enter 2nd number:"
num_2 = gets.chomp
puts "Select the operator: +, -, /, *, %"
operator = gets.chomp
25.times { print "-" }
puts
puts "The result is: #{calculate(num_1, num_2, operator)}"