11
--exercise11
local counter={}
for line in io.lines("redundant.txt","l") do
for word in string.gmatch( line,"%w+" ) do
counter[word]=-1
end
end
for line in io.lines("stdin.txt","l") do
for word in string.gmatch( line,"%w+" ) do
if #word<4 or counter[word]==-1 then
goto countinue
end
counter[word]=(counter[word] or 0)+1
::countinue::
end
end
local words={}
for w in pairs(counter) do
if counter[w]==-1 then
goto countinue
end
words[#words+1]=w
::countinue::
end
table.sort(words,function(w1,w2)
return counter[w1]>counter[w2] or counter[w1]==counter[w2] and w1<w2
end)
local n=math.min(tonumber(arg[1]) or math.huge,#words)
for i=1,n do
io.write(words[i],"\t",counter[words[i]],"\n")
end
12章
日期秒数,1970年1月1日8:00(WINDOWS10,MINGW-gcc编译)到当前的秒数
日期表,包含year\month\day\hour\min\sec\wday\yday\isdst,wday表示本周总第几天(第一天是星期天),yday表示当年中第几天,isdst是夏时令时为true
os.time() 返回当前日期秒数
os.time(date) date为时间表,year、month、day不可忽略,hour、min、sec默认12:00:00。
os.date("*t") 返回当前时间表(isdst总是为false)
os.date("*t",sec) sec为时间秒数,返回此时间秒数的时间表(isdst总是为false)
os.date指示符
%a | 星期简写 |
%A | 星期全名 |
%b | 月份简写 |
%B | 月份全名 |
%c | 日期和时间 |
%d | 一个月中第几天(01-31) |
%H | 24小时制小时 |
%I | 12小时制小时 |
%j | 一年中第几天(001-366) |
%m | 月份(01-12) |
%M | 分钟(00-59) |
%p | am/pm |
%S | 秒数(00-61,允许闰秒) |
%w | 星期(0-6=Sunday-Saturday) |
%W | 一年中第几周(00-53) |
%x | 日期 |
%X | 时间 |
%y | 两位数的年份 |
%Y | 完整年份(2018) |
%z | 时区(中国标准时间) |
%% | % |
当 os.date("!*t") 以 ! 开头时,时间表将以UTC时间返回。
-->UTC是GMT在原子时TAI的基础上校正而得,比GMT更加精确,每过几年通过闰秒调整一次。
os.difftime 计算两个日期秒数的差值。
os.clock 返回程序消耗的CPU时间,单位是s,但通常可以精确到微秒;常用来测试程序时间性能。
12.1
function exercise12_1(tab)
tab.month=tab.month+1
return os.date("*t",os.time(tab))
end
print(exercise12_1(os.date("*t")).month)
12.2
function exercise12_2(tab)
return math.tointeger(os.date("%w",os.time(tab))+1)
end
print(exercise12_2(os.date("*t")))
12.3
function exercise12_3(tab)
local temp={}
for i,j in pairs(tab) do
temp[i]=j
end
temp.hour=0
temp.min=0
temp.sec=0
return math.tointeger(os.difftime(os.time(tab),os.time(temp)))
end
print(exercise12_3(os.date("*t")))
12.4
function exercise12_4(year)
local nowdate={year=year,month=1,day=1}
local temp=math.tointeger(os.date("%w",os.time(nowdate)))
local sub=5-temp
if sub<0 then
sub=7-(temp-5)
end
return sub+1
end
print(exercise12_4(2017))
12.5
function exercise12_5(date1,date2)
return os.difftime(os.time(date1),os.time(date2))/(24*3600)
end
temp1=os.date("*t")
temp2=os.date("*t")
temp2.year=temp2.year+2
print(exercise12_5(temp2,temp1))
12.6
function exercise12_6(date1,date2)
local allmonth=date2.year*12+date2.month
return date1.year*12+date1.month-allmonth
end
temp1=os.date("*t")
temp2=os.date("*t")
temp2.year=temp2.year+2
print(exercise12_6(temp2,temp1))
12.7
function exercise12_7()
local date={year=2018,month=8,day=31}
date.month=date.month+1
date=os.date("*t",os.time(date))
date.day=date.day+1
date=os.date("*t",os.time(date))
print(date.month,date.day)
local date={year=2018,month=8,day=31}
date.day=date.day+1
date=os.date("*t",os.time(date))
date.month=date.month+1
date=os.date("*t",os.time(date))
print(date.month,date.day)
return
end
exercise12_7()
不同,月份天数不同就会导致不一样,如8月31日,结果是10月1日和10月2日。
12.8
function exercise12_8()
return os.date("%z")
end
print(exercise12_8())