*/
* Copyright (c) 2016,烟台大学计算机与控制工程学院
* All rights reserved.
* 文件名:time.cpp
* 作者:常轩
* 微信公众号:Worldhello
* 完成日期:2016年4月12日
* 版本号:V1.4
* 问题描述:
* 程序输入:无
* 程序输出:见运行结果
*/
#include <iostream>
using namespace std;
class Date;
class Time{
public:
Time(int,int,int);
void add_a_second(Date &);
void display(Date &);
private:
int hour;
int minute;
int sec;
};
Time::Time(int a,int b,int c)
{
hour=a;
minute=b;
sec=c;
}
class Date{
public:
Date(int,int,int);
friend class Time;
private:
int month;
int day;
int year;
};
Date::Date(int m,int d,int y)
{
month=m;
day=d;
year=y;
}
int main()
{
Time t1(23,59,32);
Date d1(12,31,2013);
for(int i=0;i<=100;i++)
{
t1.add_a_second(d1);
t1.display(d1);
}
return 0;
}
int days(int x,int y);
void Time::add_a_second(Date &p)
{
sec++;
if(sec>59)
{
sec=0;
minute++;
if(minute>59)
{
hour++;
minute=0;
}
if(hour>23)
{
p.day++;
hour=0;
}
if(p.day>days(p.month,p.year))
{
p.month++;
p.day=1;
}
if(p.month>12)
{
p.year++;
p.month=1;
}
}
}
void Time::display(Date &p)
{
cout<<p.year<<"年"<<p.month<<"月"<<p.day<<"日";
cout<<hour<<":"<<minute<<":"<<sec<<endl;
}
int days(int x,int y)
{
int d[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
if((y%4==0&&y%100!=0)||(y%400==0))
d[2]=29;
return d[x];
}
运行结果:
心得:
虽然过程很坎坷,但是最终还是做出来了