/*
* Copyright (c) 2014, 烟台大学计算机学院
* All rights reserved.
* 文件名称:test.cpp
* 作 者:刘佳琦
* 完成日期:2014年 12 月 22 日
* 版 本 号:v1.0
*
* 问题描述:定义一个结构体变量(包括年、月、日),要求输入年、月、日,计算输出该日是该年的第几天。
* 程序输出:
*/
#include <iostream>
using namespace std;
struct Time
{
int year;
int month;
int day;
int hour;
int minute;
int second;
}t1,t2;
int month(Time &t);
int days(Time &t);
void input(Time &t);
void outputTime(Time &t);
int daysOfYear(int y);
int secondOfDay(Time &t);
Time afterDays(Time &t,int d);
void aftersecond(int h, int m, int s);
void compare(Time x,Time y);
int main()
{
Time t,nt,d;
cout<<"请输入一个时间(包括年,月,日,时,分,秒):"<<endl;
input(t);
outputTime(t);
cout<<"这是这一年中的第"<<days(t)<<"天。"<<endl;
cout<<"这是这一天中的第"<<secondOfDay(t)<<"秒。"<<endl;
cout<<"这是这一年中的第"<<days(t)*24*3600+secondOfDay(t)<<"秒."<<endl;
int n;
cout<<"请输入一个天数";
cin>>n;
nt=afterDays(t,n);
cout<<"这一天后"<<n<<"天是:";
outputTime(nt);
aftersecond(t.hour, t.minute, t.second);
cout<<"两个时间:"<<endl;
cin>>t1.year>>t1.month>>t1.day>>t1.hour>>t1.minute>>t1.second;
cin>>t2.year>>t2.month>>t2.day>>t2.hour>>t2.minute>>t2.second;
compare(t1,t2);
return 0;
}
int month(Time &t)
{
int days=0,i,a;
i=t.month;
if(i==1||i==3||i==5||i==7||i==8||i==10||i==12)
a=31;
else if(i==2)
{
if(t.year%4==0&&t.year%100!=0||t.year%400==0)
a=29;
else
a=28;
}
else
a=30;
return a;
}
int days(Time &t)
{
int days=0,i,a;
//计算days
for(i=1; i<t.month; i++)
{
if(i==1||i==3||i==5||i==7||i==8||i==10||i==12)
a=31;
else if(i==2)
{
if(t.year%4==0&&t.year%100!=0||t.year%400==0)
a=29;
else
a=28;
}
else
a=30;
days+=a;
}
days+=t.day;
return days;
}
void input(Time &t)
{
cin>>t.year>>t.month>>t.day>>t.hour>>t.minute>>t.second;
}
void outputTime(Time &t)
{
cout<<"时间为: "<<t.year<<"年"<<t.month<<"月"<<t.day<<"日"<<t.hour<<"时"<<t.minute<<"分"<<t.second<<"秒"<<endl;
}
//一年有多少天(365或366天)
int daysOfYear(int y)
{
return ((y%4==0&&y%100!=0)||y%400==0)?366:365;
}
//一天的第几秒
int secondOfDay(Time &t)
{
return t.hour*3600+t.minute*60+t.second;
}
//
int secondofYear(Time &t)
{
return (days(t)-1)*24*3600+secondOfDay(t);
}
//求你输入的时间d天后是哪年哪月哪日
Time afterDays(Time &t,int d)
{
Time t1=t;
int d1=d+days(t);
t1.month=1;
t1.day=0; //这样,将问题转换为在当年1月0日基础上加d1天(这个0有意思),避免以后老为2月操心,以及剩余天数一加以后持续进位
while(d1>daysOfYear(t1.year)) //天数还够一个整年
{
d1-=daysOfYear(t1.year);
++t1.year;
}
//天数不够一整年后,再考虑月,因为从1月1日开始,不用担心Nt.year再加1年
while(d1>month(t)) //天数还够一个整月
{
d1-=month(t);
++t1.month;
}
//剩全天数加到日上
t1.day+=d1;
return t1;
}
void aftersecond(int h, int m, int s)
{
struct Times //定义一个保存目标日期的新的结构体
{
int hour;
int minute;
int second;
};
Times time;
cout<<"请输入经过的秒数:";
int se;
cin>>se;
time.hour=(se+s)/3600;
time.minute=(se+s-time.hour*3600)/60;
time.second=se-time.hour*3600-time.minute*60+s;
time.hour+=h;
time.minute+=m;
cout<<se<<"秒后是"<<time.hour<<"时"<<time.minute<<"分"<<time.second<<"秒"<<endl;
}
void compare(Time x,Time y)
{
int days1,days2,days3,s;
days1=days(x);
days2=days(y);
days3=days1-days2;
if(days3<0)
days3=-days3;
s=secondofYear(x)-secondofYear(y);
if(s<0)
s=-s;
cout<<"相差天数为:"<<days3;
cout<<"相差秒数为:"<<s;
}
运行结果:
学习心得:在求解万日后的哪年哪月上参考了贺老师的,自己的程序略复杂,情况罗列好多,好多东西可以从调用里用,取经了,其余的部分都很好解决~