-
Notifications
You must be signed in to change notification settings - Fork 0
/
calc.rb
79 lines (72 loc) · 1.7 KB
/
calc.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
70
71
72
73
74
75
76
77
78
def print_action(command)
puts "Hit '+' to add"
puts "Hit '-' to subtract"
puts "Hit '*' to multiply"
puts "Hit '/' to divide"
puts "Hit '2' to square"
puts "Type STOP to exit the program"
end
def print_second_number(string)
puts "Enter the second number:"
end
puts "Welcome to the calculator"
puts "Enter the first number:"
num1 = gets.chomp
# THIS WAS MY ORIGINAL SOLUTION -EH
# loop do
# print_action('command')
# function = gets.chomp
# if function == '+'
# print_second_number('string')
# num2 = gets.chomp
# result = num1.to_i + num2.to_i
# elsif function == '-'
# print_second_number('string')
# num2 = gets.chomp
# result = num1.to_i - num2.to_i
# elsif function == '*'
# print_second_number('string')
# num2 = gets.chomp
# result = num1.to_i * num2.to_i
# elsif function == '/'
# print_second_number('string')
# num2 = gets.chomp
# result = num1.to_f / num2.to_f
# elsif function == '2'
# result = num1.to_i * num1.to_i
# elsif function == 'STOP'
# break
# else
# puts "Sorry I didn't understand that."
# result = num1
# end
# puts "====> " + result.to_s
# num1 = result
# end
loop do
print_action('command')
function = gets.chomp
if %w(+ - * /).include?(function)
print_second_number('string')
num2 = gets.chomp
end
case function
when '+'
result = num1.to_i + num2.to_i
when '-'
result = num1.to_i - num2.to_i
when '*'
result = num1.to_i * num2.to_i
when '/'
result = num1.to_f / num2.to_f
when '2'
result = num1.to_i * num1.to_i
when 'STOP'
break
else
puts "Sorry I didn't understand that."
result = num1
end
puts "====> " + result.to_s
num1 = result
end