万年历
/*
能够显示公元后任意年份的日历,日历以月份顺序排列,每月以星期顺序排列,类似于一般挂历上的格式。
先以2000年1月1日周六作为推算起点来判定实现指定2000年后的日历
再将起点日期改为公元元年一月一日是星期几
注意判断闰年、月、周的判别*/
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
class Date
{
private:
int year;
public:
int monthDay(int year1,int month1);
void setDate();
void getDate();
bool isYear(int year1);
void print();
int sumYearDay();
};
void Date::setDate()
{
cout<<"请输入你想显示的年"<<endl;
cin>>year;
}
void Date::print()
{
int week,sum=sumYearDay();
if(year>=2000)
week=(6+sum)%7;
else
week=(7-(sum+1)%7)%7;
//week=((year-1)+ (year-1)/4-(year-1)/100+(year-1)/400+1)%7;
string weekday[7]={"星期天","星期一","星期二","星期三","星期四","星期五","星期六"};
cout<<" "<<year<<"年年历"<<endl<<endl;
for(int i=0;i<12;i++)
{
cout<<" "<<i+1<<"月\n"<<endl;;
cout<<setw(7)<<"星期天"<<setw(7)<<"星期一"<<setw(7)<<"星期二"<<setw(7)
<<"星期三"<<setw(7)<<"星期四"<<setw(7)<<"星期五"<<setw(7)<<"星期六"<<endl;
for(int k=0;k<week;k++)
cout<<" ";
for(int j=0;j<monthDay(year,i);j++)
{
cout<<setw(7)<<j+1;
week++;
if(week%7==0)
{
cout<<endl;
week=0;
}
}
cout<<endl;
}
}
int Date::sumYearDay()
{
int sum=0;
if(year>=2000)
{
for(int i=2000;i<year;i++)
{
if(isYear(i))
sum+=366;
else
sum+=365;
}
}
else
{
for(int i=1999;i>=year;i--)
{
if(isYear(i))
sum+=366;
else
sum+=365;
}
}
return sum;
}
int Date::monthDay(int year1,int month1)
{
int day[12]={31,28,31,30,31,30,31,31,30,31,30,31};
if(isYear(year1)&&month1==1)
return 29;
else
return day[month1];
}
bool Date::isYear(int year1)
{
if((year1%4==0&&year1%100!=0)||year1%400==0)
return true;
else
return false;
}
void main()
{
Date a;
a.setDate();
a.print();
}