![](https://img-blog.csdnimg.cn/20201014180756925.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
lua笔记
fuluoyide312
《如何写出无法维护的代码》
展开
-
lua程序设计(第四版)练习答案自做(第二十五章)
文章目录25.125.225.325.425.525.625.725.825.1#!/usr/bin/luafunction getvarvalue(...) local co=... if type(co)=="thread" then local name,level,isenv=select(2,...) local value local found=false原创 2021-05-11 21:28:20 · 137 阅读 · 0 评论 -
lua程序设计(第四版)练习答案自做(第三十一章)
文章目录31.131.2和31.331.431.531.1#include <stdarg.h>#include <stdlib.h>#include <lua.h>#include <lauxlib.h>#include <limits.h>#define BITS_PER_WORD (CHAR_BIT*sizeof(unsigned int))#define I_WORD(i) ((unsigned int)(i)/BITS_P原创 2021-04-20 22:45:57 · 137 阅读 · 0 评论 -
lua程序设计(第四版)练习答案自做(第三十章)
文章目录30.130.230.330.430.530.630.1#include <stdio.h>#include <lua.h>#include <lualib.h>#include <lauxlib.h>int filter(lua_State *L){ int index=1; luaL_checktype(L,1,LUA_TTABLE); luaL_checktype(L,2,LUA_TFUNCTION); int len=lu原创 2021-04-15 20:57:06 · 122 阅读 · 0 评论 -
lua程序设计(第四版)练习答案自做(第二十九章)
文章目录29.129.229.329.429.529.629.1static int summation(lua_State *L){ int parnum; double sum=0.0; parnum=lua_gettop(L); for(int i=1;i<=parnum;i++) { sum+=luaL_checknumber(L,i); } lua_pushnumber(L,sum); return 1;}29.2static int pack(lua_St原创 2021-04-12 21:49:26 · 146 阅读 · 0 评论 -
lua程序设计(第四版)练习答案自做(第二十八章)
文章目录28.128.228.328.1#include <stdio.h>#include <stdlib.h>#include <lua.h>#include <lualib.h>#include <lauxlib.h>void error(lua_State *L,const char *fmt,...){ va_list argp; va_start(argp,fmt); vfprintf(stderr,fmt,arg原创 2021-04-09 20:17:34 · 217 阅读 · 0 评论 -
lua程序设计(第四版)练习答案自做(第二十七章)
文章目录27.127.227.327.427.1#include <stdio.h>#include <string.h>#include "lua.h"#include "lauxlib.h"#include "lualib.h"int main(void){ char buff[256]; int error; lua_State *L=luaL_newstate(); luaL_openlibs(L); while(fgets(buff,sizeof原创 2021-04-07 22:16:26 · 159 阅读 · 0 评论 -
lua程序设计(第四版)练习答案自做(第十六章)
文章目录16.116.216.316.416.1#!/usr/bin/luafunction loadwithprefix(prefix,main) if type(main)=="function" then local t={} t[1]=prefix for w in main do t[#t+1]=w end return load(table.concat(t)) else return load(prefix..main) endend16.2#原创 2021-03-23 11:09:27 · 222 阅读 · 0 评论 -
lua程序设计(第四版)练习答案自做(第二十六章)
#!/usr/bin/lualocal socket=require"socket"function receive(connection) connection:settimeout(0) local s,status,partial=connection:receive(2^10) if status=="timeout" then coroutine.yield(connection) end return s or partial,statusendfunction downl原创 2021-03-21 21:58:14 · 91 阅读 · 0 评论 -
lua程序设计(第四版)练习答案自做(第二十三章)
文章目录23.123.223.323.423.5仓库23.1待完善23.2 没有进行过垃圾收集的话,会在lua虚拟机关闭后调用它的析构器,所以依然会输出那条信息。 调用os.exit以及程序因为出错而退出时,不会调用析构器。23.3 使用弱引用值的表,并且将函数返回的字符串放进表中,再将表存入弱引用表的相对应的键值对中。23.4 第一次print时,a是由10000个表组成的数组,并且count的值是10000。然后将a赋值nil后,调用垃圾收集,这时候会将数组内的表标记为活跃状态并放入原创 2021-03-10 21:45:39 · 142 阅读 · 0 评论 -
lua程序设计(第四版)练习答案自做(第二十二章)
文章目录22.122.222.3仓库22.1#!/usr/bin/luafunction getfield(f) local v=_G w=string.match(f,"[^%w%._]") if w then error("seperation character was wrong!") end for w in string.gmatch(f,"([%a_][%w_]*)(%.?)") do v=v[w] end return vend22.2 1 lo原创 2021-03-09 00:32:48 · 185 阅读 · 0 评论 -
lua程序设计(第四版)练习答案自做(第二十一章)
文章目录21.121.221.321.4仓库21.1#!/usr/bin/luaStack={}function Stack:isempty() return #self==0endfunction Stack:push(v) table.insert(self,v)endfunction Stack:pop() if #self~=0 then table.remove(self) else error("Stack is empty") endendfunctio原创 2021-03-07 16:14:05 · 173 阅读 · 0 评论 -
lua程序设计(第四版)练习答案自做(第二十章)
文章目录20.120.220.320.420.5仓库20.1#!/usr/bin/lualocal Set={}local mt={}function Set.new(l) local set={} setmetatable(set,mt) for _,v in pairs(l) do set[v]=true end return setendfunction Set.union(a,b) if getmetatable(a)~=mt or getmetatable(b)~=原创 2021-02-28 15:19:11 · 189 阅读 · 0 评论 -
lua程序设计(第四版)练习答案自做(第十九章)
文章目录19.1仓库19.1#!/usr/bin/luamath.randomseed(os.time())function allwords() local line=io.read() local pos=1 return function () while line do local w,e=string.match(line,"(%w+[,;.:]?)()",pos) if w then pos=e return w else line=原创 2021-02-26 13:37:17 · 107 阅读 · 0 评论 -
lua程序设计(第四版)练习答案自做(第十八章)
文章目录18.118.218.318.418.5仓库18.1 数值型for应该指的是for i=n,m,1的形式,也就是i从n开始按步长1逐渐递增,直到大于m结束循环function fromto(n,m) n=n-1 return function () n=n+1 if n<=m then return n end endend 无状态迭代器#!/usr/bin/lualocal function iter(m,n) --注意m和n原创 2021-02-25 14:35:55 · 256 阅读 · 1 评论 -
lua程序设计(第四版)练习答案自做(第十七章)
文章目录17.117.217.317.4仓库17.1#!/usr/bin/lualocal DoubleList={}function DoubleList.listNew() return {first=0,last=-1}endfunction DoubleList.pushFirst(list,value) local first=list.first-1 list[first]=value list.first=firstendfunction DoubleList.pus原创 2021-02-24 13:08:45 · 137 阅读 · 0 评论 -
lua程序设计(第四版)练习答案自做(第十五章)
文章目录15.115.215.315.415.515.1#!/usr/bin/luafunction basicSerialize(o) return string.format("%q",o)endfunction serialize(o,deep) deep=deep or 0 local t=type(o) if t=="boolean" or t=="number" or t=="string" or t=="nil" then io.write(string.format("原创 2021-02-19 20:19:37 · 123 阅读 · 0 评论 -
lua程序设计(第四版)练习答案自做(第十四章)
文章目录14.114.214.314.4仓库14.1#!/usr/bin/lua--假定稀疏矩阵是用不规则数组表示的function add(a,b) local c={} for i=1,#a do c[i]={} for j=1,#a[i] do c[i][j]=a[i][j]+b[i][j] end end return cenda={{1,2,3},{1,2,3},{1,2,3}}b={{1,1,1},{2,2,2},{3,3,3}}c=add(a,b)原创 2021-02-13 22:18:16 · 165 阅读 · 0 评论 -
lua程序设计(第四版)练习答案自做(第十三章)
文章目录正文page136page138page14113.113.213.313.413.513.613.7仓库正文page136 string.format("%x",0xff & 0xabcd)上例中f即二进制1111,0xff前面的所有位为0,后面八位为1。& 运算中, &1即不变, &0即为0,故0xabcd只有后面的cd不变,前面的位都变为0. string.format("%x",0xff | 0xabcd)同上,但是 | 运算中, |1即为1原创 2021-02-10 23:04:22 · 129 阅读 · 0 评论 -
lua程序设计(第四版)练习答案自做(第十二章)
文章目录12.112.212.312.412.512.612.712.8仓库12.1#!/usr/bin/luafunction nextmonth(num) temp=os.date("*t",num) temp.month=temp.month+1 return os.time(temp)end12.2#!/usr/bin/luafunction wday(num) result=os.date("%w",num) return math.floor(result+1)en原创 2021-02-09 19:41:35 · 181 阅读 · 0 评论 -
lua程序设计(第四版)练习答案自做(第十一章)
文章目录11.111.2仓库11.1#!/usr/bin/env lualocal counter={}for line in io.lines() do for word in string.gmatch(line,"%w%w%w%w+") do counter[word]=(counter[word] or 0)+1 endendlocal words={}for word in pairs(counter) do words[#words+1]=wordendtable.原创 2021-01-26 12:13:17 · 160 阅读 · 0 评论 -
lua程序设计(第四版)练习答案自做(第十章)
文章目录10.110.210.310.410.5lua模式匹配[^2]相关函数string.find()string.match()string.gmatch()string.gsub()限定字符仓库10.1#!/usr/bin/env luafunction split(s,d) local word={} d=("[".."^"..d.."]".."+") for w in string.gmatch(s,d) do word[#word+1]=w endreturn worde原创 2021-01-25 21:52:54 · 366 阅读 · 0 评论 -
lua程序设计(第四版)练习答案自做(第九章)
文章目录9.19.29.39.49.5仓库9.1function integral (f,delta) delta=delta or 1e-4 return function (a,b) local sp=0 for i=a,b,delta do sp=sp+f(i) --得到分割成的矩形的高,并把所有的高求和 end return sp*delta --得到所有矩形的面积的和 endendc=integral(math.cos)print(math.sin(10)原创 2021-01-19 23:45:41 · 221 阅读 · 0 评论 -
lua程序设计(第四版)练习答案自做(第八章)
文章目录8.18.28.38.48.58.68.1大概是因为lua不支持swtich结构,并且每个代码块结束都有一个end,如果多次使用ifelse^{else}_{if}ifelse这种写法的话,end的个数太多,影响代码可读性。8.2while true do expressionendfor i=1,math.huge do expressionendrepeat expressionuntil nil::s1:: do expression goto s1en原创 2021-01-11 11:30:20 · 258 阅读 · 0 评论 -
lua程序设计(第四版)练习答案自做(第七章)
文章目录7.17.27.37.47.57.67.77.1见仓库7.2见仓库7.3一次性读取整个文件只与内存限制有关,函数本身并没有限制。7.4见仓库7.5见仓库7.6见仓库7.7无法改变正在运行的文件的路径原创 2021-01-10 10:49:57 · 208 阅读 · 0 评论 -
lua程序设计(第四版)练习答案自做(第六章)
文章目录前言6.16.26.36.46.56.6前言 正文内代码6.1#!/usr/bin/luafunction put(array) print(table.unpack(array))end6.2#!/usr/bin/luafunction foo(...) return select(2,...)end6.3#!/usr/bin/luafunction foo(...) local arg=table.pack(...) arg[arg.n]=nil arg.原创 2021-01-05 22:06:14 · 340 阅读 · 0 评论 -
lua程序设计(第四版)练习答案自做(第五章)
文章目录123456781monday sunday sunday2a a.a a.a.a a.a.a.a都指向同一个表执行a.a.a.a=3即指定该表关键字为字符串“a”的值为3,这时候再执行a.a.a.a会发生错误,因为a.a的值为3.3 t={["\a"]="\007",["\t"]="\009",["\n"]="\010"}4#!/usr/bin/lua--[[假设多项式是3x^5+4x^4-2x^3+8x^2-x^1+9]]function polynomial(x原创 2020-12-19 22:11:09 · 465 阅读 · 3 评论 -
lua程序设计(第四版)练习答案自做(第四章)
文章目录练习4.1练习4.2练习4.3练习4.4练习4.5练习4.6练习4.7练习4.8练习4.9练习4.1string=[==[<![CDATE[ Hello world]]>]==]或者string1="\060\033\091CDATA\091\010\009Hello\032world\010\093\093\062"练习4.2使用\x样式的转义字符序列。练习4.3见仓库练习4.4待完善练习4.5见仓库练习4.6待完善练习4.7见仓库练习4.8原创 2020-12-15 13:37:27 · 581 阅读 · 0 评论 -
lua程序设计(第四版)练习答案自做(第二章)
文章目录正文练习正文八皇后问题的实现练习输出一个解后停止,只需加一个os.exit()即可。代码见仓库原创 2020-12-09 15:13:22 · 163 阅读 · 0 评论 -
lua程序设计(第四版)练习答案自做(第三章)
文章目录正文练习lua运算符优先级正文正文中的代码练习.0e12=0.0e12=0.0e12=00x12=180x12=180x12=180xA=100xA=100xA=100xFFFFFFFF=42949672950xFFFFFFFF=42949672950xFFFFFFFF=42949672950x1p10=1024.00x1p10=1024.00x1p10=1024.00.1e1=1.00.1e1=1.00.1e1=1.00x0.1p1=0.1250x0.1p1=0.1250原创 2020-12-09 15:08:42 · 321 阅读 · 0 评论 -
lua程序设计(第四版)练习答案自做(第一章)
目录前言第一章1.11.21.31.41.51.61.71.8前言代码在gitee仓库内。第一章1.1栈溢出,修改后。1.2略1.3SQL1.4___ _end End NULL1.5false,因为type()返回字符串,与nil不同类型。1.6排除nil之后穷举比较a==nil or a==true or a==false1.7不必要,因为优先级从高等地分别是not and or,但推荐使用括号,增加可读性。1.8见仓库...原创 2020-12-03 22:43:13 · 427 阅读 · 0 评论