C++,笔试面试,使用C++编程,实现万年历
2016-01-31 21:32
841 查看
要求实现:基本成员函数,日期加减天数,日期与日期相减
#include<iostream> using namespace std; class Date { public: Date(int year = 1900, int month = 1, int day = 1) :_year(year), _month(month), _day(day) { //cout << "Date(int year, int month, int day)" << endl; if (!_IsEffective()) { printf("日期无效,已置为1900-1-1\n"); _year = 1900; _month = 1; _day = 1; } } Date(const Date& x) { //cout << "Date(Date& x)" << endl; _year = x._year; _month = x._month; _day = x._day; } ~Date() { //cout << "~Date()" << endl; } void Display() { cout << _year << "-" << _month << "-" << _day << endl; } bool operator>(const Date& d) { if (_year > d._year) return true; else if (_year == d._year&&_month > d._month) return true; else if (_month == d._month&&_day > _day) return true; else return false; } bool operator==(const Date& d) { if (_year == d._year&&_month == d._month&&_day == d._day) return true; return false; } bool operator>=(const Date& d) { return operator==(d) || operator>(d); } bool operator<(const Date& d) { return !operator>=(d); } bool operator<=(const Date& d) { return !operator>(d); } Date operator+(const int day) { Date tmp = *this; if (day < 0) operator-(-day); tmp._day += day; while (tmp._day>tmp._MonthDay()) { tmp._day -= tmp._MonthDay(); tmp._month++; if (tmp._month > 12) { tmp._year++; tmp._month = 1; } } return tmp; } Date operator+= (const int day) { if (day < 0) { operator-=(-day); return *this; } *this = this->operator+(day); return *this; } Date operator- (const int day) { Date tmp = *this; if (day < 0) operator+(-day); tmp._day -= day; while (tmp._day<=0) { tmp._month--; tmp._day += tmp._MonthDay(); if (tmp._month <1) { tmp._year--; tmp._month = 12; } } return tmp; } Date operator-= (const int day) { if (day < 0) { operator+=(-day); return *this; } *this = this->operator-(day); return *this; } Date operator++()//前置++ { Date tmp = *this; operator+=(1); return tmp; } Date operator++(int)//后置++ { operator+=(1); return *this; } Date operator--()//前置-- { Date tmp = *this; operator-=(1); return tmp; } Date operator--(int)//后置-- { operator-=(1); return *this; } int operator-(const Date& d) { int days = _day; Date tmp = d; if (*this < tmp) return -tmp.operator-(*this); while (*this>tmp) { days += tmp._MonthDay(); tmp._month++; if (tmp._month > 12) { tmp._year++; tmp._month = 1; } } return days - tmp._day; } private: bool _IsLeapYear() { if (_year % 4 == 0 && _year % 100 != 0 || _year % 400 == 0) return true; else return false; } int _MonthDay() { int monthdays[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if (_IsLeapYear() == 1&&_month == 2)//不是二月没有必要改变到29(算是一个优化) monthdays[2] += 1; return monthdays[_month]; } int _IsEffective() { if (_month > 0 && _month < 13 && _day > 0 && _day <= _MonthDay()) return 1; else return 0; } private: int _year; int _month; int _day; }; void Test1() { Date d1(2018, 2, 20); Date d2(2016, 2, 20); Date d3(2016, 2, 30); printf("%d\n", d1 > d2); printf("%d\n", d1 < d2); printf("%d\n", d1 >= d2); printf("%d\n", d1 <= d2); printf("%d\n", d1 == d2); d1.Display(); d3.Display(); (d1 + 8).Display(); //+(day) (d1 + 9).Display(); //+(day) (d1 + 10).Display(); //+(day) (d2 + 10).Display(); //+(day) (d2 + 31).Display(); //+(day) printf("\n"); d1++; //++ d1.Display(); ++d1; //++ d1.Display(); d1 += 10;//+= d1.Display(); d2 += 10; d2.Display(); printf("\n"); d1--; //++ d1.Display(); --d1; //++ d1.Display(); d1 -= 10;//+= d1.Display(); d2 -= 10; d2.Display(); printf("\n"); d1 += -33; d1.Display(); d1 -= -33; d1.Display(); printf("\n"); cout << "d1 - d2 = " << d1 - d2 << endl; cout << "d1 - d2 = " << d2 - d1 << endl; } int main() { Test1(); system("pause"); return 0; }
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++高级程序员成长之路
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例
- C与C++之间相互调用实例方法讲解
- 解析C++中派生的概念以及派生类成员的访问属性