• 企业400电话
  • 网络优化推广
  • AI电话机器人
  • 呼叫中心
  • 全 部 栏 目

    网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    Lua编程示例(三):稀疏表、双端队列、格式化输出、表和循环表的格式化输出
    POST TIME:2021-10-18 16:49
    a={}
    for i=1,10 do
     a[i]={}
     for j=0,10 do
     if(i%2==0) then
      a[i][j]=0
     end
     end
    end
    
    print(a[9][10])
    print(a[10][10])
    print()
    
    --双端队列
    
    List={}
    
    function List.new()
     return {first = 0,last = -1}
    end
    
    function List.pushleft(list,value)
     local first= list.first-1
     list[first] = value
     list.first= first
    end
    
    function List.pushright(list,value)
     local last = list.last+1
     list[last]= value
     list.last=last
    end
    
    function List.popleft(list)
     local first=list.first
     if(first>list.last) then
     error("list is empty")
     end
     local res= list[first]
     list[first]=nil
     list.first=list.first+1
     return res
    end
    
    function List.popright(list)
     local last = list.last
     if lastfirst then
     error("the list is empty")
     end
     local res = list[last]
     list[last]= nil
     list.last= list.last-1
     return res
    end
    
    function List.display(list)
     if(list.first>list.last) then
     error("the list is empty",2)
     end
     for i=list.first ,list.last do
     print(list[i])
     end
    end
    
    mylist=List.new()
    List.pushleft(mylist,12)
    List.pushleft(mylist,"00")
    List.pushright(mylist,34)
    List.pushright(mylist,56)
    List.display(mylist)
    print()
    
    function newStack ()
     return {""}
    end
    
    
    function serialize(o)
     if type(o) == "number" then
     io.write(o)
     elseif type(o) == "string" then
      --不要手动加入引号,否则会有边际效应
     io.write(string.format("%q",o))
     elseif type(o) == "table" then
     io.write("{\n")
     for i,v in pairs(o) do
      io.write("  "..i.." = ")
      serialize(v)
      io.write(",\n")
     end
     io.write("}\n")
     end
    end
    
    serialize(123)
    print()
    serialize("112233")
    print()
    tab = { a=11,haha="www" ,c=333}
    serialize(tab)
    
    function basicSerialize (o)
     if type(o) == "number" then
      return tostring(o)
     else
      return string.format("%q", o)
     end
    end
    
    function save (name, value, saved)
     saved = saved or {}  -- 参数未传入的初始化
     io.write(name, " = ")
     if type(value) == "number" or type(value) == "string" then
     io.write(basicSerialize(value), "\n")
     elseif type(value) == "table" then
     if saved[value] then
      io.write(saved[value], "\tcircle\n")
     else
      saved[value] = name
      io.write("{}\n")
      for k,v in pairs(value) do
       local fieldname = string.format("%s[%s]", name,
        basicSerialize(k))
       save(fieldname, v, saved)
      end
     end
     else
     error("cannot save a " .. type(value))
     end
    end
    
    a = {x=1, y=2; {3,4,5}}
    a[2] = a  -- 循环表
    a.z = a[1]  -- 共享子表
    
    save('a',a)
    

    运行结果:

    nil
    0
    
    00
    12
    34
    56
    
    123
    "112233"
    {
      a = 11,
      c = 333,
      haha = "www",
    }
    a = {}
    a[1] = {}
    a[1][1] = 3
    a[1][2] = 4
    a[1][3] = 5
    a[2] = a circle
    a["y"] = 2
    a["x"] = 1
    a["z"] = a[1] circle
    

    您可能感兴趣的文章:
    • Lua编程中使用嵌套循环的使用教程
    • Lua中的repeat...until循环语句使用教程
    • 详解Lua中repeat...until循环语句的使用方法
    • Lua中for循环语句的使用教程
    • 详解Lua中的while循环语句的使用
    • Lua中数字for循环实例
    • Lua中变相实现continue跳出循环
    • Lua中的for循环和迭代器的秘密探究
    • Lua中三种循环语句的使用讲解
    上一篇:Lua编程示例(二):面向对象、metatable对表进行扩展
    下一篇:Lua编程示例(四):Lua标准库之表库、字符串库、系统库
  • 相关文章
  • 

    关于我们 | 付款方式 | 荣誉资质 | 业务提交 | 代理合作


    © 2016-2020 巨人网络通讯

    时间:9:00-21:00 (节假日不休)

    地址:江苏信息产业基地11号楼四层

    《增值电信业务经营许可证》 苏B2-20120278

    X

    截屏,微信识别二维码

    微信号:veteran88

    (点击微信号复制,添加好友)

     打开微信