先將一行的全部字元訪問一行的字元至序列,利用table.sort排序後輸出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 i***ist
repeat
i***ist=io.input():read(1)
until i***ist=="y" or i***ist=="n"
if i***ist=="n" then
return -1
else
filetest:close()
endend
outputf=io.open(filename2,"w")
elseif filename1 then
inputf=io.open(filename1,"r")
outputf=io.stdout
else
inputf=io.stdin
outputf=io.stdout
endrepeat
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
endtab[inx-1]=nil
table.sort(tab)
outputf:write(table.concat(tab),"\n")
until sp
inputf:close()
outputf:close()
endexercise7_1and2("stdin.txt","stdout.txt")
最長字串支援(2^31-1 = 2147483647位元組),檔案大小在該長度下(筆記本,速度僅供參考)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)
endprint(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)
endprint(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)
endprint(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()
--]==]
endbigfile=io.open("bigfile","w")
bigfile:write(string.rep("a",2147483647))
bigfile:close()
exercise7_3("bigfile","stdout.txt")
按位元組:2234s
按行:162s
按塊(每個塊8kb):28s
一次性讀取整個檔案:30s
最後一種一次性讀取整個檔案最大支援大小,就是字串最大長度。
利用seek從檔案尾部向上查詢換行,注意檔案只有一行的情況!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)
endlocal ansstr=inputf:read("a")
inputf:close()
return ansstr
endprint(exercise7_4("stdin.txt"))
同上,預設行數為1function 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)
endlocal ansstr=inputf:read("a")
inputf:close()
return ansstr
endprint(exercise7_5("stdin.txt",2))
不能,不可以改變正在執行檔案的路徑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")
endexercise7_6()
《Lua程式設計 第4版 》 第9章練習答案
lua的閉包真的很強大!function derivative f,delta delta delta or 1e 5 return function x return f x delta f x delta endendfunction integral f,delta delta delta o...
《Lua程式設計 第4版 》 第5章練習答案
monday sunday sunday 一樣,都指向該錶。a.a.a.a 3,執行的是該錶的索引 a 賦值為3,之後的a.a.a.a將會引發異常,因現a.a 3,而非表。在方括號裡寫索引值 tab for i,j in pairs tab do io.write i,j,n endfunction...
Lua程式設計第4版第6章課後練習答案
6.1 略 6.2 用lselect 2,函式 6.3function f63 local t table.pack t t.n nil t.n t.n 1return table.unpack t end print f63 2 3,4 5 6.4 說下思路 pair每乙個元素,把每個元素都隨機從...