C++编程题 定义满足如下要求的Date类 (1)数据成员int year,month,day分 100
C++编程题定义满足如下要求的Date类(1)数据成员intyear,month,day分别表示年、月、日(2)成员函数voiddisp()用下面的格式输出日期:年/月/...
C++编程题
定义满足如下要求的Date类
(1)数据成员int year,month,day分别表示年、月、日
(2)成员函数void disp()用下面的格式输出日期:年/月/日
(3)可以在日期上加一个天数,用成员函数重载日期类Date的+运算符
注:能被4整除但不能被100整除的年份或者能被400整除的年份是闰年。 展开
定义满足如下要求的Date类
(1)数据成员int year,month,day分别表示年、月、日
(2)成员函数void disp()用下面的格式输出日期:年/月/日
(3)可以在日期上加一个天数,用成员函数重载日期类Date的+运算符
注:能被4整除但不能被100整除的年份或者能被400整除的年份是闰年。 展开
3个回答
展开全部
亲测可用,谢谢!望采纳!已经考虑了加上新的日期后月份和年的变化,同时还考虑了每个月份的不同天数
#include<iostream>
using namespace std;
class Date
{
public:
Date(int year, int month, int day)
:m_year(year)
,m_month(month)
,m_day(day)
{
if(m_year > 9999 || m_year < 1000 ||
m_month>12 || m_month < 1 ||
m_day>31 || m_day<1)
throw("Invalid Date Format!");
// 闰年
if (m_year % 4 == 0 && m_year % 100 != 0 || m_year % 400 == 0)
{
if(m_month == 2)
{
if(m_day >m_daysPerMonthLeap[1])
{
throw("Invalid Data Format!");
}
}
}
if(m_day > m_daysPerMonth[m_month])
throw("Invalid Data Format!");
}
Date (const Date& d)
{
m_year = d.m_year;
m_month = d.m_month;
m_day = d.m_day;
}
void display()
{
std::cout<<m_year<<"年"<<m_month<<"月"<<m_day<<"日"<<std::endl;
}
Date operator+(int days)
{
m_day += days;
while(m_day > m_daysPerMonth[m_month-1])
{
if ((m_year % 4 == 0 && m_year % 100 != 0 || m_year % 400 == 0) && m_month ==2)
{
m_day = m_day - m_daysPerMonthLeap[m_month-1];
}
else
m_day = m_day - m_daysPerMonth[m_month-1];
m_month++;
while(m_month > 12)
{
m_year++;
m_month = m_month - 12;
}
}
return *this;
}
private:
static int m_daysPerMonth[12];
static int m_daysPerMonthLeap[12];
int m_year;
int m_month;
int m_day;
};
int Date::m_daysPerMonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int Date::m_daysPerMonthLeap[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int main()
{
Date d(2015, 2, 25);
d = d+5;
d.display();
}
展开全部
I got the better solution for your code. You might or not accpt mine as the final answer in response to your request. But it is never worse to share the code online for later users who pursuit the answer to similiar questions like yours.
#include <iostream>
using namespace std;
class Date {
private:
int day, month, year;
Date();
int mm_calendar( int _mm, bool _is ) const {
if ( _is ) {
int _month[] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
return _month[ _mm-1 ];
} else {
int _month[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
return _month[ _mm-1 ];
}
}
bool is_year ( int _yy ) {
if ( _yy%4 == 0 && _yy%10 != 0 ) { return true; }
else if ( _yy%100 == 0 ) { return true; }
else { return false; }
}
public:
Date( int dd, int mm, int yy ): day( dd ), month( mm ), year( yy ) {}
Date operator + ( int days ) {
// show the date before the calculation
this -> display();
cout << " + " << days << " Days\n";
// new properties
int dd_new = day+days;
int mm_new = month;
int yy_new = year;
int sum = mm_calendar( mm_new, is_year(yy_new) );
// calculation - day / month / year
if ( dd_new <= mm_calendar( mm_new, is_year(year) ) ) {
day = dd_new;
}
while ( dd_new > sum ) {
dd_new -= sum; mm_new++; /* month increase */
if ( mm_new > 12 ) {
mm_new = 1; yy_new++;
} /* year increase */
sum = mm_calendar( mm_new, is_year(yy_new) );
}
day = dd_new;
month = mm_new;
year = yy_new;
// show the date after calculation
this -> display();
return *this;
}
void display() {
cout << day << "-" << month << "-" << year << '\t';
if ( is_year(year) ) { cout << "[special year]\n"; }
else { cout << "[No special year]\n"; }
}
};
int main(int argc, char *argv[]) {
Date day( 1, 2, 2012 );
day = day+400;
Date other( day );
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你这题目不全吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询