合肥网站建设哪家公司好,在北京做家教的网站,锦州网站推广,深圳网站制作公司报价使用重载运算符#xff08;#xff0c;#xff0c;等#xff09;实现日期类的操作。功能包括#xff1a; 1#xff09;设置日期#xff0c;如果日期设置不符合实际#xff0c;则设置为默认日期#xff08;1900年1月1日#xff09; 2#xff09;在日期对象中…使用重载运算符等实现日期类的操作。功能包括 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%40y%100!0)||y%4000){ //闰年 flag11;}else{flag10;}if(m7){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(m2)flag31; //二月 else flag3 0;}void update(){int brk0;int cunt0;while(cunt15){flag(); if(d28){if(d29){if(flag10flag31){ //平年且2月 d-28;m1;}else brk1;}else if(d30){if(flag11flag31){ //闰年且2月 d-29;m1;}else brk1;}else if(d31){if(flag20){ //若为小月 d-30;m1; }else brk1;}else{ //此时无论如何要进 1d-31;m1;} }else{brk1;}if(m12){m-12;y1;}if(brk1)break;cunt;}}MyDate operator(int n){this-dn;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.y1900||date.m0||date.m12||date.d0||date.d31){date.y1900;date.m1;date.d1;outJanuary 1, 1900endl;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;}outmonth date.d,date.yendl;return out;
}int main()
{int m,d,y;MyDate d1,d2,d3;cinmdy;d1.setDate(m,d,y);cinmdy;d2.setDate(m,d,y);cinmdy;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 \nd2 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;
}