C++日期计算器的实现
2017-07-06 14:28
176 查看
先来波剧透:
具体实现代码如下:
test.h
date.cpp
test.cpp
具体实现代码如下:
test.h
#include<iostream> using namespace std; class Date { friend void print_date(int year, int month);//打印日历 friend ostream& operator<<(ostream&_cout, const Date& d); friend istream& operator>>(istream&_cin, Date& d); public: int days_aimmonth(int year, int month);//所求月的天数 int week_firstday(int year, int month);//统计目标月份第一天是周几 Date(); Date(int year, int month, int day); Date(const Date&d); Date& operator=(const Date& d); Date& operator+(int day); Date& operator++(int); Date& operator++(); Date& operator--(); Date& operator--(int); int operator-(Date& d); Date& operator-(int day); bool operator>(const Date d); bool operator<(const Date d); bool operator>=(const Date d); bool operator<=(const Date d); bool operator==(const Date d); bool operator!=(const Date d); private: int _year; int _month; int _day; }; bool Judge_leap(int year);//判断是否为闰年 static int week_firstday(int year, int month);
date.cpp
#include "test.h" ostream& operator<<(ostream&_cout, const Date& d) { _cout << d._year << "-" << d._month << "-" << d._day; return _cout; } istream& operator>>(istream&_cin, Date& d) { _cin >> d._year >> d._month >> d._day; return _cin; } Date::Date() {} Date::Date(int year = 2017, int month = 1, int day = 1) :_year(year), _month(month), _day(day) { if (day > days_aimmonth(_year,_month)) { _day = day - days_aimmonth(_year, _month); _month += 1; if (month > 12) { _month -= 12; _year += 1; } } } Date::Date(const Date &d) { _year = d._year; _month = d. _month; _day = d._day; } Date& Date::operator=(const Date &d) { if (*this != d) { _year = d._year; _month = d._month; _day = d._day; } return *this; } Date& Date::operator+(int day) { _day = _day + day; while (_day > days_aimmonth(_year, _month)) { _day = _day - days_aimmonth(_year, _month); _month += 1; if (_month > 12) { _year += 1; _month = 1; } } return *this; } Date& Date::operator++(int) { ++_day; if (_day > days_aimmonth(_year, _month)) { _day = _day - days_aimmonth(_year, _month); _month += 1; if (_month > 12) { _year+=1; _month -= 12; } } return *this; } Date& Date::operator++() { _day++; if (_day > days_aimmonth(_year, _month)) { _day = _day - days_aimmonth(_year, _month); _month += 1; if (_month > 12) { _year += 1; _month -= 12; } } return *this; } Date& Date::operator-(int day) { _day = _day - day; while (_day <= 0) { _day += days_aimmonth(_year, _month); _month -= 1; if (_month <= 0) { _year -= 1; _month = 12; } } return *this; } int Date::operator-(Date& d) //日期减日期 { Date min(*this); Date max(d); int tag = -1; int count = 0; if (d < *this) { min = d; max = *this; tag = 1; } while (min != max) { ++min; count++; } return tag*count; } Date& Date::operator--() { _day--; if (_day == 0) { _day = days_aimmonth(_year, _month); _month -= 1; if (_month == 0) { _year -= 1; _month = 12; } } return *this; } Date& Date::operator--(int) { --_day; if (_day == 0) { _day = days_aimmonth(_year, _month); _month -= 1; if (_month == 0) { _year -= 1; _month = 12; } } return *this; } bool Date::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 Date::operator<(const Date d) { if (_year < d._year) return true; else if (_year > d._year) return false; else { if (_month < d._month) return true; else if (_month > d._month) return false; else { if (_day<d._day) return true; else return false; } } } bool Date::operator>=(const Date d) { if (_year >= d._year) return true; else if (_month >= d._month) return false; else { if (_month >= d._month) return true; else if (_month <= d._month) return false; else { if (_day>=d._day) return true; else return false; } } } bool Date::operator<=(const Date d) { if (_year <= d._year) return true; else if (_month <= d._month) return false; else { if (_month <= d._month) return true; else if (_month <= d._month) return false; else { if (_day<=d._day) return true; else return false; } } } bool Date::operator==(const Date d) { if ((_year == d._year) && (_month == d._month) && (_day == d._day)) return true; else return false; } bool Date::operator!=(const Date d) { if (*this == d) return false; else return true; } void print_date(int year, int month)//打印目标月份日历 { cout << year << "年" << month << "月" << endl; cout << "日 " << "一 " << "二 " << "三 " << "四 " << "五 " << "六 " << endl; int i = 0; int j = 0; Date temp(year, month, 1); int week = week_firstday(year, month); int day = temp.days_aimmonth(year, month); int k = 1; for (i = 0; i < week; i++) { cout << " "; } for (i = 0; i < 6; i++) { for (j = 0; j < 7; j++) { if (i == 0) { if (j == 7 - week) break; } if (k <= day) printf("%2d ", k++); } cout << endl; } } static bool Judge_leap(int year)//判断是否为闰年 { if ((year % 4 == 0) || (year % 400 == 0) && (year % 100 != 0)) return true; else return false; } int Date::days_aimmonth(int year, int month)//求目标月份天数 { int day[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if (Judge_leap(year) == true) { day[2] = 29; } return day[month]; } static int week_firstday(int year, int month) { Date d1(1949, 9, 1);//新中国自1949年开始使用公元纪年法 int week = 4;//公元1949年9月1日为周四 int day = 0; Date d2(year, month, 1); day = d2 - d1; week += day % 7; if (week > 7) { week -= 7; } return week; }
test.cpp
#include "test.h" void menu() { cout << "#### ---请输入想进行的运算的序号--- ####" << endl; cout << "#### 1.打印目标月份日历(格式:年 月)####" << endl; cout << "#### 2.相隔天数计算 #####################" << endl; cout << "#### 3.日期推算(填负数向前推进) ##########" << endl; cout << "#### 0.退出###############################" << endl; } int main() { menu(); while (1) { int num = 0; cin >> num; switch (num) { case 1: { cout << "请输入年份月份:"; int a = 0, b = 0; cin >> a; cin >> b; print_date(a, b); break; } case 2: { cout << "请输入日期:"; Date d1; cin >> d1 ; cout << "请输入日期:"; Date d2; cin >> d2; int day = 0; day = d1 - d2; cout <<"日期相差:"<< day <<"天"<< endl; break; } case 3: { cout << "请输入日期:"; Date d1,d2; cin >> d1; cout << "请输入推算的天数:"; int n = 0; cin >> n; cout << "推算后日期为:"; if (n > 0) { d2 = d1 + n; } else { d2 = d1 - (-n); } cout<<d2; break; } case 0: return 0; } } system("pause"); return 0; }
相关文章推荐
- 利用C++日期类实现简单的日期计算器
- 利用C++日期类实现简单的日期计算器
- C++ 实现 Data类(简单的日期计算器)
- 【c++巩固练习】c++实现日期计算器、日期万年历(日期类)
- C++【面试题】:类实现万年历(日期计算器),(含构造函数、拷贝构造、运算符重载、析构函数)
- time travel-c++实现日期计算器
- C/C++实现日期计算器的示例代码
- C++【面试题】:类实现万年历(日期计算器),(含构造函数、拷贝构造、运算符重载、析构函数)
- C/C++ — 实现日期计算器
- Singleton模式的C++实现研究(示例代码)
- 在C++中实现“属性 (Property)”
- C++机理:虚拟机制的实现[兼谈对比于传统机制]
- 用C++ std::priority_queue 实现哈夫曼算法
- 用C++实现C#中的委托/事件(标准C++之升级版)
- 再探C++的单件实现
- 用 C++ 实现 C# 中的 委托/事件 (2-delegate event functor)
- 在C++中实现属性
- 用PHP实现通过Web执行C/C++程序
- 使用c++实现Format函数
- More Effective C++ Item 附2:一个auto_ptr的实现实例