当前位置: 首页 > 编程笔记 >

C++实现万年历功能

万俟小林
2023-03-14
本文向大家介绍C++实现万年历功能,包括了C++实现万年历功能的使用技巧和注意事项,需要的朋友参考一下

本文实例为大家分享了C++实现万年历的具体代码,供大家参考,具体内容如下

1.此万年历功能

1>日期加减天数

2>日期与日期之间的差值

3>输入年月显示当月日历

2.代码实现

#include<iostream>
#include<iomanip>
using namespace std;
 
class Date
{
public:
 Date(int year = 1990, int month = 1, int day = 1) //构造函数
 :_year(year), _month(month), _day(day)
 {
 if (JudgeRightDate())   //判断传入的值是否是合法的,不合法则置成1990年1月1日
 {
  _year = 1990;
  _month = 1;
  _day = 1;
 }
 }
 
 bool JudgeRightDate()  //判断值是否合法函数
 {
 if (_year < 1 || ((_month< 1)||_month>12) ||
  (_day<1)||_day>GetMonthDay(_year,_month))
 {
  return true;
 }
 else
 {
  return false;
 }
 }
 
 int JudgeYear(int year)  //判断是否是闰年的函数
 {
 if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
 {
  return 1;
 }
 else
  return 0;
 }
 
