📄 programs.shar
字号:
X }X}XX/*X * Function: FactorialX * Usage: f = Factorial(n);X * ------------------------X * This function returns the factorial of n, which is definedX * as the product of all integers from 1 up to n.X */XXint Factorial(int n)X{X int product, i;XX product = 1;X for (i = 1; i <= n; i++) {X product *= i;X }X return (product);X}END_OF_FILEif test 970 -ne `wc -c <'programs/05-Functions/fact.c'`; then echo shar: \"'programs/05-Functions/fact.c'\" unpacked with wrong size!fi# end of 'programs/05-Functions/fact.c'fiif test -f 'programs/05-Functions/gameloop.c' -a "${1}" != "-c" ; then echo shar: Will not clobber existing file \"'programs/05-Functions/gameloop.c'\"elseecho shar: Extracting \"'programs/05-Functions/gameloop.c'\" \(711 characters\)sed "s/^X//" >'programs/05-Functions/gameloop.c' <<'END_OF_FILE'X/*X * File: gameloop.cX * ----------------X * This program is used as an illustration of theX * StringEqual function.X */XX#include <stdio.h>X#include "genlib.h"X#include "simpio.h"X#include "strlib.h"XX/* Function prototypes */XXvoid PlayOneGame(void);XX/* Main program */XXmain()X{X string answer;XX while (TRUE) {X PlayOneGame();X printf("Would you like to play again? ");X answer = GetLine();X if (StringEqual(answer, "no")) break;X }X}XX/*X * Function: PlayOneGameX * Usage: PlayOneGame();X * ---------------------X * This is a dummy function that could be replaced byX * any game-playing program.X */XXvoid PlayOneGame()X{X printf(". . . play the game . . .\n");X}END_OF_FILEif test 711 -ne `wc -c <'programs/05-Functions/gameloop.c'`; then echo shar: \"'programs/05-Functions/gameloop.c'\" unpacked with wrong size!fi# end of 'programs/05-Functions/gameloop.c'fiif test -f 'programs/05-Functions/iseven.c' -a "${1}" != "-c" ; then echo shar: Will not clobber existing file \"'programs/05-Functions/iseven.c'\"elseecho shar: Extracting \"'programs/05-Functions/iseven.c'\" \(656 characters\)sed "s/^X//" >'programs/05-Functions/iseven.c' <<'END_OF_FILE'X/*X * File: iseven.cX * --------------X * This program prints a list of the even numbers betweenX * 1 and 10. In an ideal implementation, constants wouldX * be used for the limits, but this program is designed toX * match the program example in the text.X */XX#include <stdio.h>X#include "genlib.h"XX/* Function prototypes */XXbool IsEven(int n);XX/* Main program */XXmain()X{X int i;XX for (i = 1; i <= 10; i++) {X if (IsEven(i)) printf("%2d\n", i);X }X}XX/*X * Function: IsEvenX * Usage: if (IsEven(n)) . . .X * ---------------------------X * This function returns TRUE if n is even.X */XXbool IsEven(int n)X{X return (n % 2 == 0);X}END_OF_FILEif test 656 -ne `wc -c <'programs/05-Functions/iseven.c'`; then echo shar: \"'programs/05-Functions/iseven.c'\" unpacked with wrong size!fi# end of 'programs/05-Functions/iseven.c'fiif test -f 'programs/06-Algorithms/Makefile' -a "${1}" != "-c" ; then echo shar: Will not clobber existing file \"'programs/06-Algorithms/Makefile'\"elseecho shar: Extracting \"'programs/06-Algorithms/Makefile'\" \(1765 characters\)sed "s/^X//" >'programs/06-Algorithms/Makefile' <<'END_OF_FILE'X# Makefile for cbook/06-Algorithms/programsX# Created by the ExportAll facilityX# ***************************************************************XXPROGRAMS = \X primes1 \X primes2 \X primes3 \X gcd \X euclid \X nsqrt \X zeno \X tsqrt XX# ***************************************************************X# Parameters to control Makefile operationX# Note that the gccx command script must be definedXXCC = gccxXCFLAGS = XX# ***************************************************************X# Entry to bring the package up to dateXXall: $(PROGRAMS)XX# ***************************************************************X# Standard entries to remove files from the directoriesX# tidy -- eliminate unwanted filesX# scratch -- delete derived files in preparation for rebuildXXtidy:X rm -f ,* .,* *~ core a.out graphics.psXXscratch: tidyX rm -f *.o *.a $(PROGRAMS)XX# ***************************************************************X# C compilationsXXprimes1.o: primes1.cX $(CC) $(CFLAGS) -c primes1.cXXprimes2.o: primes2.cX $(CC) $(CFLAGS) -c primes2.cXXprimes3.o: primes3.cX $(CC) $(CFLAGS) -c primes3.cXXgcd.o: gcd.cX $(CC) $(CFLAGS) -c gcd.cXXeuclid.o: euclid.cX $(CC) $(CFLAGS) -c euclid.cXXnsqrt.o: nsqrt.cX $(CC) $(CFLAGS) -c nsqrt.cXXzeno.o: zeno.cX $(CC) $(CFLAGS) -c zeno.cXXtsqrt.o: tsqrt.cX $(CC) $(CFLAGS) -c tsqrt.cXXXprimes1: primes1.oX $(CC) $(CFLAGS) -o primes1 primes1.oXXprimes2: primes2.oX $(CC) $(CFLAGS) -o primes2 primes2.oXXprimes3: primes3.oX $(CC) $(CFLAGS) -o primes3 primes3.oXXgcd: gcd.oX $(CC) $(CFLAGS) -o gcd gcd.oXXeuclid: euclid.oX $(CC) $(CFLAGS) -o euclid euclid.oXXnsqrt: nsqrt.oX $(CC) $(CFLAGS) -o nsqrt nsqrt.oXXzeno: zeno.oX $(CC) $(CFLAGS) -o zeno zeno.oXXtsqrt: tsqrt.oX $(CC) $(CFLAGS) -o tsqrt tsqrt.oEND_OF_FILEif test 1765 -ne `wc -c <'programs/06-Algorithms/Makefile'`; then echo shar: \"'programs/06-Algorithms/Makefile'\" unpacked with wrong size!fi# end of 'programs/06-Algorithms/Makefile'fiif test -f 'programs/06-Algorithms/euclid.c' -a "${1}" != "-c" ; then echo shar: Will not clobber existing file \"'programs/06-Algorithms/euclid.c'\"elseecho shar: Extracting \"'programs/06-Algorithms/euclid.c'\" \(938 characters\)sed "s/^X//" >'programs/06-Algorithms/euclid.c' <<'END_OF_FILE'X/*X * File: euclid.cX * --------------X * This program computes a greates common divisor usingX * Euclid's algorithm.X */XX#include <stdio.h>X#include "genlib.h"X#include "simpio.h"XX/* Function prototypes */XXint GCD(int x, int y);XX/* Main program */XXmain()X{X int x, y;XX printf("This program calculates greatest common divisors.\n");X printf("Enter two integers, x and y.\n");X printf("x = ? ");X x = GetInteger();X printf("y = ? ");X y = GetInteger();X printf("The gcd of %d and %d is %d.\n", x, y, GCD(x, y));X}XX/*X * Function: GCDX * Usage: gcd = GCD(x, y);X * -----------------------X * Returns the greatest common divisor of x and y,X * calculated by Euclid's algorithm, which is discussedX * as Proposition 7 in Book II of Euclid's Elements.X */XXint GCD(int x, int y)X{X int r;XX while (TRUE) {X r = x % y;X if (r == 0) break;X x = y;X y = r;X }X return (y);X}END_OF_FILEif test 938 -ne `wc -c <'programs/06-Algorithms/euclid.c'`; then echo shar: \"'programs/06-Algorithms/euclid.c'\" unpacked with wrong size!fi# end of 'programs/06-Algorithms/euclid.c'fiif test -f 'programs/06-Algorithms/gcd.c' -a "${1}" != "-c" ; then echo shar: Will not clobber existing file \"'programs/06-Algorithms/gcd.c'\"elseecho shar: Extracting \"'programs/06-Algorithms/gcd.c'\" \(870 characters\)sed "s/^X//" >'programs/06-Algorithms/gcd.c' <<'END_OF_FILE'X/*X * File: gcd.cX * -----------X * This program computes a greatest common divisor usingX * a brute-force algorithm.X */XX#include <stdio.h>X#include "genlib.h"X#include "simpio.h"XX/* Function prototypes */XXint GCD(int x, int y);XX/* Main program */XXmain()X{X int x, y;XX printf("This program calculates greatest common divisors.\n");X printf("Enter two integers, x and y.\n");X printf("x = ? ");X x = GetInteger();X printf("y = ? ");X y = GetInteger();X printf("The gcd of %d and %d is %d.\n", x, y, GCD(x, y));X}XX/*X * Function: GCDX * Usage: gcd = GCD(x, y);X * -----------------------X * Returns the greatest common divisor of x and y,X * calculated by the brute-force method of testingX * every possibility.X */XXint GCD(int x, int y)X{X int g;XX g = x;X while (x % g != 0 || y % g != 0) {X g--;X }X return (g);X}END_OF_FILEif test 870 -ne `wc -c <'programs/06-Algorithms/gcd.c'`; then echo shar: \"'programs/06-Algorithms/gcd.c'\" unpacked with wrong size!fi# end of 'programs/06-Algorithms/gcd.c'fiif test -f 'programs/06-Algorithms/nsqrt.c' -a "${1}" != "-c" ; then echo shar: Will not clobber existing file \"'programs/06-Algorithms/nsqrt.c'\"elseecho shar: Extracting \"'programs/06-Algorithms/nsqrt.c'\" \(2633 characters\)sed "s/^X//" >'programs/06-Algorithms/nsqrt.c' <<'END_OF_FILE'X/*X * File: nsqrt.cX * -------------X * This program calculates square roots using Newton'sX * method for successive approximation.X */XX#include <stdio.h>X#include <math.h>X#include "genlib.h"XX/*X * Constants:X * ----------X * Epsilon -- The acceptable fraction of difference betweenX * two values that are approximately equal.X */XX#define Epsilon 0.0000000001XX/* Function prototypes */XXvoid TestSqrt(double x);Xdouble Sqrt(double x);Xbool ApproximatelyEqual(double x, double y);Xdouble MinF(double x, double y);XX/* Main program */XXmain()X{X TestSqrt(0.0);X TestSqrt(0.25);X TestSqrt(1);X TestSqrt(2);X TestSqrt(3);X TestSqrt(4);X TestSqrt(49);X TestSqrt(200);X TestSqrt(1.0E10);X}XX/*X * Function: TestSqrtX * Usage: TestSqrt(x);X * -------------------X * Generates a line of the test table.X */XXvoid TestSqrt(double x)X{X double computed, actual;XX computed = Sqrt(x);X actual = sqrt(x);X printf("Sqrt(%g) = %g sqrt(%g) = %g\n", x, computed, x, actual);X if (!ApproximatelyEqual(computed, actual)) {X printf("Warning! Computed value does not match actual!\n");X }X}XX/*X * Function: SqrtX * Usage: root = Sqrt(x);X * ----------------------X * Returns the square root of x, calculated usingX * Newton's algorithm, as described in the text.X */XXdouble Sqrt(double x)X{X double g;XX if (x == 0) return (0);X if (x < 0) Error("Sqrt called with negative argument %g", x);X g = x;X while (!ApproximatelyEqual(x, g * g)) {X g = (g + x / g) / 2;X }X return (g);X}XX/*X * Function: ApproximatelyEqualX * Usage: if (ApproximatelyEqual(x, y)) . . .X * ------------------------------------------X * Returns TRUE if x and y are approximately equal, asX * indicated by the formula:X *X * | x - y |X * ------------- < EpsilonX * min(|x|, |y|)X *X * To avoid the possibility of division by 0, the functionX * first tests to make sure that adding the denominator toX * the numerator of this fraction changes the nominator.X * This test has the same p
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -