文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>C程序练习-判断当天是一年中的第几天

C程序练习-判断当天是一年中的第几天

时间:2010-07-19  来源:chengxiaopeng

    有一道练习题:输入某年某月某日,判断这一天是这一年的第几天?看到这个题,我想到了,这个月以前的天数加上这个月的天数,即可完成。然后根据年份,判断是否闰月,来判断2月份的天数代码如下:

#include <stdio.h>

int main(int arc,char *argv[])
{
    int year,month,day;
    int sum = 0;
    int leap,error=0;
    printf("please intput year,month,day?");
    scanf("%d,%d,%d",&year,&month,&day);
    switch(month)
    {
                 case 1:
                   sum = 0;
                   break;
                 case 2:
                      sum = 31;
                      break;
                 case 3:
                      sum = 31 + 28;
                      break;
                 case 4:
                      sum = 31 + 28 + 31;
                      break;
                 case 5:
                      sum = 31 + 28 + 31 + 30;
                      break;
                 case 6:
                      sum = 31 + 28 + 31 + 30 + 31;
                      break;
                 case 7:
                      sum = 31 + 28 + 31 + 30 + 31 + 30;
                      break;
                 case 8:
                      sum = 31 + 28 + 31 + 30 + 31 + 30 + 31;
                      break;
                 case 9:
                      sum = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31;
                      break;
                 case 10:
                      sum = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30;
                      break;
                 case 11:
                       sum = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31;
                       break;
                 case 12:
                       sum = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30;
                       break;
                 default:
                         printf("month is error,please check it.");
                         error = 1;
                         break;
    }
    
    if (error != 1)
    {
      sum += day;
      
      if ((year % 100 == 0 && year % 400 == 0 ) || (year % 4 == 0 && year % 100 != 0))
      {
         leap = 1;
      }
      else
      {
          leap = 0;
      }
      
      if (leap == 1 && month > 2)
      {
         sum += 1;
      }
      
      printf("it is the %d day in year.",sum);
    }
    system("pause");
    return 1;
}


相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载