c++c程序设计c++程序设计题:1,定义一个日期类,要求:(1)实现带3个参数的构造函数; 15
(2)实现不带参数但将日期初始化为指定日期;(3),实现显示日期的成员函数showdate(),日期格式为“2011-11-09”...
(2)实现不带参数但将日期初始化为指定日期;(3),实现显示日期的成员函数showdate(),日期格式为“2011-11-09”
展开
3个回答
2011-11-08
展开全部
#include <stdio.h>
class Date
{
public:
int year,month,day;
public:
Date()
{
year=2011;
month=11;
day=8;
}
Date(int y,int m,int d)
{
year=y;
month=m;
day=d;
}
void showdate()
{
printf("%d-%d-%d\n",year,month,day);
}
};
void main()
{
Date d1;
Date d2(1990,12,10);
d1.showdate();
d2.showdate();
}
class Date
{
public:
int year,month,day;
public:
Date()
{
year=2011;
month=11;
day=8;
}
Date(int y,int m,int d)
{
year=y;
month=m;
day=d;
}
void showdate()
{
printf("%d-%d-%d\n",year,month,day);
}
};
void main()
{
Date d1;
Date d2(1990,12,10);
d1.showdate();
d2.showdate();
}
追问
c++
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
没人回答,我来贴个代码溜溜
不好意思,刚才代码有点小错误,现在改过来了。
#include <iostream>
using namespace std;
class Date
{
private:
int year,month,day;
public:
Date();
Date(int y,int m,int d)
{
year=y;
month=m;
day=d;
}
~Date();
void setDate(int year = 2011,int month=11,int day=07);
void print();
void addyear(int n);
void addmonth(int n);
void showDate();
};
void main()
{
/* Date hao;
hao.setDate();
hao.print();
hao.addyear(4);
hao.addmonth(2);
hao.print();
*/
Date d1;
Date d2(1990,12,10);
d1.showDate();
d2.showDate();
}
Date::Date()
{
this->year = 2011;
this->month = 11;
this->day = 9;
}
Date::~Date()
{
}
void Date::setDate(int year,int month,int day)
{
this->year = year;
this->month = month;
this->day = day ;
}
void Date::print()
{
cout<<year<<"年"<<month<<"月"<<day<<"日"<<endl;
}
void Date::addyear(int n)
{
this->year += n;
}
void Date::addmonth(int n)
{
if((this->month + n) % 12 == 0)
{
this->month = 12;
this->year += (this->month + n) / 12 - 1;
}
else
{
this->year += (this->month + n) / 12;
this->month = (this->month + n) % 12;
}
}
void Date::showDate()
{
cout<<year;
if(month<10)
cout<<"-0"<<month;
else
cout<<"-"<<month;
if(day<10)
cout<<"-0"<<day<<endl;
else
cout<<"-"<<day<<endl;
}
不好意思,刚才代码有点小错误,现在改过来了。
#include <iostream>
using namespace std;
class Date
{
private:
int year,month,day;
public:
Date();
Date(int y,int m,int d)
{
year=y;
month=m;
day=d;
}
~Date();
void setDate(int year = 2011,int month=11,int day=07);
void print();
void addyear(int n);
void addmonth(int n);
void showDate();
};
void main()
{
/* Date hao;
hao.setDate();
hao.print();
hao.addyear(4);
hao.addmonth(2);
hao.print();
*/
Date d1;
Date d2(1990,12,10);
d1.showDate();
d2.showDate();
}
Date::Date()
{
this->year = 2011;
this->month = 11;
this->day = 9;
}
Date::~Date()
{
}
void Date::setDate(int year,int month,int day)
{
this->year = year;
this->month = month;
this->day = day ;
}
void Date::print()
{
cout<<year<<"年"<<month<<"月"<<day<<"日"<<endl;
}
void Date::addyear(int n)
{
this->year += n;
}
void Date::addmonth(int n)
{
if((this->month + n) % 12 == 0)
{
this->month = 12;
this->year += (this->month + n) / 12 - 1;
}
else
{
this->year += (this->month + n) / 12;
this->month = (this->month + n) % 12;
}
}
void Date::showDate()
{
cout<<year;
if(month<10)
cout<<"-0"<<month;
else
cout<<"-"<<month;
if(day<10)
cout<<"-0"<<day<<endl;
else
cout<<"-"<<day<<endl;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
//输出格式改了下
#include <iostream>
#include <iomanip>
using namespace std;
class ymd
{
public:
ymd()
{
y=2011;
m=11;
d=9;
}
ymd(int yy,int mm,int dd)
{
if((yy>0&&mm>1&&mm<=12&&dd>=1&&dd<=dmax(yy,mm)))
{
y=yy;
m=mm;
d=dd;
}
else
{
cerr<<"该日期不存在:"
<<setw(4)<<setfill('0')<<yy<<"-"
<<setw(2)<<setfill('0')<<mm<<"-"
<<setw(2)<<setfill('0')<<dd;
}
}
ymd(ymd& yymmdd)
{
y=yymmdd.y;
m=yymmdd.m;
d=yymmdd.d;
}
void showdate()
{
cout<<setw(4)<<setfill('0')<<y<<"-"
<<setw(2)<<setfill('0')<<m<<"-"
<<setw(2)<<setfill('0')<<d;
}
private:
int isly(int year)
{
return ((!(year%4))&&(year%100)||(!(year%400)));
}
int dmax(int y,int m)
{
if(m==2)
{
return 28+(isly(y));
}
if((m==1||m==3||m==5||m==7||m==8||m==10||m==12))
return 31;
return 30;
}
int y;
int m;
int d;
};
int main()
{
ymd ymd1;
ymd1.showdate();
ymd ymd2(2012,12,12);
cout<<endl;
ymd2.showdate();
ymd1=ymd2;
cout<<endl;
ymd1.showdate();
cout<<endl;
ymd ymd3(2011,2,29);
cout<<endl;
return 0;
}
2011-11-09
2012-12-12
2012-12-12
该日期不存在:2011-02-29
请按任意键继续. . .
#include <iostream>
#include <iomanip>
using namespace std;
class ymd
{
public:
ymd()
{
y=2011;
m=11;
d=9;
}
ymd(int yy,int mm,int dd)
{
if((yy>0&&mm>1&&mm<=12&&dd>=1&&dd<=dmax(yy,mm)))
{
y=yy;
m=mm;
d=dd;
}
else
{
cerr<<"该日期不存在:"
<<setw(4)<<setfill('0')<<yy<<"-"
<<setw(2)<<setfill('0')<<mm<<"-"
<<setw(2)<<setfill('0')<<dd;
}
}
ymd(ymd& yymmdd)
{
y=yymmdd.y;
m=yymmdd.m;
d=yymmdd.d;
}
void showdate()
{
cout<<setw(4)<<setfill('0')<<y<<"-"
<<setw(2)<<setfill('0')<<m<<"-"
<<setw(2)<<setfill('0')<<d;
}
private:
int isly(int year)
{
return ((!(year%4))&&(year%100)||(!(year%400)));
}
int dmax(int y,int m)
{
if(m==2)
{
return 28+(isly(y));
}
if((m==1||m==3||m==5||m==7||m==8||m==10||m==12))
return 31;
return 30;
}
int y;
int m;
int d;
};
int main()
{
ymd ymd1;
ymd1.showdate();
ymd ymd2(2012,12,12);
cout<<endl;
ymd2.showdate();
ymd1=ymd2;
cout<<endl;
ymd1.showdate();
cout<<endl;
ymd ymd3(2011,2,29);
cout<<endl;
return 0;
}
2011-11-09
2012-12-12
2012-12-12
该日期不存在:2011-02-29
请按任意键继续. . .
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询