forked from Ada-C9/Calculator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculator.rb
102 lines (86 loc) · 2.34 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
92
93
94
95
96
97
98
99
100
101
102
# Intro for user
puts "\nI'm a calculator.\n\nI can add, subtract, multiply and divide."
# Method to determine whether input is numeric
def numeric(num)
if num =~ /[-+]?([0-9]*\.[0-9]+|[0-9]+)/
return true
else
return false
end
end
# User prompted to enter first number
print "\nGive me a number: "
num1 = gets.chomp
# User gets message to enter number again if it is not numerical
until numeric(num1)
print "\nThat's not a number! Try again: "
num1 = gets.chomp
end
# numbers are converted to either integers or floats
if num1 =~ /\A\d+\z/
num1 = num1.to_i
else
num1 = num1.to_f
end
# List of acceptable operations
operations = ["add", "+", "subtract", "-", "multiply", "*", "divide", "/"]
# Prompt user to enter operation
print "Enter an operation: "
operation = gets.chomp.downcase
# User prompted to enter valid operation if they enter something not acceptable.
until operations.include? operation
print "\nTry again! Enter valid operation (+,-,*,/): "
operation = gets.chomp
end
# Enter Number 2
print "Give me another number: "
num2 = gets.chomp
until numeric(num2)
print "\nThat's not a number! Try again: "
num2 = gets.chomp
end
if num2 =~ /\A\d+\z/
num2 = num2.to_i
else
num2 = num2.to_f
end
if operation == "divide" || operation == "/"
until num2 != 0.0
print "Error: Division by zero is undefined. Try another number: "
num2 = gets.chomp
until numeric(num2)
print "That's not a number! Try again: "
num2 = gets.chomp
end
if num2 =~ /\A\d+\z/
num2 = num2.to_i
else
num2 = num2.to_f
end
end
end
# create methods for each operation
def add(num_one, num_two)
return num_one + num_two
end
def subtract(num_one, num_two)
return num_one - num_two
end
def multiply(num_one, num_two)
return num_one * num_two
end
def divide(num_one, num_two)
return num_one / num_two
end
# the result value will be printed based on operation user input and two numbers input
if operation == "add" || operation == "+"
puts "\n#{num1} + #{num2} = #{add(num1, num2)}"
elsif operation == "subtract" || operation == "-"
puts "\n#{num1} - #{num2} = #{subtract(num1, num2)}"
elsif operation == "multiply" || operation == "*"
puts "\n#{num1} * #{num2} = #{multiply(num1, num2)}"
elsif operation == "divide" || operation == "/"
puts "\n#{num1} / #{num2} = #{divide(num1, num2)}"
else
puts "error"
end