C++ 实现 Data类(简单的日期计算器)
2015-12-13 15:02
393 查看
#include <iostream> #include <assert.h> using namespace std; //int Month1[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; //int Month2[13] = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; ////闰年判断函数 // int Data::Leep_year(int year) // { // if ((year % 400) || ((year % 4) && !(year % 100))) // { // return 1; // } // else // { // return -1; // } // } //返回x月份的天数 int GetMonthDay(int year, int month) { assert(month > 0 && month < 13); static int monthArray[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; int day = monthArray[month]; if ((month == 2) && (year % 400) || (year % 4 && year % 100)) { day += 1; } return day; } class Date { public: //声明display函数 void display(); //Data 构造函数 Date(int year = 1900, int month = 1, int day = 1) :_year(year) ,_month(month) ,_day(day) {} //重载 - 运算符 Date operator-(int n) { Date tmp(*this); tmp._day = tmp._day - n; if (n < 0) { return *this + (-n); } while (tmp._day <= 0) { if (tmp._month == 1) { tmp._month = 12; tmp._year = tmp._year - 1; } else { tmp._month = tmp._month - 1; } tmp._day = tmp._day + GetMonthDay(tmp._year, tmp._month); } return tmp; } //重载+运算符 Date operator+(int n) { Date tmp(*this); if (n >= 0) //n>=0的情况 { tmp._day = tmp._day + n; while (tmp._day > GetMonthDay(tmp._year,tmp._month)) { tmp._day = tmp._day - GetMonthDay(tmp._year, tmp._month); if (tmp._month == 12) //tmp.month=12,则tmp.month+1为1 { tmp._month = 1; tmp._year = tmp._year + 1; } else { tmp._month = tmp._month + 1; } } } else //n<0的情况 { return (*this - (-n)); } return tmp; } //重载+=运算符 Date& operator+=(int n) { *this = *this + n; return *this; } //重载-=运算符 Date& operator-=(int day) { *this = *this - day; return *this; } //重载前置++运算符 Date& operator++() { *this = *this + 1; return *this; } //重载后置++运算符 Date operator++(int) { Date tmp(*this); *this = *this + 1; return tmp; } //Date operator++(int) //{ // Date tmp(*this); // ++(*this); // return tmp; //} //重载前置--运算符 Date& operator--() { *this = *this - 1; return *this; } //重载后置--运算符 Date operator--(int) { Date tmp(*this); *this = *this - 1; return tmp; } //Date operator--(int) //{ // Date tmp(*this); // --(*this); // return tmp; //} //重载>运算符 bool operator>(const Date& d) { return (_year > d._year || (_year == d._year&&_month > d._month) || _year == d._year&&_month == d._month&&_day > d._day); } //bool 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 operator==(const Date& d) { return _year==d._year &&_month==d._month &&_day==d._day; } //重载<运算符 bool operator<(const Date& d) { return !(*this>d || *this == d); } //重载!=运算符 bool operator!=(Date& d) { return !(*this == d); } //重载<=运算符 bool operator<=(const Date& d) { return *this<d || *this==d; } //两个日期进行相减求值 int operator-(const Date& d) { int flag = 1; Date max = *this; Date min = d; if (*this < d) { min = *this; max = d; flag = -1; } int days = 0; while (min != max) { ++min; days++; //累加法 } return flag*days; } friend istream& operator>>(istream& is, Date& d); //声明友元函数 friend ostream& operator<<(ostream& os, const Date& d); //声明友元函数 private: int _year; int _month; int _day; }; //定义display函数 void Date::display() { cout << _year << "-" << _month << "-" << _day << endl; } //重载>>运算符 istream& operator>>(istream& is, Date& d) { cout<<"请输入一个日期的年月日:"; is>>d._year>>d._month>>d._day; return is; } //重载<<运算符 ostream& operator<<(ostream& os, const Date& d) { cout<<d._year<<"-"<<d._month<<"-"<<d._day<<endl; return os; } int main() { int year = 1900, month = 1, day = 1; while (1) { cout << "请输入起点日期: " << endl; cin >> year >> month >> day; Date d1(year, month, day); cout << "请输入天数:"; cin >> day; cout << "距离"<<day<<"天的日期是:" << endl; d1 = d1 + day; d1.display(); } system("pause"); return 0; }
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- 一个简单的asp数据库操作类
- C#实现用于操作wav声音文件的类实例
- C#类的多态性详解
- 在线管理数据库 类
- Lua中调用C++函数示例
- Lua面向对象之类和继承浅析
- Lua中类的实现原理探讨(Lua中实现类的方法)
- Lua教程(一):在C++中嵌入Lua脚本
- Lua中的类编程代码实例
- Lua教程(二):C++和Lua相互传递数据示例
- 如何使用Visual Studio 2010在数据库中生成随机测试数据
- C#常用目录文件操作类实例
- 对 jQuery 中 data 方法的误解分析
- c# 类和成员的修饰详细介绍
- C#中实现判断某个类是否实现了某个接口
- C#类的创建与初始化实例解析