Chapter Seven Libraries and Interfaces
description
Transcript of Chapter Seven Libraries and Interfaces
![Page 1: Chapter Seven Libraries and Interfaces](https://reader036.fdocuments.net/reader036/viewer/2022062518/5681432c550346895daf9bac/html5/thumbnails/1.jpg)
1
Chapter SevenChapter Seven
Libraries and InterfacesLibraries and Interfaces
![Page 2: Chapter Seven Libraries and Interfaces](https://reader036.fdocuments.net/reader036/viewer/2022062518/5681432c550346895daf9bac/html5/thumbnails/2.jpg)
2
LibrariesLibraries
• A library is a collection of predefined functions that perform specific operations
• The standard libraries are the libraries that should be provided by every C programming system
• Programmers can also build their own libraries using the mechanisms provided by C programming systems
![Page 3: Chapter Seven Libraries and Interfaces](https://reader036.fdocuments.net/reader036/viewer/2022062518/5681432c550346895daf9bac/html5/thumbnails/3.jpg)
3
InterfacesInterfaces• A library interface is the boundary between
the implementation of a library and programs that use that library
• An interface represents a shared understanding about the nature of that boundary, providing both creators and users of a library with the critical information they need to know
• An interface gives structure to the exchange of information between the library and its users
![Page 4: Chapter Seven Libraries and Interfaces](https://reader036.fdocuments.net/reader036/viewer/2022062518/5681432c550346895daf9bac/html5/thumbnails/4.jpg)
4
An ExampleAn Example
• The math library defines the function sqrt to calculate a square root
• This function can be implemented by Newton’s algorithm, Taylor series expansion, or other algorithms
• Knowing how to call the sqrt function and knowing how to implement it are both important skills
![Page 5: Chapter Seven Libraries and Interfaces](https://reader036.fdocuments.net/reader036/viewer/2022062518/5681432c550346895daf9bac/html5/thumbnails/5.jpg)
5
An ExampleAn Example
• The programmer (implementer) who implements a library cannot anticipate all the potential uses for that library
• The programmer (client) who calls functions in a library wouldn’t know how to write them
![Page 6: Chapter Seven Libraries and Interfaces](https://reader036.fdocuments.net/reader036/viewer/2022062518/5681432c550346895daf9bac/html5/thumbnails/6.jpg)
6
Function PrototypeFunction Prototype
• For each function in the library, the client must know the following:
• Its name
• The arguments it requires and the types of those arguments
• The type of result it returns
![Page 7: Chapter Seven Libraries and Interfaces](https://reader036.fdocuments.net/reader036/viewer/2022062518/5681432c550346895daf9bac/html5/thumbnails/7.jpg)
7
Function PrototypeFunction Prototype
Client interface implementer
How a function both sides agree on: How a function is used function prototype works
![Page 8: Chapter Seven Libraries and Interfaces](https://reader036.fdocuments.net/reader036/viewer/2022062518/5681432c550346895daf9bac/html5/thumbnails/8.jpg)
8
Header FilesHeader Files
• C uses header files to provide a concrete realization of an interface
• The header file for a library contains the prototypes for all the functions exported by the library as well as associated symbolic constants, macros, and types
![Page 9: Chapter Seven Libraries and Interfaces](https://reader036.fdocuments.net/reader036/viewer/2022062518/5681432c550346895daf9bac/html5/thumbnails/9.jpg)
9
A Graphics LibraryA Graphics Library
• When you start up the library, a graphics window is created on the screen and used as the drawing surface
• All drawing in the graphics window takes places on a conceptual grid of points
• Points are identified by specifying their position relative to the origin, which is the point at the lower left corner of the graphics window
![Page 10: Chapter Seven Libraries and Interfaces](https://reader036.fdocuments.net/reader036/viewer/2022062518/5681432c550346895daf9bac/html5/thumbnails/10.jpg)
10
Grid and CoordinatesGrid and Coordinates
(0, 0) (3, 0)
(0, 3) (3, 3)
x-axis
y-axis
![Page 11: Chapter Seven Libraries and Interfaces](https://reader036.fdocuments.net/reader036/viewer/2022062518/5681432c550346895daf9bac/html5/thumbnails/11.jpg)
11
CoordinatesCoordinates
• Absolute coordinates specify a point in the window by giving its coordinates with respect to the origin
• Relative coordinates specify a point in the window by giving its coordinates with respect to the last position specified
![Page 12: Chapter Seven Libraries and Interfaces](https://reader036.fdocuments.net/reader036/viewer/2022062518/5681432c550346895daf9bac/html5/thumbnails/12.jpg)
12
CoordinatesCoordinates
(0, 0) (3, 0)
(0, 3) (3, 3)
x-axis
y-axis(2, 1.5) (0.5, 0)
![Page 13: Chapter Seven Libraries and Interfaces](https://reader036.fdocuments.net/reader036/viewer/2022062518/5681432c550346895daf9bac/html5/thumbnails/13.jpg)
13
DrawingDrawing
• The mental model for the drawing process can be thought as a pen positioned and moved over the grid of points
• Each drawing process begins by positioning the pen to a particular position using the absolute coordinates
• You can then draw lines or arcs by moving the pen relative to its current position
![Page 14: Chapter Seven Libraries and Interfaces](https://reader036.fdocuments.net/reader036/viewer/2022062518/5681432c550346895daf9bac/html5/thumbnails/14.jpg)
14
DrawingDrawing
(0, 0) (3, 0)
(0, 3) (3, 3)
x-axis
y-axis(2, 1.5) (0.5, 0)
![Page 15: Chapter Seven Libraries and Interfaces](https://reader036.fdocuments.net/reader036/viewer/2022062518/5681432c550346895daf9bac/html5/thumbnails/15.jpg)
15
FunctionsFunctions• The library contains the following functions:
initGraphics() Initializes the librarymovePen(x, y) Moves the pendrawLine(dx, dy) Draws a linedrawArc(r, b, s) Draws a arcgetWindowWidth() Returns the widthgetWindowHeight() Returns the heightgetCurrentX() Returns x-coordinategetCurrentY() Returns y-coordinate
![Page 16: Chapter Seven Libraries and Interfaces](https://reader036.fdocuments.net/reader036/viewer/2022062518/5681432c550346895daf9bac/html5/thumbnails/16.jpg)
16
initGraphicsinitGraphics/* * Function: initGraphics * Usage: initGraphics(); * ---------------------------- * This function creates the graphics window on the * screen. The call to initGraphics must precede any * calls to other functions in the library and must also * precede any printf output. In most cases, the * initGraphics call is the first statement in the main. */
void initGraphics(void);
![Page 17: Chapter Seven Libraries and Interfaces](https://reader036.fdocuments.net/reader036/viewer/2022062518/5681432c550346895daf9bac/html5/thumbnails/17.jpg)
17
movePenmovePen/* * Function: movePen * Usage: movePen(x, y); * ---------------------------- * This function moves the current point to the * position (x, y), without drawing a line. The model * is that of the pen being lifted off the graphics * window surface and then move to its new position. */
void movePen(double x, double y);
![Page 18: Chapter Seven Libraries and Interfaces](https://reader036.fdocuments.net/reader036/viewer/2022062518/5681432c550346895daf9bac/html5/thumbnails/18.jpg)
18
drawLinedrawLine/* * Function: drawLine * Usage: drawLine(dx, dy); * ------------------------------- * This function draws a line extending from the * current point by moving the pen dx inches in the x * axies and dy inches in the y axies. The final position * becomes the new position. */
void drawLine(double dx, double dy);
![Page 19: Chapter Seven Libraries and Interfaces](https://reader036.fdocuments.net/reader036/viewer/2022062518/5681432c550346895daf9bac/html5/thumbnails/19.jpg)
19
An ExampleAn Example
main(){ initGraphics(); movePen(0.5, 0.5); drawLine(0.0, 1.0); drawLine(1.0, 0.0); drawLine(0.0, -1.0); drawLine(-1.0, 0.0);}
![Page 20: Chapter Seven Libraries and Interfaces](https://reader036.fdocuments.net/reader036/viewer/2022062518/5681432c550346895daf9bac/html5/thumbnails/20.jpg)
20
An ExampleAn Example
(0, 0) (3, 0)
(0, 3) (3, 3)
x-axis
y-axis
![Page 21: Chapter Seven Libraries and Interfaces](https://reader036.fdocuments.net/reader036/viewer/2022062518/5681432c550346895daf9bac/html5/thumbnails/21.jpg)
21
drawArcdrawArc/* * Function: drawArc * Usage: drawArc(r, b, s); * ----------------------------- * This function draws a circular arc, which always * begins at the current point. The arc has radius r, and * begins at the angle b and extends an angle of s. The * The angles are measured in degrees counterclockwise * from the 3 o’clock position along the x-axies. */
void drawArc(double r, double b, double s);
![Page 22: Chapter Seven Libraries and Interfaces](https://reader036.fdocuments.net/reader036/viewer/2022062518/5681432c550346895daf9bac/html5/thumbnails/22.jpg)
22
An ExampleAn Example
main(){ initGraphics(); movePen(1.5, 1.0); drawArc(0.5, 0, 360);}
![Page 23: Chapter Seven Libraries and Interfaces](https://reader036.fdocuments.net/reader036/viewer/2022062518/5681432c550346895daf9bac/html5/thumbnails/23.jpg)
23
An ExampleAn Example
(0, 0) (3, 0)
(0, 3) (3, 3)
x-axis
y-axis
![Page 24: Chapter Seven Libraries and Interfaces](https://reader036.fdocuments.net/reader036/viewer/2022062518/5681432c550346895daf9bac/html5/thumbnails/24.jpg)
24
getWindowWidth & HeightgetWindowWidth & Height/* * Functions: getWindowWidth, getWindowHeight * Usage: width = getWindowWidth(); * height = getWindowHeight(); * ---------------------------------------------------------- * These functions return the width and height of the * graphics window, in inches. */
double getWindowWidth(void);double getWindowHeight(void);
![Page 25: Chapter Seven Libraries and Interfaces](https://reader036.fdocuments.net/reader036/viewer/2022062518/5681432c550346895daf9bac/html5/thumbnails/25.jpg)
25
An ExampleAn Example
main(){ initGraphics(); movePen(getWindowWidth()/2, getWindowHeight()/2); drawArc(0.5, 0, 360);}
![Page 26: Chapter Seven Libraries and Interfaces](https://reader036.fdocuments.net/reader036/viewer/2022062518/5681432c550346895daf9bac/html5/thumbnails/26.jpg)
26
An ExampleAn Example
(0, 0) (3, 0)
(0, 3) (3, 3)
x-axis
y-axis
![Page 27: Chapter Seven Libraries and Interfaces](https://reader036.fdocuments.net/reader036/viewer/2022062518/5681432c550346895daf9bac/html5/thumbnails/27.jpg)
27
getCurrentX & YgetCurrentX & Y/* * Functions: getCurrentX, getCurrentY * Usage: x = getCurrentX(); * y = getCurrentY(); * --------------------------------------------- * These functions return the current x and y positions. */
double getCurrentX(void);double getCurrentY(void);
![Page 28: Chapter Seven Libraries and Interfaces](https://reader036.fdocuments.net/reader036/viewer/2022062518/5681432c550346895daf9bac/html5/thumbnails/28.jpg)
28
An ExampleAn Example
main(){ initGraphics(); movePen(1.5, 1.0); drawArc(0.5, 0, 360); movePen(getCurrentX() + 1, getCurrentY() + 1); drawArc(0.5, 0, 360);}
![Page 29: Chapter Seven Libraries and Interfaces](https://reader036.fdocuments.net/reader036/viewer/2022062518/5681432c550346895daf9bac/html5/thumbnails/29.jpg)
29
An ExampleAn Example
(0, 0) (3, 0)
(0, 3) (3, 3)
x-axis
y-axis
![Page 30: Chapter Seven Libraries and Interfaces](https://reader036.fdocuments.net/reader036/viewer/2022062518/5681432c550346895daf9bac/html5/thumbnails/30.jpg)
30
DocumentationDocumentation/* * File: graphics.h * ------------------- * This interface provides … */
/* * Overview * ------------ * This library provides … */
![Page 31: Chapter Seven Libraries and Interfaces](https://reader036.fdocuments.net/reader036/viewer/2022062518/5681432c550346895daf9bac/html5/thumbnails/31.jpg)
31
Build Your Own ToolsBuild Your Own Tools
• Based on the simple functions provided in a library, you can construct more sophisticated functions
• For example, you can construct functions that draw boxes and circles
• You can build a new library that consists of these new functions and provides a higher level of abstraction
![Page 32: Chapter Seven Libraries and Interfaces](https://reader036.fdocuments.net/reader036/viewer/2022062518/5681432c550346895daf9bac/html5/thumbnails/32.jpg)
32
New FunctionsNew Functions
• The following new functions are constructed
drawBox(x, y, w, h) Draw a boxdrawCenteredCircle(x, y, r) Draw a circleadjustPen(dx, dy) Move a pendrawLineTo(x, y) Draw a line
• These functions can be reused over and over again
![Page 33: Chapter Seven Libraries and Interfaces](https://reader036.fdocuments.net/reader036/viewer/2022062518/5681432c550346895daf9bac/html5/thumbnails/33.jpg)
33
drawBoxdrawBox/* * Function: drawBox * Usage: drawBox(x, y, width, height); * --------------------------------------------- * This function draws a rectangle of the given width * and height with its lower left corner at (x, y). */
void drawBox(double x, double y, double w, double h);
![Page 34: Chapter Seven Libraries and Interfaces](https://reader036.fdocuments.net/reader036/viewer/2022062518/5681432c550346895daf9bac/html5/thumbnails/34.jpg)
34
drawBoxdrawBoxvoid drawBox(double x, double y, double w, double h){ movePen(x, y); drawLine(0, h); drawLine(w, 0); drawLine(0, -h); drawLine(-w, 0);}
![Page 35: Chapter Seven Libraries and Interfaces](https://reader036.fdocuments.net/reader036/viewer/2022062518/5681432c550346895daf9bac/html5/thumbnails/35.jpg)
35
drawCenteredCircledrawCenteredCircle/* * Function: drawCenteredCircle * Usage: drawCenteredCircle(x, y, radius); * ------------------------------------------------- * This function draws a circle of the given radius * with its center at (x, y). */
void drawCenteredCircle(double x, double y, double radius);
![Page 36: Chapter Seven Libraries and Interfaces](https://reader036.fdocuments.net/reader036/viewer/2022062518/5681432c550346895daf9bac/html5/thumbnails/36.jpg)
36
drawCenteredCircledrawCenteredCircle
void drawCenteredCircle(double x, double y, double radius){ movePen(x + radius, y); drawArc(radius, 0, 360);}
![Page 37: Chapter Seven Libraries and Interfaces](https://reader036.fdocuments.net/reader036/viewer/2022062518/5681432c550346895daf9bac/html5/thumbnails/37.jpg)
37
adjustPenadjustPen/* * Function: adjustPen * Usage: adjustPen(dx, dy); * --------------------------------------------- * This function adjusts the current point by moving it dx * inches from its current x coordinate and dy inches from * its current y coordinate. No line is actually drawn. */
void adjustPen(double dx, double dy);
![Page 38: Chapter Seven Libraries and Interfaces](https://reader036.fdocuments.net/reader036/viewer/2022062518/5681432c550346895daf9bac/html5/thumbnails/38.jpg)
38
adjustPenadjustPen
void adjustPen(double dx, double dy){ movePen(getCurrentX() + dx, getCurrentY() + dy);}
![Page 39: Chapter Seven Libraries and Interfaces](https://reader036.fdocuments.net/reader036/viewer/2022062518/5681432c550346895daf9bac/html5/thumbnails/39.jpg)
39
drawLineTodrawLineTo/* * Function: drawLineTo * Usage: drawLineTo(x, y); * --------------------------------------------- * This function is like drawLine, except that it uses the * absolute coordinates of the endpoint rather than the * relative displacement from the current point. */
void drawLineTo(double x, double y);
![Page 40: Chapter Seven Libraries and Interfaces](https://reader036.fdocuments.net/reader036/viewer/2022062518/5681432c550346895daf9bac/html5/thumbnails/40.jpg)
40
drawLineTodrawLineTo
void drawLineTo(double x, double y){ drawLine(x - getCurrentX(), y - getCurrentY());}
![Page 41: Chapter Seven Libraries and Interfaces](https://reader036.fdocuments.net/reader036/viewer/2022062518/5681432c550346895daf9bac/html5/thumbnails/41.jpg)
41
Solving a Larger Solving a Larger ProblemProblem
• The best way to approach a large programming problem is to use the strategy of stepwise refinement to break the entire problem down into a set of simpler ones
![Page 42: Chapter Seven Libraries and Interfaces](https://reader036.fdocuments.net/reader036/viewer/2022062518/5681432c550346895daf9bac/html5/thumbnails/42.jpg)
42
Solving a Larger Solving a Larger ProblemProblem
![Page 43: Chapter Seven Libraries and Interfaces](https://reader036.fdocuments.net/reader036/viewer/2022062518/5681432c550346895daf9bac/html5/thumbnails/43.jpg)
43
Symbolic QuantitiesSymbolic Quantities
#define HouseHeight 2.0#define HouseWidth 3.0#define AtticHeight 0.7#define DoorHeight 0.7#define DoorWidth 0.4#define DoorKnobRadius 0.03#define DoorKnobInset 0.07#define PaneHeight 0.25#define PaneWidth 0.2#define FirstFloorWindows 0.3#define SecondFloorWindows 1.25
![Page 44: Chapter Seven Libraries and Interfaces](https://reader036.fdocuments.net/reader036/viewer/2022062518/5681432c550346895daf9bac/html5/thumbnails/44.jpg)
44
mainmainmain( ){ double cx, cy;
initGraphics( ); cx = getWindowWidth( ) / 2; cy = getWindowHeight( ) / 2; drawHouse(cx – HouseWidth/2, cy – (HouseHeight + AtticHeight)/2);}
![Page 45: Chapter Seven Libraries and Interfaces](https://reader036.fdocuments.net/reader036/viewer/2022062518/5681432c550346895daf9bac/html5/thumbnails/45.jpg)
45
drawHousedrawHousevoid drawHouse(double x, double y){ drawOutline(x, y); drawDoor(x+(HouseWidth-DoorWidth)/2, y); drawWindows(x, y);}
![Page 46: Chapter Seven Libraries and Interfaces](https://reader036.fdocuments.net/reader036/viewer/2022062518/5681432c550346895daf9bac/html5/thumbnails/46.jpg)
46
drawOutlinedrawOutlinevoid drawOutline(double x, double y){ drawBox(x, y, HouseWidth, HouseHeight); drawTriangle(x, y+ HouseHeight, HouseWidth, AtticHeight);}
![Page 47: Chapter Seven Libraries and Interfaces](https://reader036.fdocuments.net/reader036/viewer/2022062518/5681432c550346895daf9bac/html5/thumbnails/47.jpg)
47
drawTriangledrawTrianglevoid drawTriangle(double x, double y, double b, double h){ movePen(x, y); drawLine(b, 0); drawLine(-b / 2, h); drawLine(-b / 2, -h);}
![Page 48: Chapter Seven Libraries and Interfaces](https://reader036.fdocuments.net/reader036/viewer/2022062518/5681432c550346895daf9bac/html5/thumbnails/48.jpg)
48
drawDoordrawDoorvoid drawDoor(double x, double y){ drawBox(x, y, DoorWidth, DoorHeight); drawCenteredCircle(x + DoorWidth - DoorKnobInset, y + DoorHeight /2, DoorKnobRadius);}
![Page 49: Chapter Seven Libraries and Interfaces](https://reader036.fdocuments.net/reader036/viewer/2022062518/5681432c550346895daf9bac/html5/thumbnails/49.jpg)
49
drawWindowsdrawWindowsvoid drawWindows(double x, double y) { double xleft, xright; xleft = x + HouseWidth * 0.25; xright = x + HouseWidth * 0.75; drawGrid(xleft-PaneWidth*1.5, y+FirstFloorWindows, paneWidth, PaneHeight, 3, 2); drawGrid(xright-PaneWidth*1.5, y+FirstFloorWindows, paneWidth, PaneHeight, 3, 2); drawGrid(xleft-PaneWidth, y+SecondFloorWindows, paneWidth, PaneHeight, 2, 2); drawGrid(xright-PaneWidth, y+SecondFloorWindows, paneWidth, PaneHeight, 2, 2);}
![Page 50: Chapter Seven Libraries and Interfaces](https://reader036.fdocuments.net/reader036/viewer/2022062518/5681432c550346895daf9bac/html5/thumbnails/50.jpg)
50
drawGriddrawGridvoid drawGrid(double x, double y, double w, double h, double c, double r){ int i, j;
for (i = 0; i < c; i++) { for (j = 0; j < r; j++) { drawBox(x + i * w, y + j * h, w, h); } }}