c++实现日期类,日历计算器

在写日期类日期计算器之前先实现了一个简单的复数类
//引用做参数,1.swap--在函数内部改变参数,2.Bigdata提高效率
//内联函数必须在定义处加上inline
//定义在类内部的函数默认为内联函数
//c++中尽量使用const,枚举,内联去替代宏
//宏没有类型安全的检查,在预处理的时候替换了所以无法调试,宏安全性不高

class Complex
{
public:
Complex(double real = 0.0, double image = 0.0)//定义了一个全缺省的构造函数
:_real(real)
, _image(image)
{
cout << "Complex()" << endl;//这样可以在屏幕上输出构造函数的信息,方便分析构造函数的调用
}

Complex(const Complex& c)//拷贝构造函数
{
cout << "Complex(const Complex& c)" << endl;
_real = c._real;
_image = c._image;
}

~Complex()//析构函数
{
cout << "~Complex()" << endl;
}

Complex& operator=(const Complex& c)// Complex返回值支持连续的赋值
{
cout << "Complex& operator=()" << endl;
if (this != &c)
{
_real = c._real;
_image = c._image;
}

return *this;
}
void Display()
{
cout << _real << "-" << _image << this-="">_real++;
this->_image++;
return *this;
}
Complex operator++(int)//后置++,返回加之前的值,int用来占位,形成重载
{
Complex ret(*this);
this->_real++;
this->_image++;
return ret;
}

bool operator== (const Complex& c)
{
IsEqual(c);
}
bool IsEqual(const Complex& c)
{
return _image == c._image&&_real == c._real;
}

bool operator> (const Complex& c)
{
MoreThan(c);
}

bool MoreThan(const Complex& c)
{
if (_real > c._real)
{
return true;
}
else if (_real == c._real
&&_image > c._image)
{
return true;
}
return false;
}

Complex operator+ (const Complex& c)
{
/*Complex ret;
ret._real = _real + c._real;
ret._image = _image + c._image;*/

Complex ret(*this);
ret._real += c._real;
ret._image += c._image;
return ret;
}

Complex& operator+= (const Complex& c)
{
_real += c._real;
_image += c._image;
return *this;
}

private:
double _real;
double _image;
};
class Date
{
public:
/*Date()
{
cout << "Date()" << endl;
}*/
Date(int year = 1900, int month = 1, int day = 1)
        //使用初始化列表更高效
//必须使用初始化列表初始化的成员变量
        //1.常量成员变量
//2.引用类型成员变量
//3.没有缺省的构造函数的类成员变量
{
//cout << "Date(int year = 1900, int month = 1, int day = 1)" << if="" year=""> 1900
&& month > 0 && month<13 day="">0 && day <= GetMonthDay(year, month))
{
_year = year;
_month = month;
_day = day;
}
else
{
cout << "非法日期" << endl;
}
}

Date(const Date& d)
{
//cout << "Date(const Date& d)" << endl;
_year = d._year;
_month = d._month;
_day = d._day;
}

Date& operator=(const Date& d)
{

//cout << "Date& operator=(const Date& d)" << endl;
if (this != &d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
return *this;
}

~Date()
{
//cout << "~Date" << void="" -=""> const Date* this
{
cout << _year << "-" << _month << "-" << _day << public:="" bool="" operator="" return="" _year="=" d._year="" _month="=" d._month="" _day="=" const="" this="="> (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;
return (_year > d._year)
|| ((_year == d._year) && (_month > d._month))
|| ((_year == d._year) && (_month == d._month) && (_day > d._day));
}
bool operator >= (const Date& d)
{
return (*this > d) || (*this == d);
}
bool operator <(const return="" this="">= d);
}
bool operator<=(const return="" this=""> d);
}
//日期计算器
Date operator+(int day)
{

if (day<0) return="" this="" -="" date="" tmp._day="" while="">GetMonthDay(tmp._year, tmp._month))
{
tmp._day -= GetMonthDay(tmp._year, tmp._month);

if (tmp._month == 12)
{
tmp._year++;
tmp._month = 1;
}
else
{
++tmp._month;
}
}
return tmp;
}

Date& operator+=(int day)
{
/*if (day < 0)
{
return *this - (-day);
}

_day += day;
while (_day > GetMonthDay(_year, _month))
{
_day -= GetMonthDay(_year, _month);

if (_month == 12)
{
_year++;
_month = 1;
}
else
{
++_month;
}
}
return *this;*/
*this =*this + day;
return *this;
}

{
if (day < 0)
{
return *this + (-day);
}
Date operator-(int day)
Date tmp(*this);
tmp._day -= day;
while (tmp._day <= 0)
{

if (tmp._month == 1)
{
tmp._year--;
tmp._month = 12;
}
else
{
tmp._month--;
}
tmp._day += GetMonthDay(tmp._year, (tmp._month));
}
return tmp;
}
Date& operator-=(int day)
{
/*if (day < 0)
{
return *this + (-day);
}
_day -= day;
while (_day < 0)
{
_day += GetMonthDay(_year, (--_month));
if (_month == 1)
{
_year--;
_month = 12;
}
else
{
_month--;
}
}
return *this;*/
*this =*this - day;
return *this;
}

Date operator++() //前置++
{
*this += 1;
return *this;
}
Date operator++(int)//后置++
{
/*
Date tmp(*this);
tmp._day += 1;

while (_day > GetMonthDay(tmp._year, tmp._month))
{
tmp._day -= GetMonthDay(tmp._year, tmp._month);
if (tmp._month == 12)
{
tmp._year++;
tmp._month = 1;
}
else
{
tmp._month++;
}
}

return tmp;*/
Date tmp(*this);
*this += 1;
return *this;
}

