日期计算器
来源:互联网 发布:中国输电网络 编辑:程序博客网 时间:2024/05/17 09:08
一、完成功能
1.日期+/-天数=返回日期
2.日期-日期=返回天数
3.打印某年某月的本月日历;
二、功能分析
见图:
三、源代码
//完成功能//1.日期+/-天数=返回日期//2.日期-日期=返回天数//3.打印某年某月的本月日历;#pragma once;#include<iostream>using namespace std;class Date{friend ostream& operator<<(ostream& os,const Date& d);friend istream& operator>>(istream& is,Date& d);public: Date(int year=1949,int month=10,int day=1)//全缺省构造 :_year(year) ,_month(month) ,_day(day) {}public: Date& operator+(const int& day)//日期+天数=返回日期 { return Approach(*this,day); } Date& operator-(const int& day)//日期-天数=返回日期 { return Past(*this,day); } int operator-(const Date& d2) //日期-日期=返回天数 { int ret=DayNum(*this,d2); return ret; } void PrintfMonth()//打印该日期所在月份日历 { _PrintfMonth(*this); } bool operator<(const Date& d)const//重载小于 { if ((_year<d._year)|| (_year==d._year&&_month<d._month)|| (_year==d._year&&_month==d._month&&_day<d._day)) { return true; } return false; } bool operator==(const Date& d)const//重载等于 { if ((_year==d._year)&&(_month==d._month)&&(_day==d._day)) { return true; } return false; } bool operator!=(const Date& d)const//重载不等于 { if (!(*this==d)) { return true; } return false; } bool operator>(const Date& d)const//重载大于 { if (!(*this<d && *this==d)) { return true; } return false; } void operator++()//重载前置++ { _day+=1; if (_day>GetMonthDay(*this)) { _month+=1; if (_month>12) { _year+=1; _month=1; } _day=1; } }private: Date& Approach(Date& d1, const int& day)//日期+天数=返回日期-----✔ { int Allday=day+d1._day; while(Allday>GetMonthDay(d1)) { Allday=Allday-GetMonthDay(d1); d1._month+=1; if (d1._month>12) { d1._year+=1; d1._month=1; } } d1._day=Allday; return d1; } Date& Past( Date& d1, const int& day)//日期-天数=返回日期-----✔ { int __day=d1._day-day; if (__day>0) { d1._day=__day; } else { d1._month-=1; if (d1._month<1) { d1._month=12; d1._year-=1; } while (GetMonthDay(d1)-(-__day)<=0)//注意等于0,也要向前进一个月 { __day=GetMonthDay(d1)-(-__day); d1._month-=1; if (d1._month<1) { d1._month=12; d1._year-=1; } } d1._day=GetMonthDay(d1)-(-__day); } return d1; } int DayNum( Date& d1,const Date& d2)//日期-日期=返回天数-----✔ { int count=0; if (d1<d2)//d1<d2 { while (d1!=d2) { count++; ++d1; } return count; } else//d1>=d2 { Date tmpd1=(Date)d2; Date tmpd2=d1; DayNum(tmpd1,tmpd2); } } void _PrintfMonth(const Date& d)//打印某个日期当月日历 { cout.width(7); cout<<"日"; cout.width(7); cout<<"一"; cout.width(7); cout<<"二"; cout.width(7); cout<<"三"; cout.width(7); cout<<"四"; cout.width(7); cout<<"五"; cout.width(7); cout<<"六"<<endl; int year=d._year; int month=d._month; if ((d._month==1)||(d._month==2))//将当月1,2月份看为上一年的13,14月份 { year-=1; month+=12; } //******************以下代码只考虑1582年10月4日之后的月历打印*************** //蔡勒公式:w=y+[y/4]+[c/4]-2c+[26(m+1)/10]+d-1 //w:星期; w对7取模得:0-星期日,1-星期一,2-星期二,3-星期三,4-星期四,5-星期五,6-星期六 //y:年(年数后两位数)c:世纪-1(年数前两位数)d:日 //m:月(m大于等于3,小于等于14,即在蔡勒公式中,某年的1、2月要看作上一年的13、14月来计算,比如2003年1月1日要看作2002年的13月1日来计算) int c=int(year/100); int y=year-100*c; //计算当前月份第一天为星期几,d==1 int w=y+int(y/4)+int(c/4)-2*c+(26*(month+1)/10)+1-1;//***一定要注意带方括号取整数的算式,要加上int的强制类型转换 w=(w%7+7)%7;//处理负数的情况----我在这里花了一早上才看出来问题 for (int i=0;i<w;i++)//处理第一行空白处 { cout.width(7); cout<<" "; } for (int i=0;i<7-w;i++)//处理第一行日期 { cout.width(7); cout<<i+1; } cout<<endl; int count=0; for (int i=7-w;i<GetMonthDay(d);i++) { cout.width(7); cout<<i+1; count++; if ((count)/7==1) { cout<<endl; count=0; } } cout<<endl; } bool LeapYear(const Date& d)//判断瑞年 { if ((d._year%4==0 && d._year%100!=0)||(d._year%400==0)) { return true; } return false; } int GetMonthDay(const Date& d)//获取某个月份的天数 { int arr[]={31,28,31,30,31,30,31,31,30,31,30,31}; if(LeapYear(d)&&(d._month==2)) { return arr[1]+1; } else return arr[d._month-1]; }private: int _year; int _month; int _day;};ostream& operator<<(ostream& os,const Date& d){ os<<d._year<<"-"<<d._month<<"-"<<d._day; return os;}istream& operator>>(istream& is, Date& d){ is>>d._year>>d._month>>d._day; return is;}void menu(){ cout<<"*******************欢迎使用日期计算器*************"<<endl; cout<<"*************请根据下面提示按键符号选择功能*************"<<endl; cout<<"************* 1.计算多少天以后日期 *************"<<endl; cout<<"************* 2.计算多少天以前日期 *************"<<endl; cout<<"************* 3.计算二个日期之间的天数 *************"<<endl; cout<<"************* 4.打印某年某月的日历 *************"<<endl; cout<<"************* 0.退出 *************"<<endl;}int main(){ menu(); int n=1; while (n) { cout<<"请按键选择相应的服务:"; cin>>n; switch(n) { case 1: { Date d; int n=0; cout<<"请输入开始日期:"; cin>>d; cout<<"请输入天数:"; cin>>n; cout<<n<<"天以后的日期是:"<<(d+n)<<endl; cout<<"请继续使用"<<endl; break; } case 2: { Date d; int n=0; cout<<"请输入开始日期:"; cin>>d; cout<<"请输入天数:"; cin>>n; cout<<n<<"天以前的日期是:"<<(d-n)<<endl; cout<<"请继续使用"<<endl; break; } case 3: { Date d; Date d1; cout<<"请输入开始日期:"; cin>>d; cout<<"请输入终止日期:"; cin>>d1; cout<<"两个日期之前相差:"<<(d-d1)<<"天"<<endl; cout<<"请继续使用"<<endl; break; } case 4: { cout<<"请输入日期"; Date d; cin>>d; cout<<"本月的日历为:"<<endl; d.PrintfMonth(); break; } case 0: break; default: cout<<"输入错误!"<<endl; break; } } return 0;}
总结:
(1)注意引用的使用可以提高程序的运行效率;
(2)静态成员函数的使用,可以使得面向对象的程序的变得容易;
(3)新类型对象要进行输入输入,必须进行输入输出元运算符,并在类内声明为友缘函数;
(4)善于重在运算符可以使对象之间或者对象自己的有关问题,变得简单;
(5)注意函数的复用会试代码量变小,且问题变得简单,直观;
(6)注意蔡勒公式中对第一次算出的w的值进行 负数处理;
0 0
- 日期计算器
- 日期计算器
- 日期计算器
- 日期计算器
- 日期计算器
- 日期计算器
- 日期计算器
- 日期类->日期计算器
- 日期计算器的实现
- C++ 日期计算器
- 实现一个日期计算器
- 【C++】日期类+日期万年历+日期计算器
- 原生js的日期计算器
- C++日期计算器的实现
- 日期类——日期计算器
- 日期类——日期计算器
- 实现日期类(日期计算器)
- 来个超级大模拟-----日期计算器
- 解决 通过JDBC向MySQL插入数据的中文乱码 问题
- 105. Construct Binary Tree from Preorder and Inorder Traversal
- ggplot作图2
- 去掉Box边框刻度
- 软考是有成本投入的
- 日期计算器
- 关于0.3-0.2 != 0.1 的问题
- VNC远程连接服务器出现 A problem has occurred and the system can't recover. 错误
- 微信模板消息换行
- Android开发之RecyclerView添加头部和底部
- [HDU](2045)LELE的RPG难题
- 【LeetCode】Isomorphic Strings 解题报告
- Java从入门到精通_5 修饰符
- 招聘工资给高些