第九章 作业-电子科技大C与C++程序设计

本篇博客介绍了如何使用C++编程实现一个带有日期功能的时钟类,包括日期类Date的设计,实现days方法计算月份天数,NewDay方法增加日期,以及时钟类ClockWithDate的继承与功能实现,如showTime方法显示时间和日期,run方法增加时间并处理跨日期的情况。通过示例展示了程序的运行输入输出过程。
摘要由CSDN通过智能技术生成

2实现带日期的时钟类(100分)

实现带日期的时钟类,具体要求如下:

已知时钟类的定义如下:

#include<iostream>

using namespace std;

bool leap(int year)

{

       if(year%400==0||(year%4==0 && year%100!=0))

              return true;

       return false;

}

class Clock{

public:

 Clock(int h,int m,int s)

 {

  hour =(h>23? 0:h);

  minute = (m>59?0:m);

  second = (s>59?0:s);

 }

 virtual void run()

 {

  second = second+1;

  if (second>59)

  {

   second =0;

   minute+=1;

  }

  if (minute>59)

  {

   minute =0;

   hour+=1;

  }

  if (hour>23)

  {

   hour =0;

  }

 }

 virtual void showTime()

 {

  cout<<"Now:"<<hour<<":"<<minute<<":"<<second<<endl;

 }

 int getHour(){return hour;}

 int getMinute(){return minute;}

 int getSecond(){return second;}

 

protected:

 int hour;

 int minute;

 int second;

};

日期类定义如下:

class Date{

public:

 Date(int y=1996,int m=1,int d=1)

 {

 

  if (m>12||m<1)

  {

   m=1;

  }

  if(d>days(y,m))

  {

   day = 1;

  }

  day =d;

  year =y;

  month =m;

 };

 int days(int year,int month);

 void NewDay();

 void showTime()

 {

  cout<<year<<"-"<<month<<"-"<<day<<endl;

 }

protected:

 int year;

 int month;

 int day;

};
int main()

{

       int h,m,s,day,month,year;

       cin>>h>>m>>s>>day>>month>>year;

       ClockWithDate cd1(h,m,s,day,month,year);

       cd1.showTime();

       cout<<"现在运行x秒:";

       int x;

       cin>>x;

       for(int i=0;i<x;++i)

              cd1.run();

       cd1.showTime();

       return 0;

}

需要类外实现Date类的days方法,根据年和月,返回该年该月对应的天数

实现Date类的NewDay方法,该方法将Date代表的日期增加一天。

实现ClockWithDate类,它继承至Clock类和Date类,记录时间和日期

重新实现ClockWithDate类的showTime方法和run方法。

showTime方法输出当的时间和日期,先输出时间再输出日期。

run方法每次将现在的时间增加一秒,并且当时间超过23:59:59时,更新日期。

比如某次程序运行输入当前时间是:1 1 1 7 10 2000(2000年10月7号1点1分1秒),然后输入运行时间x: 5,则程序运行的输入输出如下:

输入:

1 1 1 7 10 2000

5

输出:

Now:1:1:1

2000-10-7

现在运行x秒:Now:1:1:6

2000-10-7

时间限制:500ms内存限制:32000kb

#include<iostream>

using namespace std;

bool leap(int year)

{

       if(year%400==0||(year%4==0 && year%100!=0))

              return true;

       return false;

}

class Clock{

public:

 Clock(int h,int m,int s)

 {

  hour =(h>23? 0:h);

  minute = (m>59?0:m);

  second = (s>59?0:s);

 }

 virtual void run()

 {

  second = second+1;

  if (second>59)

  {

   second =0;

   minute+=1;

  }

  if (minute>59)

  {

   minute =0;

   hour+=1;

  }

  if (hour>23)

  {

   hour =0;

  }

 }

 virtual void showTime()

 {

  cout<<"Now:"<<hour<<":"<<minute<<":"<<second<<endl;

 }

 int getHour(){return hour;}

 int getMinute(){return minute;}

 int getSecond(){return second;}

 

protected:

 int hour;

 int minute;

 int second;

};
class Date{

public:

 Date(int y=1996,int m=1,int d=1)

 {

 

  if (m>12||m<1)

  {

   m=1;

  }

  if(d>days(y,m))

  {

   day = 1;

  }

  day =d;

  year =y;

  month =m;

 };

 int days(int year,int month);

 void NewDay();

 void showTime()

 {

  cout<<year<<"-"<<month<<"-"<<day<<endl;

 }

protected:

 int year;

 int month;

 int day;

};
void Date::NewDay()
{
	day = day + 1;
	switch (month)
	{
	case 1:
	case 3:
	case 5:
	case 7:
	case 8:
	case 10:
	case 12:
		if (day>31)
		{
			day = 1; month = month + 1;
		}break;
	case 4:
	case 9:
	case 11:
	case 6:
		if (day>30)
		{
			day = 1; month++;
		}break;
	case 2:
		if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0) && day==29)
		{
			day = 1; month++;
		}
		else if (day>28)
		{
			day = 1;
			month++;
		}

	}
	if (month>12)
	{
		year++;
		month = 1;
	}
}
int Date::days(int year, int month)
{
	int num;
	switch (month)
	{
	case 1:
	case 3:
	case 5:
	case 7:
	case 8:
	case 10:
	case 12:
		num = 31; break;
	case 2:
		if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
			num = 29;
		else
			num = 28;
		break;
	case 4:
	case 6:
	case 9:
	case 11:
		num = 30;
		break;
	default:
		num = 31;
	}
	return num;
}

class ClockWithDate :public Clock, public Date {
private:
	int ye, mon, da, hou, min, sec;
public:
	ClockWithDate(int h, int mi, int s,int y, int m, int d ) : Clock(h, mi, s),Date(y, m, d){
		this->ye = y;
		this->mon = m;
		this->da = d;
		this->hou = h;
		this->min = mi;
		this->sec = s;
	}
	virtual void run() {
		sec = sec + 1;
		if (sec>59)
		{
			sec = 0;
			min += 1;
		}
		if (min>59)
		{
			min = 0;
			hou += 1;
		}
		if (hou>23)
		{
			hou = 0;
			da += 1;
			switch (mon)
			{
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12:
				if (da>31)
				{
					da = 1; mon = mon + 1;
				}break;
			case 4:
			case 9:
			case 11:
			case 6:
				if (da>30)
				{
					da = 1; mon++;
				}break;
			case 2:
				if (((ye % 4 == 0 && ye % 100 != 0) || (ye % 400 == 0)) && da>28)
				{
					//da = 1; mon++;
				}
				else if (da>28)
				{
					da = 1;
					mon++;
				}

			}
			if (mon>12)
			{
				ye++;
				mon = 1;
			}
		}

	}
	void showTime() {
		cout << "Now:" << hou << ":" << min << ":" << sec << endl;
		cout << ye << "-" << mon << "-" << da << endl;
	}
};

Clock* createClockWithDate(int h, int m, int s, int year, int month, int day) {
	return new ClockWithDate(h, m, s, year, month, day);} 
 

int main()

{

       int h,m,s,day,month,year;

       cin>>h>>m>>s>>day>>month>>year;

       ClockWithDate cd1(h,m,s,year,month,day);

       cd1.showTime();

       cout<<"现在运行x秒:";

       int x;

       cin>>x;

       for(int i=0;i<x;++i)

              cd1.run();

       cd1.showTime();

       return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值