欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

《Lua程序设计(第4版)》:第7章练习答案

程序员文章站 2024-03-17 23:05:46
...

练习7-1、2

function exercise7_1and2(filename1,filename2)
  local inputF,outputF
  if filename1 and filename2 then
    inputF=io.open(filename1,"r")
    local filetest=io.open(filename2,"r")
    if filetest then
      io.output():write(filename2," is exist, rewrite it? (Y/N)\n")
      local isexist
      repeat
        isexist=io.input():read(1)
      until isexist=="Y" or isexist=="N"
      if isexist=="N" then
        return -1
      else
        filetest:close()
      end
    end
    outputF=io.open(filename2,"w")
  elseif filename1 then
    inputF=io.open(filename1,"r")
    outputF=io.stdout
  else
    inputF=io.stdin
    outputF=io.stdout
  end
  repeat
    local inx,sp=1,false
    local tab={}
    repeat
      tab[inx]=inputF:read(1)
      inx=inx+1
    until (tab[inx-1]=="\n" or tab[inx-1]==nil)
    if tab[inx-1]==nil then
      sp=true
    end
    tab[inx-1]=nil
    table.sort(tab)
    outputF:write(table.concat(tab),"\n")
  until sp
  inputF:close()
  outputF:close()
end

exercise7_1and2("stdin.txt","stdout.txt")

先将一行的全部字符存取一行的字符至序列,利用table.sort排序后输出

练习7-3

function exercise7_3(fileinput,fileoutput)
  local outputF,time
  ---[==[test for type
  outputF=io.open(fileoutput,"w")
  time=os.time()
  for i in io.lines(fileinput,1) do
    outputF:write(i)
  end
  print(os.time()-time)
  outputF:close()
  --]==]
  ---[==[test for line
  outputF=io.open(fileoutput,"w")
  time=os.time()
  for i in io.lines(fileinput,"L") do
    outputF:write(i)
  end
  print(os.time()-time)
  outputF:close()
  --]==]
  ---[==[test for block/chuck
  outputF=io.open(fileoutput,"w")
  time=os.time()
  for i in io.lines(fileinput,2^13) do
    outputF:write(i)
  end
  print(os.time()-time)
  outputF:close()
  --]==]
  ---[==[test for wholefile
  local inputF=io.open(fileinput,"r")
  outputF=io.open(fileoutput,"w")
  time=os.time()
  local iput=inputF:read("a")
  outputF:write(iput)
  print(os.time()-time)
  inputF:close()
  outputF:close()
  --]==]
end

BigFile=io.open("BigFile","w")
BigFile:write(string.rep("a",2147483647))
BigFile:close()
exercise7_3("BigFile","stdout.txt")

最长字符串支持(2^31-1 = 2147483647字节),文件大小在该长度下(笔记本,速度仅供参考)

按字节:2234s

按行:162s

按块(每个块8KB):28s

一次性读取整个文件:30s

最后一种一次性读取整个文件最大支持大小,就是字符串最大长度。

练习7-4

function exercise7_4(inputfile)
  local inputF=io.open(inputfile,"r")
  local seeknow=0
  inputF:seek("end")
  repeat
    inputF:seek("cur",-1)
    local linecheck=inputF:read(1)
    seeknow=inputF:seek("cur",-1)
  until linecheck=="\n" or seeknow==0
  if seeknow~=0 then
      inputF:seek("cur",1)
  end
  local ansstr=inputF:read("a")
  inputF:close()
  return ansstr
end

print(exercise7_4("stdin.txt"))

利用seek从文件尾部向上查找换行,注意文件只有一行的情况!

练习7-5

function exercise7_5(inputfile,cnt)
  cnt=cnt or 1
  local inputF=io.open(inputfile,"r")
  local spcnt,seeknow=0,0
  inputF:seek("end")
  repeat
    inputF:seek("cur",-1)
    local linecheck=inputF:read(1)
    seeknow=inputF:seek("cur",-1)
    if linecheck=="\n" then
      spcnt=spcnt+1
      inputF:seek("cur",-1)
    end
    --print(linecheck)
  until spcnt==cnt or seeknow==0 
  if seeknow~=0 then
    inputF:seek("cur",2)
  end
  local ansstr=inputF:read("a")
  inputF:close()
  return ansstr
end
print(exercise7_5("stdin.txt",2))

同上,默认行数为1

练习7-6

function exercise7_6()
  --os.execute("copy /V /Y /B stdin.txt copy.txt")
  --os.execute("copy copy.txt+stdin.txt tog.txt /B")
  --os.execute("del t**")
  --os.execute("fc /B copy.txt stdin.txt")

  --os.execute("dir /B /O:S")
  --os.execute("mkdir dir")
  --os.execute("rmdir dir")
  
  --io.popen("dir /B /O:S","w")
  --io.popen("mkdir dir","w")
  --io.popen("rmdir dir","w")
end
exercise7_6()

练习7-7

不能,不可以改变正在运行文件的路径

END