《Lua程序设计第四版》 第二部分9~13章自做练习题答案 ...

怀念夏天  金牌会员 | 2023-8-28 11:04:47 | 来自手机 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 903|帖子 903|积分 2709

Lua程序设计第四版第二部分编程实操自做练习题答案,带⭐为重点。
9.1

请编写一个函数integral,该函数以一个函数f为参数并返回其积分的近似值
使用右矩阵法近似积分值
  1. function integral(f)
  2.     return function(a, b)
  3.         local sum = 0
  4.         for i = 1, 10000, 1 do
  5.             sum = sum + f(a + (b - a) * i / 10000)
  6.         end
  7.         return sum * (b - a) / 10000
  8.     end
  9. end
  10. function x3(x)
  11.     return 2 * x + 3 * x ^ 3
  12. end
  13. jf = integral(x3)
  14. print(jf(0, 10)) -- 7601.510075 近似 7600
复制代码
9.2

如下代码段将输出什么结果
  1. function F(x)
  2.     return {
  3.         set = function(y)
  4.             x = y
  5.         end,
  6.         get = function()
  7.             return x
  8.         end
  9.     }
  10. end
  11. o1 = F(10)
  12. o2 = F(20)
  13. print(o1.get(), o2.get())
  14. o2.set(100)
  15. o1.set(300)
  16. print(o1.get(), o2.get())
  17. -- 10      20
  18. -- 300     100
复制代码
9.3 ⭐

编写练习5.4的柯里化版本
柯里化(Currying)是把接受多个参数的函数变换成接受一个单一参数(最初函数的第一个参数)的函数,并且返回接受余下的参数且返回结果的新函数的技术。
  1. function newpoly(t)
  2.     return function(x)
  3.         local sum = 0
  4.         for i, v in ipairs(t) do
  5.             sum = sum + v * x ^ (i - 1)
  6.         end
  7.         return sum
  8.     end
  9. end
  10. t = newpoly({3, 0, 1})
  11. print(t(0))
  12. print(t(5))
  13. print(t(10))
复制代码
9.4

使用几何区域系统的例子,绘制一个北半球所能看到的峨眉月
编写一个被其他函数B包含的函数A时,被包含的函数A可以访问包含其的函数B的所有局部变量,这种特性被称为词法定界
  1. -- 利用高阶函数和词法定界,定义一个指定圆心和半径创建圆盘的工厂 --
  2. function disk(cx, cy, r)
  3.     return function(x, y)
  4.         return (x - cx) ^ 2 + (y - cy) ^ 2 <= r ^ 2
  5.     end
  6. end
  7. -- 创建一个指定边界的轴对称图形
  8. function rect(left, right, top, bottom)
  9.     return function(x, y)
  10.         return left <= x and x <= right and bottom <= y and y <= top
  11.     end
  12. end
  13. -- 创建任何区域的补集
  14. function complement(r)
  15.     return function(x, y)
  16.         return not r(x, y)
  17.     end
  18. end
  19. -- 创建任何区域的并集
  20. function union(r1, r2)
  21.     return function(x, y)
  22.         return r1(x, y) or r2(x, y)
  23.     end
  24. end
  25. -- 交集
  26. function intersection(r1, r2)
  27.     return function(x, y)
  28.         return r1(x, y) and r2(x, y)
  29.     end
  30. end
  31. -- 差集
  32. function difference(r1, r2)
  33.     return function(x, y)
  34.         return r1(x, y) and not r2(x, y)
  35.     end
  36. end
  37. -- 按指定增量平移区域
  38. function translate(r, dx, dy)
  39.     return function(x, y)
  40.         return r(x - dx, y - dy)
  41.     end
  42. end
  43. function plot(r, M, N, file)
  44.     f = io.open(file, "w")
  45.     f:write("P1\n", M, " ", N, "\n")
  46.     for i = 1, N, 1 do
  47.         local y = (N - i * 2) / N
  48.         for j = 1, M do
  49.             local x = (j * 2 - M) / M
  50.             f:write(r(x, y) and "1" or "0")
  51.         end
  52.         f:write("\n")
  53.     end
  54.     f:close()
  55. end
  56. circle = disk(0, 0, 0.5)
  57. circle2 = translate(circle, -0.3, 0.2)
  58. shape = difference(circle, circle2)
  59. plot(shape, 512, 512, "test.pbm")
