第二题我给做了.....考虑的东西比较多....
int leap(int year)/*判断是否为闰年,是返回1,否则返回0*/
{return year%4==0&&year%100!=0||year%400==0;
}
int judge(int a,int b,int c) /*判断输入年月日是否合法*/
{int i=0;
if(a<1900||a>2006||b<1||b>12)
i=1;
else if(leap(a)==1&&b==2&&(c<1||c>29))
i=1;
else if(leap(a)==0&&b==2&&(c<1||c>28))
i=1;
else if((leap(a)==0)&&(b==1||b==3||b==5||b==7||b==8||b==10||b==12)&&(c<1||c>31))
i=1;
else if(leap(a)==0&&(c<1||c>30))
i=1;
return i;
}
int caculatebirth(int a,int b,int c) /*计算出生年或今年的天数*/
{int all;
switch(b)
{case 1: all=c;break;
case 2: all=31+c;break;
case 3: all=59+c;break;
case 4: all=90+c;break;
case 5: all=120+c;break;
case 6: all=151+c;break;
case 7: all=181+c;break;
case 8: all=212+c;break;
case 9: all=243+c;break;
case 10:all=273+c;break;
case 11:all=304+c;break;
case 12:all=334+c;
}
if(leap(a))all=all+1;
return all;
}
main()
{int year[2],month[2],day[2];
int middleyear,num=0,i,y;
long yeartotal,monthtotal,daytotal,total;
input1: printf("Input your birthday,the style like this:1985,9,11\n");
scanf("%d,%d,%d",&year[0],&month[0],&day[0]);
if(judge(year[0],month[0],day[0])==1)
{printf("error input,please input again\n");
goto input1;
}
input2: printf("Input the current date,the style like this:2006,11,6\n");
scanf("%d,%d,%d",&year[1],&month[1],&day[1]);
if(judge(year[1],month[1],day[1])==1||year[1]<year[0]||(year[1]==year[0]&&month[1]<month[0])||(year[1]==year[0]&&month[1]==month[0]&&day[1]<day[0]))
{printf("error input,please input again\n");
goto input2;
}
y=year[0]+1;
middleyear=year[1]-year[0]-1;
if(middleyear>=0)
{for(i=1;i<=middleyear;i++,y++)
if(leap(y))num++;
total=middleyear*365+num+365-caculatebirth(year[0],month[0],day[0])+1+caculatebirth(year[1],month[1],day[1]);
if(leap(year[0]))total=total+1;
}
else total=caculatebirth(year[1],month[1],day[1])-caculatebirth(year[0],month[0],day[0]);
yeartotal=total/365;
monthtotal=(total-yeartotal*365)/30;
daytotal=total-yeartotal*365-monthtotal*30;
printf("your birthday is\nyear: %d month: %d day: %d\n\n",year[0],month[0],day[0]);
printf("today is\nyear: %d month: %d day: %d\n\n",year[1],month[1],day[1]);
printf("till now,you have enjoyed your life\nyear : %ld month : %ld day : %ld\n\n",yeartotal,monthtotal,daytotal);
printf("and the total days you have existed are %ld\n",total);
getch();
}
[此贴子已经被作者于2006-11-8 13:00:53编辑过]