写的一个C日历大家帮我看一下,我蛋都快碎了.在输入年月日时会出错
#include <stdio.h>#include <stdlib.h>
#include<time.h>
int leap(int a)/*自己定义计算闰年的函数*/
{if((a%4==0)&&(a%100!=0)||(a%400==0))
return 1;
else
return 0;
}
int sumdays(int a)/*自定义计算总天数的函数*/
{int i,j=0,sum;
for(i=1940;i<a;i++)
{if(leap(i)==1)
j=j+1;}
sum=366*j+365*(a-1940-j);
return sum;
}
void year(int a)/*自定义函数,在只输入"年"时调用,也就是说这是一个年历函数*/
{int i,j,k,days,temp,eday;
int month[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int day[7][14]={0};
days=sumdays(a);
for(temp=1;temp<=6;temp++)
{switch(temp)
{case 1: printf("\t January %d\t\tFebruary %d\n",a,a);break;
case 2: printf("\t March %d\t\t April %d\n",a,a);break;
case 3: printf("\t May %d\t\t June %d\n",a,a);break;
case 4: printf("\t July %d\t\t August %d\n",a,a);break;
case 5: printf("\tSeptember%d\t\t October %d\n",a,a);break;
case 6: printf("\tNovember %d\t\tDecember %d\n",a,a);break;
}
printf("Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat Sun");
printf("\n");
for(i=0;i<6;i++)
for(j=0;j<14;j++)
day[i][j]=0;
i=0;
j=days%7-1;
if(days%7==0)
j=6;
eday=1;
while(eday<=month[2*temp-1])
{day[i][j++]=eday;
eday++;
days++;
if(j==7)
i++,j=0;
}
eday=1,i=0,j=days%7+6;
if(days%7==0)
j=13;
if(leap(a)==1&&temp==1)
while(eday<=month[2*temp]+1)
{day[i][j++]=eday;
eday++;days++;
if(j==14)
i++,j=7;
}
else
while(eday<=month[2*temp])
{day[i][j++]=eday;
eday++;days++;
if(j==14)
i++,j=7;
}
for(i=0;i<6;i++)
{for(j=0;j<14;j++)
{if(day[i][j]==0)
printf(" ");
else
printf("% -4d",day[i][j]);
if(j==6)
printf(" ");
}
printf("\n");
}
if(getch()=='t')
exit(0);
}
}
void monthy(int a,int b)/*月历函数*/
{int i,j,days,eday,k;
int month[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int day[6][7]={0};
days=sumdays(a);
printf("\t%d %d\n",a,b);
printf("Mon Tue Wed Thu Fri Sat Sun");
printf("\n");
for(i=0;i<b;i++)
days=days+month[i];
if(leap(a)==1&&b>2)
days=days+1;
j=days%7-1;
if(j==0)
j=6;
for(i=0;i<6;i++)
for(k=0;k<7;k++)
day[i][k]=0;
k=i=0;
if(leap(a)==1&&b==2)
while(i<=month[b]+1)
{day[k][j++]=i;
i++;
if(j==7)
k++,j=0;}
else
while(i<=month[b])
{day[k][j++]=i;
i++;
if(j==7)
k++,j=0;}
for(i=0;i<6;i++)
for(j=0;j<7;j++)
{if(day[i][j]==0)
printf(" ");
else
printf("% -4d",day[i][j]);
if(j==6)
printf("\n");}
getch();
}
void pday(int a,int b,int c)/*天的函数,这个函数有三个功能,1.计算输入的日期距当前的天数,2.输入日期的星期3.前一天和后的操作*/
{int i,j,days,z,x,y,v;
int month[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
char week[7][4]={"Mon", "Tue", "Wed", "Thu" ,"Fri" ,"Sat" ,"Sun"};
time_t nowtime;
struct tm *p;
time(&nowtime);
p=localtime(&nowtime);
x=1900+p->tm_year,y=1+p->tm_mon,v=p->tm_mday;
z=sumdays(x);
for(i=0;i<y;i++)
z=z+month[i];
z=z+v;
if(y>2&&leap(x)==1)
z=z+1;
days=sumdays(a);
for(i=0;i<10000;i++)
days=days+month[i];
if(b>2&&leap(a)==1)
days=days+c+1;
else
days=days+c;
z=z-days;
j=days%7;
if(j==0)
j=7;
printf(" 'l' to before day 'r' to after day 't' to exit");
printf("\n");
printf("%d %d %d %s ",a,b,c,week[j-1]);
printf("distance%d \n",z);
if(b==10&&c==1)
printf("national day\n");
if(b==5&&c==1)
printf("labour day\n");
for(i=0;i<1000;i++)
{if(getch()=='l')
{j--,c--;
if(j==0)
j=7;
if(c==0)
b--,c=month[b];
printf("%d %d %d %s ",a,b,c,week[j-1]);
z=z+1;
printf("distance %d\n ",z);
if(b==10&&c==1)
printf("national day\n");
if(b==5&&c==1)
printf("labour day\n");}
if(getch()=='r')
{j++,c++;
if(j==8)
j=1;
if(c>month[b])
b++,c=1;
printf("%d %d %d %s ",a,b,c,week[j-1]);
z=z-1;
printf("distance%d \n",z);
if(b==10&&c==1)
printf("national day\n");
if(b==5&&c==1)
printf("labour day\n");}
if(getch()=='t')
exit(0);
}
getch();}
main()/*主函数*/
{int a=0,b=0,c=0;
printf("please input the date(1940~2040)\nif you want search year,you need input:xxxx(year)\n");
printf("if you want search year and month input:xxxx(year),xx(month)\n");
printf("if you want search year,month and day input:xxx(year),xx(month),xx(day)\n");
scanf("%d,%d,%d",&a,&b,&c);
if(a<1940||a>2040)
{printf("fuck ,you are fool !!");
exit(0);}
if(a!=0&&b==0&&c==0)
year(a);
else if(a!=0&&b!=0&&c==0)
monthy(a,b);
else if(a!=0&&b!=0&&c!=0)
pday(a,b,c);
else
{printf("fuck ,you are fool !!");}
}
昨天发帖时快断电了,所以有点匆忙.这个程序要实现1940~~2040的日历,开学时刚编好的时候每一个功能的正常,但我昨天拿出一看,却发现,在输入年,月,日时距离的天会有误,但其他功能正常.
(在这个程序中输入的年月日之间用 ","隔开,只有年或有年月或者说年月日都有,为自动识别(如:2011或2011,6或2011,6,9就是默认的输入方式)
[ 本帖最后由 x6988312 于 2012-11-9 22:32 编辑 ]