计算某日期是当年的第几天

  1. typedef int INTEGER;
  2. typedef int NUM[4];
  3. struct DATE{
  4.   int year;
  5.   int month;
  6.   int day;
  7. };
  8. main()
  9. {
  10.     INTEGER num;
  11.     num = 1999;
  12.     printf("%d/n",num);
  13.     NUM scores;
  14.     scores[0] = 98;
  15.     scores[1] = 89;
  16.     scores[2] = 95;
  17.     scores[3] = 88;
  18.     INTEGER i = 0;
  19.     for(i = 0; i < 4; i++)
  20.     {
  21.         printf("%d/n",scores[i]);
  22.     }
  23.     struct DATE day1;
  24.     day1.year = 2008;
  25.     day1.month = 9;
  26.     day1.day = 9;
  27.     printf("%d is leap?%d/n",day1.year,isleap(day1.year));
  28.     printf("1999-9-9 is the %d of the year!/n",days(&day1));
  29. }
  30. int days(struct DATE *p)
  31. {
  32.     int leap = isleap(p->year);
  33.     int tdays = 0;
  34.     if(leap == 1)//闰年
  35.     {
  36.         if(p->month < 3)
  37.         {
  38.             tdays = (p->month - 1) * 30 + (int)((p->month + 1) / 2) + p->day;
  39.         }
  40.         else if( p->month >= 3 && p->month <=12)
  41.         {
  42.             tdays = (p->month - 1) * 30 + (int)((p->month + 1) / 2) - 1 + p->day;
  43.         }
  44.     }
  45.     else
  46.     {
  47.         if(p->month < 3)
  48.         {
  49.             tdays = (p->month - 1) * 30 + (int)((p->month + 1) / 2) + p->day;
  50.         }
  51.         else if( p->month >= 3 && p->month <=12)
  52.         {
  53.             tdays = (p->month - 1) * 30 + (int)((p->month + 1) / 2) - 2 + p->day;
  54.         }
  55.     }
  56.     return tdays;
  57. }
  58. int isleap(int year)
  59. {
  60.     int leap = 0;
  61.     if(year%4==0)
  62.     {
  63.         if(year%100==0)
  64.         {
  65.             if(year%400==0)
  66.             {
  67.                 leap = 1;
  68.             }
  69.         }
  70.         else
  71.         {
  72.             leap = 1;
  73.         }
  74.     }
  75.     return leap;
  76. }

这是谭老师的书《C程序设计第二版》中第11章(page296)第1、2道习题。 我参考前面的例题做出来的答案,不知道对错。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值