Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- frame = script.Parent
- buttonPlus = frame.Plus
- buttonMinus = frame.Minus
- buttonDiv = frame.Div
- buttonMul = frame.Mul
- number1 = frame.Number1
- number2 = frame.Number2
- numberAnswer = frame.NumberAnswer
- function sum(x, y)
- return x + y
- end
- function sub(x, y)
- return x - y
- end
- function div(x, y)
- return y == 0 and "На ноль делить нельязя" or x / y -- а вот и тернарная конструкция
- end
- function mul(x, y)
- return x*y
- end
- function printAnswer()
- local x = tonumber(number1.Text) or 0 -- а вот и преобразование типов (Downcasting) и необходимость писать локальные переменные для чистых функций
- local y = tonumber(number2.Text) or 0 -- параметр 0 по умолчанию на случай когда преобразование не удалось и получили nil
- numberAnswer.Text = x .. opChar .. y .. "=" .. operation(x, y) -- преобразование типов из number в text (Upcasting) и конкатенация строк пригодилась
- end
- operation = sum -- переменная которая хранит функцию, которая определяет как совершать вычисления между числами
- opChar = "+"
- buttonPlus.MouseButton1Click:Connect(function()
- operation = sum
- opChar = "+"
- printAnswer()
- end)
- buttonMinus.MouseButton1Click:Connect(function()
- operation = sub
- opChar = "-"
- printAnswer()
- end)
- buttonDiv.MouseButton1Click:Connect(function()
- operation = div
- opChar = "/"
- printAnswer()
- end)
- buttonMul.MouseButton1Click:Connect(function()
- operation = mul
- opChar = "x"
- printAnswer()
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement