-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFirstCalc.lua
More file actions
246 lines (207 loc) · 5.61 KB
/
FirstCalc.lua
File metadata and controls
246 lines (207 loc) · 5.61 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
--[[ INFO ]]--
--[[
Name: FirstCalc
Version: 0.2.1
Date: 10/08/2013
Author: Alex Crawford
Notes: My first attempt at programming in LUA (and at all really, besides simple HTML and CSS), through ComputerCraft. I used the calculator tutorial on the ComputerCraft wiki as a starting point, and elaborated on that, adding to and modifying it. ]]--
--[[ VARIABLES ]]--
local Memory = {}
local UserOpSel = nil
local Num1 = nil
local Num2 = nil
local Result = nil
local QueryMess = {
[1] = "What operation would you like to perform?",
[2] = "What is the first number?",
[3] = "What is the second number?",
[4] = "Would you like to store the result in the memory?",
[5] = "Which memory bank would you like to store the result in?",
[6] = "Would you like to overwrite the memory bank?",
[7] = "Would you like to do another equation?",
[8] = "Would you like to retrieve a result from memory?",
[9] = "Which memory bank would you like to load for the first number?",
[10] = "Which memory bank would you like to load for the second number?",
}
--[[ FUNCTIONS ]]--
function GetInput() -- -- Gets and returns user input. Useless.
local Input = read()
return Input
end
function DisplayMenu() -- Displays the main menu.
term.clear()
term.setCursorPos(1,2)
print[[
--------------------------------
FirstCalc v0.2.1
--------------------------------
1. Add
2. Subtract
3. Multiply
4. Divide
5. View Memory
6. Exit
--------------------------------
]]
end
function QueryUserMenu() -- Asks the user what operation they'd like to perform.
local OpMess = {
[1] = "You chose addition.",
[2] = "You chose subtraction.",
[3] = "You chose multiplication.",
[4] = "You chose division.",
[5] = "You chose to view the memory.",
[6] = "You chose exit.",
}
print(QueryMess[1] .. "\n")
local x = string.lower(read())
if x == "add" then
UserOpSel = string.gsub(x, "add", 1)
UserOpSel = tonumber(UserOpSel)
elseif x == "subtract" then
UserOpSel = string.gsub(x, "subtract", 2)
UserOpSel = tonumber(UserOpSel)
elseif x == "multiply" then
UserOpSel = string.gsub(x, "multiply", 3)
UserOpSel = tonumber(UserOpSel)
elseif x == "subtract" then
UserOpSel = string.gsub(x, "divide", 4)
UserOpSel = tonumber(UserOpSel)
elseif x == "exit" then
UserOpSel = string.gsub(x, "exit", 5)
UserOpSel = tonumber(UserOpSel)
elseif x == "1" or x == "2" or x == "3" or x == "4" or x == "5" or x == "6" then
UserOpSel = tonumber(x)
else
write("\nInvalid input. Enter 1 - 5.\n\n")
os.pullEvent(key)
CoreFunc()
end
if UserOpSel == 5 then
GetMem()
elseif UserOpSel == 6 then
os.reboot()
end
if UserOpSel ~= nil then
term.clear()
term.setCursorPos(1,1)
write("\n" .. OpMess[UserOpSel] .. "\n\n")
end
end
function GetNum() -- Gets the numbers the user wants to operate on.
write(QueryMess[2] .. "\n\n")
local x = read()
Num1 = x
print("")
write(QueryMess[3] .. "\n\n")
local x = read()
Num2 = x
print("")
end
function DoMath() -- Does the math using the numbers inputted by the user.
term.clear()
term.setCursorPos(1,2)
if UserOpSel == 1 or UserOpSel == "add" then
Result = Num1 + Num2
write(Num1 .. " + " .. Num2 .. " = " .. Result .. "\n\n")
elseif UserOpSel == 2 or UserOpSel == "subtract" then
Result = Num1 - Num2
write(Num1 .. " - " .. Num2 .. " = " .. Result .. "\n\n")
elseif UserOpSel == 3 or UserOpSel == "multiply" then
Result = Num1 * Num2
write(Num1 .. " * " .. Num2 .. " = " .. Result .. "\n\n")
elseif UserOpSel == 4 or UserOpSel == "divide" then
Result = Num1 / Num2
write(Num1 .. " / " .. Num2 .. " = " .. Result .. "\n\n")
end
end
function StoreData() -- Asks the user whether they want to store the result or not, and then does so, or does not.
write(QueryMess[4] .. "\n\n")
local x = string.lower(read())
print("")
if x == "yes" or x == "y" then
write(QueryMess[5] .. "\n")
print("")
local x = tonumber(read())
print("")
if Memory[x] == nil then
Memory[x] = Result
MoreMath()
else
write(QueryMess[6] .. "\n\n")
local x = string.lower(read())
print("")
if x == "yes" or x == "y" then
Memory[x] = Result
MoreMath()
elseif x == "no" or x == "n" then
StoreData()
end
end
elseif x == "no" or x == "n" then
MoreMath()
else
write("Please enter (Y)es or (N)o.\n")
os.pullEvent()
StoreData()
end
end
function CheckMem() -- Checks to see if the memory contains anything.
local Check = #Memory
if Check ~= 0 then
write(QueryMess[8] .. "\n")
local x = string.lower(read())
print("")
if x == "yes" or x == "y" then
write(QueryMess[9] .. "\n")
local x = read()
print("")
Num1 = Memory[x]
write(QueryMess[10] .. "\n")
local x = read()
print("")
Num2 = Memory[x]
elseif x == "no" or x == "n" then
GetNum()
else
write("Invalid input.\n")
CheckMem()
end
else
GetNum()
end
end
function GetMem() -- Displays what's stored in the memory.
term.clear()
term.setCursorPos(1,2)
local x = #Memory
if Memory ~= nil then
for i, v in pairs(Memory) do
print(i, ". ", v)
end
else
write("There is nothing stored in the memory.")
end
os.pullEvent()
CoreFunc()
end
function MoreMath() -- Asks the user whether they would like to perform another equation.
write(QueryMess[7] .. "\n\n")
local x = read()
print("")
if x == "yes" or x == "y" then
CoreFunc()
elseif x == "no" or x == "n" then
os.reboot()
end
end
function CoreFunc () -- All of the functions combined into one function. Why? I don't know.
DisplayMenu()
QueryUserMenu()
CheckMem()
DoMath()
StoreData()
MoreMath()
end
--[[ RUN CORE ]]--
CoreFunc() -- Runs everything.