编写程序计算所输日期是当年的第几天

/* 1.输入年月日,编写程序计算所输日期是当年的第几天 */
/* 2.已知列车隔日发车,且1/1/2006不发车(无ticket),如果所输入数据在此日期之后,则输出有没有车票,否则仅输出上一步结果。*/

/* month/date/year is which day of the year */
/* is there a ticket today(2006.01.01--yeath!)?? */

#include <stdio.h>

int year,month,date;

void input()
/* read input number and judge is it legal*/
{
 puts("please input year,month,date:");
 scanf("%d%d%d",&year,&month,&date);
 while(date>31||date<1||month>12||month<1)
 {
  puts("month OR date Wrong!!!");
  puts("please input year,month,date:");
  scanf("%d%d%d",&year,&month,&date);
 }

}
int isleapyear(int year)      
/* is the year a leapyear */
{if(year%4==0&&year%100!=0||year%400==0) return(1);
 else return(0);
}

int days(int year,int month,int date)
/* Calculate the days since Jan,1,year.(you input the "year") */
{int day;
 switch(month)
 {
  case 1:day=date;break;
  case 2:day=date+31;break;
  case 3:day=date+59;break;
  case 4:day=date+90;break;
  case 5:day=date+120;break;
  case 6:day=date+151;break;
  case 7:day=date+181;break;
  case 8:day=date+212;break;
  case 9:day=date+243;break;
  case 10:day=date+273;break;
  case 11:day=date+304;break;
  case 12:day=date+334;break;
  default:printf("Data error!!!");
 }
if(isleapyear(year)&&month>2) day+=1;
return(day);
}

int main(void)
{
 int i,m=0,n;
  /* "i" is a counter;"m" is the days since Jan,1,2006 ;"n"the days since Jan,1,year.*/
 input();

 n=days(year,month,date);

 if(year>2005)       /*is there a m ticket today which is later than 2006.01.20 */
 {
  for (i=2006;i<year;i++)
  /* add the days from 2006 to year-1.(if it is a leapyear add 366,or add 365)*/
   if(isleapyear(i)) m+=366;
   else m+=365;    

  m=m+n-days(2006,1,1);
 
  if(m%2==1)puts("Ticket:Yeath!!/n");
 
  else puts("Ticket:Sorry!!!/n");
 }

 printf("%d/%d/%d  day:%d/n",month,date,year,n);
 
 puts("Press anykey to quit!");
 
 getch();

return 0;
}


  PS:1.本人初学,不求其他只愿代码能够尽量简洁高效,欢迎各位多多批评、多多挑毛病^_^。加我QQ 505011298。

   2.文章全部为原创,过程中可能有些文章参考了某些文章的思路,但本人宗旨和目标只是:a.不如我之人可以向我学习b.希望高手可以对我指点一二,以求做到最好;c.资源共享;别无其他。
   3.TC2.0下通过。当然,解法很多,如果哪位找到更好方法的话,如若可以分享我将很荣幸。


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/perforce/archive/2007/04/26/1585191.aspx

posted @ 2010-01-11 09:43  kevinzhwl  阅读(326)  评论(0编辑  收藏  举报