有两处错误,见下面代码
#include
int yearday(int year);
int monthday(int year,int month);
int day(int year,int month);
void main()
{
int year,month,week;
int i,j;
printf("please enter your year and month\n");
scanf("%d%d",&year,&month);
week=(day(year,month)+1)%7;
printf("Sun Mon Tue Wed Thu Fri Sta\n");
for(j=0;j
printf("\t");
}
for(i=1;i<=monthday(year,month);i++)
{
printf("%d\t",i);
week++;
week%=7;
if(week==0)
printf("\n");
}
printf("\n");
}
int yearday(int year)
{
if((year%4==0&&year%100!=0)||(year%400==0))
return(366);
else
return(365);
}
int monthday(int year,int month)
{
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
return(31);
else if(month==4||month==6||month==9||month==11)
return(30);
else if(yearday(year)==365)
return(28);
else return(29);
}
int day(int year,int month)
{
int juli=0,i,m;
for(i=0;i<(year-1990);i++)//年份错了
{
juli+=yearday(year);
}
for(m=1;m
juli+=monthday(year,m);//参数错了
}
return(juli);
}
#include
int days[]={0,31,59,90,120,151,181,212,243,273,304,334,365};
int month[]={31,28,31,30,31,30,31,31,30,31,30,31};
int week(int y,int m,int d)
{
int day=0;
day=days[m-1]+d-1;
return (y-1+(y-1)/4-(y-1)/100+(y-1)/400+day)%7;
}
void printmonth(int y,int m)
{
int i,j,w;
w=(week(y,m,1)+1)%7;
puts(" SUN MON TUE WED THU FRI STA");
for(i=1;i<=w;i++)
printf("%5c",' ');
for(j=i;j {
printf("%5d",j-i+1);
if(j%7==0)
puts("");
}
if((j-1)%7)
puts("");
}
int main()
{
int y,m;
char op[1];
do
{
puts("请输入要打印的年月");
scanf("%d%d",&y,&m);
printf(" %4.4d年%2.2d月\n",y,m);
printmonth(y,m);
puts("是否继续打印(Y/N)?");
scanf("%s",op);
}while(op[0]=='Y'||op[0]=='y');
}
一楼的代码是可以的。
如果要在你的源代码上修改,2楼的改法还是有问题。
问题主要出在day函数的实现:
int day(int year,int month)
{
int juli=0,i,m;
for(i=1900;i
juli+=yearday(i);//注意这个传入参数
}
for(m=1;m
juli+=monthday(year,m);//输入当前月份m
}
return(juli);
}