Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2539735
  • 博文数量: 308
  • 博客积分: 5547
  • 博客等级: 大校
  • 技术积分: 3782
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-24 09:47
个人简介

hello world.

文章分类

全部博文(308)

分类: C/C++

2010-07-19 12:16:55

    有一道练习题:输入某年某月某日,判断这一天是这一年的第几天?看到这个题,我想到了,这个月以前的天数加上这个月的天数,即可完成。然后根据年份,判断是否闰月,来判断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;
}


阅读(3524) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~