⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 standard.shar

📁 书名:C语言科学与艺术,以前交钱下载的
💻 SHAR
📖 第 1 页 / 共 5 页
字号:
X * provided by the <stdlib.h> library and requires anX * integer argument.  The time function is providedX * by <time.h>.X */XXvoid Randomize(void)X{X    srand((int) time(NULL));X}XX/*X * Function: RandomIntegerX * -----------------------X * This function first obtains a random integer inX * the range [0..RAND_MAX] by applying four steps:X * (1) Generate a real number between 0 and 1.X * (2) Scale it to the appropriate range size.X * (3) Truncate the value to an integer.X * (4) Translate it to the appropriate starting point.X */XXint RandomInteger(int low, int high)X{X    int k;X    double d;XX    d = (double) rand() / ((double) RAND_MAX + 1);X    k = (int) (d * (high - low + 1));X    return (low + k);X}XX/*X * Function: RandomRealX * --------------------X * The implementation of RandomReal is similar to thatX * of RandomInteger, without the truncation step.X */XXdouble RandomReal(double low, double high)X{X    double d;XX    d = (double) rand() / ((double) RAND_MAX + 1);X    return (low + d * (high - low));X}XX/*X * Function: RandomChanceX * ----------------------X * This function uses RandomReal to generate a numberX * between 0 and 100, which it then compares to p.X */XXbool RandomChance(double p)X{X    return (RandomReal(0, 1) < p);X}END_OF_FILEif test 1718 -ne `wc -c <'cslib/random.c'`; then    echo shar: \"'cslib/random.c'\" unpacked with wrong size!fi# end of 'cslib/random.c'fiif test -f 'cslib/random.h' -a "${1}" != "-c" ; then   echo shar: Will not clobber existing file \"'cslib/random.h'\"elseecho shar: Extracting \"'cslib/random.h'\" \(1962 characters\)sed "s/^X//" >'cslib/random.h' <<'END_OF_FILE'X/*X * File: random.hX * Version: 1.0X * Last modified on Fri Jul 22 16:44:36 1994 by erobertsX * -----------------------------------------------------X * This interface provides several functions for generatingX * pseudo-random numbers.X */XX#ifndef _random_hX#define _random_hXX#include "genlib.h"X#include <stdlib.h>XX/*X * Constant: RAND_MAXX * ------------------X * Unfortunately, several libraries that supposedly conform toX * the ANSI standard do not define RAND_MAX in <stdlib.h>.  ToX * reduce portability problems, this interface defines RAND_MAXX * to be the largest positive integer if it is undefined.X */XX#ifndef RAND_MAXX#  define RAND_MAX ((int) ((unsigned) ~0 >> 1))X#endifXX/*X * Function: RandomizeX * Usage: Randomize();X * -------------------X * This function sets the random seed so that the random sequenceX * is unpredictable.  During the debugging phase, it is best notX * to call this function, so that program behavior is repeatable.X */XXvoid Randomize(void);XX/*X * Function: RandomIntegerX * Usage: n = RandomInteger(low, high);X * ------------------------------------X * This function returns a random integer in the range low to high,X * inclusive.X */XXint RandomInteger(int low, int high);XX/*X * Function: RandomRealX * Usage: d = RandomReal(low, high);X * ---------------------------------X * This function returns a random real number in the half-openX * interval [low .. high), meaning that the result is alwaysX * greater than or equal to low but strictly less than high.X */XXdouble RandomReal(double low, double high);XX/*X * Function: RandomChanceX * Usage: if (RandomChance(p)) . . .X * ---------------------------------X * The RandomChance function returns TRUE with the probabilityX * indicated by p, which should be a floating-point number betweenX * 0 (meaning never) and 1 (meaning always).  For example, callingX * RandomChance(.30) returns TRUE 30 percent of the time.X */XXbool RandomChance(double p);XX#endifEND_OF_FILEif test 1962 -ne `wc -c <'cslib/random.h'`; then    echo shar: \"'cslib/random.h'\" unpacked with wrong size!fi# end of 'cslib/random.h'fiif test -f 'cslib/simpio.c' -a "${1}" != "-c" ; then   echo shar: Will not clobber existing file \"'cslib/simpio.c'\"elseecho shar: Extracting \"'cslib/simpio.c'\" \(3581 characters\)sed "s/^X//" >'cslib/simpio.c' <<'END_OF_FILE'X/*X * File: simpio.cX * Version: 1.0X * Last modified on Fri Jul 15 14:10:41 1994 by erobertsX * -----------------------------------------------------X * This file implements the simpio.h interface.X */XX#include <stdio.h>X#include <string.h>XX#include "genlib.h"X#include "strlib.h"X#include "simpio.h"XX/*X * Constants:X * ----------X * InitialBufferSize -- Initial buffer size for ReadLineX */XX#define InitialBufferSize 120XX/* Exported entries */XX/*X * Functions: GetInteger, GetLong, GetRealX * ---------------------------------------X * These functions first read a line and then call sscanf toX * translate the number.  Reading an entire line is essential toX * good error recovery, because the characters after the point ofX * error would otherwise remain in the input buffer and confuseX * subsequent input operations.  The sscanf line allows white spaceX * before and after the number but no other extraneous characters.X */XXint GetInteger(void)X{X    string line;X    int value;X    char termch;XX    while (TRUE) {X        line = GetLine();X        switch (sscanf(line, " %d %c", &value, &termch)) {X          case 1:X            FreeBlock(line);X            return (value);X          case 2:X            printf("Unexpected character: '%c'\n", termch);X            break;X          default:X            printf("Please enter an integer\n");X            break;X        }X        FreeBlock(line);X        printf("Retry: ");X    }X}XXlong GetLong(void)X{X    string line;X    long value;X    char termch;XX    while (TRUE) {X        line = GetLine();X        switch (sscanf(line, " %ld %c", &value, &termch)) {X          case 1:X            FreeBlock(line);X            return (value);X          case 2:X            printf("Unexpected character: '%c'\n", termch);X            break;X          default:X            printf("Please enter an integer\n");X            break;X        }X        FreeBlock(line);X        printf("Retry: ");X    }X}XXdouble GetReal(void)X{X    string line;X    double value;X    char termch;XX    while (TRUE) {X        line = GetLine();X        switch (sscanf(line, " %lf %c", &value, &termch)) {X          case 1:X            FreeBlock(line);X            return (value);X          case 2:X            printf("Unexpected character: '%c'\n", termch);X            break;X          default:X            printf("Please enter a real number\n");X            break;X        }X        FreeBlock(line);X        printf("Retry: ");X    }X}XX/*X * Function: GetLineX * -----------------X * This function is a simple wrapper; all the work is done byX * ReadLine.X */XXstring GetLine(void)X{X    return (ReadLine(stdin));X}XX/*X * Function: ReadLineX * ------------------X * This function operates by reading characters from the fileX * into a dynamically allocated buffer.  If the buffer becomesX * full before the end of the line is reached, a new bufferX * twice the size of the previous one is allocated.X */XXstring ReadLine(FILE *infile)X{X    string line, nline;X    int n, ch, size;XX    n = 0;X    size = InitialBufferSize;X    line = GetBlock(size + 1);X    while ((ch = getc(infile)) != '\n' && ch != EOF) {X        if (n == size) {X            size *= 2;X            nline = (string) GetBlock(size + 1);X            strncpy(nline, line, n);X            FreeBlock(line);X            line = nline;X        }X        line[n++] = ch;X    }X    if (n == 0 && ch == EOF) {X        FreeBlock(line);X        return (NULL);X    }X    line[n] = '\0';X    nline = (string) GetBlock(n + 1);X    strcpy(nline, line);X    FreeBlock(line);X    return (nline);X}END_OF_FILEif test 3581 -ne `wc -c <'cslib/simpio.c'`; then    echo shar: \"'cslib/simpio.c'\" unpacked with wrong size!fi# end of 'cslib/simpio.c'fiif test -f 'cslib/simpio.h' -a "${1}" != "-c" ; then   echo shar: Will not clobber existing file \"'cslib/simpio.h'\"elseecho shar: Extracting \"'cslib/simpio.h'\" \(2008 characters\)sed "s/^X//" >'cslib/simpio.h' <<'END_OF_FILE'X/*X * File: simpio.hX * Version: 1.0X * Last modified on Wed Apr 27 07:29:13 1994 by erobertsX * -----------------------------------------------------X * This interface provides access to a simple package ofX * functions that simplify the reading of input data.X */XX#ifndef _simpio_hX#define _simpio_hXX#include "genlib.h"XX/*X * Function: GetIntegerX * Usage: i = GetInteger();X * ------------------------X * GetInteger reads a line of text from standard input and scansX * it as an integer.  The integer value is returned.  If anX * integer cannot be scanned or if more characters follow theX * number, the user is given a chance to retry.X */XXint GetInteger(void);XX/*X * Function: GetLongX * Usage: l = GetLong();X * ---------------------X * GetLong reads a line of text from standard input and scansX * it as a long integer.  The value is returned as a long.X * If an integer cannot be scanned or if more characters followX * the number, the user is given a chance to retry.X */XXlong GetLong(void);XX/*X * Function: GetRealX * Usage: x = GetReal();X * ---------------------X * GetReal reads a line of text from standard input and scansX * it as a double.  If the number cannot be scanned or if extraX * characters follow after the number ends, the user is givenX * a chance to reenter the value.X */XXdouble GetReal(void);XX/*X * Function: GetLineX * Usage: s = GetLine();X * ---------------------X * GetLine reads a line of text from standard input and returnsX * the line as a string.  The newline character that terminatesX * the input is not stored as part of the string.X */XXstring GetLine(void);XX/*X * Function: ReadLineX * Usage: s = ReadLine(infile);X * ----------------------------X * ReadLine reads a line of text from the input file andX * returns the line as a string.  The newline characterX * that terminates the input is not stored as part of theX * string.  The ReadLine function returns NULL if infileX * is at the end-of-file position.X */XXstring ReadLine(FILE *infile);XX#endifEND_OF_FILEif test 2008 -ne `wc -c <'cslib/simpio.h'`; then    echo shar: \"'cslib/simpio.h'\" unpacked with wrong size!fi# end of 'cslib/simpio.h'fiif test -f 'cslib/strlib.c' -a "${1}" != "-c" ; then   echo shar: Will not clobber existing file \"'cslib/strlib.c'\"elseecho shar: Extracting \"'cslib/strlib.c'\" \(5382 characters\)sed "s/^X//" >'cslib/strlib.c' <<'END_OF_FILE'X/*X * File: strlib.cX * Version: 1.0X * Last modified on Fri Jul 15 14:10:41 1994 by erobertsX * -----------------------------------------------------X * This file implements the strlib.h interface.X */XX/*X * General implementation notes:X * -----------------------------X * This module implements the strlib library by mapping allX * functions into the appropriate calls to the ANSI <string.h>X * interface.  The implementations of the individual functionsX * are all quite simple and do not require individual comments.X * For descriptions of the behavior of each function, see theX * interface.X */XX#include <stdio.h>X#include <string.h>X#include <ctype.h>XX#include "genlib.h"X#include "strlib.h"XX/*X * Constant: MaxDigitsX * -------------------X * This constant must be larger than the maximumX * number of digits that can appear in a number.X */XX#define MaxDigits 30XX/* Private function prototypes */XXstatic string CreateString(int len);XX/* Section 1 -- Basic string operations */XXstring Concat(string s1, string s2)X{X    string s;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -