forked from Ada-C12/calculator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.rb
91 lines (79 loc) · 3.21 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# Calculator.rb assignment
# array of arrays
operators = [
["add", "+"],
["subtract", "-"],
["multiply", "*"],
["divide", "/"]
]
# added this operator_check long array to ensure user's input is only valid if on this list
operators_check =
["add", "+",
"subtract", "-",
"multiply", "*",
"divide", "/"]
# only accept digits as input
def is_number(string)
if string.length == 0
return false
end
return string.scan(/\D/).empty?
end
# welcome user to program, provide list of oeprators, looping through list options
puts "Welcome to the Calculator program! I hear you need help..."
puts "Which operator would you like to use from the below list?"
4.times do |i|
puts "#{i+1}: #{operators[i][0]} (#{operators[i][1]})"
end
# get user's operator pick and reinstate. Throw error if selection is not on offered list
puts "Enter your chosen operator now (name or symbol)."
operator_pick = gets.chomp
# use method.include to throw error if user input is not on the operator list and prompt user to try again
until operators_check.include?(operator_pick)
puts "ERROR: invalid entry. Please select operator from list."
puts "Enter your chosen operator now (name or symbol)."
operator_pick = gets.chomp
puts "You've selected the #{operator_pick} operator."
end
# get user's first number and reinstate. Error if less than 0
# initially used .to_i on gets.chomp but this converted string to 0
# instead, check if num_one_pick is a number, before calling to_i. If not a number, throw error
# include .to_f to handle float entries as well (but now all outputs are floats? hm)
puts "Next: select a number for this math operation."
num_one_pick = gets.chomp
while is_number(num_one_pick) == false
puts "ERROR: this entry is not valid."
puts "Next: select a number for this math operation."
num_one_pick = gets.chomp
end
num_one_pick = num_one_pick.to_i.to_f
puts "You've selected #{num_one_pick} as your first number."
puts "#{num_one_pick} is a great number."
# get user's second number and reinstate. Error if less than 0
# ensure user input is numerical and reject if non-numerical
# if operator chosen is division, (to prevent division by 0) throw error, prompting the user to enter another number
puts "Finally: select your second number for the Calculator."
num_two_pick = gets.chomp
while is_number(num_two_pick) == false || (num_two_pick == "0" && (operator_pick == "divide" || operator_pick == "/"))
puts "ERROR: this entry is not valid."
puts "Please enter a different number for the Calculator."
num_two_pick = gets.chomp
end
num_two_pick = num_two_pick.to_i.to_f
puts "You've selected #{num_two_pick} as your second number."
puts "#{num_two_pick} is also a great number."
# wrap up and reinstate equation, along with solution
puts "You have created the following problem: #{num_one_pick} #{operator_pick} #{num_two_pick}"
puts "The solution to your problem is: "
# use case command to complete equation
case operator_pick
when "add", "+"
puts (num_one_pick + num_two_pick)
when "subtract", "-"
puts (num_one_pick - num_two_pick)
when "multiply", "*"
puts (num_one_pick * num_two_pick)
when "divide", "/"
puts (num_one_pick / num_two_pick)
end
puts "Thank you for using The Calculator. Come back next time you need help!"