• 两日期间的天数


    #include "stdio.h"

    int days[2][13]={365,31,28,31,30,31,30,31,31,30,31,30,31,
                    366,31,29,31,30,31,30,31,31,30,31,30,31};

    int Leap(int year)        //判断是否为闰年
    {
        return ( (year%400==0) || (year%4==0 && year%100!=0));
    }

    int Deal(int dat0,int dat1)
    {
        int i,temp,sum0,sum;
        int year0,month0,day0,year1,month1,day1;
        sum0 = sum = 0;
        year0 = dat0/10000;    month0 = dat0/100%100;    day0 = dat0%100;
        year1 = dat1/10000;    month1 = dat1/100%100;    day1 = dat1%100;

        /**//*************************************************************/
        /**//* 在起始日期和终止日期在同一年                                         */
        /**//*************************************************************/
        if((year1-year0)==0)
          {
            if((month1-month0)==0)
                return (day1-day0);
            else
              {
                temp = Leap(year0);
                sum = days[temp][month0] - day0 +1;
                for(i=month0+1;i<=month1-1;i++)
                    sum = sum + days[temp][i];
                sum =sum + day1;
                return sum;
            }        
        }

        /**//************************************************************************/
        /**//* 在起始日期和终止日期不在同一年                                       */
        /**//************************************************************************/
        //计算开始年剩余的天数
        temp = Leap(year0);
        sum0 = sum0 + (days[temp][month0] - day0) + 1;
        for (i=month0+1; i<=12; i++)
            sum0 = sum0 + days[temp][i];
        
        sum = sum +sum0;
        sum0 = 0;

        //计算中间年份的总天数
        for (i=year0+1;i<=year1-1;i++)
          {
            temp = Leap(i);
            sum = sum + days[temp][0];
        }

        //计算结束年过去的天数
        temp = Leap(year1);
        for(i=1;i<=month1-1;i++)
            sum0 = sum0 + days[temp][i];
        sum0 = sum0 + day1;

        sum = sum + sum0;

        return sum;
    }

    void main()
      {
        int dat0,dat1,sum;
        FILE *fp = NULL;
        if(fp = fopen("riqi.dat","r"))
          {
            while (fscanf(fp,"%d%d",&dat0,&dat1)==2)
              {
                if(dat0==0 && dat1==0)    break;
                sum = Deal(dat0,dat1);
                printf("天数为:%d ",sum);
            }
        }
        fclose(fp);
    }
  • 相关阅读:
    MR 原理
    Hadoop yarn任务调度策略介绍
    Hadoop 新 MapReduce 框架 Yarn 详解
    Hadoop 架构与原理
    Linux-top命令详解
    硬中断与软中断的区别
    Protocol_BGP
    Protocol_ISIS
    20条Linux命令面试问答
    Protocol_OSPF
  • 原文地址:https://www.cnblogs.com/hbt19860104/p/2626370.html
Copyright © 2020-2023  润新知