#include <iostream.h>
#include<stdio.h>
#define N 30
void menu();
void yeardays(int year);
void monthdays(int year,int month);
void daydata(int year,int month,int day);
void menuyear();
void menumonth();
void menuday();
/*日期类开始*/
class Calender
{
public:
Calender()
{
Year=1902;
Month=1;
Day=1;
weeknumber=2;
}
Calender(int year)
{
Year=year;
Month=1;
weeknumber=2;
Day=1;
}
Calender(int year,int month)
{
Year=year;
Month=month;
weeknumber=2;Day=1;
}
Calender(int year,int month,int day)
{
Year=year;
Month=month;
Day=day;
weeknumber=2;
}
void showcalender(int year);
void showcalender(int year,int month);
void showcalender(int year,int month,int day);
int getyear();
int getmonth();
int getday();
private:
int isruiyear(int year);
int firstyear(int year);
int firstmonth(int year,int month,int firstday);
int Year;
int Month;
int Day;
int weeknumber;
}; /*类定义部分*/
void Calender::showcalender(int year)
{
int i;
cout<<" 欢迎查看"<<year<<"的日历"<<endl;
for(i=1;i<=12;i++)
showcalender(year,i);
}
void Calender::showcalender(int year,int month)
{
char weekdate[7][10]={{"星期一"},{"星期二"},{"星期三"},{"星期四"},{"星期五"},{"星期六"},{"星期日"}};
int firstday=firstyear(year);
int i,k,j=isruiyear(year);
cout<<endl<<year<<"年"<<month<<"月的日历"<<endl;
switch(month)
{
case 1:
{
i=firstmonth(year,month,firstday);
for(k=0;k<7;k++)
cout<<weekdate[k]<<" ";
cout<<endl;
for(k=0;k<31+i;k++)
{
if(k<i)
cout<<" ";
else
if((k-i+1)<10)
cout<<(k-i+1)<<" ";
else
cout<<(k-i+1)<<" ";
if((k+1)%7==0)
cout<<endl;
}
}break;
case 2:
{
i=firstmonth(year,month,firstday);
for(k=0;k<7;k++)
cout<<weekdate[k]<<" ";
cout<<endl;
for(k=0;k<28+j+i;k++)
{
if(k<i)
cout<<" ";
else
if((k-i+1)<10)
cout<<(k-i+1)<<" ";
else
cout<<(k-i+1)<<" ";
if((k+1)%7==0)
cout<<endl;
}
}break;
case 3:
{
i=firstmonth(year,month,firstday);
for(k=0;k<7;k++)
cout<<weekdate[k]<<" ";
cout<<endl;
for(k=0;k<31+i;k++)
{
if(k<i)
cout<<" ";
else
if((k-i+1)<10)
cout<<(k-i+1)<<" ";
else
cout<<(k-i+1)<<" ";
if((k+1)%7==0)
cout<<endl;
}
}break;
case 4:
{
i=firstmonth(year,month,firstday);
for(k=0;k<7;k++)
cout<<weekdate[k]<<" ";
cout<<endl;
for(k=0;k<30+i;k++)
{
if(k<i)
cout<<" ";
else
if((k-i+1)<10)
cout<<(k-i+1)<<" ";
else
cout<<(k-i+1)<<" ";
if((k+1)%7==0)
cout<<endl;
}
}break;
case 5:
{
i=firstmonth(year,month,firstday);
for(k=0;k<7;k++)
cout<<weekdate[k]<<" ";
cout<<endl;
for(k=0;k<31+i;k++)
{
if(k<i)
cout<<" ";
else
if((k-i+1)<10)
cout<<(k-i+1)<<" ";
else
cout<<(k-i+1)<<" ";
if((k+1)%7==0)
cout<<endl;
}
}break;
case 6:
{
i=firstmonth(year,month,firstday);
for(k=0;k<7;k++)
cout<<weekdate[k]<<" ";
cout<<endl;
for(k=0;k<30+i;k++)
{
if(k<i)
cout<<" ";
else
if((k-i+1)<10)
cout<<(k-i+1)<<" ";
else
cout<<(k-i+1)<<" ";
if((k+1)%7==0)
cout<<endl;
}
}break;
case 7:
{
i=firstmonth(year,month,firstday);
for(k=0;k<7;k++)
cout<<weekdate[k]<<" ";
cout<<endl;
for(k=0;k<31+i;k++)
{
if(k<i)
cout<<" ";
else
if((k-i+1)<10)
cout<<(k-i+1)<<" ";
else
cout<<(k-i+1)<<" ";
if((k+1)%7==0)
cout<<endl;
}
}break;
case 8:
{
i=firstmonth(year,month,firstday);
for(k=0;k<7;k++)
cout<<weekdate[k]<<" ";
cout<<endl;
for(k=0;k<31+i;k++)
{
if(k<i)
cout<<" ";
else
if((k-i+1)<10)
cout<<(k-i+1)<<" ";
else
cout<<(k-i+1)<<" ";
if((k+1)%7==0)
cout<<endl;
}
}break;
case 9:
{
i=firstmonth(year,month,firstday);
for(k=0;k<7;k++)
cout<<weekdate[k]<<" ";
cout<<endl;
for(k=0;k<30+i;k++)
{
if(k<i)
cout<<" ";
else
if((k-i+1)<10)
cout<<(k-i+1)<<" ";
else
cout<<(k-i+1)<<" ";
if((k+1)%7==0)
cout<<endl;
}
}break;
case 10:
{
i=firstmonth(year,month,firstday);
for(k=0;k<7;k++)
cout<<weekdate[k]<<" ";
cout<<endl;
for(k=0;k<31+i;k++)
{
if(k<i)
cout<<" ";
else
if((k-i+1)<10)
cout<<(k-i+1)<<" ";
else
cout<<(k-i+1)<<" ";
if((k+1)%7==0)
cout<<endl;
}
}break;
case 11:
{
i=firstmonth(year,month,firstday);
for(k=0;k<7;k++)
cout<<weekdate[k]<<" ";
cout<<endl;
for(k=0;k<30+i;k++)
{
if(k<i)
cout<<" ";
else
if((k-i+1)<10)
cout<<(k-i+1)<<" ";
else
cout<<(k-i+1)<<" ";
if((k+1)%7==0)
cout<<endl;
}
}break;
case 12:
{
i=firstmonth(year,month,firstday);
for(k=0;k<7;k++)
cout<<weekdate[k]<<" ";
cout<<endl;
for(k=0;k<31+i;k++)
{
if(k<i)
cout<<" ";
else
if((k-i+1)<10)
cout<<(k-i+1)<<" ";
else
cout<<(k-i+1)<<" ";
if((k+1)%7==0)
cout<<endl;
}
}break;
}
cout<<endl;
}
void Calender::showcalender(int year,int month,int day)
{
char weekdate[7][10]={{"星期一"},{"星期二"},{"星期三"},{"星期四"},{"星期五"},{"星期六"},{"星期日"}};
int firstday=firstyear(year);
int i=firstmonth(year,month,firstday);
int number=(day-1)%7;
if(i+number<=6)
number=i+number;
else
number=i+number-7;
cout<<year<<"年"<<month<<"月"<<day<<"号 "<<weekdate[number]<<endl;
}
int Calender::isruiyear(int year)
{
if((year%4==0 && year%100!=0) || year%400==0)
return 1;
else
return 0;
}
int Calender::firstyear(int year) /*每年第一天的星期序号*/
{
int i,counts=0;
for(i=1902;i<year;i++)
{
if(isruiyear(i)==1)
counts=counts+366;
else
counts=counts+365;
}
counts=counts%7;
if(weeknumber+counts<=6)
return(weeknumber+counts);
else
return(weeknumber+counts-7);
}
int Calender::firstmonth(int year,int month,int firstday) /*每月第一天的星期序号*/
{
int j,counts=0;
if(isruiyear(year)==1)
j=29;
else
j=28;
switch(month)
{
case 1:counts=0+counts;break;
case 2:counts=31;break;
case 3:counts=31+j;break;
case 4:counts=31*2+j;break;
case 5:counts=31*2+30+j;break;
case 6:counts=31*3+30+j;break;
case 7:counts=31*3+30*2+j;break;
case 8:counts=31*4+30*2+j;break;
case 9:counts=31*5+30*2+j;break;
case 10:counts=31*5+30*3+j;break;
case 11:counts=31*6+30*3+j;break;
case 12:counts=31*6+30*4+j;break;
}
counts=counts%7;
if(firstday+counts<=6)
return(firstday+counts);
else
return(firstday+counts-7);
}
int Calender::getyear()
{
return Year;
}
int Calender::getmonth()
{
return Month;
}
int Calender::getday()
{
return Day;
} /*日期类结束*/
/*函数定义部分*/
void menu()
{
int i=1,select;
while(i)
{
cout<<" 欢迎进入万年历信息系统"<<endl;
cout<<"1 显示一年的日历信息"<<endl<<"2 显示 一月的日历信息"<<endl;
cout<<"3 显示某一天的日历信息"<<endl<<"0 退出"<<endl;
cin>>select;
if(select>3 || select<0)
{
cout<<"输入选择有错误,重新输入"<<endl;
continue;
}
else
{
switch(select)
{
case 1:menuyear();break;
case 2:menumonth();break;
case 3:menuday();break;
/* case 0:exit;break;*/
}
}
}
}
void menuyear()
{
int year;
cout<<"liubiao"<<endl;
cout<<" 欢迎进入万年历信息系统"<<endl;
cout<<" 请输入你要查看的年份"<<endl;
cin>>year;
yeardays(year);
}
void menumonth()
{
int year,int month;
cout<<"liubiao"<<endl;
cout<<" 欢迎进入万年历信息系统"<<endl;
cout<<" 请输入你要查看的年份"<<endl;
cin>>year;
cout<<" 请输入你要查看的月份"<<endl;
cin>>month;
monthdays(year,month);
}
void menuday()
{
int year,month,day;
cout<<" 欢迎进入万年历信息系统"<<endl;
cout<<" 请输入你要查看的年份"<<endl;
cin>>year;
cout<<" 请输入你要查看的月份"<<endl;
cin>>month;
cout<<" 请输入你要查看的日期"<<endl;
cin>>day;
daydata(year,month,day);
}
void yeardays(int year)
{
class Calender Year;
Year.showcalender(year);
}
void monthdays(int year,int month)
{
class Calender Year;
Year.showcalender(year,month);
}
void daydata(int year,int month,int day)
{
class Calender Year;
Year.showcalender(year,month,day);
}
/*函数实现部分*/
int main()
{
menu();
return 0;
}
|
|