万年历的部分功能

来源:互联网 发布:mmd r18动作数据 编辑:程序博客网 时间:2024/06/04 18:36


python源代码:

#Print the calendar for a month in a yeardef printMonth(year, month):    printMonthTitle(year, month)    printMonthBody(year, month)#Print the month title, eg: May 1999def printMonthTitle(year, month):    print '       ', getMonthName(month), ' ', year     print '-----------------------------------'    print ' Sun  Mon  Tue  Wed  Thu  Fri  Sat'#Print month bodydef printMonthBody(year, month):    startDay = getStartDay(year, month)    numberOfDaysInMonth = getNumberOfDaysInMonth(year, month)    i = 0    for i in range(startDay): #Pad space before the first day of the month        print '    ',    for i in range(1, numberOfDaysInMonth + 1):        print format(i, "4d"),        if (i + startDay) % 7 == 0: #jump to the new line            print ''#Get the English name for the monthdef getMonthName(month):    if month == 1:        monthName = "January"    elif month == 2:        monthName = "February"    elif month == 3:        monthName = "March"    elif month == 4:        monthName = "April"    elif month == 5:        monthName = "May"    elif month == 6:        monthName = "June"    elif month == 7:        monthName = "July"    elif month == 8:        monthName = "August"    elif month == 9:        monthName = "September"    elif month == 10:        monthName = "October"    elif month == 11:        monthName = "November"    else:        monthName = "December"    return monthNamedef getStartDay(year, month):    START_DAY_FOR_JAN_1_1800 = 3    totalNumberOfDays = getTotalNumberOfDays(year, month)    return (totalNumberOfDays + START_DAY_FOR_JAN_1_1800) % 7#Get total number days since January 1, 1800def getTotalNumberOfDays(year, month):    total = 0    for i in range(1800, year):        if isLeapYear(i):            total = total + 366        else:            total = total + 365    for i in range(1, month):        total = total + getNumberOfDaysInMonth(year, i)    return total#Get the number of days in a monthdef getNumberOfDaysInMonth(year, month):    if(month == 1 or month == 3 or month == 5 or month == 7 or month == 8 or month== 10 or month == 12):        return 31    if month == 4 or month == 6 or month == 9 or month == 11:        return 30    if month == 2:        if isLeapYear(year):            return 29        else:            return 28    return 0#Determine if it is a leap yeardef isLeapYear(year):    return year%400==0 or (year%4==0 and year%100!=0)def main():    #Prompt the user to enter the year and month    year = eval(raw_input("Enter year(example, 2001): "))    month = eval(raw_input("Enter month(1~12): "))    #Print calendar for the month of the year    printMonth(year, month)#call the main functionmain()


0 0
原创粉丝点击