题目:输入一个日期。年月日。然后输入第二天的日期。。和星期几。。。
本以为写个小程序很简单。。
后来写了。
才知道。。
要考虑的东西真不少。。
#!/usr/bin/env python
import re
import sys
def nextdate(year, month, day, flag):
year = int(year)
month = int(month)
day = int(day)
leap = leap_year(year)
if leap == -1:
print "ERROR"
sys.exit()
if leap:
the_day_of_month = {"1":31, "2":29, "3":31, "4":30, "5":31, "6":30, "7":31, "8":31, "9":30, "10":31, "11":30, "12":31}
else:
the_day_of_month = {"1":31, "2":28, "3":31, "4":30, "5":31, "6":30, "7":31, "8":31, "9":30, "10":31, "11":30, "12":31}
if day > the_day_of_month.get(str(month)):
print "There is no such day"
return
if flag == 'bc':
day = day + 1
if day == the_day_of_month.get(str(month)) + 1:
if month == 12:
year = year - 1
if year == 0:
year = 1
flag = 'ac'
month = 1
day = 1
else:
month = month + 1
day = 1
elif flag == "ac":
day = day + 1
if day == the_day_of_month.get(str(month)) + 1:
month += 1
day = 1
if month == 13:
year += 1
month = 1
if flag == "bc":
print (("-%d/%d/%d") % (year, month, day))
else:
print (("%d/%d/%d") % (year, month, day))
week(year, month, day, flag)
return
def is_valid_day(year, month, day):
if type(year) != type(123):
print "Argument type error"
return "error"
if month < 1 or month > 12:
return False
leap = leap_year(year)
if leap == 1:
the_day_of_month = {"1":31, "2":29, "3":31, "4":30, "5":31, "6":30, "7":31, "8":31, "9":30, "10":31, "11":30, "12":31}
elif leap == 0:
the_day_of_month = {"1":31, "2":28, "3":31, "4":30, "5":31, "6":30, "7":31, "8":31, "9":30, "10":31, "11":30, "12":31}
else:
print "leap_year function error"
return "error"
if day < the_day_of_month:
return True
else:
return False
def leap_year(year):
if type(year) != type(123):
return -1
if year < 0:
year = -year
if year % 4 == 0:
if year % 100 != 0:
return 1
else:
if year % 400 == 0:
return 1
else:
return 0
else:
return 0
def week(year, month, day, flag):
now_year = 2011
now_month = 1
now_day = 1
now_week = 6
if now_year < year:
while now_year != year:
leap = leap_year(now_year)
if leap:
now_year += 1
if now_year == 0:
now_year = 1
now_week += 2
else:
now_year += 1
if now_year == 0:
now_year = 1
now_week += 1
leap = leap_year(year)
if leap == 1:
the_day_of_month = {"1":31, "2":29, "3":31, "4":30, "5":31, "6":30, "7":31, "8":31, "9":30, "10":31, "11":30, "12":31}
elif leap == 0:
the_day_of_month = {"1":31, "2":28, "3":31, "4":30, "5":31, "6":30, "7":31, "8":31, "9":30, "10":31, "11":30, "12":31}
while now_month != month:
if now_month < month:
while True:
now_day += 1
now_week += 1
if now_day == the_day_of_month[str(now_month)] + 1:
now_day = 1
now_month += 1
break
if now_month > month:
while True:
now_day -= 1
now_week -= 1
if now_day == 0:
now_day = the_day_of_month[str(now_month -1)]
now_month -= 1
break
while now_day != day:
if now_day < day:
now_day += 1
now_week += 1
if now_week == 8:
now_week = 1
if now_day > day:
now_day -= 1
now_week -= 1
while now_week <= 0:
now_week += 7
if now_week > 7:
now_week %= 7
if now_week == 0:
now_week = 7
print str(now_week)
return
else:
if flag == 'bc':
year = -year
while now_year != year:
leap = leap_year(now_year)
if leap == 1:
now_year -= 1
now_week -= 2
else:
now_year -= 1
now_week -= 1
leap = leap_year(year)
if leap == 1:
the_day_of_month = {"1":31, "2":29, "3":31, "4":30, "5":31, "6":30, "7":31, "8":31, "9":30, "10":31, "11":30, "12":31}
elif leap == 0:
the_day_of_month = {"1":31, "2":28, "3":31, "4":30, "5":31, "6":30, "7":31, "8":31, "9":30, "10":31, "11":30, "12":31}
while now_month != month:
if now_month < month:
while True:
now_day += 1
now_week += 1
if now_day == the_day_of_month[str(now_month)] + 1:
now_day = 1
now_month += 1
break
if now_month > month:
while True:
now_day -= 1
now_week -= 1
if now_day == 0:
now_month -= 1
now_day = the_day_of_month[str(now_month)]
break
while now_day != day:
if now_day < day:
now_day += 1
now_week += 1
if now_day > day:
now_day -= 1
now_week -= 1
while now_week <= 0:
now_week += 7
if now_week > 7:
now_week %= 7
if now_week == 0:
now_week = 7
print str(now_week)
return
def main():
while True:
date = raw_input("please input date:(format is (-)year/month/day):\n")
match = re.match("^(-)?(\d+)\/(\d{1,2})\/(\d{1,2})$", date)
if match != None:
if match.group(1) == '-':
flag = 'bc'
else:
flag = 'ac'
if is_valid_day(int(match.group(2)), int(match.group(3)), int(match.group(4))):
break
nextdate(int(match.group(2)), int(match.group(3)), int(match.group(4)), flag)
return
if __name__ == '__main__':
main()