// Calendar.cpp : Defines the entry point for the console application.
//WangBin @2006-10-12
//#include "stdafx.h"
#include <conio.h>
#include <time.h>
#include <iostream>
using namespace std;
#define START_YEAR (-5600)
#define START_WEEK_DAY 6
class Calendar
{
public:
Calendar();
Calendar(int year, int month = 1);
virtual ~Calendar();
void SetDate(int year, int month =1);
void SetThisDate();
Calendar operator + (int n);
Calendar operator - (int n);
static bool CheckValid(int year, int month);
int GetWeekDay(int year = 0, int month = 0, int day = 0);
virtual void PrintMonth();
virtual void Print2Month(Calendar& right);
protected:
bool IsLeapYear(int year);
virtual void PrintLine(int line, bool endline = true);
virtual void Print2Line(int line, Calendar &right);
private:
int m_Year;
int m_Month;
public:
static int a[];
};
int Calendar::a[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
bool Calendar::IsLeapYear(int year)
{
return (0 == year % 400) || (0 == year % 4) && (0 != year %100);
}
int Calendar::GetWeekDay(int year,int month,int day)
{
int y = year - START_YEAR;
int m = month - 1;
int d = day -1;
int w = START_WEEK_DAY;
a[2] = IsLeapYear(year) ? 29 : 28;
if(y > START_YEAR || m > 2)
{
w ++;
}
w += y * 365;
w += y / 4;
w -= y / 100;
w += y / 400;
for(int i = 1; i <= m; i++)
{
w += Calendar::a[i];
}
w += d;
w %= 7;
return w;
}
Calendar::Calendar()
{
SetThisDate();
}
Calendar::Calendar(int year, int month)
{
SetDate(year, month);
}
Calendar::~Calendar()
{
}
void Calendar::PrintLine(int line,bool endline)
{
char str[16];
int w = GetWeekDay(m_Year, m_Month, 1);
cout<<'\t';
for(int i=1+(line-1)*7;i<=line*7;i++)
{
if(i<=w || i>Calendar::a[m_Month]+w)
cout<<" ";
else
{
sprintf(str,"%2d ",i-w);
cout<<str;
}
}
if(endline)
cout<<endl;
else
cout<<'\t';
}
void Calendar::Print2Line(int line, Calendar &right)
{
PrintLine(line, false);
right.PrintLine(line);
}
void Calendar::PrintMonth()
{
char str[256];
cout<<endl;
sprintf(str,"\t*****%04d年%02d月*****",
m_Year,m_Month);
cout<<str<<endl;
cout<<"\t日 一 二 三 四 五 六 "<<endl;
for(int i=1;i<=6;i++)
{
PrintLine(i);
}
}
void Calendar::Print2Month(Calendar &right)
{
char str[256];
cout<<endl;
sprintf(str,"\t*****%04d年%02d月*****",
m_Year,m_Month);
cout<<str<<"\t";
sprintf(str,"\t*****%04d年%02d月*****",
right.m_Year,right.m_Month);
cout<<str<<endl;
cout<<"\t日 一 二 三 四 五 六 "
<<"\t\t日 一 二 三 四 五 六 "<<endl;
for(int i=1;i<=6;i++)
{
Print2Line(i, right);
}
}
void Calendar::SetDate(int year, int month)
{
m_Year = year;
m_Month = month;
}
void Calendar::SetThisDate()
{
struct tm *newtime;
time_t long_time;
time( &long_time );
newtime = localtime( &long_time );
m_Year = newtime->tm_year+1900;
m_Month = newtime->tm_mon+1;
}
Calendar Calendar::operator + (int n)
{
int year = m_Year,
month = m_Month + n;
year += month / 13;
month %= 13;
month = month?month:1;
return Calendar(year, month);
}
Calendar Calendar::operator - (int n)
{
int year = m_Year,
month = m_Month - n;
year += month / 13;
if(month<0 && month>-13)
year -= 1;
while(month<=0)
month += 12;
month %= 13;
month = month?month:1;
return Calendar(year, month);
}
bool Calendar::CheckValid(int year, int month)
{
return year>=-5600 && year<=5600 && month>0 && month<=12;
}
void cls()
{
system("cls");
}
int main(int argc, char* argv[])
{
Calendar c[4];
int year,month;
while(true)
{
cout<<"\n\t\t\t\t日 历"<<endl;
for(int i=0;i<3;i++)
c[i+1]=c[i]+1;
c[0].Print2Month(c[1]);
c[2].Print2Month(c[3]);
cout<<endl<<endl
<<" 按Esc键退出,按N键输入年月,"\
"按PageUp或↑键上翻,按PageDown或↓下翻。"
<<endl;
char key = getch();
if(27 == key)
{
break;
}
switch(key)
{
case 'n':
case 'N':
cls();
do{
cout<<"请输入年份:";
cin>>year;
cout<<"请输入月份:";
cin>>month;
cls();
if(cin.fail())
{
cin.clear();
cin.ignore();
month = 0;
}
}while(!Calendar::CheckValid(year,month));
c[0].SetDate(year,month);
if(!Calendar::CheckValid(year,month))
{
c[0].SetThisDate();
}
break;
case 81:
case 80:
c[0] = c[0] + 4;
cls();
break;
case 73:
case 72:
c[0] = c[0] - 4;
cls();
break;
default:
cls();
break;
}
}
return 0;
}