Date operator--()//前置--
{
*this -= 1;
return *this;
}
Date operator--(int)//后置--
{
/*Date tmp(*this);
tmp._day -= 1;
while (tmp._day < 0)
{
tmp._day += GetMonthDay(tmp._year, (--tmp._month));
if (tmp._month == 1)
{
tmp._year--;
tmp._month = 12;
}
else
{
tmp._month--;
}
}
return tmp;*/
Date tmp(*this);
*this -= 1;
return *this;
}

int operator-(const Date& d)
{
int flag = 1;
Date max = *this;
Date min = d;

if (max < min)
{
swap(max._year, min._year);
swap(max._month, min._month);
swap(max._day, min._day);

flag = -1;
}
int days = 0;
while (min != max)
{
++min;
++days;
}
return days*flag;
}
private:
bool _IsLeapyear(int year)
{
if ((year % 4 == 0 && year % 100 != 0)
|| year % 400 == 0)
return true;
}

int GetMonthDay(int year, int month)
{
int monthArray[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int day = monthArray[month];
if (month == 2 && _IsLeapyear(year))
{
day += 1;
}
return day;
}

private:
int _year;
int _month;
int _day;
};
时间: 2024-08-08 13:16:14

c++实现日期类,日历计算器的相关文章

Java:日历类、日期类、数学类、运行时类、随机类、系统类

一:Calendar类 java.util 抽象类Calendar   1.static Calendar getInstance()使用默认时区和语言环境获得一个日历. 2. int get(int field) 返回给定日历字段的值. java.lang.Object 继承者 java.util.Calendar 所有已实现的接口: Serializable, Cloneable, Comparable<Calendar> 直接已知子类: GregorianCalendar 对于日期字段:

【C++】日期类+日期万年历+日期计算器

对于日期类,我们主要实现一下日期类的基本函数,构造,拷贝构造,运算符的重载,析构.当然这里运算符的重载需要实现的还是挺多的,如:=.<.>.<=.>=.等 #include <iostream> using namespace std; class Date { public:     Date(int year = 1990, int month = 1, int day = 1)     {         _year = year;         _month 

Date日期类,Canlendar日历类,Math类,Random随机数学类

Date日期类,SimpleDateFormat日期格式类 Date  表示特定的时间,精确到毫秒 常用方法 getTime() setTime() before() after() compareTo() 比较 toString() DateFormat是日期/时间格式化抽象类 SimpleDateFormat日期/时间格式化子类 SimpleDateFormat(模板字符串) 常用方法: format()  Date转换成字符串 parse ()  字符串转换成Date package co

day17 包装类、日期类

包装类 作用:1.丰富了基本数据类型只能存放值的问题,还提供了大量的方法或常量. 2.包装类充当了基本数据类型和引用数据类型转换的桥梁. 应用层面:包装类.String.基本数据类型的互相转换. 1.基本数据类型转为String: String str = Integer.toString(10): String str = 10 + "": 2.String转换为基本数据类型: int i = Integer.parseInt(str): 3.基本数据类型和包装类互转换: 语法糖--

JAVASE学习笔记:第八章 经常使用类Util工具包之日期类、数字类

一.Date类   日期类 所在java.Util工具包 before(Date when)   測试此日期是否在指定日期之前. getDay()  获取星期的某一天 getDate() 获取月中的某一天 二. Calendar类  日历类  所在java.Util工具包 Calendar c1=Calendar.getInstance(); //使用默认时区和语言环境获得一个日历 int year=c1.get(c1.YEAR);  //获取当前日期的年份 int mon=c1.get(Cal

I学霸官方免费教程二十三:Java常用类之日期类 Date类 SimpleDateFormat类 Calendar类

Date 类 创建对象时,默认获取系统当前时间 SimpleDateFormat类 用来格式化日期的:创建对象是可以传入格式:new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");主要方法format(Date) Calendar类 可以使用SimpleDateFormat类中的getCalendar()方法获取对象.常用方法:get(int); 常用属性:YEAR MONTH... 实例: package common_class; import j

Java核心API -- 4(日期类)

1. Date类(Java.utilDate) java.util.Date类用于封装日期及时间信息,一般仅用它显示某个日期,不对他作任何操作处理,作处理用Calendar类,计算方便. //创建一个Date实例,默认的构造方法创建的日期代表当前系统时间 Date date=new Date(); //此类重写了toString()方法,输出的是日期格式 System.out.println(date); //查看date内部的毫秒值 long time=date.getTime(); // 设

JAVASE学习笔记:第八章 常用类Util工具包之日期类、数字类

一.Date类   日期类 所在java.Util工具包 before(Date when)   测试此日期是否在指定日期之前. getDay()  获取星期的某一天 getDate() 获取月中的某一天 二. Calendar类  日历类  所在java.Util工具包 Calendar c1=Calendar.getInstance(); //使用默认时区和语言环境获得一个日历 int year=c1.get(c1.YEAR);  //获取当前日期的年份 int mon=c1.get(Cal

日期类的使用(java)-蓝桥杯

蓝桥杯日期问题常考,java提供了日期类很方便: //日历类 Calendar c = Calendar.getInstance(); // 获取实例化对象 Date date =c.getTime();      // 日期类得到c的时间: SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss"); // 修改格式 SimpleDateFormat sdf2 = new SimpleDateFormat(&q