6-48.日期类的设计与实现

使用重载运算符(++,+=,<<等)实现日期类的操作。功能包括:
1)设置日期,如果日期设置不符合实际,则设置为默认日期(1900年1月1日)
2)在日期对象中向日期添加1或者加若干天(加入日期值后根据实际的日期进行月份、年份的变化)
3)重载流插入运算符进行

输入样例:

在这里给出一组输入。例如:

13 38 100

12 31 2009

2 28 2000

输出样例:

在这里给出相应的输出。例如:

d1 is January 1, 1900

d2 is December 31, 2009

d1 += 7 is January 8, 1900

d2 is December 31, 2009

++d2 is January 1, 2010

Testing the prefix increment operator:

d3 is February 28, 2000

++d3 is February 29, 2000

d3 is February 29, 2000

Testing the postfix increment operator:

d3 is February 29, 2000

d3++ is February 29, 2000

d3 is March 1, 2000

日期的输出,其中月份要用名称表示

代码实现:

#include  <string>
#include  <iostream>
using  namespace  std;

/*  请在这里填写答案  */
class MyDate{
	private:
		int y,m,d;
		int flag1,flag2,flag3;
	public:
		void setDate(int ma,int da,int ya){
			y = ya;
			m = ma;
			d = da;
		}
		friend ostream& operator <<(ostream &out,MyDate &date);
		
		void flag(){
				if((y%4==0&&y%100!=0)||y%400==0){		//闰年 
				flag1=1;
			}else{
				flag1=0;
			}
			if(m<=7){
				switch(m%2){						//大月小月 
					case 0 :
						flag2 = 0;
						break;
					case 1:
						flag2 = 1;
						break;
				}
			}else{
				switch(m%2){
					case 0:
						flag2 = 1;
					case 1:
						flag2 = 0; 
				}
			}
			if(m==2)flag3=1;						//二月 
			else flag3 = 0;
			
		}
		void update(){
			int brk=0;
			int cunt=0;
			while(cunt<15){
				flag();	
					if(d>28){
						if(d==29){
							if(flag1==0&&flag3==1){			//平年且2月 
								d-=28;
								m+=1;
							}else brk=1;
						}else if(d==30){
							if(flag1==1&&flag3==1){			//闰年且2月 
								d-=29;
								m+=1;
							}else brk=1;
						}else if(d==31){
							if(flag2==0){					//若为小月 
								d-=30;
								m+=1; 
							}else brk=1;
						}else{								//此时无论如何要进 1
							d-=31;
							m+=1;
						} 
						
					}else{
						brk=1;
					}
					if(m>12){
						m-=12;
						y+=1;
					}
					if(brk==1)break;
					cunt++;
			}
		}
		MyDate& operator+=(int n){
			this->d+=n;
			update();
			return *this;
		}
		MyDate& operator++(){
			d++;
			update();
			return *this;
		}
		MyDate& operator++(int){
			MyDate *rs = new MyDate(*this);
			++d;
			update();
			return *rs;
		}	
};
ostream& operator <<(ostream &out,MyDate &date){
	string month;
	if(date.y<1900||date.m<=0||date.m>12||date.d<0||date.d>31){
		date.y=1900;
		date.m=1;
		date.d=1;
		out<<"January 1, 1900"<<endl;
		return out;
	}
	switch(date.m){
		case 1:
			month = "January";break;
		case 2:
			month = "February";break;
		case 3:
			month = "March";break;
		case 4:
			month = "April";break;
		case 5:
			month = "May";break;
		case 6:
			month = "June";break;
		case 7:
			month = "July";break;
		case 8:
			month = "Augest";break;
		case 9:
			month = "September";break;
		case 10:
			month = "October";break;
		case 11:
			month = "November";break;
		case 12:
			month = "December";break;
	}
	out<<month<<" "<<date.d<<","<<date.y<<endl;
	return out;
}

int  main()
{
        int  m,d,y;
        MyDate  d1,d2,d3;
        cin>>m>>d>>y;
        d1.setDate(m,d,y);
        
        cin>>m>>d>>y;
        d2.setDate(m,d,y);
        
        cin>>m>>d>>y;
        d3.setDate(m,d,y);
        
        cout  <<  "d1  is  "  <<  d1  <<  "\nd2  is  "  <<  d2;
        cout  <<  "\n\nd1  +=  7  is  "  <<  (  d1  +=  7  );
        cout  <<  "\n\n  d2  is  "  <<  d2;
        cout  <<  "\n++d2  is  "  <<  ++d2;
        cout  <<  "\n\nTesting  the  prefix  increment  operator:\n"<<  "  d3  is  "  <<  d3  <<  endl;
        cout  <<  "++d3  is  "  <<  ++d3  <<  endl;
        cout  <<  "  d3  is  "  <<  d3;
        cout  <<  "\n\nTesting  the  postfix  increment  operator:\n"<<  "  d3  is  "  <<  d3  <<  endl;
        cout  <<  "d3++  is  "  <<  d3++  <<  endl;
        cout  <<  "  d3  is  "  <<  d3  <<endl;
}  

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值