C++ 万年历 面向对象 使用类写的 可以计算两个日期的相隔天数

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

#ifndefDATE_H_INCLUDED#defineDATE_H_INCLUDED/***Copyright(C)sRyanAllrightsreserved.*/#includeiostream#includevector#includeiomanipusingnamespacestd;classDate{private:intyear;//Äê·Ýintmonth;//Ô·Ýintday;//¼¸ºÅintdays;//¸ÃÔ¹²Óм¸ÌìintlDays;//µ±Ç°ÈÕÆÚºóÃæµÄÌìÊý£¬lDays=days-day;vectorintmyYear;//´æ·ÅÍòÄêÀúÈÕÆÚÅÅÁеÄÈÝÆ÷voidSet();//ÊäÈëÁËÈÕÆÚºó³õʼ»¯dayslDaysmyYearpublic:Date(inty=1900,intm=1,intd=0):year(y),month(m),day(d){myYear.resize(40);Set();}voidShow()const;voidshowSign()const;voidshowAll()const;voidReset(inty,intm,intd=0);voidclear();boolisR(intn);intoperator-(constDate&);};voidDate::Show()const{coutyearÄêmonthÔÂ(;showSign();cout)endlendl;coutsetw(5)ÈÕsetw(5)Ò»setw(5)¶þsetw(5)Èýsetw(5)ËÄsetw(5)Îåsetw(5)Áùendl;for(vectorint::size_typei=0;imyYear.size();i++){if(myYear[i]==0)coutsetw(5);elsecoutsetw(5)myYear[i];if((i+1)%7==0)coutendl;}coutendl;}voidDate::Set(){shortbeginDay=1;for(inti=1901;i=year;i++)//Äê·Ý²úÉúµÄ³õʼλÖõĸıä{if(!isR(i-1))beginDay++;elsebeginDay+=2;if(beginDay6)beginDay%=7;}for(inti=2;i=month;i++)//Ô·ݲúÉúµÄ³õʼλÖõĸıä{switch(i-1){case1:case3:case5:case7:case8:case10:case12:{beginDay+=3;if(beginDay6)beginDay%=7;break;}case2:{if(isR(year))beginDay++;if(beginDay6)beginDay%=7;break;}default:{beginDay+=2;if(beginDay6)beginDay%=7;break;}}}for(inti=0;i31;i++)//½«ÈÝÆ÷È«²¿ÉèÖÃΪ31ÌìmyYear[beginDay+i]=i+1;switch(month)//ÖØÉèβ²¿¼¸Ìì{case4:case6:case9:case11:{myYear[beginDay+30]=0;days=30;break;}case2:{myYear[beginDay+30]=0;myYear[beginDay+29]=0;days=29;if(!isR(year)){myYear[beginDay+28]=0;days=28;}break;}default:{days=31;break;}}lDays=days-day;}voidDate::Reset(inty,intm,intd){this-clear();year=y;month=m;day=d;Set();}boolDate::isR(intn){if(n%100!=0&&n%4==0)return1;elseif(n%100==0&&n%400==0)return1;elsereturn0;}voidDate::showSign()const{switch((year%1900)%12){case0:coutÊó;break;case1:coutÅ£;break;case2:cout»¢;break;case3:coutÍÃ;break;case4:coutÁú;break;case5:coutÉß;break;case6:coutÂí;break;case7:coutÑò;break;case8:coutºï;break;case9:cout¼¦;break;case10:cout¹·;break;case11:coutÖí;break;}}voidDate::showAll()const{Dateda;for(inti=0;i12;i++){da.Reset(this-year,i+1);da.Show();coutendlendlendl;da.clear();}}voidDate::clear(){myYear.clear();myYear.resize(40);}intDate::operator-(constDate&da){intsum=0;Datetemp;if(this-yearda.year)//¼¸¸öÎÞ·¨¼ÆËãµÄÌáʾ{cout±ØÐëÒÔ½ÏÔçµÄÈÕÆÚ¼õÈ¥½Ï³ÙµÄÈÕÆÚ£¡\n;return-1;}elseif(this-year==da.year&&this-monthda.month){cout±ØÐëÒÔ½ÏÔçµÄÈÕÆÚ¼õÈ¥½Ï³ÙµÄÈÕÆÚ£¡\n;return-2;}elseif(this-year==da.year&&this-month==da.month&&this-dayda.day){cout±ØÐëÒÔ½ÏÔçµÄÈÕÆÚ¼õÈ¥½Ï³ÙµÄÈÕÆÚ£¡\n;return-3;}else{if(year==da.year&&month==da.month&&day!=da.day)//ͬÄêͬÔ²»Í¬ÈÕ{returnday-da.day;}if(year==da.year&&monthda.month)//ͬÄ겻ͬÔÂ{for(inti=da.month+1;imonth;i++){temp.Reset(year,i);sum+=temp.lDays;}returnsum+da.lDays+day;}else{for(inti=da.year+1;iyear;i++){if(isR(i))sum+=366;elsesum+=365;}for(inti=da.month+1;i13;i++){temp.Reset(da.year,i);sum+=temp.lDays;}sum+=da.lDays;for(inti=1;imonth;i++){temp.Reset(year,i);sum+=temp.lDays;}sum+=day;returnsum;}}}#endif//DATE_H_INCLUDED

1 / 6
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功