程式碼示例:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int year,month,day;
int leapyear=0;
int daytime=0;
int sum;
int index=0;
static int month_date[12]={31,28,31,30,31,30,31,31,30,31,30,31};
printf("請輸入日期:");
scanf("%d-%d-%d",&year,&month,&day);
printf("請輸入天數:");
scanf("%d",&daytime);
sum=daytime+day;
if(month==2)
if(year%400==0||(year%100!=0&&year%4==0))
leapyear=1;
month_date[month-1]+=leapyear;//如果是閏年則2月加一天
}
else
month_date[month-1] = 28;//如果不是閏年,則二月置為28天
printf("%d年的二月份是:%d天\n",year,month_date[month-1]);
for(int i=0;i<month;i++ ){
index +=month_date[month-1];
index +=day;//計算天數
printf("該日期是第%d天\n",index);
do
month_date[month-1]+=leapyear;
month_date[month-1] = 28;
if(sum>month_date[month-1])
sum-=month_date[month-1];
month++;
if(month==13)
year++;
month=1;
} while(sum>month_date[month-1]);
day=sum;
printf("\n第%d天后是%d-%d-%d",daytime,year,month,day);
return 0;
程式碼示例:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int year,month,day;
int leapyear=0;
int daytime=0;
int sum;
int index=0;
static int month_date[12]={31,28,31,30,31,30,31,31,30,31,30,31};
printf("請輸入日期:");
scanf("%d-%d-%d",&year,&month,&day);
printf("請輸入天數:");
scanf("%d",&daytime);
sum=daytime+day;
if(month==2)
{
if(year%400==0||(year%100!=0&&year%4==0))
{
leapyear=1;
month_date[month-1]+=leapyear;//如果是閏年則2月加一天
}
else
{
month_date[month-1] = 28;//如果不是閏年,則二月置為28天
}
printf("%d年的二月份是:%d天\n",year,month_date[month-1]);
}
for(int i=0;i<month;i++ ){
index +=month_date[month-1];
}
index +=day;//計算天數
printf("該日期是第%d天\n",index);
do
{
if(month==2)
{
if(year%400==0||(year%100!=0&&year%4==0))
{
leapyear=1;
month_date[month-1]+=leapyear;
}
else
{
month_date[month-1] = 28;
}
printf("%d年的二月份是:%d天\n",year,month_date[month-1]);
}
if(sum>month_date[month-1])
{
sum-=month_date[month-1];
month++;
if(month==13)
{
year++;
month=1;
}
}
} while(sum>month_date[month-1]);
day=sum;
printf("\n第%d天后是%d-%d-%d",daytime,year,month,day);
return 0;
}