万年历查询系统

#include <iostream>
#include<iomanip>
#include<cstring>
#include<string>
using namespace std;

int m[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
string xq[7] = { "星期日","星期一","星期二","星期三","星期四","星期五","星期六" };

bool run(int x)
{

	if (x % 400 == 0)return true;
	if (x % 100 != 0 && x % 4 == 0)return true;
	return false;

}

//1980年1月1日是星期二
int pandunaxq(int year, int month, int day)
{
	int sum = 0;
	for (int i = 1980; i < year; i++)
	{
		if (run(i))
			sum += 366;
		else 
			sum += 365;
	}

	for (int i = 1; i < month; i++)
	{
		if (run(year) && i == 2)
			sum+=1;
		sum += m[i];
	}
	sum += day -1;
	int suoyin = (sum + 2) % 7;
	return suoyin;

}
void printf_yue(int year, int month, bool p)
{
	int d = pandunaxq(year, month, 1);

	if (p)
		m[2] += 1;


	int cnt = d + 1;

	for (int i = 0; i < 7; i++)
	{
		cout << setw(6)<<xq[i] << " ";
	}

	cout << endl;

	for (int i = 0; i < d; i++)
		cout << setw(6) << " " << " ";

	for (int i = 1; i <= m[month]; i++)
	{

		cout << setw(6) << i << " ";

		if (cnt == 7)
		{
			cout << endl;
			for (int i = 0; i < 7; i++)
			{
				cout << setw(6) << xq[i] << " ";
			}
			cnt = 0;
			cout << endl;
		}

		cnt++;

	}

	if (p)m[2] -= 1;

	cout << endl << endl;
}


int main()
{

	cout << "---------------" << endl;
	cout << "|   万年历     |" << endl;
	cout << "---------------" << endl;


	while (1)
	{

		cout << "输入操作" << endl;

		cout << "1.查询某年的日历" << endl;
		cout << "2.查询某月的日历" << endl;
		cout << "3.查询某日的星期" << endl;

		int cz;

		cin >> cz;
		if (cz != 1 && cz != 2 && cz != 3)
		{
			cout << "输入错误" << endl;
			cout << endl;
			continue;
		}
		cout << endl;
		int year, month, day;
		bool p;



		if (cz == 1)
		{
			cout << "请输入年份" << endl;
			cin >> year;
			p = run(year);
			for (int i = 1; i <= 12; i++)
			{
				cout << i << "月:" << endl;
				printf_yue(year, i, p);
			}

		}

		if (cz == 2)
		{
			cout << "请输入年份和月份" << endl;
			cin >> year >> month;
			p = run(year);
			cout << month << "月:" << endl;
			printf_yue(year, month, p);

		}

		if (cz == 3)
		{
			cout << "请输入年份和月份和日" << endl;
			cin >> year >> month >> day;
			int d = pandunaxq(year, month, day);
			cout << endl;
			cout << xq[d] << endl << endl;

		}


	}

	return 0;
}