完成CDate类的几个函数

实现一个CDate类

class CDate
{
public:
CDate(int year = 1900, int month = 1, int day = 1)
: _year(year)
, _month(month)
, _day(day)
{
if (!(_year >= 1900 &&
(_month > 0 && _month < 13) &&
(_day > 0 && _day <= _GetMonthDay(_year, _month))))
{
_year = 1900;
_month = 1;
_day = 1;
}
}
 
     bool IsLeap(int year)//判断闰年
{
if ((year % 4 == 0 && year % 100 != 0) ||
(year % 400 == 0))
{
return true;
}
 
return false;
}
private:
int _GetMonthDay(int year, int month)
{
int days[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (IsLeap(year) && month == 2)//闰年并且月份为2
{
days[month] += 1;
}
 
return days[month];
}
 private:
int _year;
int _month;
int _day;
};
#include <iostream>
using namespace std;
class CDate
{
public:
CDate(int year = 1900, int month = 1, int day = 1)
: _year(year)
, _month(month)
, _day(day)
{
if (!(_year >= 1900 &&
(_month > 0 && _month < 13) &&
(_day > 0 && _day <= _GetMonthDay(_year, _month))))
{
_year = 1900;
_month = 1;
_day = 1;
}
}
//比较日期
bool operator<(const CDate& d)
{
if (_year < d._year)
{
return true;
}
else if (_year == d._year && _month < d._month)
{
return true;
}
else if (_year == d._year && _month == d._month && _day<d._day)
{
return true;
}
 
return false;
}
bool operator==(const CDate& d)
{
return (_year == d._year &&
_month == d._month &&
_day == d._day);
}
bool operator>(const CDate& d)
{
return (!(*this < d || *this == d));
}
//日期加法
CDate operator+(int day)
{
if (day < 0)
{
return *this - (0 - day);
}
 
CDate temp(*this);
temp._day += day;
 
while (temp._day > _GetMonthDay(temp._year, temp._month))
{
temp._day -= _GetMonthDay(temp._year, temp._month);
if (temp._month == 12)
{
temp._year++;
temp._month = 1;
}
else
{
temp._month++;
}
}
 
return temp;
}
//日期减法
CDate operator-(int day)
{
if (day < 0)
{
return *this + (0 - day);
}
 
CDate temp(*this);
temp._day -= day;
 
while (temp._day <= 0)
{
if (temp._month == 1)
{
temp._year--;
temp._month = 12;
}
else
{
temp._month--;
}
 
temp._day += _GetMonthDay(temp._year, temp._month);
}
 
return temp;
}
//前置加加
CDate& operator++()
{
*this = *this + 1;
return *this;
}
 
//后置加加
CDate operator++(int)
{
CDate temp(*this);
*this = *this + 1;
return temp;
}
//前置减减
CDate& operator--()
{
*this = *this - 1;
return *this;
}
//后置减减
CDate operator--(int)
{
CDate temp(*this);
*this = *this - 1;
return temp;
}
//两者作差
int operator-(const CDate& d)
{
CDate min(*this);
CDate max(d);
if (min > max)
{
min = d;
max = *this;
}
 
int count = 0;
while (min < max)
{
min = min + 1;
count++;
}
 
return count;
}
 
bool IsLeap(int year)
{
if ((year % 4 == 0 && year % 100 != 0) ||
(year % 400 == 0))
{
return true;
}
 
return false;
}
private:
int _GetMonthDay(int year, int month)
{
int days[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (IsLeap(year) && month == 2)//闰年并且月份为2
{
days[month] += 1;
}
 
return days[month];
}
 
friend ostream& operator<<(ostream& _cout, const CDate& d)
{
_cout << d._year << "-" << d._month << "-" << d._day;
return _cout;
}
private:
int _year;
int _month;
int _day;
 
 
};
int main()
{
CDate d(1992, 1, 26);
cout << d + 4 << endl;
cout << d + 34 << endl;
cout << d + 10000 << endl;
 
cout << d - 26 << endl;
 
CDate d1(1992, 1, 1);
cout << d - d1 << endl;
cout << d1 - d << endl;
 
cout << d1++ << endl;
cout << ++d1 << endl;
system("pause");
return 0;
}

时间: 2024-08-03 01:44:24

完成CDate类的几个函数的相关文章

检测某个方法是否属于某个类中--解析php函数method_exists()与is_callable()的区别

php函数method_exists() 与is_callable()的区别在哪?在php面相对象设计过程中,往往我们需要在调用某一个方法是否属于某一个类的时候做出判断,常用的方法有 method_exists()和is_callable() 相比之下,is_callable()函数要高级一些,它接受字符串变量形式的方法名作为 第一个参数,如果类方法存在并且可以调用,则返回true.如果要检测类中的方法是否能被调用,可以给函数传递一个数组而不是类的方法名作为参数.数组必须包含对象或类名,以将其作

添加、移除class类选择器的公共函数

/********************移除class类选择器的公共函数************************///提供需要操作的元素对象以及需要删除的className名即可function removeclassName(element,name){ var classes=element.className.split(' '); if(classes.indexOf(name)!=-1){ classes.splice(classes.indexOf(name)); } el

C++ 类模板二(类模版与友元函数)

//类模版与友元函数 #include<iostream> using namespace std; template<typename T> class Complex{ public: Complex(T a,T b); void Print() const//const修饰的是this指针 { cout << this->Real << ":" <<this->Image<< endl; } /*

C++:抽象基类和纯虚函数的理解

转载地址:http://blog.csdn.net/acs713/article/details/7352440 抽象类是一种特殊的类,它是为了抽象和设计的目的为建立的,它处于继承层次结构的较上层. ⑴抽象类的定义: 称带有纯虚函数的类为抽象类. ⑵抽象类的作用: 抽象类的主要作用是将有关的操作作为结果接口组织在一个继承层次结构中,由它来为派生类提供一个公共的根,派生类将具体实现在其基类中作为接口的操作.所以派生类实际上刻画了一组子类的操作接口的通用语义,这些语义也传给子类,子类可以具体实现这些

CImage类提供了GetBits()函数原理及实现

CImage类提供了GetBits()函数来读取数据区,GetBits()函数返回的是图片最后一行第一个像素的地址,网上有人说返回指针的起始位置是不同的,有些图片返回的是左上角像素的地址,有些是左下角像素的地址,跟图片内部顺序有关.GetPitch( ) 图像的间距. 如果返回值为负,位图是一个从下到上 DIB,并且原点是左下角. 如果返回值为正的,位图是一组 DIB,并且原点为左上角两个函数GetPitch()和GetHeight()一起使用就可以得到图片数据取得起始位置 img_Data=(

(继承及其访问限定符)&&(派生类及其默认成员函数)&&(赋值兼容规则)

◆继承: ★继承概念 继承(inheritance)机制是面向对象程序设计使代码可以复用的最重要的手段,它允许程序员在保持原有类特性的基础上进行扩展,增加功能.这样产生新的类,称派生类.继承呈现了面向对象程序设计的层次结构,体现了由简单到复杂的认知过程. 继承定义格式 ★继承关系&访问限定符 class Base { public: Base() { cout<<"B()" <<endl; } ~Base () { cout<<"~

自绘CListCtrl类,重载虚函数DrawItem

[cpp] view plain copy //自绘CListCtrl类,重载虚函数DrawItem void CNewListCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) { // TODO: Add your code to draw the specified item ASSERT(lpDrawItemStruct->CtlType == ODT_LISTVIEW); CDC dc; dc.Attach(lpDrawItemStruc

创建一个三角形类并且通过成员函数计算三角形的周长和面积《1》

首先定义一个三角形类 class Triangle//三角形类 { public: double getA(void);//得到a的值 double getB(void);//得到b的值 double getC(void);//得到c的值 void setA(double x);//设置a的值 void setB(double y);//设置b的值 void setC(double z);//设置c的值 bool isTriangle(void);//取三边的值 double Perimeter

创建一个三角形类并且使用成员函数计算三角形的周长和面积《2》

首先创建一个三角形类 class Triangle//三角形类 { public: void Setabc(double x, double y, double z);//置三边的值,注意要能成三角形 void Getabc(double *x, double *y, double *z);//取三边的值 double Perimeter(void);//计算三角形的周长 double Area(void);//计算并返回三角形的面积 private: double a, b, c; //三边为