我写的是不是太长了???
刚学不久 献丑了 哪里可以精简告诉我一下哈。。
#include<stdio.h>
int leapyear(int year)
{
if(!(year%4) && (year%100)) return 1;
else if(!(year%100) && !(year%400)) return 1;
return 0;
}
int checkdate(int year, int month, int day)
{
if(month<=12)
{
if(month<8)
{
if(month%2)
{
if(day<=31) return 1;
}
else if(2==month)
{
if(leapyear(year))
{
if(day<=29) return 1;
}
else if(day<=28) return 1;
}
else if(day<=30) return 1;
}
else if(month%2)
{
if(day<=30) return 1;
}
else
{
if(day<=31) return 1;
}
}
return 0;
}
int check2(int year1, int year2)
{
return (year2>year1)?1:0;
}
int days1(int year, int month, int day)
{
int sum=0, i;
for(i=1;i<month;i++)
{
if(i<8)
{
if(i%2) sum+=31;
else if(2==i)
{
if(leapyear(year)) sum+=29;
else sum+=28;
}
else sum+=30;
}
else
{
if(i%2) sum+=31;
else sum+=30;
}
}
sum+=day;
return sum;
}
long days2(int year1, int year2)
{
int sum=0, i, j=0;
for(i=0;i<year2-year1;i++)
if(leapyear(i+year1)) j++;
sum=365*i+j;
return sum;
}
main()
{
int year[2], month[2], day[2];
long sum=0;
clrscr();
while(1)
{
printf("Please intput the frist date(yyyy mm dd): ");
scanf("%d%d%d",&year[0],&month[0],&day[0]);
if(checkdate(year[0], month[0], day[0])) break;
printf("Data Error!\n");
}
while(1)
{
printf("Please intput the second date(yyyy mm dd): ");
scanf("%d%d%d",&year[1],&month[1],&day[1]);
if(checkdate(year[1], month[1], day[1]) && check2(year[0], year[1])) break;
printf("Data Error!\n");
}
sum=days2(year[0], year[1])+days1(year[1], month[1], day[1])-days1(year[0], month[0], day[0]);
printf("\nThere are %ld day(s) from ", sum);
printf("%d-%d-%d to ",year[0], month[0], day[0]);
printf("%d-%d-%d.\n",year[1], month[1], day[1]);
printf("\nThanks for using!\n\nPress any key..\n");
getch();
}