//从公元元年一月一日是星期一开始计算
//用类实现求万年历的算法
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
class Calendar
{
private:
int month;
int date;
string Day[7];//char Day[7][10];//私有成员不能初始化,而且字符数组要想赋值,要么就一个一个的赋值
int Month[13];
int sumday;
int year;
public:
Calendar(int y,int m,int d)
{
year=y;
month=m;
date=d;
//Day[7]={"日","一","二","三","四","五","六"};
//Month[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
Month[0]=0;Day[0]="日";
Month[1]=31;Day[1]="一";
Month[2]=28;Day[2]="二";
Month[3]=31;Day[3]="三";
Month[4]=30;Day[4]="四";
Month[5]=31;Day[5]="五";
Month[6]=30;Day[6]="六";
Month[7]=31;
Month[8]=31;
Month[9]=30;
Month[10]=31;
Month[11]=30;
Month[12]=31;
sumday=0;
}
bool Leapyear(int y)
{
if((y%400==0)||((y%4==0)&&(y%100!=0)))
{
Month[2]=29;
return true;
}
else
{
Month[2]=28;
return false;
}
}
int Dateday()
{
return date;
}
int Monthday(int m)
{
return Month[m];
}
void Yearday(int y)
{
if(Leapyear(y)) sumday=sumday+366;
else
sumday=sumday+365;
}
//int SumMonthday()
//{
// int t=month,sum=0;
// for(;t!=0;)
//{
// t--;
// sum=sum+Monthday(j);
//}
// return sum;
void Sumday() //计算从2000年1月1日起是第几天,包括了2000年1月1日这一天
{
int i=1;//j=month;
for(;i!=year;i++)
{
Yearday(i);
}
Leapyear(year);
//for(;j!=0;)
//{
//j--;
// sumday=sumday+Monthday(j);
// }
// sumday+=Dateday();
//sumday+=1;
}
int Onedate(int t) //返回这一年的第一天是星期几
{
t=sumday%7;
if(t==1) return 1;
if(t==2) return 2;
if(t==3) return 3;
if(t==4) return 4;
if(t==5) return 5;
if(t==6) return 6;
if(t==0) return 7;
}
void Output()
{
//sumday=1;
cout<<" 公元"<<year<<"的日历"<<endl<<endl;
int i,k,k1,t;
//cout<<"日"<<setw(4)<<"一"<<setw(4)<<"二"<<setw(4)<<"三"<<setw(4)<<"四"<<setw(4)<<"五"<<setw(4)<<"六"<<endl;
for(i=1;i<=month;i++)
{
cout<<" ***"<<i<<"月***"<<endl;
cout<<"----------------------------\n";
for(k1=0;k1<7;k1++)
cout<<setw(4)<<Day[k1];
cout<<endl;
for(k=1;k<=Monthday(i);k++)
{
sumday++;
t=Onedate(sumday);
if(t==7)cout<<endl<<setw(4)<<k;
else
if(k==1)cout<<setw(4*(t+1))<<k;
else
cout<<setw(4)<<k;
}
cout<<endl<<"----------------------------\n";
cout<<endl;
}
cout<<endl;
cout<<sumday<<endl;
}
};
int main()
{
int year,month,date;
cout<<"输入年月日:";
cin>>year>>month>>date;
Calendar a(year,month,date);
a.Sumday();
a.Output();
return 0;
}
c++实现万年历,从公元1年1月1日开始,这一天是星期一
最新推荐文章于 2023-01-13 20:31:25 发布