⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 calendar.cpp

📁 c++输出日历,包括横排和竖排.........
💻 CPP
字号:
#include "Calendar.h"
#include <iostream>
#include <iomanip>
using namespace std;

const int ROW = 4;
const int COL = 3;

CCalendar::CCalendar(int year)                   //构造函数
{
	this -> m_year = year;
	enum weekday {SUN = 1, MON, TUE, WED, THU, FRI, SAT};
}

CCalendar::~CCalendar(void)                      //析构函数
{
}

void CCalendar::SetYear(int year)                         //SetYear 设置日历年份
{
	this -> m_year = year;
}

bool CCalendar::IsRunYear(int year)                          //IsRunNian 判断某年是否是闰年
{
	return (0 == year % 4&& 0 != year % 100) ||
		0 == year % 400;
}

int CCalendar::YearFirstday(int year)                             //计算某年第一天是星期几
{
	int yeardays = 0,sum = 0, i = 1980;
	if (1 == year)                                         //01年第一天是星期一
		return 1;
	else
		for(i = 1; i <= year; i++)
		{
			if (IsRunYear(i - 1))
				yeardays = 366;
			else
				yeardays = 365;
			sum = sum + yeardays;
		}
		return (sum % 7 + 3);
}


int CCalendar::RowFirstday(int n, int line)           //横排日历中判断某第一天的日期
{
	return ( 7 * line - n + 1 );
}


void CCalendar::PrintCalendar(bool vertical)                   //PrintCalendar 打印日历
{
	if (0 == vertical)                                             //横向输出
	{
		int month = 1, day = 1,monthfirstday = 1;
		int monthdays[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
		if (IsRunYear(m_year))
			monthdays[2] = 29;
		monthfirstday = YearFirstday(m_year);
		int space = YearFirstday(m_year);                           //space 为输出空格
		for(int monthrow = 0; monthrow < ROW; monthrow++)     //monthrow 为月份的排数,因窗口大小原因,每排有三列,共四排,第一排记为0
		{
			int m1,m2,m3;

			cout<<"        "<<monthrow * 3 + 1<<" 月                  "<<monthrow * 3 + 2<<" 月                  "<<monthrow * 3 + 3<<" 月"<<endl<<endl;
			cout<<"日 一 二 三 四 五 六   日 一 二 三 四 五 六   日 一 二 三 四 五 六"<<endl;
			for(int j = 0; j < space - 1; j++)
				cout<<"   ";
			for(int j = 1; j <= (7 - space + 1); j++)
				cout<<setw(2)<<j<<" ";
			cout<<"  ";
			m1 = space - 1;
			space = (monthdays[monthrow*3 + 1] + space - 1) % 7 + 1;
			m2 = space - 1;
			for(int j = 0; j < space - 1; j++)
				cout<<"   ";
			for(int j = 1;j <= (7 - space + 1); j++)
				cout<<setw(2)<<j<<" ";
			cout<<"  ";
			space = (monthdays[monthrow*3 + 2] + space - 1) % 7 + 1;
			m3 = space - 1;
			for(int j = 0; j < space - 1; j++)
				cout<<"   ";
			for(int j = 1; j <= (7 - space + 1); j++)
				cout<<setw(2)<<j<<" ";
			cout<<endl;
			space = (monthdays[monthrow*3 + 3] + space - 1) % 7 + 1;
			for(int line = 1; line <= 4; line++)                            //line为每月日期的排数,设置为5排,因第一排之前已经输出,所以此处只有四排
			{
				int n1,n2,n3;
				for(n1 = RowFirstday(m1,line); (n1 < (RowFirstday(m1,line) + 7 )) && (n1 <= monthdays[monthrow*3 + 1]); n1++)
					cout<<setw(2)<<n1<<" ";
				if((line == 4) && (n1 > monthdays[monthrow*3 + 1]))
					for(int j = 0; j < (7 - m2) % 7; j++)
						cout<<"   ";
				if((line == 5) && (n1 > monthdays[monthrow*3 + 1]))
					cout<<"                     ";
				cout<<"  ";
				for(n2 = RowFirstday(m2,line); (n2 < (RowFirstday(m2,line) + 7)) && (n2 <= monthdays[monthrow*3 + 2]); n2++)
					cout<<setw(2)<<n2<<" ";
				cout<<"  ";
				if((line == 4) && (n2 > monthdays[monthrow*3 + 2]))
				{
					while(n2 < (RowFirstday(m2,line) + 7))
					{
						cout<<" "<<setw(2)<<" ";
						n2++;
					}
				}

				for(n3 = RowFirstday(m3,line); (n3 < (RowFirstday(m3,line) + 7)) && (n3 <= monthdays[monthrow*3 + 3]); n3++)
					cout<<setw(2)<<n3<<" ";
				cout<<endl;
			}
			cout<<"------------------------------------------------------------------"<<endl<<endl;
		}
	}
	else                                                            //纵向输出
	{
		int month = 1, day = 1,monthfirstday = 1;
		int monthdays[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
		char Month[12][5] = {"一", "二", "三", "四", "五", "六", "七'", "八", "九", "十", "十一", "十二"};
		if ( IsRunYear(m_year) )
			monthdays[2] = 29;
		monthfirstday = YearFirstday(m_year);
		for (month = 1; month <= 12; month++)
		{
			cout<<endl<<endl<<endl;
			cout<<"                   "<<Month[month-1]<<"月"<<endl<<endl; 
			cout<<" 日  一  二  三  四  五  六"<<endl;
			for (day = 1; day < monthfirstday; day++)
				cout<<"   ";
			for (day = 1; day <= monthdays[month]; day++)
			{
				cout<<" "<<setw(2)<<day<<" ";
				if((day + monthfirstday -1) % 7 == 0)
					cout<<endl;

			}
			monthfirstday =( monthfirstday + monthdays[month]) % 7;
			if (monthfirstday == 0)
				monthfirstday = 7;
			cout<<endl<<"-----------------------------------------"<<endl;
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -