Module:Champ

Une page de Wikiversité, la communauté pédagogique libre.

Ce module sert d'exemple dans la leçon Initiation au Lua avec Scribunto. son utilisation est décrite en détail dans celle-ci et, par conséquent, ce module ne doit pas être modifié sans tenir compte de la leçon.


local p = {}

local B = {3,7}
local C = {1,9}
local Point = {}

setmetatable(B,Point)
setmetatable(C,Point)

local t = {"Piano","Boule","Tortue","Nénuphar"}
local mt = {}

setmetatable(t,mt)

function mt.__index(tab,cle)
	return "(la clé "..cle .." n'existe pas dans cette table.)"
end

function p.vindex()
	local reponse = " "
	for i = 1,5 do
		reponse = reponse.."<br />a la clé "..i..", on trouve : "..t[i]
	end
	return reponse
end

function Point.__add(s,t)
	local u = {}
	u[1] = s[1] + t[1]
	u[2] = s[2] + t[2]
	return u
end

function p.vadd()
	local A = {}
	A = B + C
	return "Le point A à pour coordonnées "..A[1].." et "..A[2]
end

function Point.__sub(s,t)
	local u = {}
	u[1] = s[1] - t[1]
	u[2] = s[2] - t[2]
	return u
end

function p.vsub()
	local A = {}
	A = B - C
	return "Le point A à pour coordonnées "..A[1].." et "..A[2]
end

function Point.__mul(s,t)
	local p
	p = s[1]*t[1]+s[2]*t[2]
	return p
end

function p.scalaire()
	local sca
	sca = B*C
	return "Le produit scalaire des vecteurs B et C est "..sca
end

function Point.__div(s,a)
	local q = {}
	q[1] = s[1]/a
	q[2] = s[2]/a
	return q
end

function p.milieu()
	local I = {}
	local S = {}
	S = B + C
	setmetatable(S,Point)
	I = S/2
	return "Le milieu des deux points B et C a pour coordonnées "..I[1].." et "..I[2]
end

function Point.__concat(s,t)
	local q = {}
	q[1] = tonumber(s[1]..t[1])
	q[2] = tonumber(s[2]..t[2])
	return q
end

function p.concatene()
	local S = {}
	S = B..C
	return "En concatènant les deux tables B et C, avons obtenu : ("..S[1]..","..S[2]..")."
end

function Point.__pow(s,e)
	local q = {}
	q[1] = s[1]^e
	q[2] = s[2]^e
	return q
end

function p.puissance()
	local S = {}
	S = B^2
	return "En élevant la table B à la puissance 2, nous obtenons : ("..S[1]..","..S[2]..")."
end

function Point.__mod(s,m)
	local q = {}
	q[1] = s[1]%m
	q[2] = s[2]%m
	return q
end

function p.modulo()
	local S = {}
	S = B%3
	return "En calculant la classe modulo 3 de la table B, nous obtenons : ("..S[1]..","..S[2]..")."
end

function Point.__unm(s,m)
	local q = {}
	q[1] = -s[1]
	q[2] = -s[2]
	return q
end

function p.negation()
	local S = {}
	S = -B
	return "En calculant l'opposée de la table B, nous obtenons : ("..S[1]..","..S[2]..")."
end

function Point.__eq(s,t)
	if math.abs(s[1] - t[1]) < 0.1 and math.abs(s[2] - t[2]) < 0.1 then
		return true
	else
		return false
	end
end

function p.egal()
	if B == C then
		return "Les tables B et C sont égales."
	else
		return "Les tables B et C sont différentes."
	end
end

function Point.__le(s,t)
	if s[1] + s[2] >= t[1] + t[2] then
		return true
	else
		return false
	end
end

function p.superieur()
	if B >= C then
		return "La table B est supérieure ou égale à la table C."
	else
		return "La table B est strictement infèrieure à la table C."
	end
end

function Point.__lt(s,t)
	if s[1] + s[2] < t[1] + t[2] then
		return true
	else
		return false
	end
end

function p.inferieur()
	if B < C then
		return "La table B est strictement infèrieure à la table C."
	else
		return "La table B est supérieure ou égale à la table C."
	end
end

local souk = {"flute", "pipo", "manche à balaie", "serpière", "jeu de cartes", "coton tige", "tourne vis", "rateau", "stylo", "poupée"}
local entrepot = {}
setmetatable(souk,entrepot)

function suivant(tab,n)
	if n == nil then n = -1 end
	if tab[n+2] == nil then
		return nil,nil
	else
		return n+2,tab[n+2]
	end
end

function entrepot.__ipairs(t)
	return suivant,t,nil
end

function p.liste()
	local reponse = " "
	for index, objet in ipairs(souk)  do
		reponse = reponse.."<br />à la clé numéro "..index.." se trouve l’objet "..objet.."."
	end
	return reponse
end

Point.__metatable = "Coordonnées"

function p.obtenir()
	return getmetatable(B)
end

function Point.__tostring(q)
	return "("..q[1]..","..q[2]..")"
end

function p.convertir()
	return tostring(B)
end

local newtable = {}

function newtable.__call(tab,x)
	return x + 3
end

function p.appel()
	local tb = {}
	setmetatable(tb,newtable)
	return tb(17)
end

function newtable.__newindex(tab,cle,valeur)
	return "Bienvenue dans la table"
end

function p.assignation1()
	local tb = {}
	local reponse
	setmetatable(tb,newtable)
	tb[2] = "Framboise"
	return type(tb[2])
end

function p.assignation2()
	local tb = {"Moustique","Montagne","boulet"}
	local reponse
	setmetatable(tb,newtable)
	tb[2] = "Framboise"
	return tb[2]
end

newtable.__mode = "Je suis une chaine"

function p.faible()
	local reponse
	local tb = {}
	setmetatable(tb,newtable)
	reponse = tb
	return reponse
end

return p