forked from Ada-C10/calculator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator.rb
More file actions
74 lines (66 loc) · 1.92 KB
/
Copy pathcalculator.rb
File metadata and controls
74 lines (66 loc) · 1.92 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
# calculates answer after taking user input for operation to be
# checks to make sure that symbol is valid
def symbol_entry(symbol)
if symbol != "add" && symbol != "+" && symbol != "subtract" && symbol != "-" && symbol != "multiply" && symbol != "*" && symbol != "divide" && symbol != "/" && symbol != "^" && symbol != "%" && symbol != "exponent" && symbol != "module"
puts "Invalid symbol input!"
end
end
# does the math according to numerical operator
def symbol_check(symbol, value1, value2)
print "Answer: "
case symbol
when "add", "+"
print "#{value1} + #{value2} = "
ans = value1 + value2
when "subtract", "-"
print "#{value1} - #{value2} = "
ans = value1 - value2
when "multiply" , "*"
print "#{value1} * #{value2} = "
ans = value1 * value2
when "divide", "/"
if value2 != 0
print"#{value1} / #{value2} = "
ans = value1 / value2
else
puts "Infinity"
end
when "^", "exponent"
print"#{value1} ** #{value2} = "
ans = value1 ** value2
when "%" , "module"
print"#{value1} % #{value2} = "
ans = value1 % value2
else
puts "Unable to calculate!"
end
if ans % 1 > 0.0
puts ans.round(4)
else
puts ans.to_i
end
end
# prints the options for user to select function
puts "Welcome to the calculator program! Which operator would you like to use? "
puts "1. add(+)"
puts "2. subtract(-)"
puts "3. multiply(*)"
puts "4. divide(/)"
puts "5. exponent(^)"
puts "6. module(%)"
print "Please use one operator name or symbol: "
symbol = gets.chomp
# takes in the first value
print "Please enter first number: "
value1 = gets.chomp.to_f
if value1.to_i == 0 && value1 != "0"
puts "Invalid number!"
end
# takes in the second value
print "Please enter second number: "
value2 = gets.chomp.to_f
if value2.to_f == 0.0
puts "Invalid number!" && value2 != "0"
end
symbol_entry(symbol)
symbol_check(symbol, value1, value2)