C语言编程:求n天后的日期

2024-12-19 19:48:07
推荐回答(3个)
回答1:

//#include "stdafx.h"//If the vc++6.0, with this line.
#include "stdio.h"
#include "time.h"
struct tm *SecondDay(int y,int m,int d){
    struct tm tm={0,0,0,d,m,y,};
    time_t lt=mktime(&tm)+86400;
    return localtime(<);
}
int main(void){
    int year,month,day,n,m[12]={31,28,31,30,31,30,31,31,30,31,30,31},i;
    struct tm *ptr;
    while(1){
        printf("Input Year, Month, Day and n(0        if(scanf("%d%d%d%d",&year,&month,&day,&n),year>1900){
            m[1]=28+(year%400==0 || year%4==0 && year%100);
            if(month>0 && month<13 && day>0 && day<=m[month-1] && n>0 && n<=2000)
                break;
        }
        printf("Error, redo: ");
    }
    for(ptr=SecondDay(year-1900,month-1,day),i=1;i        ptr=SecondDay(ptr->tm_year,ptr->tm_mon,ptr->tm_mday);
    printf("%d %d %d\n",ptr->tm_year+1900,ptr->tm_mon+1,ptr->tm_mday);
    return 0;
}

回答2:

#include 
using namespace std;
int s[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
class Date
{
 private: 
    int year;
    int  month;
    int day;
    bool leap()
    {
     if(year%4!=0 || year%100==0 && year%400!=0)
       return false;
     else
     return true;  
      }


      Date add(Date today,int days)
      {
       Date d;
       d.year=today.year;
       d.month=today.month;
       d.day=d.day+days;
       if(d.leap()) s[2]=29;else s[2]=28;
       while(d.day>s[d.month])
       {
        d.day=d.year-s[d.month];
        d.month++;
        if(d.month>12)
        {
         d.year++;
         if(d.leap()) s[2]=29;else s[2]=28;
        }          
       }
       return d;
      }
};

Date add(Date today,int days)这个 函数就是你要的功能

回答3:

写个函数,逐天加吧,每加1天,判断月和年是否要进位

~
~
~