time travel-c++实现日期计算器

来源:互联网 发布:nginx 内置函数 编辑:程序博客网 时间:2024/06/06 20:20

用到了操作符重载,具体请看代码:

这个功能我调试了几天,经过测试我认为没有问题,如果你发现有bug,感谢指正!

#ifndef __Date_H__#define __Date_H__#include <iostream>using namespace std;class Date{public:Date(int year = 2017, int month = 10, int day = 16):_year(year),_month(month), _day(day){}bool operator>(const Date& d);bool operator<(const Date& d);bool operator>=(const Date& d);bool operator<=(const Date& d);Date& operator=(const Date& d);bool operator==(const Date& d);bool operator!=(const Date& d);int GetMonthDay(int year, int month);//得到每个月的天数,用于之后的计算Date operator+(int day);Date operator-(int day);Date operator+=(int day);Date operator-=(int day);Date operator++();//前置Date operator--();Date operator++(int);//后置Date operator--(int);int operator-(const Date& d);void show(){cout << _year << "-" << _month << "-" << _day << endl;}~Date(){}private:int _year;int _month;int _day;};#endif //__Date_H__


需要注意的是,代码的复用性,比如,已经写了operator>,那么operator<就可以复用operator>

#include "Date.h"bool Date::operator>(const Date& d){if (this->_year > d._year){return true;}else if (this->_year == d._year){if (this->_month > d._month){return true;}else if (this->_month == d._month){if (this->_day > d._day){return true;}}}return false;}bool Date::operator<(const Date& d)//增强了复用性{return !(*this > d || *this == d);}bool Date::operator>=(const Date& d){if (*this > d || *this == d){return true;}return false;}bool Date::operator<=(const Date& d){if (*this < d || *this == d){return true;}return false;}bool Date::operator==(const Date& d){return this->_year == d._year &&this->_month == d._month &&this->_day == d._day;}bool Date::operator!=(const Date& d){return !(*this == d);}Date& Date::operator=(const Date& d){if (*this != d){this->_year = d._year;this->_month = d._month;this->_day = d._day;}return *this;}int Date::GetMonthDay(int year, int month){int MonthDay[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };int ret = MonthDay[month];if ((year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) && (month == 2)){ret++;}return ret;}Date Date::operator+(int day){Date tmp(*this);tmp._day += day;while (tmp._day > GetMonthDay(tmp._year, tmp._month)){tmp._day -= GetMonthDay(tmp._year, tmp._month);tmp._month++;if (tmp._month > 12){tmp._year++;tmp._month = 1;}}return tmp;}Date Date::operator-(int day){Date tmp(*this);tmp._day -= day;while (tmp._day <= 0){tmp._month--;if (tmp._month == 0){tmp._month = 12;tmp._year--;}tmp._day += GetMonthDay(tmp._year, tmp._month);}return tmp;}Date Date::operator+=(int day){*this = *this + day;return *this;}Date Date::operator-=(int day){*this = *this - day;return *this;}Date Date::operator++(){*this += 1;return *this;}Date Date::operator--(){*this -= 1;return *this;}Date Date::operator++(int){Date tmp(*this);tmp += 1;return tmp;}Date Date::operator--(int){Date tmp(*this);tmp -= 1;return tmp;}int Date::operator-(const Date& d){Date big = *this;Date small = d;int flag = 1;if (*this < d){big = d;small = *this;flag *= -1;}int count = 0;while (small < big){++small;count++;}return count*flag;}

如果再写一个菜单,就可以美美的了生气

void menu(){printf("just it is     Time Travel\n");printf("______________________________________________\n");printf("************   欢迎来到世间旅行    ************\n");printf("************   1.距此时x天之后     ***********\n");printf("************   2.在此刻x天之前     ***********\n");printf("************   3.两时刻,差多少     ***********\n");printf("************   0.退出              ***********\n");printf("______________________________________________\n");printf("\n");}void testmenu(){int year = 0;int month = 0;int day = 0;int num = 0;int input = 0;do{menu();scanf("%d",&input);switch(input){case 1:{printf("今天是什么日子:\n");scanf("%d %d %d",&year,&month,&day);Date d1(year,month,day);printf("要加上多少天:\n");scanf("%d",&num);Date test = d1+num;printf("结果是:\n");test.show();}break;case 2:{printf("今天是什么日子:\n");scanf("%d %d %d",&year,&month,&day);Date d2(year,month,day);printf("要减去多少天:\n");scanf("%d",&num);Date test2 = d2-num;printf("结果是:\n");test2.show();}break;case 3:{printf("第一个时刻是:\n");scanf("%d %d %d",&year,&month,&day);Date d3(year,month,day);printf("第二个时刻是:\n");scanf("%d %d %d",&year,&month,&day);Date d4(year,month,day);printf("那么它们相差:\n");cout<<(d3-d4)<<endl;}break;}}while(input);}
int main(){testmenu();return 0;}
我来帮舍友算一算距离考研还有多久?


来验证一下,结果是对的:


原创粉丝点击