lua程序设计(第四版)练习答案自做(第二十章)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了lua程序设计(第四版)练习答案自做(第二十章),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2612字,纯文字阅读大概需要4分钟。
内容图文
![lua程序设计(第四版)练习答案自做(第二十章)](/upload/InfoBanner/zyjiaocheng/602/b6e407e9bb90451d80cafeccc605bc14.jpg)
文章目录
20.1
#!/usr/bin/lua
local Set={}
local mt={}
function Set.new(l)
local set={}
setmetatable(set,mt)
for _,v in pairs(l) do
set[v]=true
end
return set
end
function Set.union(a,b)
if getmetatable(a)~=mt or getmetatable(b)~=mt then
error("attempt to 'add' a set with a non-set value",2)
end
local res=Set.new{}
for k in pairs(a) do
res[k]=true
end
for k in pairs(b) do
res[k]=true
end
return res
end
function Set.intersection(a,b)
if getmetatable(a)~=mt or getmetatable(b)~=mt then
error("attempt to 'multiply' a set with a non-set value",2)
end
local res=Set.new{}
for k in pairs(a) do
res[k]=b[k]
end
return res
end
function Set.subtraction(a,b)
if getmetatable(a)~=mt or getmetatable(b)~=mt then
error("attempt to 'subtract' a set with a non-set value",2)
end
local res=Set.new{}
for k in pairs(a) do
res[k]=true
end
for k in pairs(b) do
res[k]=nil
end
return res
end
function Set.tostring(set)
local l={}
for e in pairs(set) do
l[#l+1]=e
end
return "{"..table.concat(l,",").."}"
end
function Set.length(set)
local count=0
for i in pairs(set) do
count=count+1
end
return count
end
mt.__add=Set.union
mt.__mul=Set.intersection
mt.__sub=Set.subtraction
mt.__len=Set.length
mt.__tostring=Set.tostring
mt.__le=function (a,b)
for k in pairs(a) do
if not b[k] then
return false
end
end
return true
end
mt.__lt=function (a,b)
return a<=b and not(b<=a)
end
mt.__eq=function (a,b)
return a<=b and b<=a
end
return Set
for test
#!/usr/bin/lua
t=require("20-1and20-2")
a=t.new({1,2,3,4})
b=t.new{3,5}
print(a-b)
print(a+b)
print(a*b)
print("#a="..#a)
20.2
同上
20.3
#!/usr/bin/lua
local proxy={}
local mt={}
function readOnly(t)
mt.__index=function (_,k) return rawget(t,k) end
mt.__newindex=function (t,k,v) error("attempt to update a read-only table",2) end
setmetatable(proxy,mt)
return proxy
end
days={"Sunday","Monday","Tuesday","Wednesday","Tursday","Friday","Saturday"}
days=readOnly(days)
print(days[1])
days[2]="Noday"
20.4
#!/usr/bin/lua
function fileAsArray(filename)
local proxy={}
local filearray={}
local file=assert(io.open(filename,"r"))
for block in file:lines(1) do
filearray[#filearray+1]=block
end
file:close()
local mt={
__index=function (_,k) return filearray[k] end,
__newindex=function (_,k,v)
filearray[k]=v
local file=assert(io.open(filename,"w"))
for _,w in ipairs(filearray) do
file:write(w)
end
file:close()
end,
__pairs=function ()
return function (_,k)
local nextkey,nextvalue=next(filearray,k)
return nextkey,nextvalue
end
end,
__len=function ()
return #filearray
end
}
setmetatable(proxy,mt)
return proxy
end
--the content under the line are for test
f=fileAsArray("text")
print(f[2])
f[2]="i"
for x,y in pairs(f) do
print(x,y)
end
print(#f)
20.5
同上
内容总结
以上是互联网集市为您收集整理的lua程序设计(第四版)练习答案自做(第二十章)全部内容,希望文章能够帮你解决lua程序设计(第四版)练习答案自做(第二十章)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。