A 4-Hour journey into scripting a working calculator
I wanted to push boundaries with scripting and decided on building calculator with limited time set for 4 hours. I created a GUI with nice dark/orange. With buttons I set most important things like numbers, basic equations (+, -, * and /), clear and equation. Rest are randomly picked from Windows 10 built-in calculator. All for that I finished it in around half of hour.
Final result (4 hour and 13 min in total)
I finally created a calculator and it’s working like charm. Some features are not-fixed in time but overall I’m happy about final result. During time I was continuously rewrite 3 times to get basic and sturdy code’s structure to hold without much issue but at the end I have created a lot “loopholes”.
Here is a video…
Calculator.wmv (927.6 KB)
I also provide a place where can physically look into my calculator.
Codes
local Calc = script.Parent.Calc
local Output = script.Parent.Out
local CurrentNumber = ""
local PreviousNumber = ""
local Operator = ""
local OperLists = {
{name = "Add", symbol = "+"},
{name = "Subtract", symbol = "-"},
{name = "Multiply", symbol = "Ă—"},
{name = "Divide", symbol = "Ă·"},
}
function Update(final)
if PreviousNumber ~= "" then
local Symbol = ""
for _, operator in ipairs(OperLists) do
if operator.name == Operator then
Symbol = operator.symbol
break
end
end
Output.Bottom.Showcase.Outputs.Text = PreviousNumber .. (Operator ~= "" and " " .. Symbol or "")
else
Output.Bottom.Showcase.Outputs.Text = ""
end
Output.Top.Outputs.Text = CurrentNumber
print(CurrentNumber.." "..PreviousNumber.." "..Operator)
end
function ClearEqual(Action)
if Action == "Clear" then
PreviousNumber = ""
CurrentNumber = ""
Operator = ""
Update()
elseif Action == "Equal" then
if CurrentNumber ~= "" and PreviousNumber ~= "" then
Operation(Operator)
Operator = ""
Update()
end
end
end
function Operation(Action)
if PreviousNumber == "" then
Operator = Action
end
if CurrentNumber ~= "" then
local SpecifActions = {"Add", "Subtract", "Multiply", "Divide"}
if table.find(SpecifActions, Action) then
if PreviousNumber ~= "" then
if Action == SpecifActions[1] or Action == SpecifActions[2] then
if CurrentNumber == "" and Operator ~= "" then
CurrentNumber = "0"
end
if Action == "Add" then
PreviousNumber = tonumber(PreviousNumber) + tonumber(CurrentNumber)
CurrentNumber = ""
else
PreviousNumber = tonumber(PreviousNumber) - tonumber(CurrentNumber)
CurrentNumber = ""
end
else
if CurrentNumber == "" and Operator == "" then
CurrentNumber = "0"
end
if Action == "Multiply" then
if CurrentNumber ~= "" then
PreviousNumber = tonumber(PreviousNumber) * tonumber(CurrentNumber)
end
CurrentNumber = ""
else
if CurrentNumber ~= "0" and CurrentNumber ~= "" then
PreviousNumber = tonumber(PreviousNumber) / tonumber(CurrentNumber)
end
CurrentNumber = ""
end
end
CurrentNumber = PreviousNumber
PreviousNumber = ""
else
PreviousNumber = CurrentNumber
CurrentNumber = ""
end
end
if PreviousNumber == "" then
if Action == "Negav" then
CurrentNumber = tostring(-tonumber(CurrentNumber))
Operator = ""
end
if Action == "Power" then
CurrentNumber = tonumber(CurrentNumber) * tonumber(CurrentNumber)
Operator = ""
end
if Action == "Sqr" then
CurrentNumber = math.sqrt(CurrentNumber)
Operator = ""
end
if Action == "Log" then
CurrentNumber = math.log(CurrentNumber, 2)
Operator = ""
end
if Action == "Factor" then
local result = 1
for i = 1, tonumber(CurrentNumber) do
result = result * i
end
CurrentNumber = tostring(result)
Operator = ""
end
end
end
Update()
end
function Numbers(N)
if N == "Dot" then
if not string.find(CurrentNumber, "%.") then
CurrentNumber = CurrentNumber .. "."
elseif CurrentNumber == "" then
CurrentNumber = CurrentNumber .. "0."
end
elseif N == "Pi" then
CurrentNumber = math.pi
elseif N == "Exp" then
CurrentNumber = math.exp(1)
else
if Operator == "" then
PreviousNumber = ""
end
CurrentNumber = CurrentNumber .. N
end
Update()
end
function triggers(button)
if button then
if button.Name == "Clear" or button.Name == "Equal" then
ClearEqual(button.Name)
elseif string.find(button.Name, "Number") or button.Name == "Dot" or button.Name == "Pi" or button.Name == "Exp" then
Numbers(string.gsub(button.Name, "Number", ""))
else
Operation(button.Name)
end
end
end
for _, button in ipairs(Calc:GetChildren()) do
if button:IsA("TextButton") then
spawn(function()
button.MouseButton1Click:Connect(function()
triggers(button)
end)
button.MouseEnter:Connect(function()
button.UIStroke.Transparency = 0
end)
button.MouseLeave:Connect(function()
button.UIStroke.Transparency = 1
end)
end)
end
end
How much is worthy of my calculator?
- Legendary
- Epic
- Rare
- Common
- Poor
0 voters