chap0818

8.18 给出年、月、日,计算日是该年的第几天。

    

解:主函数接收从键盘输入的日期,并调用sum_day和leap函数计算天数。

其N-S图见图8.10。sum_day计算输入日期的天数。Leap函数返回是否闰年的信息。

    


    

    

程序如下:(xt8-18.c)

#include <stdio.h>

void main()

{ int sum_day( int month, int day);

     int year,month,days;

    printf( "Input date(year,month,day):");

    scanf( "%d,%d,%d",&year,&month,&days);

    printf( "%d/%d/%d ",year,month,days);

    days=sum_day(month,days);                             /*调用函数一*/

     if(leap(year)&&month>=3)                             /*调用函数二*/

        days=days+1;

    printf( "is the %dth day in this year.\n",days);

}

    

int day_tab[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};     /*外部数组*/

int sum_day( int month, int day)                     /*函数一:计算日期*/

{ int i;                                                                 /*累加所在月之前的天数*/

     for(i=1;i<month;i++)

        day+=day_tab[i];

     return(day);

}

    

int leap( int year)                                             /*函数二:判断是否为闰年*/

{ int leap;

    leap=(year%4==0&&year%100!=0||year%400==0);

     return(leap);

}

    

运行结果:

Input date(year,month,day):2000,10,1

2000/10/1 is the 275th day in this year

你可能感兴趣的:(函数,休闲,Leap,CHAP,sum_day)