正在加载图片...
1.1 Program Organization and Control Structures 13 than one place)becomes true.At that point you wish to exit the loop and proceed with what comes after it.In C the structure is implemented with the simple break statement,which terminates execution of the innermost for,while,do,or switch construction and proceeds to the next sequential instruction.(In Pascal and standard FORTRAN,this structure requires the use of statement labels,to the detriment of clear programming.)A typical usage of the break statement is: for(;;){ [statements before the test] if (...break; [statements after the test] [next sequential instruction] 18881892 Here is a program that uses several different iteration structures.One of us was once asked,for a scavenger hunt,to find the date of a Friday the 13th on which the moon was full.This is a program which accomplishes that task,giving incidentally from NUMERICAL RECIPES I all other Fridays the 13th as a by-product. #include <stdio.h> (Nort server #include <math.h> #define ZON -5.0 Time zone -5 is Eastern Standard Time. #define IYBEG 1900 The range of dates to be searched. #define IYEND 2000 ART int main(void)/Program badluk * Programs void flmoon(int n,int nph,long *jd,float *frac); long julday(int mm,int id,int iyyy); SCIENTIFIC int ic,icon,idwk,im,iyyy,n; float timzon ZON/24.0,frac; long jd,jday; printf("\nFull moons on Friday the 13th from %5d to %5d\n",IYBEG,IYEND); COMPUTING for (iyyy-IYBEG;iyyy<-IYEND;iyyy++){ Loop over each year, for(1m=1;1m<=12;im++)[ and each month. 19200 jday=julday(im,13,iyyy); Is the 13th a Friday? (ISBN idwk=(int)((jday+1)%7); if (idwk ==5){ Numerical Recipes 21 n=(1nt)(12.37*(1yyy-1900+(1m-0.5)/12.0); This value n is a first approximation to how many full moons have occurred since 1900.We will feed it into the phase routine and adjust it up or down 43108 until we determine that our desired 13th was or was not a full moon.The variable icon signals the direction of adjustment. icon=0; (outside for (;;) Software. flmoon(n,2,&id,&frac); Get date of full moon n. North frac=24.0*(frac+timzon); Convert to hours in correct time zone. if (frac <0.0){ Convert from Julian Days beginning at --jd; noon to civil days beginning at mid- frac+=24.0; night. if (frac 12.0){ ++1d frac-=12.0; else frac+=12.0; if (id ==jday){ Did we hit our target day? printf("\n%2d/13/%4d\n",im,iyyy); printf("%s %5.1f %s\n","Full moon",frac,1.1 Program Organization and Control Structures 13 Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copyin Copyright (C) 1988-1992 by Cambridge University Press. Programs Copyright (C) 1988-1992 by Numerical Recipes Software. Sample page from NUMERICAL RECIPES IN C: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43108-5) g of machine￾readable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books or CDROMs, visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to directcustserv@cambridge.org (outside North America). than one place) becomes true. At that point you wish to exit the loop and proceed with what comes after it. In C the structure is implemented with the simple break statement, which terminates execution of the innermost for, while, do, or switch construction and proceeds to the next sequential instruction. (In Pascal and standard FORTRAN, this structure requires the use of statement labels, to the detriment of clear programming.) A typical usage of the break statement is: for(;;) { [statements before the test] if (...) break; [statements after the test] } [next sequential instruction] Here is a program that uses several different iteration structures. One of us was once asked, for a scavenger hunt, to find the date of a Friday the 13th on which the moon was full. This is a program which accomplishes that task, giving incidentally all other Fridays the 13th as a by-product. #include <stdio.h> #include <math.h> #define ZON -5.0 Time zone −5 is Eastern Standard Time. #define IYBEG 1900 The range ofdates to be searched. #define IYEND 2000 int main(void) /* Program badluk */ { void flmoon(int n, int nph, long *jd, float *frac); long julday(int mm, int id, int iyyy); int ic,icon,idwk,im,iyyy,n; float timzon = ZON/24.0,frac; long jd,jday; printf("\nFull moons on Friday the 13th from %5d to %5d\n",IYBEG,IYEND); for (iyyy=IYBEG;iyyy<=IYEND;iyyy++) { Loop over each year, for (im=1;im<=12;im++) { and each month. jday=julday(im,13,iyyy); Is the 13th a Friday? idwk=(int) ((jday+1) % 7); if (idwk == 5) { n=(int)(12.37*(iyyy-1900+(im-0.5)/12.0)); This value n is a first approximation to how many full moons have occurred since 1900. We will feed it into the phase routine and adjust it up or down until we determine that our desired 13th was or was not a full moon. The variable icon signals the direction ofadjustment. icon=0; for (;;) { flmoon(n,2,&jd,&frac); Get date offull moon n. frac=24.0*(frac+timzon); Convert to hours in correct time zone. if (frac < 0.0) { Convert from Julian Days beginning at noon to civil days beginning at mid￾night. --jd; frac += 24.0; } if (frac > 12.0) { ++jd; frac -= 12.0; } else frac += 12.0; if (jd == jday) { Did we hit our target day? printf("\n%2d/13/%4d\n",im,iyyy); printf("%s %5.1f %s\n","Full moon",frac
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有