溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

c++實現(xiàn)日期類,日歷計算器

發(fā)布時間:2020-07-17 22:27:03 來源:網絡 閱讀:630 作者:Zxiaoxue 欄目:編程語言

在寫日期類日期計算器之前先實現(xiàn)了一個簡單的復數(shù)類 //引用做參數(shù),1.swap--在函數(shù)內部改變參數(shù),2.Bigdata提高效率 //內聯(lián)函數(shù)必須在定義處加上inline //定義在類內部的函數(shù)默認為內聯(lián)函數(shù) //c++中盡量使用const,枚舉,內聯(lián)去替代宏 //宏沒有類型安全的檢查,在預處理的時候替換了所以無法調試,宏安全性不高

class Complex
{
public:
Complex(double real = 0.0, double p_w_picpath = 0.0)//定義了一個全缺省的構造函數(shù)
:_real(real)
, _p_w_picpath(p_w_picpath)
{
cout << "Complex()" << endl;//這樣可以在屏幕上輸出構造函數(shù)的信息,方便分析構造函數(shù)的調用
}

Complex(const Complex& c)//拷貝構造函數(shù)
{
cout << "Complex(const Complex& c)" << endl;
_real = c._real;
_p_w_picpath = c._p_w_picpath;
}

~Complex()//析構函數(shù)
{
cout << "~Complex()" << endl;
}

Complex& operator=(const Complex& c)// Complex返回值支持連續(xù)的賦值
{
cout << "Complex& operator=()" << endl;
if (this != &c)
{
_real = c._real;
_p_w_picpath = c._p_w_picpath;
}

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

bool operator== (const Complex& c)
{
IsEqual(c);
}
bool IsEqual(const Complex& c)
{
return _p_w_picpath == c._p_w_picpath&&_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
&&_p_w_picpath > c._p_w_picpath)
{
return true;
}
return false;
}

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

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

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

private:
double _real;
double _p_w_picpath;
};
class Date
{
public:
/*Date()
{
cout << "Date()" << endl;
}*/
Date(int year = 1900, int month = 1, int day = 1)
        //使用初始化列表更高效
//必須使用初始化列表初始化的成員變量
        //1.常量成員變量
//2.引用類型成員變量
//3.沒有缺省的構造函數(shù)的類成員變量
{
//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;
};
向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。

AI