曰期计算器java,日期计算器

标签:

实现日期加减天数,得到一个日期;日期减日期,得到一个天数。

#include

#include

#include

using namespace std;

//添加打印指定月的信息,一个月的每一天和对应

enum

{

zero,

one,

tow,

three

};

class Date

{

public:

Date(int year = 2016, int month = 7, int day = 20, int week = 3) :_year(year)

, _month(month)

, _day(day)

, _week(week)

{

}

Date operator+ (int day);

Date operator- (int day);

int operator- (const Date& d);

void display()const;

friend int bissextile(const Date&, const Date&);

friend istream& operator>>(istream& is, Date& d);

int count() const;

//private:

int _year;

int _month;

int _day;

int _week;

static int month[12];

};

int Date::month[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

int Date::count() const //计算从当前年份开始到当前日期已经过了多少天

{

int i = 0;

int sum = 0;

for (i = 0; i <_month i>

{

sum += month[i];

}

return sum + _day;

}

int bissextile(const Date& d1, const Date& d2) //判断两个日期之间有多少个闰年

{

int i = 0;

int j = 0;

if (d1._month>2)

{

i = d1._year;

}

else

{

i = d1._year - 1;

}

if (d2._month <= 2)

{

j = d2._year;

}

else

{

j = d2._year + 1;

}

int sum = 0;

for (; i >= j; i--)

{

if (((i % 4 == 0) && (i % 100 != 0)) || (i % 400 == 0))

{

sum++;

}

}

return sum;

}

Date Date::operator+ (int day) //日期加天数

{

Date tmp = *this;

_week = (_week + day - 1) % 7 + 1; //求出这一天对应的星期

_year += (day / 365);

day = day % 365;

if (day - (month[_month - 1] - _day)>0)

{

day -= (month[_month - 1] - _day);

_day = 0;

}

else

{

_day += day;

return *this;

}

for (int i = _month; day >month[i % 12]; i++)

{

day -= month[i % 12];

_month++;

}

_year += (_month / 12);

_month = _month % 12 + 1;

_day = day;

return *this;

}

Date Date::operator- (int day) //日期减天数

{

int d = day % 7;

if (_week >d)

{

_week = _week - d;

}

else

{

_week = 7 - (d - _week);

}

Date tmp = *this;

_year -= (day / 365);

day = day % 365;

if (day - _day >= 0)

{

day -= _day;

_month--;

if (_month == 0)

{

_year--;

_month = 12;

}

_day = 0;

}

else

{

_day -= day;

return *this;

}

for (int i = _month - 1; day - month[i] >= 0; i--)

{

day -= month[i];

_month--;

if (_month == 0)

{

_year--;

_month = 12;

}

if (i == 0)

{

i = 12;

}

}

_day = month[_month - 1] - day;

return *this;

}

int Date::operator- (const Date& d) //日期-日期

{

int sum = 0;

sum = bissextile(*this, d); //判断两个日期之间有多少个闰年

return (_year - d._year) * 365 + (*this).count() - d.count() + sum;

}

void Date::display()const

{

int i = 0;

int j = 0;

int d = _day - 1;

int week = _week;

d = d % 7;

if (week >d)

{

week = week - d;

}

else

{

week = 7 - (d - week);

}

cout << " " << _year << "年" << _month << "月" << _day << "日" << "星期" << _week << endl;

cout << " 日 " << " 一 " << " 二 " << " 三 " << " 四 " << " 五 " << " 六 " << endl;

for (i = 0; i < (week % 7); i++)

{

cout << setw(4) << " ";

}

j = month[_month - 1];

if ((((_year % 4 == 0) && (_year % 100 != 0)) || (_year % 400 == 0)) && _month == 2)

{

j++;

}

for (i = 0; i

{

week++;

cout << setw(4) << setiosflags(ios::right) << (i + 1);

if (week % 7 == 0)

{

cout << endl;

}

}

cout << endl;

}

istream& operator>>(istream& is, Date& d)

{

is >> d._year >> d._month >> d._day >> d._week;

return is;

}

void test1()

{

Date d1;

Date d2;

int day = 0;

cout << "input>:日期" << endl;

cin >> d1;

cout << "input>:天数" << endl;

cin >> day;

d2 = d1 + day;

int sum = bissextile(d1, d2);

d2 = d2 - sum;

d2.display();

}

void test2()

{

Date d1;

Date d2;

Date d3;

int day = 0;

cout << "input>:日期" << endl;

cin >> d1;

cout << "input>:天数" << endl;

cin >> day;

d3 = d1;

d2 = d1 - day;

int sum = bissextile(d3, d2);

d2 = d2+sum;

d2.display();

}

void test3()

{

Date d1;

Date d2;

cout << "input>:日期" << endl;

cin >> d1;

cout << "input>:日期" << endl;

cin >> d2;

int day=0;

day = d1 - d2;

cout << "相差"<

}

void menu()

{

int n;

while (1)

{

cout << " 0.exit"<

cout <

cin >> n;

switch (n)

{

case zero:

exit(1);

break;

case one:

test1();

break;

case tow:

test2();

break;

case three:

test3();

break;

default:

cout << "输入不合法" << endl;

}

}

}

int main()

{

menu();

system("pause");

return 0;

}

标签:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值