Lua語言,兩個年份相差的天數__Lua語言

來源:互聯網
上載者:User

要求計算該日期與1949年10月1日距離多少天

例如:

使用者輸入了:1949-10-2

程式輸出:1

使用者輸入了:1949-11-1

程式輸出:31

總共是寫了兩個類,然後代碼微亂:

YearAndMonth:

require("YearPrint")

local nYear = 1949
local nMonth = 10
local nDay =1

--[[--
--@param #number year 傳遞過來的參數是年數
--@param #number month 傳遞過來的參數是月份
--@param #number day 傳遞過來的參數是日期
--@return #day 返回的是這一年的天數
--]]

local function getDays(year,month,day)
local temp ={31,28,31,30,31,30,31,31,30,31,30,31}
--判斷是否是閏年,如果為閏年,改變2月是29,否則改變2月是28
if ((year%4==0 and year%100 ~= 0) or year%400==0) then
temp[2] = 29
else
temp[2] = 28
end
for i = 1,month-1 do
day = day + temp[i];
end
-- 返回的結果是這一年到月份的天數
return day;
end

--[[--
--@param #number year 傳遞多來的年數
--@return #number 判斷年數是否為閏年,如果是閏年,返回366天,反之則返回365天
--]]
local function Days(year)
--判斷是否閏年,如果閏年,返回366天,如果不是閏年,返回365天
return ((year%4==0 and year%100 ~=0) or year%400==0) and 366 or 365;
end

--[[--
--@param #number sum 兩個年月份相差的天數
--@param #number year 輸入的年份
--@param #number month 輸入的月份
--@param #number day 輸入的日期
--@param #number nYear 初始的年份
--@param #number nMonth 初始的月份
--@param #number nDay 初始的日期
--@return #number sum是兩個年份相差的天數
--]]
local function sumDay(year,month,day)
  local sum = 0;
-- 小於nYear年
if year < nYear then
for x = year,nYear - 1 do
-- 傳遞年數過去,number型
sum = sum + Days(x);
end
-- 傳遞的是number型
sum = sum + getDays(nYear,nMonth,nDay);
sum = sum - getDays(year,month,day);
elseif year == nYear then
-- 如果是同一年,判斷月份的大小,然後去絕對值
sum = math.abs(getDays(year,month,day)-getDays(nYear,nMonth,nDay));
else
-- 大於nYear年
for i = nYear + 1 ,year do
sum = sum + Days(i);
end
sum = sum + getDays(year,month,day);
sum = sum - getDays(nYear,nMonth,nDay);

end
print("相差天數結果:" ..sum);
return sum
end

--[[--
--@param #class YearPrint YMDPrint算是檔案加密,PrintYearMonthDay()輸入年月日的方法
--@param #table data 本地化的一個數組,並接受了YearPrint累裡的資料
--@param #number sum 兩個年月份相差的天數
--@param #number year 輸入的年份
--@param #number month 輸入的月份
--@param #number day 輸入的日期
--]]

local function isLeapYear()
    local data = {}
data = YMDPrint.PrintYearMonthDay()

local year = tonumber(data.Year);
local month = tonumber(data.Month);
local day = tonumber(data.Day);
    sumDay(year,month,day)
end

--[[--
--@isLeapYear是計算年月份天數的方法
--]]
function main()
      isLeapYear()
end

main()
第二個類YearPrint:

module("YMDPrint",package.seeall);

-- isFlag初始化等於false,判斷年份是否為閏年,如果為閏年則為true,否則為false
local isFlag = false
--[[--
--@param #table adta 存年月日的一個數組
--@param #number data.Year 輸入的年份
--@param #number data.Month 輸入的月份
--@param #number data.Day 輸入的日期
--@return #table data  返回的是一個數組,儲存了輸入的年月日


--]]
function PrintYearMonthDay()
local data = {};
print("請輸入年份(0-9999),然後按斷行符號:")
data.Year = io.read()
if(tonumber(data.Year) ~= nil) then
if (tonumber(data.Year) > 0 and tonumber(data.Year) < 9999) then
if((tonumber(data.Year)%4==0 and tonumber(data.Year)%100 ~=0) or tonumber(data.Year)%400==0) then
isFlag = true
end
print("請輸入月份,然後按斷行符號:")
data.Month = io.read()
else
print("輸入的年份是1-9999,請重新輸入。")
PrintYearMonthDay()
end
else
print("格式錯誤,請重新輸入。")
PrintYearMonthDay()
end
if(tonumber(data.Month) ~= nil) then
if (tonumber(data.Month) > 0 and tonumber(data.Month) < 13) then
print("請輸入日期,然後按斷行符號:")
data.Day = io.read()
else
print("輸入的月份是1-12月,請重新輸入。")
end
if (tonumber(data.Day) > 0 and tonumber(data.Day) < 32) then
if (isFlag == true and tonumber(data.Month) == 2) then
if(tonumber(data.Day) == 29) then
print("日期輸入正確。")
else
print("是閏年,應該是29天,輸入錯誤,請重新輸入");
PrintYearMonthDay();
end
end


if(not isFlag and tonumber(data.Month) == 2) then
if(tonumber(data.Day) <= 28) then
print("日期輸入正確。\n")
else
print("不是閏年2月,應該是28天,不能大於,輸入錯誤,請重新輸入:")
PrintYearMonthDay();
end
end
else
   print("輸入的日期應在1-31之間。")
   PrintYearMonthDay();
   end
else
      print("格式錯誤,請重新輸入。")
      PrintYearMonthDay()
end
return data;
end

如果有錯誤,歡迎大家共同討論

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.