 int GetMonthDay(int year, int month) //通过年和月得到对应的天数
 {
 int arr[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
 int days = arr[month];
 if (month == 2)
 {
  days += JudgeYear(year);  //如果是闰年的二月则天数加1
 }
 return days;
 }
 
 
 Date operator +(int days)  //日期加天数函数,重载“+”实现
 {
 _day += days;      //先将天数全部加到所给日期的“天”上
 GetRightDate(_year, _month, _day); //再通过计算得到正确的日期。
 return *this;
 }
 
 void GetRightDate(int &year, int &month, int &day)  //计算出正确的日期
 {
 if (day <= 0)
 {
  while (day <= 0)
  {
  month--;
  day += GetMonthDay(year, month);
  if (month < 1)
  {
   year--;
   month = 13;
  }
  }
 }
 else
 {
  while (day>GetMonthDay(year, month))
  {
  day -= GetMonthDay(year, month);
  month++;
  if (month > 12)
  {
   year++;
   month = 1;
  }
  }
 }
 }
 
 Date operator -(int days)  //重载“-”实现日期减天数
 {
 _day -= days;
 GetRightDate(_year, _month, _day);
 return *this;
 }
 
 bool operator >(const Date &d)  //判断两个日期的大小
 {
 if (_year > d._year)
 {
  return true;
 }
 else if (_year == d._year)
 {
  if (_month > d._month)
  {
  return true;
  }
  else if (_month==d._month)
  {
  if (_day > d._day)
  {
   return true;
  }
  }
 }
 return false;
 }
 
 bool operator ==(const Date &d)  //判断两个日期是否相等
 {
 if (_year == d._year && _month == d._month && _day == d._day)
 {
  return true;
 }
 else
  return false;
 }
 
 int operator -( Date &d) //计算日期差函数,重载“-”实现
 {
 int count = 0;
 Date tmp(*this);
 if (*this > d)
 {
  tmp = d;
  d = *this;
  *this = tmp;
 }
 while (!(*this==d))
 {
  count++;
  *this =*this+1;
 }
 return count;
 }
 
 
 void print()    //打印函数
 {
 cout << _year << "-" << _month << "-" << _day;
 }
 
 
 int week()  //求出日期对应的星期函数
 {
 int w = 0;
 int y = _year;
 int m = _month;
 if (m == 1 || m == 2)
 {
  m = _month + 12;
  y = _year - 1;
 }
 w = _day + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400;
 w = w % 7 + 1;
 return w;
 }
 
 void print_week()
 {
 cout << "星期日 星期一 星期二 星期三 星期四 星期五 星期六" << endl;
 }
 
 void print_day() //根据日期和星期,正确的输出日历
 {
 int line = 1;
 int days = GetMonthDay(_year,_day);
 int w = week();
 if (w != 7)
 {
  for (int blank = w - 1; blank; --blank, ++line)
  {
  cout << setw(7) << "";
  }
 }
 for (int d = 1; d <= days; ++d, ++line)
 {
  cout << setw(7) << d;
  if (line % 7 == 0)
  {
  cout << endl;
  }
 }
 cout << endl;
 }
 
private:
 int _year;
 int _month;
 int _day;
};
void menu()
{
 cout << setw(40) <<"万年历"<< endl;
 cout << "1.日期加减天数" << endl;
 cout << "2.日期减日期" << endl;
 cout << "3.输入年月显示当月日历" << endl;
}
void choice()
{
 int num = 0;
 int year, month, day, days;
 char ch = '+';
 cin >> num;
 if (num == 1)
 {
 cout << "请输入日期:" << endl;
 cin >> year >> month >> day;
 cout << "请输入天数:" << endl;
 cin >> days;
 cout << "请输入'+'或者'-':" << endl;
 cin >> ch;
 Date d1(year, month, day);
 Date d2;
 if (ch == '+')
 {
  d2 = d1 + days;
 }
 else if (ch == '-')
 {
  d2 = d1 - days;
 }
 else
 {
  cout << "无效的输入!" << endl;
 }
 cout << "计算后的日期为:";
 d2.print();
 cout << endl;
 }
 else if (num==2)
 {
 cout << "请输入日期:" << endl;
 cin >> year >> month >> day;
 Date d3(year, month, day);
 cout << "请输入日期:" << endl;
 cin >> year >> month >> day;
 Date d4(year, month, day);
 int ret = d3 - d4;
 cout << "期间相差:" << ret << endl;
 }
 else if (num == 3)
 {
 cout << "请输入年月:" << endl;
 cin >> year >> month;
 Date d5(year, month);
 d5.print_week();
 d5.print_day();
 }
}
int main()
{
 menu();
 choice();
 system("pause");
 return 0;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持小牛知识库。

 类似资料:
  • 本文向大家介绍C++实现简易万年历,包括了C++实现简易万年历的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了C++实现简易的万年历,供大家参考,具体内容如下 代码如下: 运行结果: 代码中没有检查输入错误的机制,写的比较粗糙,有许多错误之处望指正。 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐喊教程。

  • 本文向大家介绍C语言实现万年历小程序,包括了C语言实现万年历小程序的使用技巧和注意事项,需要的朋友参考一下 一、杂谈 大一学了C之后一直困惑,C到底怎么用?它不像HTML那么直观,也没有SQL那么常用,更没有Java那么功能强大,那他为何还存在,并依然火热呢? 答案很简单:编程语言是一家,C语言结构简单,但所蕴含的逻辑思维和其他语言大致相同,适合初学者。 编程不是一蹴而就,能力需要日积月累,推荐想

  • 本文向大家介绍Java 实现万年历总结,包括了Java 实现万年历总结的使用技巧和注意事项,需要的朋友参考一下 一,Java实现万年历的代码: 二.一个Java万年历,比较简单的那种,显示年月日、星期几、当前日期是第几周、显示闰年、打印万年历等,还可显示当前日期是一年中的第几天,指定日期是星期几等,采用了基姆拉尔森计算公式 ,W= (d+2*m+3*(m+1)/5+y+y/4-y/100+y/40

  • 本文向大家介绍PHP制作万年历,包括了PHP制作万年历的使用技巧和注意事项,需要的朋友参考一下 使用PHP实现万年历功能的要点: 得到当前要处理的月份总共有多少天$days 得到当前要处理的月份的一号是星期几$dayofweek $days的作用:知道要处理的月份共有多少天,就可以通过循环输出天数了 $dayofweek的作用:只有知道每个月的1号是星期几,才能知道在输出天数之前需要输出多少空格(

  • 本文向大家介绍python万年历实现代码 含运行结果,包括了python万年历实现代码 含运行结果的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了python实现万年历的具体代码,供大家参考,具体内容如下 运行效果: 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐喊教程。

  • 本文向大家介绍批处理万年历实现代码(包括农历日期),包括了批处理万年历实现代码(包括农历日期)的使用技巧和注意事项,需要的朋友参考一下 核心源码 以下是各计算部分算法: 计算星期: 基姆拉尔森计算公式 W= (d+2*m+3*(m+1)/5+y+y/4-y/100+y/400) mod 7 在公式中d表示日期中的日数+1,m表示月份数,y表示年数。 注意:在公式中有个与其他公式不同的地方: 把一月