forked from Ada-C9/Calculator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProject_1.rb
More file actions
109 lines (87 loc) · 2.42 KB
/
Project_1.rb
File metadata and controls
109 lines (87 loc) · 2.42 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# definition of methods
def add(num1,num2)
return num1 + num2
end
def subtract(num1,num2)
return num1 - num2
end
def multiply(num1,num2)
return num1 * num2
end
def divide(num1,num2)
return num1 / num2
end
def numberic?(string)
/[%\+\*\d]/.match(string)
end
def empty?(input)
if input.empty?
puts "You did not enter anything!"
end
end
def type?(input)
if /\A\d+\z/.match(input)
return "integer"
elsif /\A\d+\.\d+\z/.match(input)
return "float"
end
end
puts "Welcome to your calculator!\n"
puts "I can add, subtract, multiply or divide\n"
puts "Please enter the operator you want to use : add, + , subtract, - , multiply, * , divide , / "
user_input = gets.chomp.downcase
empty?(user_input)
# to make sure the user input is a valid operator
arr_operators = ["add","+", "subtract", "-", "multiply", "*" , "divide", "/"]
attempt = 0
while !arr_operators.include?(user_input)
if attempt < 3
attempt += 1
puts "Please enter a valid operator. You have #{(-attempt + 3) + 1} more attempts.\n"
puts "Operator options: add, subtract, multiply, divide, +, - , *, /"
user_input = gets.chomp
else
puts "Too many attempts..start the program again"
exit
end
end
print "Please enter value 1: "
num1 = gets.chomp
empty?(num1)
until numberic? num1 do
puts "Please enter a numeric expression for value 1: "
num1 = gets.chomp
end
print "\nPlease enter value 2: "
num2 = gets.chomp
empty?(num1)
until numberic? num2 do
puts "Please enter a numeric expression for value 2: "
end
# To handle when user attempts to divide by zero.
if user_input == "divide" || user_input == "/"
while num2 == 0
puts "Cannot divide by 0. Please enter again the divisor"
num2 = gets.chomp
end
end
case user_input
when "add", "+"
puts "\nAnswer: #{results = add(eval(num1), eval(num2))}"
puts "#{num1} + #{num2} = #{results}"
when "subtract", "-"
puts "\nAnswer: #{results = subtract(eval(num1),eval(num2))}"
puts "#{num1} - #{num2} = #{results}"
when "multiply", "*"
puts "\nAnswer: #{results = multiply(eval(num1),eval(num2))}"
puts "#{num1} * #{num2} = #{results}"
when "divide", "/"
puts "\nAnswer: #{results = divide(eval(num1),eval(num2))}"
puts "#{num1} / #{num2} = #{results}"
end
# this will allow the program to know if the result should be printed as a float or an integer
if type?(num1) == "float"
results.to_f
elsif type?(num1) == "integer" && type?(num2) == "integer"
results.to_i
end