-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.rb
69 lines (50 loc) · 1.2 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# encoding: utf-8
class Integer
def factorial
f = 1
for i in 1..self
f *= i
end
f
end
end
def get_int_values(n)
n.times.map.with_index { |n|
print "Ingresa #{1 + n}° valor: "
gets.chomp.to_i
}
end
def factorial(n)
n.factorial
end
while true do
puts '¿Qué quieres hacer: [sumar], [restar], [multiplicar], [factorial] o [salir]'
operation = gets.chomp
operation.downcase!
case operation
when 'sumar'
operator = :+
when 'restar'
operator = :-
when 'multiplicar'
operator = :*
when 'factorial'
operator = :!
when 'salir'
break
else
puts 'Ingrese una operación correcta'
end
if %w(sumar restar multiplicar).include? operation
puts '¿Cuántos valores?'
num_of_values = gets.to_i
puts "¿Qué números quiere #{operation}?"
answer = get_int_values(num_of_values).inject(operator)
puts "La respuesta es ... #{answer}"
else
puts "Ingrese el número"
n = gets.chomp.to_i
answer = factorial(n)
puts "La respuesta es ... #{answer}"
end
end