lua程序设计(第四版)练习答案自做(第十七章)

文章目录

仓库

17.1

#!/usr/bin/lua
local DoubleList={}
function DoubleList.listNew()
	return {first=0,last=-1}
end
function DoubleList.pushFirst(list,value)
	local first=list.first-1
	list[first]=value
	list.first=first
end
function DoubleList.pushLast(list,value)
	local last=list.last+1
	list.last=last
	list[last]=value
end
function DoubleList.popFirst(list)
	local first=list.first
	if first>list.last then
		error("list is empty")
	end
	local value=list[first]
	list[first]=nil
	list.first=first+1
	return value
end
function DoubleList.popLast(list)
	local last=list.last
	if list.first>last then
		error("list is empty")
	end
	local value=list[last]
	list[last]=nil
	list.last=last-1
	return value
end

return DoubleList

17.2

local function disk(cx,cy,cr)
	return function (x,y)
		return (x-cx)^2+(y-cy)^2<=cr^2
	end
end

local function rect(left,right,bottom,up)
	return function (x,y)
		return left<=x and x<=right and bottom<=y and y<=up
	end
end

local function complement(r)
	return function(x,y)
		return not r(x,y)
	end
end
local function union(r1,r2)
	return function(x,y)
		return r1(x,y) or r2(x,y)
	end
end
local function intersection(r1,r2)
	return function(x,y)
		return r1(x,y) and r2(x,y)
	end
end
local function difference(r1,r2)
	return function(x,y)
		return r1(x,y) and not r2(x,y)
	end
end
local function translate(r,dx,dy)
	return function (x,y)
		return r(x-dx,y-dy)
	end
end
local function plot(r,M,N)
	io.write("P1\n",M," ",N,"\n")
	for i=1,N do
		local y=(N-i*2)/N
		for j=1,M do
			local x=(j*2-M)/M
			io.write(r(x,y) and "1" or "0")
		end
		io.write("\n")
	end
end
return {
	disk=disk,
	rect=rect,
	complement=complement,
	union=union,
	intersection=intersection,
	difference=difference,
	translate=translate,
	plot=plot,
}

17.3

 路径模板中的问号是用来替换模块名的,如果是固定路径组成,那么这个固定路径所指的文件存在的话,模块必定可以找到,但是这并不一定是希望的那个模块。

17.4

待完善

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值