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

📄 cslib.shar

📁 书名:C语言科学与艺术,以前交钱下载的
💻 SHAR
📖 第 1 页 / 共 5 页
字号:
X#    ifndef boolX#      define bool intX#    endifX#  elseX#    ifdef boolX#      define FALSE 0X#      define TRUE 1X#    elseX       typedef enum {FALSE, TRUE} bool;X#    endifX#  endifX#endifXX/*X * Type: stringX * ------------X * The type string is identical to the type char *, which isX * traditionally used in C programs.  The main point of defining aX * new type is to improve program readability.   At the abstractionX * levels at which the type string is used, it is usually notX * important to take the string apart into its component characters.X * Declaring it as a string emphasizes this atomicity.X */XXtypedef char *string;XX/*X * Type: streamX * ------------X * Like string, the stream type is used to provide additionalX * readability and is defined to be equivalent to FILE *X * (which is particularly confusing because it violatesX * standard case conventions).  This type is not used inX * the text but is preserved in genlib.h, so it is possibleX * to teach all of CS1 without exposing any pointers.X */XXtypedef FILE *stream;XX/*X * Constant: UNDEFINEDX * -------------------X * Besides NULL, the only other constant of pointer type isX * UNDEFINED, which is used in certain packages as a specialX * sentinel to indicate an undefined pointer value.  In manyX * such contexts, NULL is a legitimate data value and isX * therefore inappropriate as a sentinel.X */XX#define UNDEFINED ((void *) undefined_object)XXextern char undefined_object[];XX/* Section 2 -- Memory allocation */XX/*X * General notes:X * --------------X * These functions provide a common interface for memoryX * allocation.  All functions in the library that allocateX * memory do so using GetBlock and FreeBlock.  Even thoughX * the ANSI standard defines malloc and free for the sameX * purpose, using GetBlock and FreeBlock provides greaterX * compatibility with non-ANSI implementations, automaticX * out-of-memory error detection, and the possibility ofX * substituting a garbage-collecting allocator.X */XX/*X * Function: GetBlockX * Usage: ptr = (type) GetBlock(nbytes);X * -------------------------------------X * GetBlock allocates a block of memory of the given size.  IfX * no memory is available, GetBlock generates an error.X */XXvoid *GetBlock(size_t nbytes);XX/*X * Function: FreeBlockX * Usage: FreeBlock(ptr);X * ----------------------X * FreeBlock frees the memory associated with ptr, which mustX * have been allocated using GetBlock, New, or NewArray.X */XXvoid FreeBlock(void *ptr);XX/*X * Macro: NewX * Usage: p = New(pointer-type);X * -----------------------------X * The New pseudofunction allocates enough space to hold anX * 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/glibrary.c' -a "${1}" != "-c" ; then   echo shar: Will not clobber existing file \"'cslib/glibrary.c'\"elseecho shar: Extracting \"'cslib/glibrary.c'\" \(818 characters\)sed "s/^X//" >'cslib/glibrary.c' <<'END_OF_FILE'X/*X * File: glibrary.cX * Version: 3.0X * Last modified on Sat Oct  1 12:28:01 1994 by erobertsX * -----------------------------------------------------X * This file implements the simple functions in the glibrary.hX * interface.X */XX#include <stdio.h>X#include <math.h>X#include "genlib.h"XX/* Constants */XX#define Pi 3.1415926535XX/* Exported entries */XXdouble GLRadians(double degrees)X{X    return (degrees * Pi / 180);X}XXdouble GLDegrees(double radians)X{X    return (radians * 180 / Pi);X}XXint GLRound(double x)X{X    return ((int) floor(x + 0.5));X}XXint GLMin(int x, int y)X{X    return ((x < y) ? x : y);X}XXint GLMax(int x, int y)X{X    return ((x > y) ? x : y);X}XXdouble GLMinF(double x, double y)X{X    return ((x < y) ? x : y);X}XXdouble GLMaxF(double x, double y)X{X    return ((x > y) ? x : y);X}END_OF_FILEif test 818 -ne `wc -c <'cslib/glibrary.c'`; then    echo shar: \"'cslib/glibrary.c'\" unpacked with wrong size!fi# end of 'cslib/glibrary.c'fiif test -f 'cslib/glibrary.h' -a "${1}" != "-c" ; then   echo shar: Will not clobber existing file \"'cslib/glibrary.h'\"elseecho shar: Extracting \"'cslib/glibrary.h'\" \(1439 characters\)sed "s/^X//" >'cslib/glibrary.h' <<'END_OF_FILE'X/*X * File: glibrary.hX * Version: 3.0X * Last modified on Sat Oct  1 12:28:02 1994 by erobertsX * -----------------------------------------------------X * This interface exports several simple, low-level functions that areX * used in various other parts of the graphics package implementation.X * Because these functions are otherwise likely to collide with studentX * functions, all external names include a GL prefix.X */XX#ifndef _glibrary_hX#define _glibrary_hXX/*X * Functions: GLRadians, GLDegreesX * Usage: radians = GLRadians(degrees);X *        degrees = GLDegrees(radians);X * ------------------------------------X * These functions convert back and forth between degrees and radians.X */XXdouble GLRadians(double degrees);Xdouble GLDegrees(double radians);XX/*X * Function: GLRoundX * Usage: n = GLRound(x);X * ----------------------X * This function rounds a double to the nearest integer.X */XXint GLRound(double x);XX/*X * Functions: GLMin, GLMaxX * Usage: min = GLMin(x, y);X *        max = GLMax(x, y);X * -------------------------X * These functions find the minimum and maximum of two integers.X */XXint GLMin(int x, int y);Xint GLMax(int x, int y);XX/*X * Functions: GLMinF, GLMaxFX * Usage: min = GLMinF(x, y);X *        max = GLMaxF(x, y);X * --------------------------X * These functions find the minimum and maximum of two doubles.X */XXdouble GLMinF(double x, double y);Xdouble GLMaxF(double x, double y);XX#endifEND_OF_FILEif test 1439 -ne `wc -c <'cslib/glibrary.h'`; then    echo shar: \"'cslib/glibrary.h'\" unpacked with wrong size!fi# end of 'cslib/glibrary.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'\" \(17740 characters\)sed "s/^X//" >'cslib/graphics.c' <<'END_OF_FILE'X/*X * File: graphics.cX * Version: 3.0X * Last modified on Sat Oct  1 12:28:01 1994 by erobertsX * -----------------------------------------------------X * This file is the top-level file in the implementation of theX * graphics.h interface for X windows.  The complete implementationX * also includes the following subsidiary modules:X *X *   glibrary.h  Various low-level mathematical functionsX *   xmanager.h  Mediates communication with the X operationsX *   xdisplay.h  Performs the actual drawing operationsX *   xcompat.h   Maintains BSD compatibility on System V.X */XX#include <stdio.h>X#include <stdlib.h>X#include <string.h>X#include <math.h>X#include <signal.h>X#include <sys/time.h>X#include <sys/types.h>XX#include "genlib.h"X#include "gcalloc.h"X#include "simpio.h"X#include "strlib.h"X#include "extgraph.h"X#include "glibrary.h"X#include "xmanager.h"X#include "xdisplay.h"X#include "xcompat.h"XX/*X * ParametersX * ----------X * DesiredWidth  -- Desired width of the graphics window in inchesX * DesiredHeight -- Desired height of the graphics window in inchesX * DefaultSize   -- Default point sizeX * MaxColors     -- Maximum number of color names allowedX * MinColors     -- Minimum number of colors the device must supportX */XX#define DesiredWidth    7.0X#define DesiredHeight   4.0X#define DefaultSize    12X#define MaxColors     256X#define MinColors      16XX/*X * Type: graphicsStateTX * --------------------X * This structure holds the variables that make up the graphics state.X */XXtypedef struct graphicsStateT {X    double cx, cy;X    string font;X    int size;X    int style;X    bool erase;X    int color;X    struct graphicsStateT *link;X} *graphicsStateT;XX/*X * Type: regionStateTX * ------------------X * The region assembly process has the character of a finite stateX * machine with the following four states:X *X *   NoRegion       Region has not yet been startedX *   RegionStarting Region is started but no line segments yetX *   RegionActive   First line segment appearsX *   PenHasMoved    Pen has moved during definitionX *X * The current state determines whether other operations are legalX * at that point.X */XXtypedef enum {X    NoRegion, RegionStarting, RegionActive, PenHasMovedX} regionStateT;XX/*X * Type: colorEntryTX * -----------------X * This type is used for the entries in the color table.X */XXtypedef struct {X    string name;X    double red, green, blue;X} colorEntryT;XX/*X * Global variablesX * ----------------X * initialized   -- TRUE if initialization has been doneX * windowTitle   -- Current window title (initialized statically)X * cmdBuffer     -- Static buffer for sending commandsX * regionState   -- Current state of the regionX * colorTable    -- Table of defined colorsX * nColors       -- Number of defined colorsX * colorOK       -- TRUE if the display supports colorX * lastColor     -- Previous color to avoid multiple changesX * fontChanged   -- TRUE if font information has changedX * windowWidth   -- Width of the window in inchesX * windowHeight  -- Height of the window in inchesX * stateStack    -- Stack of graphicStateT blocksX * cx, cy        -- Current coordinates     | TheseX * eraseMode     -- Setting of erase flag   | variablesX * textFont      -- Current font            | consitituteX * textStyle     -- Current style           | the currentX * pointSize     -- Current point size      | graphicsX * penColor      -- Color of pen            | stateX */XXstatic bool initialized = FALSE;Xstatic string windowTitle = "Graphics Window";XXstatic char cmdBuffer[CommandBufferSize];XXstatic regionStateT regionState;XXstatic colorEntryT colorTable[MaxColors];Xstatic int nColors;Xstatic bool colorOK;Xstatic int lastColor;Xstatic bool fontChanged;XXstatic double windowWidth = DesiredWidth;Xstatic double windowHeight = DesiredHeight;XXstatic graphicsStateT stateStack;XXstatic double cx, cy;Xstatic bool eraseMode;Xstatic string textFont;Xstatic int textStyle;Xstatic int pointSize;Xstatic int penColor;XX/* Private function prototypes */XXstatic void InitCheck(void);Xstatic void InitGraphicsState(void);Xstatic void InstallFont(void);Xstatic void InitColors(void);Xstatic int FindColorName(string name);Xstatic bool ShouldBeWhite(void);Xstatic bool StringMatch(string s1, string s2);Xstatic void USleep(unsigned useconds);X

⌨️ 快捷键说明

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