//输入年月日,再输入天数,求这些天之后的日期是多少
#include
int main()
{
int s[2][13]={0,31,28,31,30,31,30,31,31,30,31,30,31,
0,31,29,31,30,31,30,31,31,30,31,30,31,};
int year=0;
int month=0;
int day=0;
int n,i,daytemp;
int flag=0;
int nyear=0,nmonth=0,nday=0;
printf("输入年月日\n");
scanf("%d%d%d",&year,&month,&day);
//printf("输入天数\n");
//scanf("%d",&n);
n=1;
if(year<0||month<0||month>12||day<1||n<0)
{
printf("输入数据错误\n");
return 1;
}
daytemp=day+n;//累加天数
if((year%4==0&&year%100!=0)||year%400==0)//闰年置1
flag=1;
if(day>s[flag][month])
{
printf("输入日期与年月不符.\n");
return 1;
}
if(daytemp<=s[flag][month])//当前日期加天数在本月
{
nyear=year;
nmonth=month;
nday=daytemp;
}
else
{
i=month;
nyear=year;
while(daytemp>s[flag][i])//循环递减,直到当前日期加天数在本月
{
daytemp=daytemp-s[flag][i];
i++;
if(i>12)//超过一年,年累加
{
nyear++;
if((nyear%4==0&&nyear%100!=0)||nyear%400==0)
flag=1;
else
flag=0;
i=i-12;
}
}
nmonth=i;
nday=daytemp;
}
printf("%d年%d月%d日第%d天后是:\n",year,month,day,n);
printf("%d年%d月%d日\n",nyear,nmonth,nday);
return 0;
}
以+1天为例子
#include
struct date_rec
{
int day ;
int month ;
int year ;
};
void input_date(struct date_rec *current_date);
void increment_date(struct date_rec *current_date);
void output_date(struct date_rec *current_date);
int main()
{
struct date_rec d;
input_date(&d);
increment_date(&d);
output_date(&d);
return 0;
}
void input_date(struct date_rec *current_date)
{
printf("请输入当前日期(年 月 日):");
scanf("%d%d%d",¤t_date->year,¤t_date->month,¤t_date->day);
}
void increment_date(struct date_rec *current_date)
{
int day_a[2][12]=
{
{31,28,31,30,31,30,31,31,30,31,30,31},
{31,29,31,30,31,30,31,31,30,31,30,31}
};
int lp;
lp=(current_date->year%400==0 || (current_date->year%100!=0 && current_date->year%4==0));
if (current_date->daymonth-1])
{
current_date->day++;
}
else
{
current_date->day+=1-day_a[lp][current_date->month-1];
if (current_date->month<12)
current_date->month++;
else
{
current_date->year++;
current_date->month=1;
}
}
}
void output_date(struct date_rec *current_date)
{
printf("当前日期:%d年%d月%d日!",current_date->year,current_date->month,current_date->day);
}
定义三个int day month year
然后判断 month 是 30天 31天 还是28天,
然后就是 day +1
判断是否> month的天数
然后进位