C++,笔试面试,使用C++编程,实现万年历
来源:互联网 发布:无法备案的域名 编辑:程序博客网 时间:2024/06/08 14:56
要求实现:基本成员函数,日期加减天数,日期与日期相减
#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;}
0 0
- C++,笔试面试,使用C++编程,实现万年历
- C语言实现:万年历
- C语言实现万年历
- c语言实现万年历
- C实现万年历
- 【C++】万年历的实现
- 【C++】实现万年历
- C语言实现万年历
- 【C++】实现万年历
- c语言实现万年历
- C语言实现万年历
- C语言实现万年历
- C语言实现万年历
- C语言实现万年历程序
- 笔试面试5 实现C库函数strlen
- c万年历
- [c++]万年历
- 【C】万年历
- C/C++,数据结构单链表实现约瑟夫环
- 笔试,面试,C/C++,数据结构单链表排序(改进冒泡排序)
- 笔试,面试,C/C++,判断单链表是否带环?若带环,求环长度,求环入口点(两种方法)
- <笔试><面试>C/C++单链表相关(4)判断两链表是否相交,求交点(链表不带环/可能带环)
- <笔试><面试>C/C++单链表(最综合)最全工程从建立到相关函数实现
- C++,笔试面试,使用C++编程,实现万年历
- C/C++,笔试面试,多种方法求100以内的所有素数
- 数据库范式例子说明
- 两数的最大公约数、最小公倍数
- 两个数字交换(不使用临时变量)
- 编写程序将2000年以内的平年闰年分开并分别计算个数
- 使用C语言编写程序,求解有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个序列的前20项
- 有1、2、3、4四个数字,能组成多少个互不相同且无重复数字的三位数
- 求几个相同数字组成不同位数数字的和:s=a+aa+aaa+aaaa+aa…a的值