一个小程序

题目:输入一个日期。年月日。然后输入第二天的日期。。和星期几。。。

本以为写个小程序很简单。。

后来写了。

才知道。。

要考虑的东西真不少。。

#!/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()


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值