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

📄 standard.shar

📁 书名:C语言科学与艺术,以前交钱下载的
💻 SHAR
📖 第 1 页 / 共 5 页
字号:
X * object of the type to which pointer-type points and returnsX * a pointer to the newly allocated pointer.  Note thatX * "New" is different from the "new" operator used in C++;X * the former takes a pointer type and the latter takes theX * target type.X */XX#define New(type) ((type) GetBlock(sizeof *((type) NULL)))XX/*X * Macro: NewArrayX * Usage: p = NewArray(n, element-type);X * -------------------------------------X * NewArray allocates enough space to hold an array of nX * values of the specified element type.X */XX#define NewArray(n, type) ((type *) GetBlock((n) * sizeof (type)))XX/* Section 3 -- Basic error handling */XX/*X * Function: ErrorX * Usage: Error(msg, ...)X * ----------------------X * Error generates an error string, expanding % constructionsX * appearing in the error message string just as printf does.X * If an error handler exception has been introduced (see theX * "exception.h" facility), the ErrorException exception isX * raised with the expanded error string as argument.  IfX * there is no ErrorException defined, the program exitsX * with a status code indicating failure (as given by theX * constant ErrorExitStatus).  The length of the errorX * message string following expansion must not exceedX * MaxErrorMessage, and it is the client's responsibilityX * to ensure this.X */XXvoid Error(string msg, ...);XX/* Section 4 -- The repeat pseudo-statement */XX/*X * Statement form: repeat { ... }X * ------------------------------X * Some instructors who have taught CS1 using this libraryX * have found that usingX *X *     while (TRUE)X *X * to initiate a loop with an interior exit is confusing toX * students, particularly when it comes at the beginning ofX * the course.  This macro defines "repeat" as an infiniteX * loop construct for instructors who find it easier toX * explain, although it is not used in the text.   SimilarX * macro definitions are common in industry.X */XX#define repeat for (;;)XX#endifEND_OF_FILEif test 6232 -ne `wc -c <'cslib/genlib.h'`; then    echo shar: \"'cslib/genlib.h'\" unpacked with wrong size!fi# end of 'cslib/genlib.h'fiif test -f 'cslib/graphics.c' -a "${1}" != "-c" ; then   echo shar: Will not clobber existing file \"'cslib/graphics.c'\"elseecho shar: Extracting \"'cslib/graphics.c'\" \(4864 characters\)sed "s/^X//" >'cslib/graphics.c' <<'END_OF_FILE'X/*X * File: graphics.cX * Version: 1.0X * Last modified on Thu Sep 16 09:34:19 1993 by erobertsX * -----------------------------------------------------X * This file provides a fully standard implementation of theX * graphics.h interface that generates a PostScript file,X * suitable for printing.  None of the operations in theX * extended graphics interface are supported.  PostScript isX * a registered trademark of Adobe Systems Incorporated.X */XX/*X * General implementation notesX * ----------------------------X * This implementation of the graphics.h interface does notX * actually do any display but instead writes a PostScriptX * file containing commands that would generate the picture.X * The advantage of this implementation is portability, sinceX * it contains no code that depends on graphics primitivesX * for a particular platform.X *X * The code for this implementation is for the most partX * straightforward, because all of the graphics primitivesX * have simple PostScript equivalents.  The only hard partX * is making sure that the end of the file is correct whenX * the file is closed, usually via the exit call.  To printX * the page, the showpage command must occur at the end ofX * the file, but the graphics package does not get controlX * at that point.  To avoid the problem, each call to thisX * package writes a legal trailer to the file.  Thus, theX * invariant after each call to any of these functions isX * that the PostScript file is complete.  Before writingX * new data, these functions back up the file pointer overX * the old trailer and begin rewriting from that point.X */XX#include <stdio.h>X#include <math.h>XX#include "genlib.h"X#include "graphics.h"XX/*X * Constants: WindowHeight, WindowWidthX * ------------------------------------X * These constants are the values returned by GetWindowHeightX * and GetWindowWidth.  The assumption here is that output isX * being directed to an 8.5 x 11 page.X */XX#define WindowHeight 11.0X#define WindowWidth   8.5XX/*X * Constant: PSFileNameX * --------------------X * The name of the PostScript output file.X */XX#define PSFileName "graphics.ps"XX/*X * Private variablesX * -----------------X * initialized    Set to TRUE when InitGraphics is called.X * psfile         The file stream used for PostScript.X * cx, cy         The current x, y positions.X * nextWrite      The file index for the next PostScriptX *                command (see the general notes above).X */XXstatic bool initialized = FALSE;XXstatic FILE *psfile;XXstatic double cx, cy;XXstatic long nextWrite;XX/* Private function prototypes */XXstatic void InitCheck(void);Xstatic void WritePostScriptHeader(void);Xstatic void WritePostScriptTrailer(void);Xstatic void ResetFilePointer(void);Xstatic double Pts(double inches);Xstatic double Radians(double degrees);XX/* Public functions */XXvoid InitGraphics(void)X{X    if (initialized) fclose(psfile);X    psfile = fopen(PSFileName, "w");X    WritePostScriptHeader();X    WritePostScriptTrailer();X    cx = cy = 0;X    initialized = TRUE;X}XXvoid MovePen(double x, double y)X{X    InitCheck();X    cx = x;X    cy = y;X}XXvoid DrawLine(double dx, double dy)X{X    InitCheck();X    ResetFilePointer();X    fprintf(psfile, "newpath %g %g moveto", Pts(cx), Pts(cy));X    fprintf(psfile, " %g %g rlineto stroke\n", Pts(dx), Pts(dy));X    cx += dx;X    cy += dy;X    WritePostScriptTrailer();X}XXvoid DrawArc(double r, double start, double sweep)X{X    double x, y;XX    InitCheck();X    x = cx + r * cos(Radians(start + 180));X    y = cy + r * sin(Radians(start + 180));X    ResetFilePointer();X    fprintf(psfile, "newpath %g %g %g", Pts(x), Pts(y), Pts(r));X    fprintf(psfile, " %g %g", start, start + sweep);X    fprintf(psfile, " %s stroke\n", (sweep < 0) ? "arcn" : "arc");X    WritePostScriptTrailer();X    cx = x + r * cos(Radians(start + sweep));X    cy = y + r * sin(Radians(start + sweep));X}XXdouble GetWindowWidth(void)X{X    InitCheck();X    return (WindowWidth);X}XXdouble GetWindowHeight(void)X{X    InitCheck();X    return (WindowHeight);X}XXdouble GetCurrentX(void)X{X    InitCheck();X    return (cx);X}XXdouble GetCurrentY(void)X{X    InitCheck();X    return (cy);X}XX/* Private functions */XXstatic void InitCheck(void)X{X    if (!initialized) Error("InitGraphics has not been called");X}XXstatic void WritePostScriptHeader(void)X{X    fprintf(psfile, "%%!PS-Adobe-1.0\n");X    fprintf(psfile, "%%%%Title: graphics window\n");X    fprintf(psfile, "%%%%Pages: 1\n");X    fprintf(psfile, "%%%%EndComments\n");X}XXstatic void WritePostScriptTrailer(void)X{X    nextWrite = ftell(psfile);X    fprintf(psfile, "showpage\n");X}XXstatic void ResetFilePointer(void)X{X    fseek(psfile, nextWrite, 0);X}XXstatic double Pts(double inches)X{X    return (72 * inches);X}XXstatic double Radians(double degrees)X{X    return (degrees / 180 * 3.1415926535);X}END_OF_FILEif test 4864 -ne `wc -c <'cslib/graphics.c'`; then    echo shar: \"'cslib/graphics.c'\" unpacked with wrong size!fi# end of 'cslib/graphics.c'fiif test -f 'cslib/graphics.h' -a "${1}" != "-c" ; then   echo shar: Will not clobber existing file \"'cslib/graphics.h'\"elseecho shar: Extracting \"'cslib/graphics.h'\" \(4805 characters\)sed "s/^X//" >'cslib/graphics.h' <<'END_OF_FILE'X/*X * File: graphics.hX * Version: 1.0X * Last modified on Mon Jun  6 11:03:27 1994 by erobertsX * -----------------------------------------------------X * This interface provides access to a simple library ofX * functions that make it possible to draw lines and arcsX * on the screen.  This interface presents a portableX * abstraction that can be used with a variety of windowX * systems implemented on different hardware platforms.X */XX#ifndef _graphics_hX#define _graphics_hXX/*X * OverviewX * --------X * This library provides several functions for drawing linesX * and circular arcs in a region of the screen that isX * defined as the "graphics window."  Once drawn, theseX * lines and arcs stay in their position, which means thatX * the package can only be used for static pictures and notX * for animation.X *X * Individual points within the window are specified byX * giving their x and y coordinates.  These coordinates areX * real numbers measured in inches, with the origin in theX * lower left corner, as it is in traditional mathematics.X *X * The calls available in the package are listed below.  MoreX * complete descriptions are included with each functionX * description.X *X *   InitGraphics();X *   MovePen(x, y);X *   DrawLine(dx, dy);X *   DrawArc(r, start, sweep);X *   width = GetWindowWidth();X *   height = GetWindowHeight();X *   x = GetCurrentX();X *   y = GetCurrentY();X */XX/*X * Function: InitGraphicsX * Usage: InitGraphics();X * ----------------------X * This procedure creates the graphics window on the screen.X * The call to InitGraphics must precede any calls to otherX * functions in this package and must also precede any printfX * output.  In most cases, the InitGraphics call is the firstX * statement in the function main.X */XXvoid InitGraphics(void);XX/*X * Function: MovePenX * Usage: MovePen(x, y);X * ---------------------X * This procedure moves the current point to the positionX * (x, y), without drawing a line.  The model is that ofX * the pen being lifted off the graphics window surface andX * then moved to its new position.X */XXvoid MovePen(double x, double y);XX/*X * Function: DrawLineX * Usage: DrawLine(dx, dy);X * ------------------------X * This procedure draws a line extending from the currentX * point by moving the pen dx inches in the x directionX * and dy inches in the y direction.  The final positionX * becomes the new current point.X */XXvoid DrawLine(double dx, double dy);XX/*X * Function: DrawArcX * Usage: DrawArc(r, start, sweep);X * --------------------------------X * This procedure draws a circular arc, which always beginsX * at the current point.  The arc itself has radius r, andX * starts at the angle specified by the parameter start,X * relative to the center of the circle.  This angle isX * measured in degrees counterclockwise from the 3 o'clockX * position along the x-axis, as in traditional mathematics.X * For example, if start is 0, the arc begins at the 3 o'clockX * position; if start is 90, the arc begins at the 12 o'clockX * position; and so on.  The fraction of the circle drawn isX * specified by the parameter sweep, which is also measured inX * degrees.  If sweep is 360, DrawArc draws a complete circle;X * if sweep is 90, it draws a quarter of a circle.  If the valueX * of sweep is positive, the arc is drawn counterclockwise fromX * the current point.  If sweep is negative, the arc is drawnX * clockwise from the current point.  The current point at theX * end of the DrawArc operation is the final position of the penX * along the arc.X *X * Examples:X *   DrawArc(r, 0, 360)    Draws a circle to the left of theX *                         current point.X *   DrawArc(r, 90, 180)   Draws the left half of a semicircleX *                         starting from the 12 o'clock position.X *   DrawArc(r, 0, 90)     Draws a quarter circle from the 3X *                         o'clock to the 12 o'clock position.X *   DrawArc(r, 0, -90)    Draws a quarter circle from the 3X *                         o'clock to the 6 o'clock position.X *   DrawArc(r, -90, -90)  Draws a quarter circle from the 6X *                         o'clock to the 9 o'clock position.X */XXvoid DrawArc(double r, double start, double sweep);XX/*X * Functions: GetWindowWidth, GetWindowHeightX * Usage: width = GetWindowWidth();X *        height = GetWindowHeight();X * ------------------------------------------X * These functions return the width and height of the graphicsX * window, in inches.X */XXdouble GetWindowWidth(void);Xdouble GetWindowHeight(void);XX/*X * Functions: GetCurrentX, GetCurrentYX * Usage: x = GetCurrentX();X *        y = GetCurrentY();X * -----------------------------------X * These functions return the current x and y positions.X */XXdouble GetCurrentX(void);Xdouble GetCurrentY(void);XX#endifEND_OF_FILEif test 4805 -ne `wc -c <'cslib/graphics.h'`; then    echo shar: \"'cslib/graphics.h'\" unpacked with wrong size!fi# end of 'cslib/graphics.h'fiif test -f 'cslib/random.c' -a "${1}" != "-c" ; then   echo shar: Will not clobber existing file \"'cslib/random.c'\"elseecho shar: Extracting \"'cslib/random.c'\" \(1718 characters\)sed "s/^X//" >'cslib/random.c' <<'END_OF_FILE'X/*X * File: random.cX * Version: 1.0X * Last modified on Mon Sep 13 10:42:45 1993 by erobertsX * -----------------------------------------------------X * This file implements the random.h interface.X */XX#include <stdio.h>X#include <stdlib.h>X#include <time.h>XX#include "genlib.h"X#include "random.h"XX/*X * Function: RandomizeX * -------------------X * This function operates by setting the random numberX * seed to the current time.  The srand function is

⌨️ 快捷键说明

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