定义一个日期类CDate,要求满足如下条件

1 有三个数据成员:year month date
2 有设置日期的成员函数
3有使用格式 年/月/日 输出日期的成员函数。
4有计算当前日期加一天后显示新日期的成员函数
5 使用类的实例测试日期的显示和计算

#! usr/bin/env python
# -*- coding: utf-8 -*-

import sys

class CDate:
days = (0,30,28,31,30,31,30,31,31,30,31,30,31)
def __init__(self,y,m,d):
self.year = y
self.month = m
self.day = d
def setDate(self,y,m,d):
self.year = y
self.month = m
self.day = d
def printDate(self):
print '%d/%d/%d' % (self.year,self.month,self.day)
def isLeapYear(self):
return (self.year % 400 == 0) or (self.year % 4 == 0 and self.year % 100 != 0)
def tomorrow(self):
d = self.days[self.month]
if self.month == 2 and self.isLeapYear() : d += 1 
self.day += 1
if self.day > d :
self.month += 1
self.day = 1
if self.month > 12 :
self.year += 1
self.month = 1
return self

if __name__ == '__main__' :
CDate(2000,2,28).tomorrow().printDate()
CDate(2000,2,29).tomorrow().printDate()
CDate(2000,12,31).tomorrow().printDate()
CDate(2001,2,28).tomorrow().printDate()
CDate(2000,3,5).tomorrow().printDate()

追问

很感谢 但是我看不懂 我刚学习C++ 您能不能帮我编个基础的

追答

我晕,他说字数超过限制了,我弄在附件里面了!是C++的!


温馨提示:答案为网友推荐,仅供参考