万年历程序源代码
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include<iostream>
#include<iomanip>
#include <ctime>
using namespace std;
int dayofmonth[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; int dd[12]={ 31,28,31,30,31,30,31,31,30,31,30,31 };
class datee
{
private:
int d,m,y;
public:
int isLeap( int y );
int getDayPerMonth( int y , int m );
int getNumDay( datee D );
bool legalDate( datee D );
void count();
};
/*ÅжÏÊÇ·ñÊÇÈòÄê*/
int datee::isLeap( int y )
{
if( !(y%4)&&y%100 || !(y%400) )
return 1;
return 0;
}
/*¼ÆË㲻ͬÇé¿ö¶þÔ·ݵÄÌìÊý*/
int datee::getDayPerMonth( int y , int m )
{
if( m==2 )
return dd[1]+isLeap( y );
return dd[m-1];
}
/*¼ÆËãʱ¼ä²î*/
int datee::getNumDay( datee D )
{
int ret=0;
int i;
for( i=1 ; i<=D.y ; i++ )
ret+=365+isLeap( D.y );
for( i=1 ; i<D.m ; i++ )
ret+=getDayPerMonth( D.y , i );
ret+=D.d;
return ret;
}
/*ÔËÓò¼¶ûÐͺ¯ÊýÅжÏÊäÈëÊý¾ÝµÄÕýÎó*/
bool datee::legalDate( datee D )
{
if( D.y<0||D.y>9999 )
return false;
if( D.m<1||D.m>12 )
return false;
if( D.d<1||D.d>getDayPerMonth( D.y , D.m ) )
return false;
return true;
}
void datee::count()
{
long day;
datee sysDate,date;
cout<<"please input what date you want to compared:"<<endl; cout<<"year month day"<<endl;
cout<<"the one:";
cin>>date.y>>date.m>>date.d;
while( !legalDate(date) )
{
cout<<"illegal date,input again: "<<endl;
cin>>date.y>>date.m>>date.d;
}
cout<<"the tow:";
cin>>sysDate.y>>sysDate.m>>sysDate.d;
while( !legalDate(sysDate) )
{
cout<<"illegal system date,input again: "<<endl;
cin>>sysDate.y>>sysDate.m>>sysDate.d;
}
day=getNumDay(date)-getNumDay(sysDate);
if(day<0)
day=-day;
cout<<"the time difference is :"<<endl<<day<<endl;
}
class Date
{
private:
int year;
int month;
int day;
public:
void SetDate();
int IsLeap(int year);
void ShowDate();
int getdays(int year,int month,int day);
void showweek();
void printmonth();
void printyear();
void input()
{
cin>>year>>month;
while(month<1||month>12)
{
cout<<"illegal month,please input again:"<<endl;
cin>>year>>month;
panduan();
}
}
void panduan()
{
if(month<1||month>12)
{
cout<<"illegal month,please input again:"<<endl;
cin>>year>>month;
}
}
};
void Date::SetDate()
{
int yy,mm,dd;
cout<<"please input the date:"<<endl<<"year month date"<<endl;
cin>>yy>>mm>>dd;
if(yy<=0){ cout<<"Year value error!"<<endl;
}
if(mm<=0&&mm>12)
{
cout<<"Month value error!"<<endl;
exit(1);
}
year=yy;
month=mm;
switch(month)
{
case 4:case 6:case 9:
case 11:
{
if(dd>=1&&dd<=30)
day=dd;
else
{
cout<<"Day value error!"<<endl;
cout<<"please input again:"<<endl;
cout<<"year month date"<<endl;
cin>>yy>>mm>>dd;
day=dd;
}
} break;
case 2:
{
if(IsLeap(year))
{
if(dd>=1&&dd<=29)
day=dd;
else
{
cout<<"Day value error!"<<endl;
cout<<"please input again:"<<endl;
cout<<"year month date"<<endl;
cin>>yy>>mm>>dd;
day=dd;
}
}
else
{
if(dd>=1&&dd<=28)
day=dd;
else
{
cout<<"Day value error!"<<endl;
cout<<"please input again:"<<endl;
cout<<"year month date"<<endl;
cin>>yy>>mm>>dd;
day=dd;
}
}
}
break;
default:
{
if(dd>=1&&dd<=31)
day=dd;
else
{
cout<<"Day value error!"<<endl;
cout<<"please input again:"<<endl;
cout<<"year month date"<<endl;
cin>>yy>>mm>>dd;
day=dd;
}
}
}
}
int Date::IsLeap(int year)
{
if(year%400==0||(year%100!=0&&year%4==0))
return 1;
else
return 0;
}
void Date::ShowDate()
{
cout<<year<<"-"<<month<<"-"<<day<<" ";
}
int Date::getdays(int year,int month,int day)
{
int i,sum=0;
for(i=1;i<year;i++)
{
if(IsLeap(i))
sum+=366;
else
sum+=365;
}
if(IsLeap(year))
dayofmonth[2]=29;
for(i=1;i<month;i++)
sum+=dayofmonth[i];
sum+=day;
return sum;
}
void Date::showweek()
{
int s;
s=getdays(year,month,day)%7;
switch(s)
{
case 0:cout<<"Sunday"<<endl;break;
case 1:cout<<"Monday"<<endl;break;
case 2:cout<<"Tuesday"<<endl;break;
case 3:cout<<"Wednesday"<<endl;break;
case 4:cout<<"Thursday"<<endl;break;
case 5:cout<<"Friday"<<endl;break;
case 6:cout<<"Saturday"<<endl;break;
}
}
void Date::printmonth()
{
if(year%4==0)
cout<<" "<<year<<" "<<month<<" "<<"(ÈòÄê)"<<endl;
else
cout<<" "<<year<<" "<<month<<" ¡¡"<<"(·ÇÈòÄê)"<<endl; cout<<"ÈÕ "<<"Ò» "<<"¶þ "<<"Èý"<<" ËÄ "<<"Îå "<<"Áù "<<endl;
cout<<"Su "<<"Mo "<<"Tu "<<"We "<<"Th "<<"Fr "<<"Sa "<<endl;
int s=getdays(year,month,1);
s=s%7;
for(int m=0;m<s;m++)
cout<<" ";
cout<<setw(2)<<setfill(' ')<<"1"<<" ";
if(IsLeap(year)) dayofmonth[2]=29;
int yue=dayofmonth[month];
for(int i=2;i<8-s;i++)
cout<<setw(2)<<setfill(' ')<<i<<' ';
cout<<endl;
int j;
for(i=8-s,j=1;i<=yue;i++,j++)
{
cout<<setw(2)<<setfill(' ')<<i<<' ';
if(j%7==0)
cout<<endl;
}
cout<<endl;
}
void Date::printyear()
{
int y,m;
cin>>y;
year=y;
if(year%4==0)
cout<<"------the year is "<<year<<"£¨ÈòÄ꣩"<<"----------"<<endl;
else
cout<<"------the year is "<<year<<"£¨·ÇÈòÄ꣩"<<"---------"<<endl;
for( m=1;m<13;m++) //×ܹ²12¸öÔ·Ý
{
cout<<" ***********************"<<endl;
cout<<endl<<setw(5)<<m<<"
"<<year<<"Äê"<<setw(5)<<m<<"Ô·Ý"<<endl;
cout<<" "<<"ÈÕ "<<"Ò» "<<"¶þ "<<"Èý"<<" ËÄ "<<"Îå "<<"Áù "<<endl;
cout<<" "<<"Su "<<"Mo "<<"Tu "<<"We "<<"Th "<<"Fr "<<"Sa "<<endl;
month=m;
int s=getdays(year,month,1);
s=s%7;
for(int m=0;m<s;m++)
cout<<" ";
cout<<" "<<setw(2)<<setfill(' ')<<"1"<<" ";
if(IsLeap(year)) dayofmonth[2]=29;
int yue=dayofmonth[month];
for(int i=2;i<8-s;i++)
cout<<setw(2)<<setfill(' ')<<i<<' ';
cout<<endl<<" ";
int j;
for(i=8-s,j=1;i<=yue;i++,j++)
{
cout<<setw(2)<<setfill(' ')<<i<<' ';
if(j%7==0)
cout<<endl<<" ";}
cout<<endl<<" ***********************"<<endl;
}
}
class m
{
private:
int a,b,number;
public:
void months()
{
if (b==1||b==3||b==5||b==7||b==8||b==10||b==12)
{
number=31;
}
else
if(b==4||b==6||b==9||b==11)
{
number=30;
}
else
if(b==2)
{
if(a%4==0)
{
number=29;
}
else
{
number=28;
}
}
cout<<number<<endl;
}
void month()
{
cin>>a>>b;
if(b>0&&b<13)
months();
else
{
cout<<"error!"<<endl;
cout<<"please input again:"<<endl;
cin>>a>>b;
months();
}
}
};
int main()
{
int choose;
char ch;
Date mydate;
system("title »¶Ó-ʹÓÃÍòÄêÀú!");
while(choose!=0)
{
cout<<"********************************************************
****"<<endl;
cout<<" »¶Ó-ʹÓÃÍòÄêÀú! Ô¸ÄúʹÓõÄ˳ÐÄ£¬¿ªÐÄ!"<<endl;
/*»ñȡϵͳʱ¼ä*/
time_t timen;
time(&timen);
tm* t_tm = localtime(&timen);
cout<<" ϵͳµ±Ç°Ê±¼äΪ: "<<t_tm->tm_year+1900<<"Äê"<<t_tm->tm_mon+1<<"ÔÂ"<<t_tm->tm_mday<<
"ÈÕ"<<endl;
cout<<"ÇëÑ¡Ôñ¡°0¡«5¡±"<<endl;
cout<<"1£º¼ÆËãʱ¼ä²î 2£º²éѯijÔÂÌìÊý"<<endl;
cout<<"3£º²éѯijÈÕÐÇÆÚ 4£ºÏÔʾijÄêÈÕÀú"<<endl;
cout<<"5£º²éѯijÄêijÔÂÔÂÀú"<<endl;
cout<<"0£ºÍ˳ö"<<endl;
cout<<"******************************************************** ****"<<endl;
cin>>choose;
switch(choose) //¹¦ÄÜÑ¡Ôñ
{
case 1:
datee myday;
myday.count();
/*ÅжÏÊÇ·ñÇåÆÁ²¢ÏÔʾÖ÷²Ëµ¥*/
cout<<"please choose whether or not return the menu 'y'or other keys"<<endl;
cin>>ch;
if(ch=='y')
{
system("cls");//ÇåÆÁ£¬²¢·µ»ØÖ÷²Ëµ¥
system("COLOR a1");
break;
}
else
break; //ÏÔʾÖ÷²Ëµ¥
case 2:
cout<<"please input what you wangt to know the year,month:";
m a;
a.month();
cout<<"please choose whether or not return the menu 'y'or other keys"<<endl;
cin>>ch;
if(ch=='y')
{
system("cls");
system("COLOR 1b");
break;
}
else
break;
case 3:
mydate.SetDate();
mydate.ShowDate();
mydate.showweek();
mydate.printmonth();
cout<<"please choose whether or not return the menu 'y'or other keys"<<endl;
cin>>ch;
if(ch=='y')
{
system("cls");
system("COLOR 6e");
break;
}
else
break;
case 4:
cout<<"please input what years you want to know£º";
cout<<endl;
mydate.printyear();
cout<<"please choose whether or not return the menu 'y'or other keys"<<endl;
cin>>ch;
if(ch=='y')
{
system("cls");
system("COLOR 3d");
break;
}
else
break;
case 5:
cout<<"please input the year,month what you want to know."<<endl;
mydate.input();
mydate.printmonth();
cout<<"please choose whether or not return the menu 'y' or other keys"<<endl;
cin>>ch;
if(ch=='y')
{
system("cls");
system("COLOR 55");
break;
}
else
break;
case 0:
cout<<"ллʹÓÃÍòÄêÀúϵͳ£¬Çë¹Ø±Õ!!!!!"<<endl;exit(0);break;
default:cout<<"ÄúÊäÈëÓÐÎó£¬ÇëÖØÐÂÊäÈë"<<endl;
}
}
return 0;
}。