C++實(shí)現(xiàn)萬(wàn)年歷功能
本文實(shí)例為大家分享了C++實(shí)現(xiàn)萬(wàn)年歷的具體代碼,供大家參考,具體內(nèi)容如下
1.此萬(wàn)年歷功能
1>日期加減天數(shù)
2>日期與日期之間的差值
3>輸入年月顯示當(dāng)月日歷
2.代碼實(shí)現(xiàn)
#include<iostream>
#include<iomanip>
using namespace std;
class Date
{
public:
Date(int year = 1990, int month = 1, int day = 1) //構(gòu)造函數(shù)
:_year(year), _month(month), _day(day)
{
if (JudgeRightDate()) //判斷傳入的值是否是合法的,不合法則置成1990年1月1日
{
_year = 1990;
_month = 1;
_day = 1;
}
}
bool JudgeRightDate() //判斷值是否合法函數(shù)
{
if (_year < 1 || ((_month< 1)||_month>12) ||
(_day<1)||_day>GetMonthDay(_year,_month))
{
return true;
}
else
{
return false;
}
}
int JudgeYear(int year) //判斷是否是閏年的函數(shù)
{
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
{
return 1;
}
else
return 0;
}
int GetMonthDay(int year, int month) //通過(guò)年和月得到對(duì)應(yīng)的天數(shù)
{
int arr[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int days = arr[month];
if (month == 2)
{
days += JudgeYear(year); //如果是閏年的二月則天數(shù)加1
}
return days;
}
Date operator +(int days) //日期加天數(shù)函數(shù),重載“+”實(shí)現(xiàn)
{
_day += days; //先將天數(shù)全部加到所給日期的“天”上
GetRightDate(_year, _month, _day); //再通過(guò)計(jì)算得到正確的日期。
return *this;
}
void GetRightDate(int &year, int &month, int &day) //計(jì)算出正確的日期
{
if (day <= 0)
{
while (day <= 0)
{
month--;
day += GetMonthDay(year, month);
if (month < 1)
{
year--;
month = 13;
}
}
}
else
{
while (day>GetMonthDay(year, month))
{
day -= GetMonthDay(year, month);
month++;
if (month > 12)
{
year++;
month = 1;
}
}
}
}
Date operator -(int days) //重載“-”實(shí)現(xiàn)日期減天數(shù)
{
_day -= days;
GetRightDate(_year, _month, _day);
return *this;
}
bool operator >(const Date &d) //判斷兩個(gè)日期的大小
{
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 operator ==(const Date &d) //判斷兩個(gè)日期是否相等
{
if (_year == d._year && _month == d._month && _day == d._day)
{
return true;
}
else
return false;
}
int operator -( Date &d) //計(jì)算日期差函數(shù),重載“-”實(shí)現(xiàn)
{
int count = 0;
Date tmp(*this);
if (*this > d)
{
tmp = d;
d = *this;
*this = tmp;
}
while (!(*this==d))
{
count++;
*this =*this+1;
}
return count;
}
void print() //打印函數(shù)
{
cout << _year << "-" << _month << "-" << _day;
}
int week() //求出日期對(duì)應(yīng)的星期函數(shù)
{
int w = 0;
int y = _year;
int m = _month;
if (m == 1 || m == 2)
{
m = _month + 12;
y = _year - 1;
}
w = _day + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400;
w = w % 7 + 1;
return w;
}
void print_week()
{
cout << "星期日 星期一 星期二 星期三 星期四 星期五 星期六" << endl;
}
void print_day() //根據(jù)日期和星期,正確的輸出日歷
{
int line = 1;
int days = GetMonthDay(_year,_day);
int w = week();
if (w != 7)
{
for (int blank = w - 1; blank; --blank, ++line)
{
cout << setw(7) << "";
}
}
for (int d = 1; d <= days; ++d, ++line)
{
cout << setw(7) << d;
if (line % 7 == 0)
{
cout << endl;
}
}
cout << endl;
}
private:
int _year;
int _month;
int _day;
};
void menu()
{
cout << setw(40) <<"萬(wàn)年歷"<< endl;
cout << "1.日期加減天數(shù)" << endl;
cout << "2.日期減日期" << endl;
cout << "3.輸入年月顯示當(dāng)月日歷" << endl;
}
void choice()
{
int num = 0;
int year, month, day, days;
char ch = '+';
cin >> num;
if (num == 1)
{
cout << "請(qǐng)輸入日期:" << endl;
cin >> year >> month >> day;
cout << "請(qǐng)輸入天數(shù):" << endl;
cin >> days;
cout << "請(qǐng)輸入'+'或者'-':" << endl;
cin >> ch;
Date d1(year, month, day);
Date d2;
if (ch == '+')
{
d2 = d1 + days;
}
else if (ch == '-')
{
d2 = d1 - days;
}
else
{
cout << "無(wú)效的輸入!" << endl;
}
cout << "計(jì)算后的日期為:";
d2.print();
cout << endl;
}
else if (num==2)
{
cout << "請(qǐng)輸入日期:" << endl;
cin >> year >> month >> day;
Date d3(year, month, day);
cout << "請(qǐng)輸入日期:" << endl;
cin >> year >> month >> day;
Date d4(year, month, day);
int ret = d3 - d4;
cout << "期間相差:" << ret << endl;
}
else if (num == 3)
{
cout << "請(qǐng)輸入年月:" << endl;
cin >> year >> month;
Date d5(year, month);
d5.print_week();
d5.print_day();
}
}
int main()
{
menu();
choice();
system("pause");
return 0;
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C語(yǔ)言?超詳細(xì)總結(jié)講解二叉樹(shù)的概念與使用
二叉樹(shù)可以簡(jiǎn)單理解為對(duì)于一個(gè)節(jié)點(diǎn)來(lái)說(shuō),最多擁有一個(gè)上級(jí)節(jié)點(diǎn),同時(shí)最多具備左右兩個(gè)下級(jí)節(jié)點(diǎn)的數(shù)據(jù)結(jié)構(gòu)。本文將詳細(xì)介紹一下C++中二叉樹(shù)的概念和結(jié)構(gòu),需要的可以參考一下2022-04-04
C++利用鏈表寫(xiě)一個(gè)簡(jiǎn)單的棧實(shí)例詳解
這篇文章主要介紹了C++利用鏈表寫(xiě)一個(gè)簡(jiǎn)單的棧實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-05-05
C++實(shí)現(xiàn)LeetCode(106.由中序和后序遍歷建立二叉樹(shù))
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(106.由中序和后序遍歷建立二叉樹(shù)),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
C語(yǔ)言結(jié)課設(shè)計(jì)之計(jì)算器功能
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言結(jié)課設(shè)計(jì)之計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-02-02
Qt創(chuàng)建SQlite數(shù)據(jù)庫(kù)的示例代碼
本文主要介紹了Qt創(chuàng)建SQlite數(shù)據(jù)庫(kù)的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
用C語(yǔ)言求解一元二次方程的簡(jiǎn)單實(shí)現(xiàn)
這篇文章主要介紹了用C語(yǔ)言求解一元二次方程的簡(jiǎn)單實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11

