万年历

打印万年历
head.h

#include 
#include 
#include 

using namespace std;

void printMonth(int year, int month);
void printMonthTitle(int year, int month);
void printMonthName(int month);
void printMonthBody(int year, int month);
int getStartday(int year, int month);
int getTotalNumberOfDays(int year, int month);
int getNumberOfDays(int year, int month);
bool LeapYear(int year);

operation.cpp

#include "head.h"

void printMonth(int year, int month)
{
	//打印日历头,体
	printMonthTitle(year, month);
	printMonthBody(year, month);
 }
void printMonthTitle(int year, int month)
{
	//日历头
	printMonthName(month);
	cout << "   " << year << endl;
	cout << "************************************************" << endl;
	cout << "   日  一  二  三  四  五  六" << endl;
   //一个汉字占两个字节注意留空白的个数以保证对称
}
void printMonthBody(int year, int month)
{
	//获取本月第一天是星期几
	int startDay = getStartday(year, month);
	//获取本月天数
	int NumberOfDays = getNumberOfDays(year, month);
	//输出空白
	int i = 0;
	for (i = 0;i < startDay;i++)
		cout << "    ";

	for (i = 1;i <= NumberOfDays;i++)
	{
		cout << setw(4)<<i;
		if ((i + startDay) % 7 == 0)
			cout << endl;
	}
}
void printMonthName(int month)
{
	string s[] = { "一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一","十二" };
	switch (month)
	{
	case 1:
		cout << s[0];
		break;
	case 2:
		cout << s[1];
		break;
	case 3:
		cout << s[2];
		break;
	case 4:
		cout << s[3];
		break;
	case 5:
		cout << s[4];
		break;
	case 6:
		cout << s[5];
		break;
	case 7:
		cout << s[6];
		break;
	case 8:
		cout << s[7];
		break;
	case 9:
		cout << s[8];
		break;
	case 10:
		cout << s[9];
		break;
	case 11:
		cout << s[10];
		break;
	case 12:
		cout << s[11];
		break;
	}
}
int getStartday(int year, int month)
{
	int startDay1900 = 1;
	int totalnumberofday = getTotalNumberOfDays(year, month);
	return (startDay1900 + totalnumberofday) % 7;
}
int getTotalNumberOfDays(int year, int month)
{
	int total = 0;
	for (int i = 1990;i < year;i++)
	{
		if (LeapYear(i))
			total += 366;
		else
			total += 365;
		for (int i = 0;i < month;i++)
			total += getNumberOfDays(year, i);
		return total;
	}
}
int getNumberOfDays(int year, int month)
{
	//获取每月具体天数
	if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
		return 31;
	if (month == 4 || month == 6 || month == 9 || month == 11)
		return 30;
	if (month = 2)
		return LeapYear(year) ? 29 : 28;
	return 0;

}
bool LeapYear(int year)
{
	return (year % 400 == 0 || year % 4 == 0 && year % 100 != 0);
}

printCalender.cpp

#include "head.h"

int main()
{
	cout << "请输入年份:" << endl;
	int year;
	cin >> year;
	cout << "请输入月份:" << endl;
	int month;
	cin >> month;

	if (month < 1 || month>12)
		cout << "输入错误请重新输入";
	else
		printMonth(year, month);
	return 0;
}

你可能感兴趣的:(C++,c++)