玩日期

本文介绍了一个C++程序,用于计算输入的日期(包括年、月、日)是一年中的第几天,并进一步计算从该日期起经过指定天数后的日期。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*
* 程序的版权和版本声明部分
* Copyright (c)2013, 烟台大学计算机学院学生
* All rightsreserved.
* 文件名称: fibnacci.cpp
* 作    者:高古尊
* 完成日期:2014年2月27日
* 版本号: v1.0
*
* 输入描述:
* 问题描述:定义一个结构体变量(包括年、月、日),要求输入年、月、日,
计算输出该日是该年的第几天
* 程序输出:
* 问题分析:
*/
#include <iostream>
using namespace std;
struct Date
{
    int year;
    int moth;
    int day;
    int shi;
    int fen;
    int miao;
};
int main()
{
    int moth2;
    Date date,date2;
    cout<<"请输入年 月 日 时 分 秒:";
    cin>>date.year>>date.moth>>date.day>>date.shi>>date.fen>>date.miao;
    int days;
    if((date.year%4==0&&date.year%100!=0)||(date.year%400==0))
        moth2=29;
    else
        moth2=28;

    if(date.moth==3||date.moth==5||date.moth==7)
    {
        days=(date.moth-1)*30+date.day-(30-moth2)+(date.moth-1)/2;
    }
    else
    {
        if(date.moth==2||date.moth==4||date.moth==6)
        {
            days=(date.moth-1)*30+date.day-(30-moth2)+date.moth/2;
        }
        else
        {
            if(date.moth==8||date.moth==10||date.moth==12)
            {
                days=4*31+2*30+moth2+(date.moth-8)*31-(date.moth-8)/2+date.day;
            }
            else
            {
                if(date.moth==9||date.moth==11)
                {
                    days=4*31+2*30+moth2+31+(date.moth-8)*30+(date.moth-9)/2+date.day;
                }
                else
                {
                    if(date.moth==1)
                    {
                        days=date.day;
                    }
                    else
                    {
                        days=31+date.day;
                    }
                }
            }
        }
    }
    cout<<"是这一年的第"<<days<<"天."<<endl;
    cout<<"是这一天的第"<<(date.shi*60*60+date.fen*60+date.miao)<<"秒。"<<endl;
    cout<<"是这一年的第"<<(days*24*60*60+date.shi*60*60+date.fen*60+date.miao)<<"秒。"<<endl;
    date2=date;
    int d,m=0,n=0,t=0;
    cin>>d;
    date.moth=1;
    while(n!=d+days)
    {
        m++;
        n++;
        t++;
        if((date.year%4==0&&date.year%100!=0)||(date.year%400==0))
        {
            if(t==366)
            {
                t=0;
                date.year++;
            }
            if(date.moth==1||date.moth+1==3||date.moth+1==5||date.moth+1==7||date.moth+1==8||date.moth+1==10||date.moth+1==12)
            {
                if(m==31)
                {
                    m=0;
                    date.moth++;
                }
            }
            else
            {
                if(date.moth+1==2)
                {
                    if(m==29)
                    {
                        m=0;
                        date.moth++;
                    }
                }
                else
                {
                    if(m==31)
                    {
                        m=0;
                        date.moth++;
                    }
                }
            }
        }
        else
        {
            if(t==365)
            {
                t=0;
                date.year++;
            }
            if(date.moth+1==1||date.moth+1==3||date.moth+1==5||date.moth+1==7||date.moth+1==8||date.moth+1==10||date.moth+1==12)
            {
                if(m==31)
                {
                    m=0;
                    date.moth++;
                }
            }
            else
            {
                if(date.moth+1==2)
                {
                    if(m==28)
                    {
                        m=0;
                        date.moth++;
                    }
                }
                else
                {
                    if(m==31)
                    {
                        m=0;
                        date.moth++;
                    }
                }
            }
        }
    }
    cout<<"天后是:"<<date.year<<"年"<<date.moth<<"月"<<date.day<<"日"<<endl;
    date=date2;
    int l;
    m=0,n=0,t=0;
    cin>>l;
    d=l/(24*60*60);
    date.shi=(l%(24*60*60))/360;
    date.fen=((l%(24*60*60))%360)/60;
    date.miao=((l%(24*60*60))%360)%60;
    date.moth=1;
    while(n!=d+days)
    {
        m++;
        n++;
        t++;
        if((date.year%4==0&&date.year%100!=0)||(date.year%400==0))
        {
            if(t==366)
            {
                t=0;
                date.year++;
            }
            if(date.moth==1||date.moth==3||date.moth==5||date.moth==7||date.moth==8||date.moth==10||date.moth==12)
            {
                if(m==31)
                {
                    m=0;
                    date.moth++;
                }
            }
            else
            {
                if(date.moth==2)
                {
                    if(m==29)
                    {
                        m=0;
                        date.moth++;
                    }
                }
                else
                {
                    if(m==31)
                    {
                        m=0;
                        date.moth++;
                    }
                }
            }
        }
        else
        {
            if(t==365)
            {
                t=0;
                date.year++;
            }
            if(date.moth==1||date.moth==3||date.moth==5||date.moth==7||date.moth==8||date.moth==10||date.moth==12)
            {
                if(m==31)
                {
                    m=0;
                    date.moth++;
                }
            }
            else
            {
                if(date.moth==2)
                {
                    if(m==28)
                    {
                        m=0;
                        date.moth++;
                    }
                }
                else
                {
                    if(m==31)
                    {
                        m=0;
                        date.moth++;
                    }
                }
            }
        }
    }
    cout<<"天后是:"<<date.year<<"年"<<date.moth<<"月"<<date.day<<"日"<<date.shi<<"时"<<date.fen<<"分"<<date.miao<<"秒"<<endl;
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值