要求:
写一个程序可用实现打印任意年份/月份的日历,年历三月一层,分四层打印。
实现代码:
提示:本代码不同编译器下缩进显示可能有略微差别,自行调整。
/*************************************************************************
> File Name: calendar.cpp
> Author: 念念
> Mail: 2845906049@qq.com
> Created Time: 2021年08月17日 星期二 15时27分03秒
> Function: 根据输入的年份打印该年日历
************************************************************************/
#include <iostream>
#include <iomanip>
using namespace std;
class Calendar
{
public:
void setYear(int y);
void setMonth(int m);
int dayOfYear(int year);
int dayOfMonth(int month);
int getWeek(int year,int month);
void showMonth();
void showYear();
void showYearr();
private:
int year = 1990;
int month = 1;
};
void Calendar::setYear(int y)
{
year = y;
}
void Calendar::setMonth(int m)
{
month = m;
}
int Calendar::dayOfYear(int year)
{
if(year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))
{
return 366; //闰年366天
}
else
{
return 365;
}
}
int Calendar::dayOfMonth(int month)
{
if(month == 4 || month == 6 || month == 9 || month == 11)
{
return 30;
}
else if(month == 2 && dayOfYear(year) == 366)
{
return 29;
}
else if(month == 2 && dayOfYear(year) == 365)
{
return 28;
}
return 31;
}
int Calendar::getWeek(int year,int month)
{
//1990-01-01是周一,以此为标尺
int week = 1;
if(year >= 1990)
{
for(int i = 1990; i < year; i++)
{
week += dayOfYear(i);
}
for(int i = 1; i < month; i++)
{
week += dayOfMonth(i);
}
week %= 7;
}
return week+1;//该月第一天的星期,0是周末
}
void Calendar::showMonth()
{
cout << "\033[035m\033[1m";
cout << year << "年" << month << "月\033[0m\n";
cout << " 日 一 二 三 四 五 六\n";
for(int i = 1; i < getWeek(year,month); i++)
{
cout << setw(4) << "";
}
for(int i = 1,j= getWeek(year,month); i<= dayOfMonth(month); i++,j++)
{
cout << setw(4) << i;
if(i % 7 == 0){cout << endl;}
}
}
void Calendar::showYear()
{
//分四层打印,每层左中右三个月。
int left_month = 1,middle_month = 2,right_month = 3;//月份
int left_day= 1,middle_day= 1,right_day = 1;//日期
int week;
cout << "\033[035m\033[1m";
cout << year << "年\n";
for(;left_month < 12;left_month+=3,middle_month+=3,right_month+=3)
{
//打印月历头
cout << "\033[34m\033[1m " << left_month <<"月";
cout << setw(36) << middle_month <<"月";
cout << setw(36) << right_month <<"月\033[0m\n";
for(int i = 0; i < 3; i++)
{
cout << setw(45) << " 日 一 二 三 四 五 六 ";
}
cout << endl;
//打印第一行
for(int i= 1; i < getWeek(year,left_month); i++)
{
cout << setw(4) << "";
}
for(week = getWeek(year,left_month)-1;week < 7; left_day++,week++)
{
week %= 7;
cout << setw(4) << left_day;
}
cout << setw(10) << "";
for(int i = 1; i < getWeek(year,middle_month); i++)
{
cout << setw(4) << "";
}
for(week = getWeek(year,middle_month)-1;week < 7; middle_day++,week++)
{
week %= 7;
cout << setw(4) << middle_day;
}
cout << setw(10) << "";
for(int i = 1; i < getWeek(year,right_month); i++)
{
cout << setw(4) << "";
}
for(week = getWeek(year,right_month)-1;week <7; right_day++,week++)
{
week %= 7;
cout << setw(4) <<right_day;
}
cout << endl;
//打印后续行
for(int t = 0; t < 5; t++)
{
for(week = 0; week < 7; left_day++,week++)
{
week %= 7;
left_day<= dayOfMonth(left_month) ? cout << setw(4) << left_day: cout << setw(4) <<"";
}
cout << setw(10) << "";
for(week = 0; week < 7; middle_day++,week++)
{
week %= 7;
middle_day<= dayOfMonth(middle_month) ? cout << setw(4) << middle_day: cout << setw(4) <<"";
}
cout << setw(10) << "";
for(week = 0; week < 7 && right_day <= dayOfMonth(right_month);right_day++,week++)
{
week %= 7;
cout << setw(4) << right_day;
}
cout << endl;
}
left_day= 1;middle_day= 1;right_day = 1;
}
}
int main()
{
Calendar c;
c.setYear(2021);
c.setMonth(9);
c.showYear();//展现设置年日历
//c.showMonth();//展现设置年设置月日历
return 0;
}