Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I make a calculator perform multi-digit sums?

Asked by 8 years ago

I have programmed a simplistic, on touch calculator, using the Touched event, and a few loops. It's capable of multiplying, dividing, subtracting and adding. I think I should be able to do it in under 50 lines of code, but my current one, which is not as functional as I would like, is over 200.

I believe it can be done using tables, but I'm unsure of how to go about this. Could any one give me the methodology to do this, as opposed to the actual script?

This is the current code if anyone is interested.

Num1 = workspace.No1
Num2 = workspace.No2
Num3 = workspace.No3
Num4 = workspace.No4
Num5 = workspace.No5
Num6 = workspace.No6
Num7 = workspace.No7
Num8 = workspace.No8
Num9 = workspace.No9
Sub = workspace.Su
Add = workspace.Ad
Multi = workspace.Ti
Div = workspace.Di
h = Instance.new("Hint", workspace)

i = 0

x = 0

y = 0

db1 = false
function onTouched()
    if db1 == true then end
    db1 = true
    if i == 0 then
        i = 1
    elseif x == 0
        then x = 1 
    end
    wait(2)
    db1 = false
end

Num1.Touched:connect(onTouched)

db2 = false
function onTouched2()
    if db2 == true then end
    db2 = true
    if i == 0 then 
        i = 2
    elseif x == 0 then
        x = 2
    end
    wait(2)
    db2 = false
end

Num2.Touched:connect(onTouched2)

db3 = false
function onTouched3()
    if db3 == true then end
    db3 = true
    if i == 0 then 
        i = 3
    elseif x == 0 then
        x = 3
    end
    wait(2)
    db3 = false
end

Num3.Touched:connect(onTouched3)

db4 = false
function onTouched4()
    if db4 == true then end
    db4 = true
    if i == 0 then 
        i = 4
    elseif x == 0 then
        x = 4
    end
    wait(2) 
    db4 = false
end

Num4.Touched:connect(onTouched4)

db5 = false
function onTouched5()
    if db5 == true then end
    db5 = true
    if i == 0 then 
        i = 5
    elseif x == 0 then
        x = 5
    end
    wait(2)
    db5 = false
end

Num5.Touched:connect(onTouched5)

db6 = false
function onTouched6()
    if db6 == true then end
    db6 = true
    if i == 0 then 
        i = 6
    elseif x == 0 then
        x = 6
    end
    wait(2)
    db6 = false
end

Num6.Touched:connect(onTouched6)

db7 = false

function onTouched7()
    if db7 == true then end
    db7 = true
    if i == 0 then 
        i = 7
    elseif x == 0 then
        x = 7
    end
    wait(2)
    db7 = false
end

Num7.Touched:connect(onTouched7)

db8 = false
function onTouched8()
    if db8 == true then end
    db8 = true
    if i == 0 then 
        i = 8
    elseif x == 0 then
        x = 8
    end
    wait(2)
    db8 = false
end

Num8.Touched:connect(onTouched8)

db9 = false
function onTouched9()
    if db9 == true then end
    db9 = true
    if i == 0 then 
        i = 9
    elseif x == 0 then
        x = 9
    end
    wait(2)
    db9 = false
end

Num9.Touched:connect(onTouched9)

function M()
    if y == 0 then
        y = 1
    end
end

workspace.Ti.Touched:connect(M)

function D()
    if y == 0 then
        y = 2
    end
end

workspace.Di.Touched:connect(D)

function A()
    if y == 0 then
        y = 3
    end
end

workspace.Ad.Touched:connect(A)

function S()
    if y == 0 then
        y = 4
    end
end

workspace.Su.Touched:connect(S)






while true do
    if i > 0 and x > 0 and y > 0 then
    if y == 1 then
        h.Text = i .. " x " .. x .. " = " .. i * x
    elseif y == 2 then
        h.Text = i .. " / " .. x .. " = " .. i / x
    elseif y == 3  then
        h.Text = i .. " + " .. x .. " = " .. i + x
    elseif y == 4 then
        h.Text = i .. " - " .. x .. " = " .. i - x
    end
    y = 0
    x = 0
    i = 0
    end
    wait(1)
end



0
try using toString() and toNumber() instead of whatever is taking so many lines... KoreanBBQ 301 — 8y
0
tostring(), tonumber() not toString(), toNumber(). XToonLinkX123 580 — 8y
0
How does that work? WolfgangVonPrinz 0 — 8y

Answer this question