Press, Teukolsly, Vetterling, Flannery - Numerical Recipes in C, страница 8
Описание файла
PDF-файл из архива "Press, Teukolsly, Vetterling, Flannery - Numerical Recipes in C", который расположен в категории "". Всё это находится в предмете "численные методы" из 6 семестр, которые можно найти в файловом архиве . Не смотря на прямую связь этого архива с , его также можно найти и в других разделах. Архив можно найти в разделе "книги и методические указания", в предмете "численные методы и алгоритмы" в общих файлах.
Просмотр PDF-файла онлайн
Текст 8 страницы из PDF
Some care must beexercised in constructing nested if clauses. For example, consider the following:if (b > 3)if (a > 3) b += 1;else b -= 1;/* questionable! */As judged by the indentation used on successive lines, the intent of the writer ofthis code is the following: ‘If b is greater than 3 and a is greater than 3, thenincrement b.
If b is not greater than 3, then decrement b.’ According to the rulesof C, however, the actual meaning is ‘If b is greater than 3, then evaluate a. If a isgreater than 3, then increment b, and if a is less than or equal to 3, decrement b.’ Thepoint is that an else clause is associated with the most recent open if statement,no matter how you lay it out on the page.
Such confusions in meaning are easilyresolved by the inclusion of braces. They may in some instances be technicallysuperfluous; nevertheless, they clarify your intent and improve the program. Theabove fragment should be written asif (b > 3) {if (a > 3) b += 1;} else {b -= 1;}Here is a working program that consists dominantly of if control statements:#include <math.h>#define IGREG (15+31L*(10+12L*1582))Gregorian Calendar adopted Oct. 15, 1582.long julday(int mm, int id, int iyyy)In this routine julday returns the Julian Day Number that begins at noon of the calendar datespecified by month mm, day id, and year iyyy, all integer variables. Positive year signifies A.D.;negative, B.C.
Remember that the year after 1 B.C. was 1 A.D.{void nrerror(char error_text[]);12Chapter 1.Preliminarieslong jul;int ja,jy=iyyy,jm;if (jy == 0) nrerror("julday: there is no year zero.");if (jy < 0) ++jy;if (mm > 2) {Here is an example of a block IF-structure.jm=mm+1;} else {--jy;jm=mm+13;}jul = (long) (floor(365.25*jy)+floor(30.6001*jm)+id+1720995);if (id+31L*(mm+12L*iyyy) >= IGREG) {Test whether to change to Gregorian Calja=(int)(0.01*jy);endar.jul += 2-ja+(int) (0.25*ja);}return jul;}(Astronomers number each 24-hour period, starting and ending at noon, witha unique integer, the Julian Day Number [7].
Julian Day Zero was a very longtime ago; a convenient reference point is that Julian Day 2440000 began at noonof May 23, 1968. If you know the Julian Day Number that begins at noon of agiven calendar date, then the day of the week of that date is obtained by adding1 and taking the result modulo base 7; a zero answer corresponds to Sunday, 1 toMonday, . . . , 6 to Saturday.)While iteration. Most languages (though not FORTRAN, incidentally) providefor structures like the following C example:while (n < 1000) {n *= 2;j += 1;}It is the particular feature of this structure that the control-clause (in this casen < 1000) is evaluated before each iteration.
If the clause is not true, the enclosedstatements will not be executed. In particular, if this code is encountered at a timewhen n is greater than or equal to 1000, the statements will not even be executed once.Do-While iteration. Companion to the while iteration is a related controlstructure that tests its control-clause at the end of each iteration. In C, it lookslike this:do {n *= 2;j += 1;} while (n < 1000);In this case, the enclosed statements will be executed at least once, independentof the initial value of n.Break.
In this case, you have a loop that is repeated indefinitely until somecondition tested somewhere in the middle of the loop (and possibly tested in more1.1 Program Organization and Control Structures13than one place) becomes true. At that point you wish to exit the loop and proceedwith what comes after it. In C the structure is implemented with the simple breakstatement, which terminates execution of the innermost for, while, do, or switchconstruction and proceeds to the next sequential instruction. (In Pascal and standardFORTRAN, this structure requires the use of statement labels, to the detriment of clearprogramming.) 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 wasonce asked, for a scavenger hunt, to find the date of a Friday the 13th on which themoon was full.
This is a program which accomplishes that task, giving incidentallyall other Fridays the 13th as a by-product.#include <stdio.h>#include <math.h>#define ZON -5.0#define IYBEG 1900#define IYEND 2000Time zone −5 is Eastern Standard Time.The range of dates to be searched.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 occurredsince 1900.
We will feed it into the phase routine and adjust it up or downuntil we determine that our desired 13th was or was not a full moon. Thevariable icon signals the direction of adjustment.icon=0;for (;;) {flmoon(n,2,&jd,&frac);Get date of full moon n.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 midfrac += 24.0;night.}if (frac > 12.0) {++jd;frac -= 12.0;} elsefrac += 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,14Chapter 1.Preliminaries" hrs after midnight (EST)");break;Part of the break-structure, a match.} else {Didn’t hit it.ic=(jday >= jd ? 1 : -1);if (ic == (-icon)) break;Another break, case of no match.icon=ic;n += ic;}}}}}return 0;}If you are merely curious, there were (or will be) occurrences of a full moonon Friday the 13th (time zone GMT−5) on: 3/13/1903, 10/13/1905, 6/13/1919,1/13/1922, 11/13/1970, 2/13/1987, 10/13/2000, 9/13/2019, and 8/13/2049.Other “standard” structures.Our advice is to avoid them.
Everyprogramming language has some number of “goodies” that the designer just couldn’tresist throwing in. They seemed like a good idea at the time. Unfortunately theydon’t stand the test of time! Your program becomes difficult to translate into otherlanguages, and difficult to read (because rarely used structures are unfamiliar to thereader). You can almost always accomplish the supposed conveniences of thesestructures in other ways.In C, the most problematic control structure is the switch...case...defaultconstruction (see Figure 1.1.1), which has historically been burdened by uncertainty,from compiler to compiler, about what data types are allowed in its control expression.Data types char and int are universally supported.
For other data types, e.g., floator double, the structure should be replaced by a more recognizable and translatableif. . .else construction. ANSI C allows the control expression to be of type long,but many older compilers do not.The continue; construction, while benign, can generally be replaced by anif construction with no loss of clarity.About “Advanced Topics”Material set in smaller type, like this, signals an “advanced topic,” either one outside ofthe main argument of the chapter, or else one requiring of you more than the usual assumedmathematical background, or else (in a few cases) a discussion that is more speculative or analgorithm that is less well-tested. Nothing important will be lost if you skip the advancedtopics on a first reading of the book.You may have noticed that, by its looping over the months and years, the program badlukavoids using any algorithm for converting a Julian Day Number back into a calendar date.
Aroutine for doing just this is not very interesting structurally, but it is occasionally useful:#include <math.h>#define IGREG 2299161void caldat(long julian, int *mm, int *id, int *iyyy)Inverse of the function julday given above. Here julian is input as a Julian Day Number,and the routine outputs mm,id, and iyyy as the month, day, and year on which the specifiedJulian Day started at noon.{long ja,jalpha,jb,jc,jd,je;1.2 Some C Conventions for Scientific Computing15if (julian >= IGREG) {Cross-over to Gregorian Calendar produces this correcjalpha=(long)(((float) (julian-1867216)-0.25)/36524.25);tion.ja=julian+1+jalpha-(long) (0.25*jalpha);} else if (julian < 0) {Make day number positive by adding integer number ofja=julian+36525*(1-julian/36525);Julian centuries, then subtract them off} elseat the end.ja=julian;jb=ja+1524;jc=(long)(6680.0+((float) (jb-2439870)-122.1)/365.25);jd=(long)(365*jc+(0.25*jc));je=(long)((jb-jd)/30.6001);*id=jb-jd-(long) (30.6001*je);*mm=je-1;if (*mm > 12) *mm -= 12;*iyyy=jc-4715;if (*mm > 2) --(*iyyy);if (*iyyy <= 0) --(*iyyy);if (julian < 0) iyyy -= 100*(1-julian/36525);}(For additional calendrical algorithms, applicable to various historical calendars, see [8].)CITED REFERENCES AND FURTHER READING:Harbison, S.P., and Steele, G.L., Jr.