栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

【C++】万年历程序

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【C++】万年历程序

文章目录
  • 前言
  • 具体模块
    • 1.年月判断
    • 2.符号重载
    • 3.判断星期几
    • 4.月历
  • 完整cpp
  • 总结


前言

这是一个由C++编写的万年历程序。具有加减天数、两日期差值、当月月历、系统时间、星期功能。部分参考于网络。


具体模块 1.年月判断

代码如下(示例):

	int getMonthDay(int year, int month)//月份
	{
		static int MonthDay[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
		if (month == 2 && isLeap(year))
		{
			return 29;
		}
		return MonthDay[month];
	}

	bool isInvaild()//判断日期格式是否正确
	{
		if (_day > 0 && _day <= getMonthDay(_year, _month) && _month < 13 && _month>0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	bool isLeap(int y)//判断闰年
	{
		if ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
2.符号重载

代码如下(示例):

	Date operator+(int day)
	{
		Date& tmp = *this;
		//如果天数为负数,则可以看作是减一个天数
		if (day < 0)
		{
			return *this - (-day);
		}
		_day = day + _day;
		while (_day > tmp.getMonthDay(_year, _month))
		{
			_day -= tmp.getMonthDay(_year, _month);
			_month++;
			if (_month > 12)
			{
				_year++;
				_month = 1;
			}
		}
		return tmp;
	}
	//实现一个天数的相减,返回一个DATE类
	Date operator-(int day)
	{
		Date& tmp = *this;

		//如果天数小于零,可以看作是加一个天数
		if (day < 0)
		{
			return *this + (-day);
		}
		_day -= day;//若day>_day,得数则为负
		while (isInvaild() == false)
		{
			if (_month == 1)
			{
				_year--;
				_month = 12;
			}
			else
			{
				_month--;
			}
			tmp._day = tmp._day + tmp.getMonthDay(_year, _month);//加上一个数时
		}
		return tmp;
	}


	Date operator+=(int day)
	{
		*this = *this + day;
		return *this;
	}
	Date operator-=(int day)
	{
		*this = *this - day;
		return *this;
	}

		int operator-( Date& d)//一个日期减上一个日期是多少天
	{
		int sum1 = 0;//被减数
		for (int month = 1; month < _month; month++)
		{
			//cout << getMonthDay(_year, month)<
			//cout << getMonthDay(_year, month) << endl;
			sum2 += getMonthDay(d._year, month);
		}
		sum2 += d._day;
		//cout << d._month << " " << d._day << endl;
		int sum = sum1 - sum2;
		//cout << sum1 << "   " << sum2 << endl;
		int tmp = 0;
		if (_year == d._year)
		{
			
			return abs(sum);
		}
		else if (_year > d._year)
		{
			for (int year = d._year; year < _year; year++)
			{
				if (isLeap(year))
				{
					tmp += 366;
				}
				else
				{
					tmp += 365;
				}
			}
			return abs(tmp + sum);
		}
		else if (_year < d._year)
		{
			for (int year = _year; year < d._year; year++)
			{
				if (isLeap(year))
				{
					tmp += 366;
				}
				else
				{
					tmp += 365;
				}
			}
			return abs(tmp - sum);
		}
	}
3.判断星期几

代码如下(示例):

void CaculateWeekDay()//判断星期几
{
	if (_month == 1 || _month == 2) //把一月和二月换算成上一年的十三月和是四月  
	{
		_month += 12;
		_year--;
	}
	int Week = (_day + 2 * _month + 3 * (_month + 1) / 5 + _year + _year / 4 - _year / 100 + _year / 400) % 7;
	switch (Week)
	{
	case 0:  cout << " 星期一" << endl; break;
	case 1:  cout << " 星期二" << endl; break;
	case 2:  cout << " 星期三" << endl; break;
	case 3:  cout << " 星期四" << endl; break;
	case 4:  cout << " 星期五" << endl; break;
	case 5:  cout << " 星期六" << endl; break;
	case 6:  cout << " 星期日" << endl; break;
	default: cout << "error!" << endl;
	}
}
4.月历
void Setmonth()//月历
{
	int daycount = getMonthDay(_year, _month);//算出这个月的天数
	int space = (1 + 2 * _month + 3 * (_month + 1) / 5 + _year + _year / 4 - _year / 100 + _year / 400) % 7 +1;
		//这里算出来这个月的1号前面需要几个空格;等于0的话,前面就不需要空格
	cout << endl;
	cout << "---- " << _year << "年" << _month << "月" << "----" << endl;
		cout << "Sun   Mon   Tue   Wed   Thu   Fri   Sat" << endl;
	for (int j = 0; j < space; j++)
	{
		cout << "      ";
	}
		
	for (int i = 1, j = space + 1; i <= daycount; i++, j++)//开始循环,输出每月的号
	{
		if (i < 10)//单位
			cout << i << "     ";
		if (i >= 10)//双位
			cout << i << "    ";
		if (j % 7 == 0)
			cout << endl;
	}
	cout << endl;
	cout << endl;
}

完整cpp
#include
using namespace std;
#include//当它被执行时,判断 expression 的真假,如果为假,它就向标准错误打印一条诊断信息并终止程序。
//#include
#include
//#define _CRT_SECURE_NO_WARNINGS 1

class Date
{
public:

	Date(int year = 1900, int month = 1, int day = 1)
	{
		this->_year = year;
		this->_month = month;
		this->_day = day;
		assert(isInvaild());
	}
	void print()
	{
		cout << _year << "-" << _month << "-" << _day ;
	}

	int getMonthDay(int year, int month)//月份
	{
		static int MonthDay[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
		if (month == 2 && isLeap(year))
		{
			return 29;
		}
		return MonthDay[month];
	}

	bool isInvaild()//判断日期格式是否正确
	{
		if (_day > 0 && _day <= getMonthDay(_year, _month) && _month < 13 && _month>0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	bool isLeap(int y)//判断闰年
	{
		if ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0))
		{
			return true;
		}
		else
		{
			return false;
		}
	}


	//加一个天数,自动跳转应到的日期
	Date operator+(int day)
	{
		Date& tmp = *this;
		//如果天数为负数,则可以看作是减一个天数
		if (day < 0)
		{
			return *this - (-day);
		}
		_day = day + _day;
		while (_day > tmp.getMonthDay(_year, _month))
		{
			_day -= tmp.getMonthDay(_year, _month);
			_month++;
			if (_month > 12)
			{
				_year++;
				_month = 1;
			}
		}
		return tmp;
	}
	//实现一个天数的相减,返回一个DATE类
	Date operator-(int day)
	{
		Date& tmp = *this;

		//如果天数小于零,可以看作是加一个天数
		if (day < 0)
		{
			return *this + (-day);
		}
		_day -= day;//若day>_day,得数则为负
		while (isInvaild() == false)
		{
			if (_month == 1)
			{
				_year--;
				_month = 12;
			}
			else
			{
				_month--;
			}
			tmp._day = tmp._day + tmp.getMonthDay(_year, _month);//加上一个数时
		}
		return tmp;
	}


	Date operator+=(int day)
	{
		*this = *this + day;
		return *this;
	}
	Date operator-=(int day)
	{
		*this = *this - day;
		return *this;
	}



	int operator-( Date& d)//一个日期减上一个日期是多少天
	{
		int sum1 = 0;//被减数
		for (int month = 1; month < _month; month++)
		{
			//cout << getMonthDay(_year, month)<
			//cout << getMonthDay(_year, month) << endl;
			sum2 += getMonthDay(d._year, month);
		}
		sum2 += d._day;
		//cout << d._month << " " << d._day << endl;
		int sum = sum1 - sum2;
		//cout << sum1 << "   " << sum2 << endl;
		int tmp = 0;
		if (_year == d._year)
		{
			
			return abs(sum);
		}
		else if (_year > d._year)
		{
			for (int year = d._year; year < _year; year++)
			{
				if (isLeap(year))
				{
					tmp += 366;
				}
				else
				{
					tmp += 365;
				}
			}
			return abs(tmp + sum);
		}
		else if (_year < d._year)
		{
			for (int year = _year; year < d._year; year++)
			{
				if (isLeap(year))
				{
					tmp += 366;
				}
				else
				{
					tmp += 365;
				}
			}
			return abs(tmp - sum);
		}
	}

void CaculateWeekDay()//判断星期几
{
	if (_month == 1 || _month == 2) //把一月和二月换算成上一年的十三月和是四月  
	{
		_month += 12;
		_year--;
	}
	int Week = (_day + 2 * _month + 3 * (_month + 1) / 5 + _year + _year / 4 - _year / 100 + _year / 400) % 7;
	switch (Week)
	{
	case 0:  cout << " 星期一" << endl; break;
	case 1:  cout << " 星期二" << endl; break;
	case 2:  cout << " 星期三" << endl; break;
	case 3:  cout << " 星期四" << endl; break;
	case 4:  cout << " 星期五" << endl; break;
	case 5:  cout << " 星期六" << endl; break;
	case 6:  cout << " 星期日" << endl; break;
	default: cout << "error!" << endl;
	}
}

void Setdate() // 类体内定义成员函数
{
	cin >> _year;
	cin >> _month;
	cin >> _day;
}

void Setmonth()//月历
{
	int daycount = getMonthDay(_year, _month);//算出这个月的天数
	int space = (1 + 2 * _month + 3 * (_month + 1) / 5 + _year + _year / 4 - _year / 100 + _year / 400) % 7 +1;
		//这里算出来这个月的1号前面需要几个空格;等于0的话,前面就不需要空格
	cout << endl;
	cout << "---- " << _year << "年" << _month << "月" << "----" << endl;
		cout << "Sun   Mon   Tue   Wed   Thu   Fri   Sat" << endl;
	for (int j = 0; j < space; j++)
	{
		cout << "      ";
	}
		
	for (int i = 1, j = space + 1; i <= daycount; i++, j++)//开始循环,输出每月的号
	{
		if (i < 10)//单位
			cout << i << "     ";
		if (i >= 10)//双位
			cout << i << "    ";
		if (j % 7 == 0)
			cout << endl;
	}
	cout << endl;
	cout << endl;
}

private:
	int _year;
	int _month;
	int _day;
};

ostream& operator<<(ostream& cout, Date& da) //cout<		//左移运算符重载
	da.print();

	return cout;
}

void test()
{
}


void wannianli()
{
	int sel = 0;
	Date date1(2022, 3, 28);
	while (1)
	{	
		cout << "-----------------By KLIM---" << endl;
		SYSTEMTIME sys;
		GetLocalTime(&sys);
		Date datenow(sys.wYear, sys.wMonth, sys.wDay);
		cout << "今天日期: " << datenow;
		datenow.CaculateWeekDay();
		cout << "---------------------------" << endl;
		cout << "--------------" << endl;
		
		cout << "date1: " << date1 << endl;
		cout << "--------------" << endl;
		cout << "输入选择(1.加天数 2.减天数 3.日期差 4.距今几天 5.重设date1日期 6.月历 0.exit):";

		cin >> sel;
		switch (sel)
		{
		case 1://+++++++
		{
			cout << "--------------" << endl;
			cout << "输入一个加数:";
			int a = 0;
			cin >> a;
			date1 += a;
			cout << "--------------" << endl;
			cout << "date1加" << a << "天为:" << date1;
			date1.CaculateWeekDay();
			cout << "--------------" << endl;
			system("pause");
			system("cls");
		}break;
		case 2://-------
		{
			cout << "--------------" << endl;
			cout << "输入一个减数:";
			int b = 0;
			cin >> b;
			date1 -= b;
			cout << "--------------" << endl;
			cout << "date1减" << b << "天为:" << date1;
			date1.CaculateWeekDay();
			cout << "--------------" << endl;
			system("pause");
			system("cls");
		}break;
		case 3:
		{
			Date date2(1998, 11, 3);
			cout << "设置一个日期(yyyy mm dd):";
			date2.Setdate();
			
			cout << date2 << "和" << date1 << "相差" << date2 - date1 << "天" << endl;
			system("pause");
			system("cls");
		}break;
		case 4:
		{
			cout << datenow << "和" << date1 << "相差" << datenow - date1 << "天" << endl;
			system("pause");
			system("cls");
		}break;
		case 5:
		{
			cout << "重设date1日期(yyyy mm dd):";
			date1.Setdate();
			system("pause");
			system("cls");
		}break;
		case 6:
		{
			date1.Setmonth();
			system("pause");
			system("cls");
		}break;
		case 0://退出
		{
			exit(0);
		}break;

		}
	}
}

int main()
{
	//test();

	wannianli();

	//system("pause");
	return 0;
}

总结

c++新手,敬请各位指教

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/997583.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号