问题及代码:
/*
*Copyright (c)2014,烟台大学计算机与控制工程学院
*All rights reserved.
*文件名称:玩日期时间.cpp
*作 者:白云飞
*完成日期:2014年12月23日
*版 本 号:v1.0
*
*问题描述:定义一个表示时间(包括年、月、日、时、分、秒)的结构体,然后完成下面的功能
(1)输入一个时间,将输入的时间保存在一个结构体变量中;
(2)输出该日在本年中是第几天(注意闰年问题);
(3)输出这是这一天中的第几秒;
(4)输出这是这一年中的第几秒;(不要认为这个数荒唐,在计算中需要取随机数时需要一个不会重复的“种子数”,这个秒数是常用的。)
(5)求你输入的时间d天后是哪年哪月哪日,将结果保存在一个结构体变量中输出;(你的万天日期靠这个功能了)——插讲一个故事。当年老贺由小孩儿过百日,想到自己的万日在哪天。编程计算,结果刚过了十几天,那个懊恼啊。第二个万日得再等26年多(现在离第二个万日靠近了),第三个万日,谁敢说一定能过上?20岁左右的你,要算清楚了。在第(1)问中输入你的生日及时辰,d值取为10000,可以算出你的万日,大概在26岁多。
(6)求你输入的时间s秒后是何日何时,将结果保存在一个结构体变量中输出;
(7)用结构体变量给定两个时间,求相差多少天?相差多少秒?
*/
#include <iostream>
#include <cmath>
using namespace std;
struct Time
{
int year;
int month;
int day;
int hour;
int minute;
int second;
};
int daysOfMonth(int n,int y)//返回y年m月的天数
{
int days;
if ((y%4==0&&y%100!=0)||y%400==0)
{
if(n==1||n==3||n==5||n==7||n==8||n==10||n==12)
days=31;
else if (n==2)
days=29;
else
days=30;
}
else
{
if(n==1||n==3||n==5||n==7||n==8||n==10||n==12)
days=31;
else if (n==2)
days=28;
else
days=30;
}
return days;
}
int dayOfYear(Time &t)//这天是这一年的第几天
{
int days=0,i;
for(i=1; i<t.month; i++)
{
days+=daysOfMonth(t.month,t.year);
}
days+=t.day;
return days;
}
int secondOfDay(Time &t)//这天是这一天的第几秒
{
return t.hour*3600+t.minute*60+t.second;
}
int daysOfYear(int y)//返回一年有多少天
{
return ((y%4==0&&y%100!=0)||y%400==0)?366:365;
}
Time afterDays(Time t,int d)//求你输入的时间d天后是哪年哪月哪日
{
Time t1=t;
int d1=d+dayOfYear(t); //dayOfYear(t)求出t是当年第几天
t1.month=1;
t1.day=0; //这样,将问题转换为在当年1月0日基础上加d1天以及剩余天数一加以后持续进位
while(d1>daysOfYear(t1.year)) //天数还够一个整年
{
d1-=daysOfYear(t1.year);
++t1.year;
}
//天数不够一整年后,再考虑月,因为从1月1日开始,不用担心Nt.year再加1年
while(d1>daysOfMonth(t1.month,t1.year)) //天数还够一个整月
{
d1-=daysOfMonth(t1.month,t1.year);
++t1.month;
}
//剩全天数加到日上
t1.day+=d1;
return t1;
}
int aftersecond(Time &t)
{
struct Date //定义一个保存目标日期的结构体
{
int hour;
int minute;
int second;
};
Date date ; //目标时间
cout<<"请输入经过的秒数(一天内):";
int s; //经过的秒数
cin>>s;
date.hour=s/3600; //计算目标时间
date.minute=(s-date.hour*3600)/60;
date.second=s-date.hour*3600-date.minute*60+t.second;
date.hour+=t.hour;
date.minute+=t.minute;
cout<<s<<"秒后是"<<date.hour<<"时"<<date.minute<<"分"<<date.second<<"秒"<<endl;
return 0;
}
int d_date(Time &t1,Time &t2)
{
int s,h1,h2,h3,days1,days2;
days1=dayOfYear(t1);
days2=dayOfYear(t2);
h1=t1.hour*60*60+t1.minute*60+t1.second;
h2=t2.hour*60*60+t2.minute*60+t2.second;
h3=fabs(h1-h2); //求两段时间的绝对值
if(t1.year==t2.year)//判断年份大小
{
if(days1>days2) s=days1-days2;
else s=days2-days1;
cout<<"相差:"<<s<<"天"<<h3<<"秒"<<endl;
}
else if(t1.year>t2.year)
{
while(t1.year>=t2.year)
{
t1.year--;
if(((t1.year%4==0&&t1.year%100!=0)||t1.year%400==0)) //判断是否闰年
s+=366;
else
s+=365;
t1.year--;
}
days1+=s;
days1-=days2;
cout<<"相差:"<<days1<<"天"<<h3<<"秒"<<endl;
}
else
{
while(t1.year<=t2.year)
{
t2.year--;
if(((t2.year%4==0&&t2.year%100!=0)||t2.year%400==0)) //判断是否闰年
s+=366;
else
s+=365;
t2.year--;
}
days2+=s;
days2-=days1;
cout<<"相差:"<<days2<<"天"<<h3<<"秒"<<endl;
}
return 0;
}
int main()
{
Time t,nt,t1,t2;
//(1)输入一个时间,将输入的时间保存在一个结构体变量中;
cout<<"请输入一个时间(包括年,月,日,时,分,秒):"<<endl;
cin>>t.year>>t.month>>t.day>>t.hour>>t.minute>>t.second;
cout<<"时间为: "<<t.year<<"年"<<t.month<<"月"<<t.day<<"日"<<t.hour<<"时"<<t.minute<<"分"<<t.second<<"秒"<<endl;
//(2)输出该日在本年中是第几天(注意闰年问题);
cout<<"这是这一年中的第"<<dayOfYear(t)<<"天。"<<endl;
//(3)输出这是这一天中的第几秒;
cout<<"这是这一天中的第"<<secondOfDay(t)<<"秒。"<<endl;
//(4)输出这是这一年中的第几秒;
cout<<"这是这一年中的第"<<dayOfYear(t)*24*3600+secondOfDay(t)<<"秒。"<<endl;
//(5)求你输入的时间d天后是哪年哪月哪日,将结果保存在一个结构体变量中输出;
int d;
cout<<"请输入一个天数:";
cin>>d;
nt=afterDays(t,d);
cout<<"这一天后d(如10000)天后是:";
cout<<"时间为: "<<nt.year<<"年"<<nt.month<<"月"<<nt.day<<"日"<<nt.hour<<"时"<<nt.minute<<"分"<<nt.second<<"秒"<<endl;
aftersecond(t);
cout<<"请输入一个时间(包括年,月,日,时,分,秒):"<<endl;
cin>>t1.year>>t1.month>>t1.day>>t1.hour>>t1.minute>>t1.second;
cout<<"请再输入一个时间(包括年,月,日,时,分,秒):"<<endl;
cin>>t2.year>>t2.month>>t2.day>>t2.hour>>t2.minute>>t2.second;
d_date(t1,t2);
return 0;
}
运行结果:
学习心得:
写到第五个问题就没思路了,而且觉的好麻烦,参考了老师的代码,觉得自己的代码太繁琐了,看来还是要多练习,精炼语言,提高效率。