---- Button API Calculator 1.0, By Gustavo Wizard
---- Plz give credit for me and for the button API author if using this program in any way


os.loadAPI("btn")
m = peripheral.wrap("top")
m1 = peripheral.wrap("left")

while true do
m.setBackgroundColor(colors.black)
m.clear()
m.setCursorPos(3,1)
m.setTextColor(colors.lime)
m.write("CALCULATOR 1.0")

m1.setTextColor(colors.white)
m1.setBackgroundColor(colors.blue)
m1.clear()
m1.setCursorPos(1,1)
m1.write("Result:")

m.setTextColor(colors.white)

function fillTable()
 btn.setTable("1", one, 6,6,4,4)
 btn.setTable("2", two, 8,8,4,4)
 btn.setTable("3", tre, 10,10,4,4) 
 btn.setTable("4", four, 6,6,6,6)
 btn.setTable("5", five, 8,8,6,6)
 btn.setTable("6", six, 10,10,6,6)
 btn.setTable("7", sev, 6,6,8,8)
 btn.setTable("8", eit, 8,8,8,8)
 btn.setTable("9", nin, 10,10,8,8)
 btn.setTable("+", add, 12,12,4,4)
 btn.setTable("-", sub, 12,12,6,6)
 btn.setTable("*", mul, 12,12,8,8)
 btn.setTable("/", div, 6,6,10,10)
 btn.setTable("=", res, 8,12,10,10)
 btn.screen()
end

function getClick()
 event,side,x,y = os.pullEvent("monitor_touch")
 btn.checkxy(x,y)
end

function one()
 btn.flash("1")
 num1 = 1
end

function two()
 btn.flash("2")
 num1 = 2
end

function tre()
 btn.flash("3")
 num1 = 3
end

function four()
 btn.flash("4")
 num1 = 4
end 

function five()
 btn.flash("5")
 num1 = 5
end

function six()
 btn.flash("6")
 num1 = 6
end

function sev()
 btn.flash("7")
 num1 = 7
end

function eit()
 btn.flash("8")
 num1 = 8
end

function nin()
 btn.flash("9")
 num1 = 9
end

fillTable()

getClick()

m1.setTextColor(colors.yellow)
m1.setCursorPos(1,2)
m1.write(num1)


function add()
 btn.flash("+")
 op = "+"
end

function sub()
 btn.flash("-")
 op = "-"
end

function mul()
 btn.flash("*")
 op = "*"
end

function div()
 btn.flash("/")
 op = "/"
end

fillTable()

getClick()

m1.setCursorPos(5,3)
m1.write(op)

function one()
 btn.flash("1")
 num2 = 1
end

function two()
 btn.flash("2")
 num2 = 2
end

function tre()
 btn.flash("3")
 num2 = 3
end

function four()
 btn.flash("4")
 num2 = 4
end

function five()
 btn.flash("5")
 num2 = 5
end

function six()
 btn.flash("6")
 num2 = 6
end

function sev()
 btn.flash("7")
 num2 = 7
end

function eit()
 btn.flash("8")
 num2 = 8
end

function nin()
 btn.flash("9")
 num2 = 9
end

fillTable()

getClick()

m1.setCursorPos(1,3)
m1.write(num2)


function res()
 btn.flash("=")
end

fillTable()

getClick()

m1.setTextColor(colors.orange)
m1.setCursorPos(1,4)
m1.write("_____")
m1.setCursorPos(1,5)
if op == "+" then
m1.write(num1+num2)
end
if op == "-" then
m1.write(num1-num2)
end
if op == "*" then
m1.write(num1*num2)
end
if op == "/" then
m1.write(num1/num2)
end

sleep(4)

end