复制代码
13.4

该函数用于计算指定整数的汉明权重(数二进制表示的1的个数)
  1. -- 创建一个指定边界的轴对称图形
  2. function rect(left, right, top, bottom)
  3.     return function(x, y)
  4.         return left <= x and x <= right and bottom <= y and y <= top
  5.     end
  6. end
  7. -- 创建任何区域的并集
  8. function union(r1, r2)
  9.     return function(x, y)
  10.         return r1(x, y) or r2(x, y)
  11.     end
  12. end
  13. -- 图形根据坐标轴原点顺时针旋转d弧度
  14. function rotate(r, d)
  15.     return function(x, y)
  16.         return r(-math.cos(d) * x + y * math.sin(d), -math.sin(d) * x - math.cos(d) * y)
  17.     end
  18. end
  19. function plot(r, M, N, file)
  20.     f = io.open(file, "w")
  21.     f:write("P1\n", M, " ", N, "\n")
  22.     for i = 1, N, 1 do
  23.         local y = (N - i * 2) / N
  24.         for j = 1, M do
  25.             local x = (j * 2 - M) / M
  26.             f:write(r(x, y) and "1" or "0")
  27.         end
  28.         f:write("\n")
  29.     end
  30.     f:close()
  31. end
  32. shape1 = rect(-0.5, 0.5, 0.5, -0.5)
  33. shape2 = rect(-0.25, 0.25, 0.75, -0.25)
  34. shape1 = rotate(shape, math.pi * 0.1) -- s1所处的坐标系平面被旋转了,s2的并没有
  35. shape = union(shape1, shape2)
  36. plot(shape, 512, 512, "test.pbm")
复制代码
13.5

该函数用于判断注定整数的二进制表示是否为回文数
  1. function split(s, sep)
  2.     t = {}
  3.     -- tmpindex = 0
  4.     -- index = 1
  5.     -- index = string.find(s, sep, index, true)
  6.     -- while index do
  7.     --     table.insert(t, string.sub(s, tmpindex + 1, index))
  8.     --     tmpindex = index
  9.     --     index = string.find(s, sep, index + 1, true)
  10.     -- end
  11.     -- if index ~= #s then
  12.     --     table.insert(t, string.sub(s, tmpindex + 1, #s))
  13.     -- end
  14.     for w in string.gmatch(s, "[^" .. sep .. "]+") do
  15.         t[#t + 1] = w
  16.     end
  17.     return t
  18. end
  19. t = split("a whole new world", " ")
  20. -- t = split("", " ")
  21. for k, v in pairs(t) do
  22.     print(k, v)
  23. end
复制代码
13.6 ⭐

请在Lua语言实现一个比特数组

  • newBiteArrary(n) 创建一个具有n个比特的数组
  • setBit(a, n, v) 将布尔值v赋值给数组a的第n位
  • testBit(a, n) 将第n位的值作为布尔值返回
  1. [^%d%u] 是 (数字∪大写字母)的补集 = 排除数字和大写字母
  2. > string.find("123ABCA","[^%d%u]")
  3. nil
  4. > string.find("123ABCa","[^%d%u]")
  5. 7       7
  6. [%D%U] 是  数字的补集∪大写字母的补集 = 全集
  7. > string.find("123ABC","[%D%U]")
  8. 1       1
  9. 使用第一种是正确的
复制代码
13.7 ⭐

假设有一个以一系列记录组成的二进制文件,其中的每一个记录的格式为
  1. function transliterate(s, t)
  2.     return string.gsub(s, ".", function(w)
  3.         if t[w] == false then
  4.             return ""
  5.         else
  6.             return t[w]
  7.         end
  8.     end)
  9. end
  10. s = "apple"
  11. s = transliterate(s, {
  12.     a = "b",
  13.     p = false,
  14.     l = "i",
  15.     e = "g"
  16. })
  17. print(s)
复制代码
请编写一个程序,该程序读取这个文件,然后输出value字段的总和
[code]function saveRecords(t)    local f = io.open("records.txt", "w")    for _, v in ipairs(t) do        f:write(string.pack("j", v.x))        f:write(string.pack("z", v.code))        f:write(string.pack("n", v.value))    end    f:close()endfunction readCalcuteValue()    local f = io.open("records.txt", "rb")    local s = f:read('a')    f:close()    local i = 1    local total = 0    while i
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

怀念夏天

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表