问题描述:
某人从2010年1月1日起开始“三天打鱼两天晒网”,问这个人在以后的某一天中是“打鱼”还是“晒网”。
解题思路:
通过给定的时间结点判断从指定日期与2010年1月1日计算出时间差。每一个打鱼晒网周期为5天,用计算出来的时间差除以每一个周期所得的余数即可判定当日到底是打鱼还是晒网。
解题步骤:
-
通过输入或者读取文件中的字符串类型指定日期(String),将字符串进行适当处理(转变为int类型)将年、月、日分别提取出来
-
根据年份判断该年是否为闰年,进而判断每个月所对应的天数是否正确(小月天数为1-30,大月天数为1-31,闰年2月为29天,平年2月为28天)
-
计算从当前日期到2010年1月1日相差的天数
-
用总天数处以5所得的余数判断当天是在打鱼还是晒网(余数为1,2,3为打鱼,余数为0,4为晒网)
-
根据以上结果得出结论,并将结果保存到文件中
详细代码:
判断该年是否为闰年
#判断是否为闰年
def isBissextile(year):
if (year%4==0 and year%100!=0) or year%400==0:
return True
else:
return False
检查日期是否正确
#检查日期是否合理,否则结束程序
def checkDate(year,month,day):
if year<2010:
print("%d-%d-%d日期日期不得小于2010年"%(year,month,day))
quit()
##首先判断是否为闰年,并作标记用于检验2月分日期
if isBissextile(year):
leapyear=True
else:
leapyear=False
##其次检查输入的月份和日期是否在合理范围
if (month in range(1,13)) or (day in range(1,32)):
##最后再检查每个月份的天数是否合理
if month in(1,3,5,7,8,10,12) :
if day<1 or day>31:
print("%d-%d-%d日期有误,请检查后重试"%(year,month,day))
exit(0)
else:
if day<1 or day>30:
print("%d-%d-%d日期有误,请检查后重试"%(year,month,day))
exit(0)
else:
print("%d-%d-%d日期有误,请检查后重试"%(year,month,day))
exit()
if month==2:
if leapyear==False :
if day<1 or day>28:
print("%d-%d-%d日期有误,请检查后重试"%(year,month,day))
exit(0)
else:
if day<1 or day>29:
print("%d-%d-%d日期有误,请检查后重试"%(year,month,day))
exit(0)
计算指定日期到2010-1-1的天数
#计算从2010-1-1到指定日期的天数,并返回总天数
def getDays(year,month,day):
total_days=0
##判断该年是否为闰年,并加上相应的天数
for count in range(2010,year):
if isBissextile(count):
total_days+=366
else:
total_days+=365
days1=(31,28,31,30,31,30,31,31,30,31,30,31)#平年各月天数
days2=(31,29,31,30,31,30,31,31,30,31,30,31)#闰年各月天数
#判断如年后加上相应月数的天数
if isBissextile(year):
for each in range(0,month-1):
total_days+=days2[each]
else:
for each in range(0,month-1):
total_days+=days1[each]
#加上天数
total_days+=day
return total_days
判断当天具体的事情
#判断指定日期是在打鱼还是晒网,并返回字符串结果
def isDoing(year,month,day):
checkDate(year,month,day)
days=getDays(year,month,day)
last=days%5
print("2010年1月1日距%d年%d月%d日相隔%d天"%(year,month,day,days))
print("余数为%d:"%(last),end="")
if last in (1,2,3):
print("该天打鱼\n")
return str(year)+"-"+str(month)+"-"+str(day)+"打鱼\n"
else:
print("该天晒网\n")
return str(year)+"-"+str(month)+"-"+str(day)+"晒网\n"
从文件中得到日期(进一步要求)
对文件的读写操作
Python中文件的读取和写入https://blog.csdn.net/sinat_34474705/article/details/77389258
从文件中读入日期
##从文件中读入日期,并返回日期数组
def fileRead():
f=open("date.txt","r")#以只读方式读取文件
context=[]
while True:##将文件读取结果放入到context数组中
each=f.readline()
if each :
context.append(each.replace("\n",""))
else:
break
f.close()
print("读取文件结果:")
for each in context:
print(each)
print()
return context
对日期进行处理,提取年月日
##对日期进行处理,提取其中的年月日
import re
def getDate(context):
year=[]
month=[]
day=[]
for str_date in context:
result1=re.search(r"\d{4}-",str_date)#使用正则表达式提取字符串日期中的年份
result2=re.search(r"-\d{1,2}-",str_date)#使用正则表达式提取字符串日期中的月份
result3=re.search(r"-\d{1,2}$",str_date)#使用正则表达式提取字符串日期中的日期
try:
##去除年、月、日中的其他字符,并转换为int类型
year.append(int(result1.group(0).replace("-","")))
month.append(int(result2.group(0).replace("-","")))
day.append(int(result3.group(0).replace("-","")))
## date=datetime.datetime.
except AttributeError:
print("%d-%d-%d日期有误,请检查后重试"%(year,month,day))
date=[year,month,day]
return date
运行结果
读取文件结果:
2010-2-28
2014-5-30
2016-6-21
2018-8-29
2010年1月1日距2010年2月28日相隔59天
余数为4:该天晒网
2010年1月1日距2014年5月30日相隔1611天
余数为1:该天打鱼
2010年1月1日距2016年6月21日相隔2364天
余数为4:该天晒网
2010年1月1日距2018年8月29日相隔3163天
余数为3:该天打鱼
源代码下载:https://download.csdn.net/download/xust_kevin/10633988