C++日期计算器的实现

来源:互联网 发布:东方财富dk买卖点源码 编辑:程序博客网 时间:2024/06/06 03:25

先来波剧透:

这里写图片描述

验证

这里写图片描述
具体实现代码如下:

test.h

#include<iostream>using namespace std;class Date{    friend void print_date(int year, int month);//打印日历    friend ostream& operator<<(ostream&_cout, const Date& d);    friend istream& operator>>(istream&_cin, Date& d);public:    int days_aimmonth(int year, int month);//所求月的天数    int week_firstday(int year, int month);//统计目标月份第一天是周几    Date();    Date(int year, int month, int day);    Date(const Date&d);    Date& operator=(const Date& d);    Date& operator+(int day);    Date& operator++(int);    Date& operator++();    Date& operator--();    Date& operator--(int);    int operator-(Date& d);     Date& operator-(int day);    bool operator>(const Date d);    bool operator<(const Date d);    bool operator>=(const Date d);    bool operator<=(const Date d);    bool operator==(const Date d);    bool operator!=(const Date d);private:    int _year;    int _month;    int _day;};bool Judge_leap(int year);//判断是否为闰年static int week_firstday(int year, int month);

date.cpp

#include "test.h"ostream& operator<<(ostream&_cout, const Date& d){    _cout << d._year << "-" << d._month << "-" << d._day;    return _cout;}istream& operator>>(istream&_cin, Date& d){    _cin >> d._year >> d._month >> d._day;    return _cin;}Date::Date(){}Date::Date(int year = 2017, int month = 1, int day = 1):_year(year), _month(month), _day(day){    if (day > days_aimmonth(_year,_month))    {        _day = day - days_aimmonth(_year, _month);        _month += 1;        if (month > 12)        {            _month -= 12;            _year += 1;        }    }}Date::Date(const Date &d){    _year = d._year;    _month = d. _month;    _day = d._day;}Date& Date::operator=(const Date &d){    if (*this != d)    {        _year = d._year;        _month = d._month;        _day = d._day;    }    return *this;}Date& Date::operator+(int day){    _day = _day + day;    while (_day > days_aimmonth(_year, _month))    {        _day = _day - days_aimmonth(_year, _month);        _month += 1;        if (_month > 12)        {            _year += 1;            _month = 1;        }    }    return *this;}Date& Date::operator++(int){    ++_day;    if (_day > days_aimmonth(_year, _month))    {        _day = _day - days_aimmonth(_year, _month);        _month += 1;        if (_month > 12)        {            _year+=1;            _month -= 12;        }    }    return *this;}Date& Date::operator++(){    _day++;    if (_day > days_aimmonth(_year, _month))    {        _day = _day - days_aimmonth(_year, _month);        _month += 1;        if (_month > 12)        {            _year += 1;            _month -= 12;        }    }    return *this;}Date& Date::operator-(int day){    _day = _day - day;    while (_day <= 0)    {        _day += days_aimmonth(_year, _month);        _month -= 1;        if (_month <= 0)        {            _year -= 1;            _month = 12;        }    }    return *this;}int Date::operator-(Date& d) //日期减日期{    Date min(*this);    Date max(d);    int tag = -1;    int count = 0;    if (d < *this)    {        min = d;        max = *this;        tag = 1;    }    while (min != max)    {        ++min;        count++;    }    return tag*count;}Date& Date::operator--(){    _day--;    if (_day == 0)    {        _day = days_aimmonth(_year, _month);        _month -= 1;        if (_month == 0)        {            _year -= 1;            _month = 12;        }    }    return *this;}Date& Date::operator--(int){    --_day;    if (_day == 0)    {        _day = days_aimmonth(_year, _month);        _month -= 1;        if (_month == 0)        {            _year -= 1;            _month = 12;        }    }    return *this;}bool Date::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 Date::operator<(const Date d){    if (_year < d._year)        return true;    else if (_year > d._year)        return false;    else    {        if (_month < d._month)            return true;        else if (_month > d._month)            return false;        else        {            if (_day<d._day)                return true;            else                return false;        }    }}bool Date::operator>=(const Date d){    if (_year >= d._year)        return true;    else if (_month >= d._month)        return false;    else    {        if (_month >= d._month)            return true;        else if (_month <= d._month)            return false;        else        {            if (_day>=d._day)                return true;            else                return false;        }    }}bool Date::operator<=(const Date d){    if (_year <= d._year)        return true;    else if (_month <= d._month)        return false;    else    {        if (_month <= d._month)            return true;        else if (_month <= d._month)            return false;        else        {            if (_day<=d._day)                return true;            else                return false;        }    }}bool Date::operator==(const Date d){    if ((_year == d._year) && (_month == d._month) && (_day == d._day))        return true;    else        return false;}bool Date::operator!=(const Date d){    if (*this == d)        return false;    else        return true;}void print_date(int year, int month)//打印目标月份日历{    cout << year << "年" << month << "月" << endl;    cout << "日 " << "一 " << "二 " << "三 " << "四 " << "五 " << "六 " << endl;    int i = 0;    int j = 0;    Date temp(year, month, 1);    int week = week_firstday(year, month);    int day = temp.days_aimmonth(year, month);    int k = 1;    for (i = 0; i < week; i++)    {        cout << "   ";    }    for (i = 0; i < 6; i++)    {        for (j = 0; j < 7; j++)        {            if (i == 0)            {                if (j == 7 - week)                    break;            }            if (k <= day)                printf("%2d ", k++);        }        cout << endl;    }}static bool Judge_leap(int year)//判断是否为闰年{    if ((year % 4 == 0) || (year % 400 == 0) && (year % 100 != 0))        return true;    else        return false;}int Date::days_aimmonth(int year, int month)//求目标月份天数{    int day[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };    if (Judge_leap(year) == true)    {        day[2] = 29;    }    return day[month];}static int week_firstday(int year, int month){    Date d1(1949, 9, 1);//新中国自1949年开始使用公元纪年法    int week = 4;//公元1949年9月1日为周四    int day = 0;        Date d2(year, month, 1);    day = d2 - d1;    week += day % 7;    if (week > 7)    {        week -= 7;    }    return week;}

test.cpp

#include "test.h"void menu(){    cout << "####  ---请输入想进行的运算的序号---  ####" << endl;    cout << "#### 1.打印目标月份日历(格式:年 月)####" << endl;    cout << "#### 2.相隔天数计算  #####################" << endl;    cout << "#### 3.日期推算(填负数向前推进) ##########" << endl;    cout << "#### 0.退出###############################" << endl;}int main(){    menu();    while (1)    {        int num = 0;        cin >> num;        switch (num)        {        case 1:        {                  cout << "请输入年份月份:";                  int a = 0, b = 0;                  cin >> a;                  cin >> b;                  print_date(a, b);                  break;        }        case 2:            {                  cout << "请输入日期:";                  Date d1;                  cin >> d1 ;                  cout << "请输入日期:";                  Date d2;                  cin >> d2;                  int day = 0;                  day = d1 - d2;                  cout <<"日期相差:"<< day <<"天"<< endl;                break;            }        case 3:            {                  cout << "请输入日期:";                  Date d1,d2;                  cin >> d1;                  cout << "请输入推算的天数:";                  int n = 0;                  cin >> n;                  cout << "推算后日期为:";                  if (n > 0)                  {                      d2 = d1 + n;                  }                  else                  {                      d2 = d1 - (-n);                  }                  cout<<d2;                  break;        }        case 0:            return 0;           }    }    system("pause");    return 0;}
原创粉丝点击