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;}
我来帮舍友算一算距离考研还有多久?来验证一下,结果是对的:
阅读全文
0 0
- time travel-c++实现日期计算器
- C/C++ — 实现日期计算器
- 日期计算器的实现
- 实现一个日期计算器
- 【C++】日期类+日期万年历+日期计算器
- C++日期计算器的实现
- 实现日期类(日期计算器)
- hdu4571 Travel in time
- hdu 4118 Time travel
- HDU4571 Travel in time
- hdu 4418 Time travel
- hdu4481 Time travel
- HDU 4418 Time travel
- Travel time in Dallas
- Time travel HDU
- c语言实现计算器
- C++-------实现计算器
- 简单计算器实现(C++)
- Mybatis配置文件中mapper标签中namespace属性作用小结
- 大数据架构详解:从数据获取到深度学习(内含福利)
- python爬虫学习第一节课
- CUDA GPU编程中使用结构体传递函数参数
- 给你10个超实用的技术公众号
- time travel-c++实现日期计算器
- 十年编程,是人玩技术还是技术玩人?
- 让孩子进阶中产的唯一路径是编程?
- JavaScript内存管理机制以及四种常见的内存泄漏解析
- Docker实战:数据管理
- 20、Selenium + Python 实现 UI 自动化测试-AutoIT工具
- C# 导出DataGridView数据到Excel表
- GAN的理解与TensorFlow的实现
- 自然语言处理深度学习的7个应用