Chinaunix首页 | 论坛 | 博客
  • 博客访问: 26408
  • 博文数量: 41
  • 博客积分: 185
  • 博客等级: 入伍新兵
  • 技术积分: 260
  • 用 户 组: 普通用户
  • 注册时间: 2012-12-20 13:48
文章分类

全部博文(41)

文章存档

2013年(20)

2012年(21)

我的朋友
最近访客

分类: C/C++

2013-01-09 12:39:54

方法一:利用"swith"语句

点击(此处)折叠或打开

  1. //输入一个日期,如“2013 8 8”,输出该日期是当年第几天。
  2. #include"stdio.h"
  3. void main()
  4. {
  5.     int year,month,day,sum,leap;
  6.     sum=0;
  7.     printf("input the date:");
  8.     scanf("%d%d%d",&year,&month,&day);
  9.     switch(month)
  10.     {
  11.         case 12:sum+=30; //注意:自加的为上个月的天数;
  12.         case 11:sum+=31;
  13.         case 10:sum+=30;
  14.         case 9:sum+=31;
  15.         case 8:sum+=31;
  16.         case 7:sum+=30;
  17.         case 6:sum+=31;
  18.         case 5:sum+=30;
  19.         case 4:sum+=31;
  20.         case 3:sum+=28;
  21.         case 2:sum+=31;
  22.         case 1:sum+=day;break;
  23.         default:printf("Error!\n");
  24.     }
  25.     if((year%4==0&&year%100!=0)||year%400==0) //判断闰年
  26.         leap=1;
  27.     else
  28.         leap=0;;
  29.     if(leap&&month>2) //注意:month>2
  30.         sum++;
  31.     printf("The day is the %dth day of the year!\n",sum);
  32. }
 

方法二:构造函数

点击(此处)折叠或打开

  1. //输入一个日期,如“2013 1 4”,输出该日期是当年第几天。
  2. #include"stdio.h"
  3. int main()
  4. {
  5.     int day_sum(int month,int day);
  6.     int leap(int year);
  7.     int year,month,day,days;
  8.     printf("Please input data:");
  9.     scanf("%d%d%d",&year,&month,&day);
  10.     days=day_sum(month,day);
  11.     if(leap(year)==1&&month>2)
  12.         days=days+1;
  13.     printf("%d/%d/%d is the %dth day in this year.\n",year,month,day,days);
  14.     return 0;
  15. }



  16. //计算天数函数
  17. int day_sum(int month,int day)
  18. {
  19.     int day_tab[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
  20.     int i;
  21.     for(i=1;i<month;i++)
  22.         day=day+day_tab[i];
  23.     return day;
  24. }
  25. //判断闰年函数
  26. int leap(int year)
  27. {
  28.     int leap;
  29.     leap=year%4==0&&year%100!=0||year%400==0;
  30.     return leap;
  31. }

方法三:利用结构体变量

点击(此处)折叠或打开

  1. //定义一个结构体变量(包括年、月、日)。计算该日在本年中是第几天,注意闰年问题。
  2. #include"stdio.h"
  3. struct
  4. {
  5.     int year;
  6.     int month;
  7.     int day;
  8. }Date;
  9. void main()
  10. {
  11.     int data[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
  12.     int i,n,day,days=0;
  13.     printf("Please input the date:");
  14.     scanf("%d%d%d",&Date.year,&Date.month,&Date.day);
  15.     n=Date.month;
  16.     for(i=1;i<n;i++)
  17.         days+=data[i];
  18.     if(((Date.year%4==0&&Date.year%100!=0)||Date.year%400==0)&&Date.month>2)
  19.         day=days+Date.day+1;
  20.     else
  21.         day=days+Date.day;
  22.     printf("The day is the %dth day of the year.\n",day);
  23. }

 

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