#include <iostream.h>
#include <iomanip.h>
int month[12]={31,28,31,30,31,30,31,31,30,31,30,31};
class calendar
{
public:
calendar(int y,int m)
{
yy=y;
mm=m;
}
void displaycalendar();
int coutleapyear();
private:
int yy;
int mm;
};
void calendar::displaycalendar()
{
int day,fristDay,tempDay=0,dayMonth,totalDay,line=0;
day=(yy-1900)+coutleapyear();
fristDay=day%7;
for (int i=1;i<mm;i++)
{
tempDay+=month[i-1];
}
if ((yy%4==0&&yy%100!=0||yy%400==0)&&mm>2)
{
tempDay=tempDay+1;
}
dayMonth=month[mm-1];
if ((yy%4==0&&yy%100!=0||yy%400==0)&&mm==2)
{
dayMonth=29;
}
cout<<" 年"<<yy<<" 月"<<mm<<endl;
cout<<setw(5)<<"mon"<<setw(5)<<"tue"<<setw(5)<<"wed"<<setw(5)<<"thu"<<setw(5)<<"fri"<<setw(5)<<"sat"<<setw(5)<<"sun"<<endl;
cout<<endl;
totalDay=fristDay+tempDay;
for (i=0;i<totalDay%7;i++)
{
cout<<setw(5)<<" ";
line++;
}
for (i=1;i<=dayMonth;i++)
{
cout<<setw(5)<<i;
line++;
if (line%7==0)
{
cout<<endl;
}
}
}
int calendar::coutleapyear()
{
int count=0;
for (int i=1900;i<yy;i++)
{
if(i%4==0&&i%100!=0||i%400==0)
{
count++;
}
}
return count;
}
void main()
{
int year;
int month;
cout<<"请输入年,月"<<endl;
cin>>year>>month;
while((year<1900)&&(month<1||month>12))
{
cin>>year>>month;
}
calendar cal(year,month);
cal.displaycalendar();
cout<<endl;
}
我自己用c++语言编的电子日历
最新推荐文章于 2022-01-02 20:34:48 发布