C++程序设计语言练习 10.2 一个简单的date类
来源:互联网 发布:java常用算法手册豆瓣 编辑:程序博客网 时间:2024/06/11 09:31
代码如下:
头文件Date.h
#pragma once#include <string>#include<sstream>using std::string;using std::stringstream;class Date{public:enum Month{ jan = 1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec };// constructed functionDate();Date(int d,Month m,int y);Date(const Date&d);Date& operator=(const Date&d);// get the dateint get_day()const;Month get_month()const;int get_year()const;string string_rep() const;void char_rep(char s[])const;// relation calcbool operator==(const Date &d)const;bool operator!=(const Date &d)const;bool operator>(const Date &d)const;bool operator<(const Date &d)const;bool operator<=(const Date &d)const;bool operator>=(const Date &d)const;// increaseDate &operator++(int);Date &operator++();Date &operator--(int);Date &operator--();// assignmentDate& operator+=( int n);Date& operator-=( int n);Date& operator+( int n);Date& operator-( int n);protected:static void set_default(int d, Month m, int y);void IncDay();bool IsEndOfMonth()const;void IncMonth();void DecMonth();void DecDay();int GetDayOfMonth()const;// exception classclass Bad_date{};private:int day_, year_;Month month_;static Date default_date;};bool IsLeapYear(int y);
#include "stdafx.h"#include "Date.h"#include <iostream>using namespace std;Date Date::default_date(1, jan, 1970);void Date::set_default(int d, Month m, int y){default_date = Date(d, m, y);}// constucted function ///////////////////////////////Date::Date(){*this = default_date;}Date::Date(int d,Month m,int y){if (d == 0) d = default_date.get_day();if (m == 0) m = default_date.get_month();if (y == 0) y = default_date.get_year();int max;switch (m){case feb:max = 29 + IsLeapYear(y);break;case apr:case jun:case sep:case nov:max = 30;break;case jan:case may:case jul:case aug:case oct:case dec:max = 31;break;default:throw Bad_date();}if (d < 1 || max < d)throw Bad_date();day_ = d;month_ = m;year_ = y;}// 复制构造函数Date::Date(const Date&d){day_ = d.get_day();month_ = d.get_month();year_ = d.get_year();}// 赋值构造函数Date& Date::operator=(const Date&d){if (*this != d){year_ = d.get_year();month_ = d.get_month();day_ = d.get_day();}return *this;}// get the date /////////////////////////////////int Date::get_day() const{return day_;}Date::Month Date::get_month() const{return month_;}int Date::get_year() const{return year_;}string Date::string_rep() const{string s,st;stringstream ss;ss << this->year_;ss>>st;s += st;s += '/';ss.clear();ss << this->month_;ss >> st;s += st;s += '/';ss.clear();ss << this->day_;ss >> st;s += st;return s;}void Date::char_rep(char s[]) const{string sdate = string_rep();strcpy_s(s, sdate.length(), sdate.c_str());}int Date::GetDayOfMonth() const{int day = 0;switch (this->month_){case jan: day = 31; break;case feb: if (month_ == feb && IsLeapYear(year_))day = 29;elseday = 28;break;case mar: day = 31; break;case apr: day = 30; break;case may: day = 31; break;case jun: day = 30; break;case jul: day = 31; break;case aug: day = 31; break;case sep: day = 30; break;case oct: day = 31; break;case nov: day = 30; break;case dec: day = 31; break;default:throw Bad_date();break;}return day;}// relation calc ////////////////////////////////////bool Date::operator==(const Date &d) const{return year_ == d.year_ &&month_ == d.month_ &&day_ == d.day_;}bool Date::operator!=(const Date& d) const{return !(*this == d);}bool Date::operator>(const Date& d) const{if (year_ > d.year_){return true;}if (year_ == d.year_){if (month_ > d.month_){return true;}if (month_ == d.month_){if (day_ > d.day_)return true;}}return false;}bool Date::operator>=(const Date&d)const{return (*this > d) || (*this == d);}bool Date::operator<(const Date& d) const{return !(*this>=d);}bool Date::operator<=(const Date& d) const{return !(*this>d);}// increase calc //////////////////////////////////bool Date::IsEndOfMonth() const{if (month_ == feb && IsLeapYear(year_))return day_ == 29;elsereturn day_ == GetDayOfMonth();}void Date::IncMonth(){switch (this->month_){case jan:this->month_ = feb;break;case feb:this->month_ = mar;break;case mar:this->month_ = apr;break;case apr:this->month_ = may;break;case may:this->month_ = jun;break;case jun:this->month_ = jul;break;case jul:this->month_ = aug;break;case aug:this->month_ = sep;break;case sep:this->month_ = oct;break;case oct:this->month_ = nov;break;case nov:this->month_ = dec;break;case dec:this->month_ = jan;break;default:throw Bad_date();break;}}void Date::IncDay(){if (IsEndOfMonth()){if (month_ == dec) // 如果是12月最后一天,则加1年{day_ = 1; month_ = jan; year_++;}else // 如果是本月最后一天,则加1月{day_ = 1; IncMonth();}}elseday_++;}void Date::DecMonth(){switch (this->month_){case feb:this->month_ = jan;break;case mar:this->month_ = feb;break;case apr:this->month_ = mar;break;case may:this->month_ = apr;break;case jun:this->month_ = may;break;case jul:this->month_ = jun;break;case aug:this->month_ = jul;break;case sep:this->month_ = aug;break;case oct:this->month_ = sep;break;case nov:this->month_ = oct;break;case dec:this->month_ = nov;break;case jan:this->month_ = dec;break;default:throw Bad_date();break;}}void Date::DecDay() // dec one day{if (day_ == 1){if (month_ == jan) // 如果是1月,则变为12月,并且减1年{month_ = dec; year_--; day_ = GetDayOfMonth();}else // 否则减1月{DecMonth(); day_ = GetDayOfMonth();}}elseday_--;}Date& Date::operator++(int){IncDay();return *this;}Date& Date::operator++(){IncDay();return *this;}Date& Date::operator--(int){DecDay();return *this;}Date& Date::operator--(){DecDay();return *this;}Date& Date::operator+=( int n){for (int i = 0; i < n; i++)IncDay();return *this;}Date& Date::operator-=( int n){for (int i = n; i > 0; i--)DecDay();return *this;}Date& Date::operator+( int n){for (int i = 0; i < n; i++)IncDay();return *this;}Date& Date::operator-( int n){for (int i = n; i < 0; i--)DecDay();return *this;}// frend fun //////////////////////////////////////bool IsLeapYear(int y) // y == 0 ??? {if (y % 400 == 0 || y % 100 != 0 && y % 4 == 0){return true;}return false;}
0 0
- C++程序设计语言练习 10.2 一个简单的date类
- C程序设计语言练习1-23 编写一个删除C语言程序中所有的注释语句
- C程序设计语言 练习 第一章
- C程序设计语言,练习1-13,编写一个程序,打印输入单词的直方图(垂直方向)
- C++程序设计语言练习 12.1 虚基类的简单实现
- C程序设计语言练习2-3
- C程序设计语言练习2-4
- C程序设计语言练习2-5
- C程序设计语言练习2-10
- C程序设计语言练习3-1
- C程序设计语言练习3-2
- C程序设计语言练习3-3
- C程序设计语言练习3-4
- C程序设计语言练习3-5
- C程序设计语言练习4-1
- C程序设计语言练习4-2
- C程序设计语言练习4-3
- 《C程序设计语言》练习3-3
- java内存管理,GC,内存泄漏
- HDU 4155 (博弈 记忆化搜索)
- java 使用html2canvas实现屏幕截图
- ffmpeg 在VC2010 中的环境搭建
- Qt + Qcustomplot绘图插件安装和配置
- C++程序设计语言练习 10.2 一个简单的date类
- 交互式Ruby Shell irb
- 文本框只能输入数字
- Maven管理jar
- uva11300 Spreading the Wealth
- 一个STM32F0 的IAP应用问题
- CardView 在 Theme.AppCompat.NoActionBar时 一些问题
- 文章标题
- 2017软件开发校招内推,阿里、华为等面试经验