华科万维C++章节练习7_3

程序设计】
---------------------------------------------------------

题目:

定义一个日期类Date,包含年、月、日三个数据成员(int),定义带有3个参数的构造函数,
以及一个求日期是当年的第几天的成员函数和输出日期的成员函数,
日期的显示格式为年/月/日。编写主函数进行测试。

tips:

(每年各月天数分别为31,28,31,30,31,30,31,31,30,31,30,31,闰年2月为29天,
闰年的条件year%4==0&&year%100!=0)||year%400==0)) 

样例:

2009/2/1
32
2012/3/1
61
请按任意键继续. . . 

代码如下:

#include <iostream>
using namespace std;

/**********Program**********/
class Date{
private:
	int year,month,day;
public:
	Date();
	Date(int ,int ,int );
	Date(Date &);
	~Date(){}
	void SetD(int ,int ,int );
	int getDay();
	void show();
};
Date::Date()
{
	year=0;
	month=0;
	day=0;
}
//默认构造函数
Date::Date(int y,int m,int d)
{
	year=y;
	month=m;
	day=d;
}
//构造函数
Date::Date(Date &date)
{
	year=date.year;
	month=date.month;
	day=date.day;
}
//复制构造函数
int Date::getDay()
{
	int sequence=0;
	switch(month)
	{
	case 12:
		sequence+=30;
	case 11:
		sequence+=31;
	case 10:
		sequence+=30;
	case 9:
		sequence+=31;
	case 8:
		sequence+=31;
	case 7:
		sequence+=30;
	case 6:
		sequence+=31;
	case 5:
		sequence+=30;
	case 4:
		sequence+=31;
	case 3:
		if(year%400==0||(year%100!=0&&year%4==0))
		{
			sequence+=29;
		}
		else sequence+=28;
	case 2:
		sequence+=31;
	}
	sequence+=day;
	return sequence;
}

void Date::SetD(int a,int b,int c)
{
	year=a;
	month=b;
	day=c;
}
/**********  End  **********/
void Date::show()
{
        cout<<year<<"/"<<month<<"/"<<day<<endl;

}
int main()
{
        Date d1(2009,2,1),d2;   //d1为2009年2月1日
        d1.show();
        cout<<d1.getDay()<<endl;
        d2.SetD(2012,3,1);    //d2为2012年3月1日
        d2.show();
        cout<<d2.getDay()<<endl;
        return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

季风13

谢谢认可

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值