-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.rb
121 lines (100 loc) · 2.16 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# Convert multiple user inputs to one mathematical operator
def convert(f)
if (f == "addition" || f == "+")
"+"
elsif (f == "subtraction" || f == "-")
"-"
elsif (f == "division" || f == "/")
"/"
elsif (f == "multiplication" || f == "*")
"*"
elsif (f == "exponentiate" || f == "**")
"**"
elsif (f == "square root" || f == "sqrt" || f == "√")
"√"
else
false
end
end
#Using the converted operator, perform intended operation
def calculate(f, a, b)
if f == "+"
add(a, b)
elsif f == "-"
subtract(a, b)
elsif f == "/"
divide(a, b)
elsif f == "*"
multiply(a, b)
elsif f == "**"
exponentiate(a, b)
elsif f == "√"
sqrt
else
nil
end
end
# Define what the operations do
def add(a, b)
a + b
end
def subtract(a, b)
a - b
end
def division(a, b)
a / b
end
def multiplication(a, b)
a*b
end
def exponentiate(a, b)
a**b
end
def sqrt(a)
Math.sqrt(a)
end
# Convert "nil" to "0"
def number(a, b)
if a == nil
"0"
elsif b == nil
"0"
else
puts ""
end
end
# Greet user and give instructions
def intro()
puts "Hello there!"
puts "What kind of maths would you like to do?"
end
# Create a loop that will ask a question until correct input is received
def ask_again(n)
while convert(f) == false
"Please enter an operation"
end
end
def get_operations()
puts "Your options are addition, subtraction, multiplication, exponentiation, "
puts "and taking the square root of a single number."
operation = gets.chomp.downcase
end
ask_again(intro)
intro
get_operations()
operation = gets.chomp.downcase
# Tell ruby to convert the user input and then store it in the expression variable
operator = convert(input)
# Put that user input into a string
puts "Okay, so you want to do #{operation}, that's great!"
# Ask user for first number
puts "What's your first number?"
num1 = gets.chomp.to_i
# Ask user for second number
puts "What is your second number?"
num2 = gets.chomp.to_i
# Take the expression
answer = calculate(operator, num1, num2)
answer = "The answer is"
#print out formula, not result
puts "Ok, #{num1} #{operator} #{num2} is #{answer}."