结构指针:计算某日是该年中的第几天

| January 3, 2008 18:48 | timmy | Via Original

#include <stdio.h>
struct year_month_day {
  int year;
  int month;
  int day;
};
int day_of_year(struct year_month_day *t);
void main()
{
  struct year_month_day ymd;
  printf("Input year,month,day: ");
  scanf("%d%d%d",&ymd.year,&ymd.month,&ymd.day);
  printf("%d\n",day_of_year(&ymd));
}
int day_of_year(struct year_month_day *t)
{
  int k,leap,res;
  int tab[2][13]={
    {0,31,28,31,30,31,30,31,31,30,31,30,31},
    {0,31,29,31,30,31,30,31,31,30,31,30,31}
  };
  leap=(t->year%4==0&&t->year%100!=0||t->year%400==0);
  res=t->day;
  for(k=1;k<t->month;k++)
    res+=tab[leap][k];
  return res;
}
Tags:
Program/Code » C/C++ | Comments(0) | Trackbacks(0) | Reads(181)
Add a comment
 Site URI
 Email
  Password Optional
 Nickname  *  [Register]
               

 
Emots
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
Enable HTML
Enable UBB
Enable Emots
Hidden
Remember