7.11

<pre name="code" class="cpp">#include<iostream>  
using namespace std;
class Date {        
public: 
	Date(){}
	Date(int m,int d,int y)
	{
		month=m;
		day=d;
		year=y;
	}
	void print()
	{
		cout<<year<<"."<<month<<"."<<day<<endl;
	}
	Date operator +(int);
private:
	int month,day,year;
};
static int days[2][12]={{31,28,31,30,31,30,31,31,30,31,30,31},{31,29,31,30,31,30,31,31,30,31,30,31}};
int a(int year)
{
	if((year%4==0&&year%100!=0)||(year%400==0))
		return 1;
	else return 0;
}
Date Date::operator+(int m)
{
	int b;
	b=0;
	b=a(this->year);
	m+=this->day; 
	while (m>days[b][this->month-1])      
	{ 
		m-=days[b][this->month-1];       
		if (++(this->month)==13)  
		{ 
			this->month=1;     //当前的对象(this所指的)的成员month,被赋值1
			(this->year)++;    //当前的对象(this所指的)的成员year,加1
			b=a(this->year);     
		}       
	} 
	this->day=m;          //当前的对象(this所指的)的成员day,被赋值为m
	return *this;         //返回类对象本身
} 
int main() 
{ 
	Date d1(2,28,2012),d2;       
	d1.print();       
	d2=d1+400;       
	d2.print();   
	return 0; 
} 



                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值