万年历程序问题
#include<stdio.h>#include <time.h>
int leap_or_common(int year)
{
if(year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
return 1;
else return 0;
}
int count_year(int day, int *q)
{
int n = 0, i;
i = leap_or_common(1970);
while((i == 1 && day > 366) || (i ==0 && day > 365))
{
if(i == 1)
day-= 366;
else
day-= 365;
n++;
i = leap_or_common(1970 + n);
}
*q = day;
return 1970 + n;
}
int judge(int year,int month)
{
if(month==1||month==3||month == 5||month==7||month==8||month==10||month==12)
return(1);
else if(month==2)
{
if(year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
return(2);
else return(3);
}
else return(4);
}
int count_month(int year, int day, int *q)
{
int month = 1;
while(1)
{
if(month % 2 != 0 && month <= 7 && day >= 31)
{
day -= 31;
month++;
}
else if(month == 2 && ((leap_or_common(year) == 1 && day >= 29) || (leap_or_common(year) == 0) && day >= 28))
{
day = day - 28 - leap_or_common(year);
month++;
}
else if(month > 2 && month < 7 && month % 2 == 0 && day >= 30)
{
day -= 30;
month++;
}
else if(month >= 8 && month % 2 == 0 && day >= 31)
{
day -= 31;
month++;
}
else if(month > 8 && month % 2 != 0 && day >= 30)
{
day -= 30;
month++;
}
else
break;
}
*q = day;
return month;
}
int main(void)
{
int second, day, month, year, date, remind,i,j,k,a,n,m,count=0,count1=0,count2=0,count3=0,count4=0;
char **p;
char *week[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
second = time(NULL);
day = second / (60 * 60 * 24) + 1;
year = count_year(day, &remind);
day = remind;
month = count_month(year,day, &remind);
printf(" ========================================\n");
printf(" 今天是%d年%d月%d日.\n",year,month,remind);
printf(" ========================================\n");
if(year%4!=0||year%100==0&&year%400!=0)
count++;
for(i=1;i<month;i++)
{
if(i==1||i==3||i == 5||i==7||i==8||i==10||i==12)
count1++;
else if(i==2)
{
if(year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
count2++;
else count3++;
}
else count4++;
}
a=year*365+count+count1*31+count2*28+count3*29+count4*30-9;
n=judge(year,month);
for(j=0;j<7;j++)
{
p=week+j;
printf("%6s",*p);
}
printf("\n");
for(k=0;k<(a+1)%7;k++)
printf(" ");
for(m=1;m<32;m++)
{
printf("%6d",m);
if((a+m)%7==0)
printf("\n");
if(n==1&&m==31)
break;
else if(n==2&&m==28)
break;
else if(n==3&&m==29)
break;
else if(n==4&&m==30)
break;
}
printf("\n");
printf(" =======================================\n");
return 0;
}
那位大侠帮看看错误在哪里并帮忙改下 此程序的目的是输出本月日历,我这个程序的错误在于十月份的是正确的其他月份的是错误的
改好了且有能帮忙提意见的发到我的qq邮箱541040017@谢谢!