文章目录

  • 前言
  • 具体模块
    • 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)<<endl;sum1 += getMonthDay(_year, month);}sum1 += _day;//cout << _month << " " << _day << endl;int sum2 = 0;//date1for (int month = 1; month < d._month; 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<iostream>
using namespace std;
#include<assert.h>//当它被执行时,判断 expression 的真假,如果为假,它就向标准错误打印一条诊断信息并终止程序。
//#include<stdlib.h>
#include<Windows.h>
//#define _CRT_SECURE_NO_WARNINGS 1class 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)<<endl;sum1 += getMonthDay(_year, month);}sum1 += _day;//cout << _month << " " << _day << endl;int sum2 = 0;//date1for (int month = 1; month < d._month; 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;
{       //左移运算符重载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++新手,敬请各位指教

【C++】万年历程序相关推荐

  1. c语言万年历查询程序代码,C语言 万年历程序(示例代码)

    C语言 万年历程序 原代码:[email protected]:~/c++$ cat 123.c #include #define Mon   1 #define Tues  2 #define We ...

  2. python编写万年历程序

    好的,下面是编写万年历程序的一些建议. 首先,你可以使用 Python 的 calendar 模块来获取每个月的日历信息.这个模块提供了很多有用的函数,例如 isleap 函数可以用来判断某一年是否是 ...

  3. c语言万年历程序原理,C语言实现万年历程序

    C语言实现万年历程序 #include int year(int y) { if ((y%4==0) && (y%100!=0) || y%400==0) return 366; el ...

  4. c语言万年历代码作业,用c语言编写万年历程序

    用c语言编写万年历程序 <C 程序设计>课程设计报告 2011-2012学年第二学期 设计题目:万年历的设计 指导教师: 李素若 完成时间:2012 年 6月 1日至 2011年 6月 2 ...

  5. 用linux下的C语言编程万年历,C语言 万年历程序

    C语言 万年历程序 原代码:chunli@Linux:~/c++$ cat 123.c #include #define Mon   1 #define Tues  2 #define Wed   3 ...

  6. 使用 C++ 编写万年历程序

    输入要查看的年份(要求为公元元年以后),就可以输出这一年每个月份的天数. 例如输入:2018 本程序为实现万年历的功能,请输入要查找的年份: 2018 一月 星期一  星期二  星期三  星期四  星 ...

  7. c语言万年历程序设计方案,万年历程序设计c语言代码

    万年历程序设计 这是当时我做的一个小小的课题,希望对你有所帮助 #include "stdio.h" /* Required for MS-DOS use */ #define E ...

  8. 原 C语言实现万年历程序,C语言实现万年历源码

    本文实例为大家分享了C语言实现万年历的具体代码,供大家参考,具体内容如下 主函数所在源码 #include #include #include int GetWeek(int year,int mon ...

  9. python编写含农历,节气的万年历程序

    好的,那么我们可以先了解一下 Python 中如何获取当前的日期和时间.我们可以使用 Python 标准库中的 datetime 模块来获取当前的日期和时间. 下面是一个简单的例子,展示了如何使用 d ...

  10. 万年历程序加入农历c语言,大神们日历程序里面怎么加农历(附程序)

    时钟的时钟线引脚*/ sbit T_IO=P2^1; /* 实时时钟的数据线*/ sbit T_RST=P2^2; /* 实时时钟的复位线引脚*/ uchar bdata datbyte; sbit ...

最新文章

  1. python pandas_Python库Pandas数据可视化实战案例
  2. Apache软件基金会宣布Apache Unom成为顶级项目
  3. PowerDesigner 小工具窗
  4. mysql语句表_mysql表级sql语句
  5. 第一章 代码无错就是优吗?(简单工厂模式)
  6. war包部署-配置入口类
  7. (cljs/run-at (JSVM. :all) 一次说白DataType、Record和Protocol)
  8. CentOS SSH公钥登录问题
  9. [css] 手写一个满屏品字布局的方案
  10. 模拟iic和硬件iic区别_技术货:IIC总线的FPGA实现
  11. 18.数组(一)之认识java数组
  12. mysql和虚拟主机区别_香港空间购买,香港虚拟主机购买,香港免备案空间购买
  13. 没项目经验,这7个前端项目让你脱颖而出
  14. 计算机服务器硬件组成
  15. go 时间的操作(比较,增加)
  16. 基于 OpenSSL 生成自签名证书
  17. ios 根据日期知道周几_iOS-通过日期计算是周几
  18. 利用端端Clouduolc的双向同步和单向同步,打造多机热备份的文件下载服务器
  19. 2019-10-06 因果系统的理解
  20. linux--设置屏幕的锁屏时间

热门文章

  1. vue引入SuperMap超图 引入流程及报错处理
  2. CCNA考试题库中英文翻译版及答案13
  3. JVM - 类加载器
  4. 不使用CAD转换工具,你能转换CAD文件格式吗?
  5. 开关电源(DC/DC)和线性电源(LDO低压差线性稳压器)的区别
  6. lldp协议代码阅读_LLDP 链路层发现协议
  7. 嵌入式操作系统-ucos是什么?
  8. DW——验证注册页面 设计JavaScript
  9. VC++6.0软件安装教程(win10亲测可用)
  10. 浅谈IDEA Scratch files万能的临时文件功能