System.memclean()
Touches = {
	{ "C", "sin", "cos", "tan" },
	{ "1/x", "x^2", "sqr", "/" },
	{ "7", "8", "9", "*" },
	{ "4", "5", "6", "-" },
	{ "1", "2", "3", "+" },
	{ "0", "+/-", ".", "=" } }

blanc = Color.new (255,255,255)
noir = Color.new (0,0,0)
rouge = Color.new(255,0,0)

Fond_Calcul = Image.load("images/Calculatrice.png")
battery1 = Image.load("images/Copie de battery4.png")
battery2 = Image.load("images/Copie de battery3.png")
battery3 = Image.load("images/Copie de battery2.png")
battery4 = Image.load("images/Copie de battery1.png")

img = nil

OldPad = Controls.read()

x = 1
y = 1

Entre = "0"

DernierNombre = 0

Suppression = true

DerniereOperation = ""

Valeur = ""

Nombre = 0

while true do
pad = Controls.read()
screen:blit(0,0,Fond_Calcul)

screen:print(160,65,"C",blanc)
screen:print(205,65,"sin",blanc)
screen:print(250,65,"cos",blanc)
screen:print(290,65,"tan",blanc)
screen:print(170,93,"1/x",blanc)
screen:print(205,93,"x^2",blanc)
screen:print(240,93,"sqr",blanc)
screen:print(288,93,"/",blanc)
screen:print(168,124,"7",blanc)
screen:print(208,124,"8",blanc)
screen:print(248,124,"9",blanc)
screen:print(288,124,"*",blanc)
screen:print(168,154,"4",blanc)
screen:print(208,154,"5",blanc)
screen:print(248,154,"6",blanc)
screen:print(288,154,"-",blanc)
screen:print(168,184,"1",blanc)
screen:print(208,184,"2",blanc)
screen:print(248,184,"3",blanc)
screen:print(288,184,"+",blanc)
screen:print(168,214,"0",blanc)
screen:print(208,214,"+/-",blanc)
screen:print(248,214,".",blanc)
screen:print(289,215,"=",blanc)

screen:print(150,48,"Calcul: "..Entre,noir)


if pad ~= OldPad then
if pad:left() then
x = x - 1
if x == 0 then
x = 4
end
end

if pad:right() then
x = x + 1
if x == 5 then
x = 1
end
end

if pad:up() then
y = y - 1
if y == 0 then
y = 6
end
end

if pad:down() then
y = y + 1
if y == 7 then
y = 1
end
end

if pad:cross() then
Valeur = Touches[y][x]
Nombre = tonumber(Entre)
if Valeur == "C" then
Entre = "0"
DernierNombre = 0
DerniereOperation = ""
Suppression = true

elseif Valeur == "1/x" then
Entre = tostring(1/Nombre)
elseif Valeur == "sin" then
Entre = tostring(math.sin(Nombre))
elseif Valeur == "cos" then
Entre = tostring(math.cos(Nombre))
elseif Valeur == "tan" then
Entre = tostring(math.tan(Nombre))
elseif Valeur == "x^2" then
Entre = tostring(Nombre*Nombre)
elseif Valeur == "sqr" then
Entre = tostring(math.sqrt(Nombre))
elseif Valeur == "/" or Valeur == "*" or Valeur == "-" or Valeur == "+" or Valeur == "=" then
if DerniereOperation == "/" then
Entre = tostring(DernierNombre / Nombre)
elseif DerniereOperation == "*" then
Entre = tostring(DernierNombre * Nombre)
elseif DerniereOperation == "-" then
Entre = tostring(DernierNombre - Nombre)
elseif DerniereOperation == "+" then
Entre = tostring(DernierNombre + Nombre)
end
Nombre = tonumber(Entre)
if Valeur == "=" then
Entre = tostring(Nombre)
DerniereOperation = ""
else
DernierNombre = Nombre
DerniereOperation = Valeur
end
Suppression = true
elseif Valeur == "+/-" then
Entre = tostring(-Nombre)
else
if Suppression then
Entre = Valeur
Suppression = false
else
Entre = Entre .. Valeur
end
end
end

if pad:start() then
break
end
OldPad = pad
end

if y == 1 then
if x == 1  then
screen:print(160,65,"C",rouge)
elseif x == 2  then
screen:print(205,65,"sin",rouge)
elseif x == 3 then
screen:print(250,65,"cos",rouge)
elseif x  == 4 then
screen:print(290,65,"tan",rouge)
end
elseif y == 2 then
if x == 1 then
screen:print(170,93,"1/x",rouge)
elseif x == 2 then
screen:print(205,93,"x^2",rouge)
elseif x == 3 then
screen:print(240,93,"sqr",rouge)
elseif x == 4 then
screen:print(288,93,"/",rouge)
end
elseif y == 3 then
if x == 1 then
screen:print(168,124,"7",rouge)
elseif x == 2 then
screen:print(208,124,"8",rouge)
elseif x == 3 then
screen:print(248,124,"9",rouge)
elseif x == 4 then
screen:print(288,124,"*",rouge)
end
elseif y == 4 then
if x == 1 then
screen:print(168,154,"4",rouge)
elseif x == 2 then
screen:print(208,154,"5",rouge)
elseif x == 3 then
screen:print(248,154,"6",rouge)
elseif x == 4 then
screen:print(288,154,"-",rouge)
end
elseif y == 5 then
if x == 1 then
screen:print(168,184,"1",rouge)
elseif x == 2 then
screen:print(208,184,"2",rouge)
elseif x == 3 then
screen:print(248,184,"3",rouge)
elseif x == 4 then
screen:print(288,184,"+",rouge)
end
elseif y == 6 then
if x == 1 then
screen:print(168,214,"0",rouge)
elseif x == 2 then
screen:print(208,214,"+/-",rouge)
elseif x == 3 then
screen:print(248,214,".",rouge)
elseif x == 4 then
screen:print(289,215,"=",rouge)
end
end

if System.powerGetBatteryLifePercent() >= 0 then batteryL = battery1 end
if System.powerGetBatteryLifePercent() > 30 then batteryL = battery2 end
if System.powerGetBatteryLifePercent() > 50 then batteryL = battery3 end
if System.powerGetBatteryLifePercent() > 70 then batteryL = battery4 end

screen:blit(456,4,batteryL)

if pad:square() then
dofile("Scripts/menu.lua")
Fond_Calcul = nil
Calculette = nil
end

screen.waitVblankStart()
screen.flip()
end
