C++小项目---日历系统

#include 
#include 
using namespace std;
class CDateTime 
{
private:
	int year;
	int month;
	int day;
	int hour;
	int min;
	int sec;
public:
	CDateTime(int y = 1, int m = 1, int d = 1, int H = 0, int M = 0, int s = 0)
		:year{ y }, month{ m }, day{ d }, hour{ H }, min{ M }, sec{ s }
	{

	}
	void Now()
	{
		time_t te;
		tm tmp;
		time(&te);
		localtime_s(&tmp, &te);
		year = tmp.tm_year + 1900;
		month = tmp.tm_mon + 1;
		day = tmp.tm_mday;
		hour = tmp.tm_hour;
		min = tmp.tm_min;
		sec = tmp.tm_sec;
		printf("Now: %dyear %dmonth %dday %dh %dm %ds\n\n", year, month, day, hour, min, sec);
		PrintDate();
	}

	void NextMonth()
	{
		month += 1;
		if (month > 12)
		{
			year += 1;
		}
		month %= 12;
		printf("Next Month:\n");
		PrintDate();
	}
	void PreviousMonth()
	{
		month -= 1;
		if (month < 1)
		{
			year -= 1;
			month = 12;
		}
		printf("Previous Month:\n");
		PrintDate();
	}
	void NextYear()
	{
		year += 1;
		printf("Next Year:\n");
		PrintDate();
	}
	void  PreviousYear()
	{
		year -= 1;
		if (year < 1)
		{
			printf("error year\n");
			return;
		}
		printf("Previous Year:\n");
		PrintDate();
	}
	void MonthFirstDay_Is_WeekDay(int& weekday)const 
	{
		int c = year / 100;
		int y = year % 100;
		int m = 0;
		if (m == 1 || m == 2)
		{
			m += 12;
		}
		else
		{
			m = month;
		}
		int d = 1;
		weekday = (y + y / 4 + c / 4 - 2 * c + 26 * (m + 1) / 10 + d - 1)%7;
		//日   一   二   三   四   五   六
		// 0   1    2    3    4    5    6    
	}

	bool IsLeap()const
	{
		return (year % 4 == 0 && year % 100 == 0) || (year % 400 == 0);
	}

	int GetMonth_Day()const
	{
		static const int days[]{ 29,31,28,31,30,31,30,31,31,30,31,30,31 };
		//  0  1  2  3  4  5  6  7  8  9  10 11 12
		int m = month;
		if (m == 2 && IsLeap())
		{
			m = 0;
		}
		return days[m];
	}

	void PrintDate()const
	{
		printf("%dyear %dmonth\n", year, month);
		printf("日   一   二   三   四   五   六\n");
		int weekday = 0;
		MonthFirstDay_Is_WeekDay(weekday);
		int arr[6][7]{};
		int count = 1;
		int j = weekday;
		int d = GetMonth_Day();
		int tag = 0;
		for (int i = 0; i < 6; i++)
		{
			for (; j < 7; j++)
			{
				arr[i][j] = count++;
				if (count > d)
				{
					tag = 1;
					break;
				}
			}
			if (tag)
			{
				break;
			}
			j = 0;
		}
		int m = weekday;
		if (m == 0)
		{
			printf("");
		}
		else if(m == 1)
		{
			printf("     ");
		}
		else if(m == 2)
		{
			printf("     ");
			printf("     ");
		}
		else if (m == 3)
		{
			printf("     ");
			printf("     ");
			printf("     ");
		}
		else if (m == 4)
		{
			printf("     ");
			printf("     ");
			printf("     ");
			printf("     ");
		}
		else if (m == 5)
		{
			printf("     ");
			printf("     ");
			printf("     ");
			printf("     ");
			printf("     ");
		}
		else
		{
			printf("     ");
			printf("     ");
			printf("     ");
			printf("     ");
			printf("     ");
			printf("     ");
		}
		int k = weekday;
		for (int i = 0; i < 6; i++)
		{
			for (; k < 7; k++)
			{
				if (arr[i][k] != 0)
				{
					printf("%2d   ", arr[i][k]);
				}
			}
			k = 0;
			printf("\n");
		}
	}

	void Menu()
	{
		printf("************欢迎使用LoveHoneyYou的日历**********\n");
		printf("*******************当前日历***********************\n");
		Now();
		printf("**********   0.  按0退出            **********\n");
		printf("**********   1.  获取下一个月的日历  **********\n");
		printf("**********   2.  获取上一个月的日历  **********\n");
		printf("**********   3.  获取上一个年的日历  **********\n");
		printf("**********   4.  获取下一个年的日历  **********\n");
		int n = 0;
		printf("请输入你的选择-> ");
		scanf_s("%d", &n);
		while(n != 0)
		if (n == 1)
		{
			NextMonth();
			printf("请输入你的选择-> ");
			scanf_s("%d", &n);
		}
		else if (n == 2)
		{
			PreviousMonth();
			printf("请输入你的选择-> ");
			scanf_s("%d", &n);
		}
		else if (n == 3)
		{
			PreviousYear();
			printf("请输入你的选择-> ");
			scanf_s("%d", &n);
		}
		else if (n == 4)
		{
			NextYear();
			printf("请输入你的选择-> ");
			scanf_s("%d", &n);
		}
		else
		{
			printf("输入有误!\n");
			printf("请输入你的选择-> ");
			scanf_s("%d", &n);
		}
	}
};

int main()
{
	CDateTime c1;

	c1.Menu();
	return 0;
}

C++小项目---日历系统_第1张图片

C++小项目---日历系统_第2张图片 

 

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