c++的万年历类,支持显示某月月历与某年年历,支持计算任意两个日期的间隔天数

#include <iostream> #include <vector> #include <iomanip> using namespace std; class Date { private: int year; //年份 int month; //月份 int day; //几号 int days; //该月共有几天 int lDays; //当前日期后面的天数,lDays=days-day; vector<int> myYear; //存放万年历日期排列的容器 void Set(); //输入了日期后初始化days lDays myYear public: Date(int y=1900,int m=1,int d=0):year(y),month(m),day(d){myYear.resize(40);Set();} void Show()const; void showSign()const; void showAll()const; void Reset(int y,int m,int d=0); void clear(); bool isR(int n); int operator-(const Date&); }; void Date::Show()const { cout<<" "<<year<<"年"<<month<<"月"<<"("; showSign(); cout<<")"<<endl<<endl; cout<<setw(5)<<"日"<<setw(5)<<"一"<<setw(5)<<"二"<<setw(5)<<"三"<<setw(5)<<"四" <<setw(5)<<"五"<<setw(5)<<"六"<<endl; for(vector<int>::size_type i=0;i<myYear.size();i++) { if(myYear[i]==0) cout<<setw(5)<<" "; else cout<<setw(5)<<myYear[i]; if((i+1)%7==0) cout<<endl; } cout<<endl; } void Date::Set() { short beginDay=1; for(int i=1901;i<=year;i++) //年份产生的初始位置的改变 { if(!isR(i-1)) beginDay++; else beginDay+=2; if(beginDay>6) beginDay%=7; } for(int i=2;i<=month;i++) //月份产生的初始位置的改变 { switch(i-1) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: { beginDay+=3; if(beginDay>6) beginDay%=7; break; } case 2: { if(isR(year)) beginDay++; if(beginDay>6) beginDay%=7; break; } default: { beginDay+=2; if(beginDay>6) beginDay%=7; break; } } } for(int i=0;i<31;i++) //将容器全部设置为31天 myYear[beginDay+i]=i+1; switch(month) //重设尾部几天 { case 4: case 6: case 9: case 11: { myYear[beginDay+30]=0; days=30; break; } case 2: { 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; } void Date::Reset(int y,int m,int d) { this->clear(); year=y;month=m;day=d; Set(); } bool Date::isR(int n) { if(n%100!=0&&n%4==0) return 1; else if(n%100==0&&n%400==0) return 1; else return 0; } void Date::showSign()const { switch((year%1900)%12) { case 0: cout<<"鼠";break; case 1: cout<<"牛";break; case 2: cout<<"虎";break; case 3: cout<<"兔";break; case 4: cout<<"龙";break; case 5: cout<<"蛇";break; case 6: cout<<"马";break; case 7: cout<<"羊";break; case 8: cout<<"猴";break; case 9: cout<<"鸡";break; case 10: cout<<"狗";break; case 11: cout<<"猪";break; } } void Date::showAll()const { Date da; for(int i=0;i<12;i++) { da.Reset(this->year,i+1); da.Show(); cout<<endl<<endl<<endl; da.clear(); } } void Date::clear() { myYear.clear(); myYear.resize(40); } int Date::operator-(const Date& da) { int sum=0; Date temp; if(this->year<da.year) //几个无法计算的提示 { cout<<"必须以较早的日期减去较迟的日期!/n"; return -1; } else if(this->year==da.year&&this->month<da.month) { cout<<"必须以较早的日期减去较迟的日期!/n"; return -2; } else if(this->year==da.year&&this->month==da.month&&this->day<da.day) { cout<<"必须以较早的日期减去较迟的日期!/n"; return -3; } else { if(year==da.year&&month==da.month&&day!=da.day) //同年同月不同日 { return day-da.day; } if(year==da.year&&month>da.month) //同年不同月 { for(int i=da.month+1;i<month;i++) { temp.Reset(year,i); sum+=temp.lDays; } return sum+da.lDays+day; } else { for(int i=da.year+1;i<year;i++) { if(isR(i)) sum+=366; else sum+=365; } for(int i=da.month+1;i<13;i++) { temp.Reset(da.year,i); sum+=temp.lDays; } sum+=da.lDays; for(int i=1;i<month;i++) { temp.Reset(year,i); sum+=temp.lDays; } sum+=day; return sum; } } }

你可能感兴趣的:(C++,Date,include)