C++日期计算器的实现
来源:互联网 发布:东方财富dk买卖点源码 编辑:程序博客网 时间:2024/06/06 03:25
先来波剧透:
具体实现代码如下:
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;}
阅读全文
1 0
- 日期计算器的实现
- C++日期计算器的实现
- C/C++ — 实现日期计算器
- 类实现一个简单的日期计算器
- 简单实现日期计算器的基本功能
- 实现一个日期计算器
- 利用C++日期类实现简单的日期计算器
- 【C++】日期类+日期万年历+日期计算器
- C语言实现一个简单的计算器
- 一个计算器的C语言实现
- C语言实现的高精度计算器
- 【C++】C++顺序栈实现的计算器
- 一个用C实现的科学计算器
- 用C语言实现的计算器
- 实现日期类(日期计算器)
- c语言实现计算器
- C++-------实现计算器
- 简单计算器实现(C++)
- 浅谈SVD的理解
- Python学习4-流程控制
- BZOJ 2654: tree (MST) 题解
- spring boot log4j2配置(使用log4j2.yml文件)
- JUnit基本用法
- C++日期计算器的实现
- Java访问者模式
- Servlet——监听器Listener详解
- struts2中的特殊符号的作用(# $ %)
- live555 DESCRIBE处理详解
- 深入jar包:从jar包中读取资源文件
- Kafaka High Level Consumer
- lua语言(四)之Unity3D实践
- a && b 与 a|| b