第17周项目5-玩日期时间

来源:互联网 发布:忽略的网络怎么恢复 编辑:程序博客网 时间:2024/05/21 17:52
/*  *copyright (c)2014,烟台大学计算机学院  *All rights reserved  *文件名称:123.cpp  *作者:孙春红  *完成日期:2014年12月23日  *版本号:v1.0  *  *问题描述:定义一个结构体变量,要求输入一个时间,输出(1)该日是本年第几天;(2)输出这一天的第几秒;(3)输出这一年的第几秒;(4)输入一个天数d,输出d天后是什么时间。  *输入描述:略。  *程序输出:略。  */  #include <iostream>using namespace std;struct Time{    int year;    int month;    int day;    int hour;    int minute;    int second;};Time t;int d[13]= {0,31,28,31,30,31,30,31,31,30,31,30,31};int days(Time t);int seconds(Time t);int YS(Time t);Time ad(Time t,int);int daysofyear(int);void outputtime(Time t);int main (){    int d,s,ys;    cout<<"请输入一个时间(年、月、日、分、秒):"<<endl;    cin>>t.year>>t.month>>t.day>>t.hour>>t.minute>>t.second;    cout<<"该日是本年第 "<<days(t)<<" 天。"<<endl;    cout<<endl;    s=seconds(t);    cout<<"这是这一天的第 "<<s<<" 秒。"<<endl;    cout<<endl;    ys=YS(t);    cout<<"这是这一年中的第 "<<ys<<" 秒。"<<endl;    cout<<"请输入一个时间:"<<endl;    cin>>d;    t=ad(t,d);    outputtime(t);return 0;}int days(Time t){    int sum=t.day,i;    for (i=1; i<t.month; i++)    {        sum+=d[i];    }    if ((t.year%4==0&&t.year%100!=0||t.year%400==0)&&t.year>3)        sum++;    return sum;}int seconds(Time t){    int sum;    sum=t.hour*60*60+t.minute*60+t.second;    return sum;}int YS (Time t){    int s;    s=(days(t)-1)*24*60*60+seconds(t);    return s;}int daysofyear(int y){    return((y%4==0&&y%100!=0)||y%400==0)?366:365;}void outputtime(Time t){    cout<<"时间为:"<<endl;    cout<<t.year<<" 年"<<t.month<<" 月"<<t.day<<" 日。"<<endl;}Time ad (Time t,int d){    int x,ds;    Time t1=t;    x=d+days(t);    t1.month=1;    t1.day=0;    while (x>daysofyear(t1.year))    {        x-=daysofyear(t1.year);        ++t1.year;    }    switch (t1.month)    {    case 1:    case 5:    case 7:    case 8:    case 10:    case 12:        ds=31;        break;    case 4:    case 6:    case 9:    case 11:        ds=30;        break;    case 2:        if((t1.year%4==0&&t1.year%100!=0)||t1.year%400==0)            ds=29;        else            ds=38;        break;    }    while (x>ds)    {        x-=ds;        ++t1.month;    }    t1.day+=x;    return t1;}


运行结果:

知识点总结:

学会定义一个结构体变量解决时间问题。

0 0