-
Notifications
You must be signed in to change notification settings - Fork 0
/
calc2.rb
58 lines (46 loc) · 1.61 KB
/
calc2.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
class Calculator
def calc
#into
puts "Weclome to Calculator"
#display options, initialize variables
puts "Valid commands\n+ Add\n- Subtract\n* Multiply\n/ Divide\n** Exponential\nsqrt Square Root\nc Clear Calculator\nq Quit\nEnter numbers and commands with spaces"
input = []
current_value = 0
value1 = nil
# calculate
loop do
print ">> "
input = gets.chomp.split(" ") #get user input in a string and break into array
return if input[0] == 'q' # allow user to quit out
if input[0] == 'c' #clear the calculator
value1 = nil
current_value = 0
puts "Cleared! Current value is #{current_value}\n "
redo
end
if value1 == nil #inital input from user
value1 = input[0].to_f
value2 = input[2].to_f
operator = input[1]
else
value1 = current_value #after inital value is in calc
value2 = input[1].to_f
operator = input[0]
end
case operator #math!
when "+" then current_value = value1 + value2
when "-" then current_value = value1 - value2
when "*" then current_value = value1 * value2
when "/" then current_value = value1 / value2
when "**" then current_value = value1 ** value2
when "sqrt" then current_value = value1 **(0.5)
when nil then current_value = value1
else
puts "Invalid input!" #failsafe
end
puts "Current value is #{current_value}\n " #display current value
end
end
end
calculator = Calculator.new
calculator.calc