#include"stdio.h"
typedefstructdate
{
intyear;
intmonth;
intday;
}date;
char*week[]={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
intdays[]={31,28,31,30,31,30,31,31,30,31,30,31};
intisleapyear(intyear);
voidlistdate(intweekday,datenow,intdiff);
/*知道今天星期幾以及年月日,編寫c程式,要求能算出任意天后是星期幾以及年月日*/
voidmain()
intweekday;
datenow;
intdiff;
printf("請輸入今天是星期幾以及日期");
printf("星期幾(0..6,星期日..星期六):");
scanf("%d",&weekday);
printf("日期(yyyy-mm-dd):");
scanf("%d-%d-%d",&now.year,&now.month,&now.day);
printf("相隔天數:");
scanf("%d",&diff);
listdate(weekday,now,diff);
}
/*判斷閏年*/
intisleapyear(intyear)
return((year%400==0)||(year%4==0&&year%100!=0))?1:0;
/*已知今天是星期幾(weekday,0..6,sunday..saturday)以及日期(now),輸出diff天后是星期幾以及日期*/
/*diff>0指diff天后,diff
voidlistdate(intweekday,datenow,intdiff)
intw1;
dated1;
/*計算diff天后是星期幾*/
w1=weekday+diff;
while(w1
w1+=7;
w1=w1%7;
printf("%d天后:\n",diff);
printf("%s\n",week[w1]);
/*計算diff天的日期*/
d1.year=now.year;
d1.month=now.month;
d1.day=now.day+diff;
if(d1.day>0)
while(d1.day>days[d1.month-1])
d1.day-=days[d1.month-1]+(d1.month==2&&isleapyear(d1.year));
d1.month++;
if(d1.month>12)
d1.month=1;
d1.year++;
elseif(d1.day
while(d1.day
d1.month--;
if(d1.month
d1.month=12;
d1.year--;
d1.day+=days[d1.month-1]+(d1.month==2&&isleapyear(d1.year));
printf("%d-%d-%d\n",d1.year,d1.month,d1.day);
#include"stdio.h"
typedefstructdate
{
intyear;
intmonth;
intday;
}date;
char*week[]={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
intdays[]={31,28,31,30,31,30,31,31,30,31,30,31};
intisleapyear(intyear);
voidlistdate(intweekday,datenow,intdiff);
/*知道今天星期幾以及年月日,編寫c程式,要求能算出任意天后是星期幾以及年月日*/
voidmain()
{
intweekday;
datenow;
intdiff;
printf("請輸入今天是星期幾以及日期");
printf("星期幾(0..6,星期日..星期六):");
scanf("%d",&weekday);
printf("日期(yyyy-mm-dd):");
scanf("%d-%d-%d",&now.year,&now.month,&now.day);
printf("相隔天數:");
scanf("%d",&diff);
listdate(weekday,now,diff);
}
/*判斷閏年*/
intisleapyear(intyear)
{
return((year%400==0)||(year%4==0&&year%100!=0))?1:0;
}
/*已知今天是星期幾(weekday,0..6,sunday..saturday)以及日期(now),輸出diff天后是星期幾以及日期*/
/*diff>0指diff天后,diff
voidlistdate(intweekday,datenow,intdiff)
{
intw1;
dated1;
/*計算diff天后是星期幾*/
w1=weekday+diff;
while(w1
{
w1+=7;
}
w1=w1%7;
printf("%d天后:\n",diff);
printf("%s\n",week[w1]);
/*計算diff天的日期*/
d1.year=now.year;
d1.month=now.month;
d1.day=now.day+diff;
if(d1.day>0)
{
while(d1.day>days[d1.month-1])
{
d1.day-=days[d1.month-1]+(d1.month==2&&isleapyear(d1.year));
d1.month++;
if(d1.month>12)
{
d1.month=1;
d1.year++;
}
}
}
elseif(d1.day
{
while(d1.day
{
d1.month--;
if(d1.month
{
d1.month=12;
d1.year--;
}
d1.day+=days[d1.month-1]+(d1.month==2&&isleapyear(d1.year));
}
}
printf("%d-%d-%d\n",d1.year,d1.month,d1.day);
}