CALIFORNIA STATE UNIVERSITY, NORTHRIDGE TRACING THE MINIMAL-SPANNING TREE ALGORITHMS A graduate project submitted in partial satisfaction of the requirements for the degree of Master of Science in Computer Science By Shi-Chung Liu August, 1984 The Graduate Project of Shi-Chung Liu is approved: Robert Ja Wong D. Alanen Bob Henderson, Chair California State University, Northridge ii ACKNOWLEDGEMENT I like to thank Dr. Bob Henderson for his continued assistance and advice, as well as Dr. Jack Alanen and Dr. Robert Wong for their encouragement. I also want to express my appreciation to my wife for her patience and encouragement during the course of this work. iii TABLE OF CONTENTS ACKNOWLEDGEMENT •••••••••••••••••••••••••••••• iii LIST OF TABLES • • • • • • • • • • • • • • • • • • • • • • • • • • • • . • • vi LIST OF FIGURES •••••••••••••••••••••••••••••• vii ABSTRACT • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • i Chapter 1 X INTRODUCTION 1.1 Motivation . . . . . . . . . . . . . . . . . . . . . . . 1 1.2 Project Description •••••••••••••• 4 Chapter 2 SIMULATION TYPE CAl --- SCAI 2.1 The Behavior of SCAI •••••••••.•.• 7 2.2 Requirements 9 2.3 Overall Design ••••••••••••••••••• 11 2.4 Graph Representation ••••••••••••• 21 2.5 Program Structure •••••••••••••••• 25 Chapter 3 EXTERNAL PROCEDURES --- TRACE.STUFF 3.1 Requirements ••••••••••••••••••••• 27 3.2 Functional Specification ••••••••• 29 3.3 Data Structures •••••••••••••••••• 34 iv 3.4 Modular Design ••••••••••••••••••• 38 3.5 Error Prevention ••••••••••••••••• 43 Chapter 4 CAl GENERATOR --- CAI.WRITER 4.1 Hardware/Software Specification 48 4.2 Input/Output Specification ••••••• 49 4.3 Internal Design •••••••••••••••••• 53 Chapter 5 GRAPH EDITOR --- GRAPH.GEN 5.1 Requirements ••••••••••••••••••••• 55 5.2 Overall Design ••••••••••••••••••• 56 Chapter 6 CONCLUSIONS 6.1 Discussion 65 6.2 Further Improvements ••••••••••••• 68 REFERENCES • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • 69 Appendix A EXAMPLES ••••••••••••••••••••••••• 70 Appendix B PROGRAM LISTING •••••••••••••••••• 88 v LIST OF TABLES Table 2.3.6 Summary of User-Interaction in a SCAI Table 3.5.1 20 Status Checking of TRACE.STUFF External Procedure Calls Table 6.1.1 46 The Restrictions of Apple Pascal ...................... vi 67 LIST OF FIGURES Fig. 1.1.1 Ideal Dialogue with Smart System...................... Fig. 1.2.1 Data Flow Diagram of CAI.WRITER ..••...........••• Fig. 2.3.1 Overall Design of SCAI Fig. 2.3.2 A Snapshot of Action 1 in Prim's MST Algorithm •••••••• Fig. 2.3.3 . . . . . . . . . . . . . . . ... . 12 13 15 A Snapshot of Action 3 in Algorithm ODDSQUARE Fig. 2.3.5 5 The Breakpoints in Algorithm ODDSQUARE Fig. 2.3.4 2 17 A Snapshot of Action 4 in Algorithm ODDSQUARE 19 Fig. 2.4.1 Representation of a Graph •••• 22 Fig. 2.4.2 Vertex and Edge Representation 23 Fig. 2.4.3 Representation of a Graph and Its Minimal Spanning Tree ••• Fig. 2.5.1 24 The Main Program of the SCAI for Algorithm ODDSQUARE ••••• Fig. 3.4.1 Modular Design of TRACE.STUFF Fig. 3.4.2 Data Flow Between Module 26 40 INTERFACE and the Internal Modules . . • . . • . . . • . . • • • . . . . . . vii 41 Fig. 4.2.1 Input/Output of CAI.WRITER 50 Fig. 5.2.1 The Main Menu of GRAPH.GEN 57 Fig. 5.2.2 Locating the Vertices 57 Fig. 5.2.3 Before Entering the 61 Fig. 5.2.4 Entering the Edges and Their Fig. 5.2.5 . . . .. .. .. . . . . .. . . . . . . . . . Saving the Graph Data . . . . -. . . Cost viii . . .... . . Edges .... 62 64 ABSTRACT TRACING THE MINIMAL-SPANNING TREE ALGORITHMS by Shi-Chung Liu Master of Science in Computer Science A Computer Aided Instruction <CAl) model is designed for presenting most algorithms; it contains the following main features: user selected breakpoints double screens presentation visible and reversible execution hardcopy of trace For saving the CAl package development time, a preprocessor called CAI.WRITER is implemented for helping the CAl programmer to generate the CAl package. By using this preprocessor, the CAl programmer only needs to ix implement the algorithm and to create the tutorial text files. The job of detailed coding in CAl is left to CAI.WRITER. Appendix A shows two examples of using CAI.WRITER. Program is written in UCSD Pascal on a 64K Apple //e two disk drives system, as lists on Appendix B. Chapter 6 discusses the advantages and limitation of using Apple system to develope a large program. X Chapter 1 INTRODUCTION 1.1 Motivation With the proliferation of computers, many CAl <computer aided instruction> packages were implemented for numerous fields of education. Informally speaking, a CAl package can be treated as a video tape that is connected to a remote control device. It is user-friendly, in that the user can re-play some part of the tape by means of 'rewind' and 'forward'. Some CAis are user-interactive, requiring the user to talk to the tape. This is such a nice device for learning something, that most people agree CAis are effective teaching tools. However, to implement a CAI package is rather time consuming. The future may provide an ideal Smart System for CAI programmers to produce any CAl package. in Fig. As shown 1.1.1, the CAl programmer gives the highest level of package specification to the Smart System, and a CAl package would be generated automatically. Software packages are developed based on people's needs. For instance, word processing packages are implemented for report writing, statistical analysis packages are 1 2 Programaer: Saart Systea: I need a CAl package. OK, for which field ? Accounting Biology Cheaistry Computer Science Prograa;er: Saart Systea: Coaputer Science. for which sub-field ? Algorithm Coapiler Data Base Systea Language Programmer: Smart System: Algorithm. Which algoritha 1 and where can I find it ? Programaer: Saart System: Pria's MST algoritha 1 on Disk I 10, File 'PRIM. TEXT'. Please specify the following: t1l Host Machine t2l Options beside the standard CAl option 1: option 2: .......... Prograamer: Smart Systea: Apple //e, standard CAl ACAl package for Prim's MST algoritha is generated, and on Disk I 10 1 Files 'PRIM.CAI.TEXT' and 'PRIM.CAI.CODE'. Fig. 1.1.1 Ideal Dialogue with Smart System 3 generated for data analysis, and data base management systems are invented for helping people to store and retrieve data. As we can see, time saving and accuracy guarantee are two primary motivations for developing these packages. If the Smart System does save CAl package development time and is reliable, then there is every reason why we want it. The Brown University Algorithm Simulator and Animator• <BALSA> employed some automatic processes for generating algorithm courseware. It is reported that each lecture required only five to seventeen hours of courseware development time. This points out that part of the Smart System can be done today. This project is a small subset of the ideal Smart System. It can help the CAl programmer to generate a CAl package for most algorithms. 4 1.2 Project Description The object of this project was to write a CAI package for Minimal-Spanning Tree <MST> algorithms. It would show the behavior of two different MST algorithms to students. However, in order to save on package development time and avoid redundent code, some external procedures called TRACE.STUFF were written first. These procedures are common to most of the algorithm CAI packages. With TRACE.STUFF it is easy to write a CAl package for an algorithm: STEP 1: implement the algorithm as a UCSD Pascal program STEP 2: include the declaration of TRACE.STUFF as part of the declaration of this program STEP 3: insert some appropriate external procedure calls of TRACE.STUFF into this program The second step is easy by employing the UCSD Pascal 'include directives·, but the third step is tedious to do. One interesting point is that most of the insertions are predictable. From this point came the idea of using a code generator to include the external procedures declaration, and to insert some 5 external procedure calls into the source code. The final version of the program contains two parts CAI.WRITER and GRAPH.GEN. They are written in UCSD Pascal on an 64K Apple lie two disk drives microcomputer system. CAI.WRITER is a preprocessor in front of SYSTEM.COMPILER. The input to CAI.WRITER is a UCSD Pascal source program <hereafter referred to as the 'algorithm'} and some tutorial TEXT files. The output is a source program in UCSD Pascal plus TRACE.STUFF, acting as a simulaion type CAl (hereafter referred to as SCAI> for this input algorithm. Fig. 1.2.1 shows a simplified data flow diagram for CAI.WRITER. After compiling SCAI, the user can see an algorithm in action as each ·statement is executed and view the animation of the tree development process. Snapshots of the tree development process are also output to a printer. -Tutorial TEXT files _ __. CAI.WRITER -Algorithm TEXT file 1--- CAl Package --- ----4~...__ _ _ _ _ _.J Fig. 1.2.1 Data Flow Diagram of CAI.WRITER 6 GRAPH.BEN, a user-interactive subprogram written in Apple BRAFMODE, allows the user to create graphic data easily, and to save data on diskette for later use. In the following chapters we will discuss the requirements, design, and implementation for the following four systems: <1> SCAI The output of CAI.WRITER, a package written in Pascal plus TRACE.STUFF, acts as a simulation type CAl for an algorithm. <2> TRACE.STUFF A set of external procedures specialized for program and graph tracing. (3) CAI.WRITER A system for generating SCAI. (4) GRAPH.BEN A system for generating graphic data. Chapter 2 SIMULATION TYPE CAl --- SCAI 2.1 The Behavior of SCAI SCAI <a Simulation type CAl> has three modes: introduction-mode, selection-mode, and tracing-mode. Tracing-mode is valid only when the selection-mode has been done. None of these three modes can be nested in another. Introduction-mode is used to describe the algorithm which SCAI will trace or to explain how to use this package. Selection-mode allows the user to select a set of user-breakpoints in the algorithm, and select a graphic data from a graph library. In the tracing-mode, SCAI simulates the execution of the algorithm. If a statement is an assignment statement and the left operand is of type integer, character, or real, then the value of that operand will be displayed. SCAI stops the trace and interacts with the user at each user-breakpoint. User can control the execution of the algorithm step by step in two directions: backward. forward and Some commamds are available for the user to 7 8 get rid of the user-interaction. 9 2.2 Requirements This section serves to document the requirements analysis and specification for the CAl model SCAI. The purpose of SCAI for an algorithm is to get the user involved in the execution of this algorithm. SCAI must have some way to show the algorithm execution to the user, and allow the user to control the execution. SCAI must be able to show as many variable values of each statement as possible, and also allow the user to transfer the algorithm trace to hard-copy at anytime. For SCAI of a graphic algorithm, SCAI must be able to show the user what the graph <or tree> looks like, in addition to the algorithm trace. Because SCAI is designed for students with minimal programming background, the display of a graph should be understood by at least 90 % of Computer Science majors. The graph trace and the algorithm trace must be shown simultaneously on the screen, or be switched within 1 second if they are on different pages. SCAI must have the flexibility to allow the user to input their own graphic data and specify any set of breakpoints. 10 The Pascal Operating System4 on the Apple II computers uses single letter commands. for Filer. For instance, 'F' stands Because SCAI is running under this environment and to make all the command formats consistent, SCAI should provide a set of single letter commands for the user-interaction. 11 2.3 Fig. Overall Design 2.3.1 shows the overall design of SCAI. The first column presents the action flow of SCAI, the second column specifies the action mode, and the third column indicates which screen mode is used for each action. TEXT means a 24-lines x SO-columns text display mode. GRAPH means a 192 x 280 pixels resolution graphic mode. Both action 1 <present the user algorithm> and action 5 (discussion> use INTRODUCTION mode. This mode provides the user with two introduction-control commands: command function prompt N N>ext continue the text file display Q Q>uit stop this action The contents of a text file are displayed double spaced on the bottom 20 lines of TEXT screen. Enabling the user to use these commands easily, the TEXT screen shows a prompt of the commands on the top. Fig. 2.3.2 is a snapshot of action 1 in the SCAI of Prim's MST algorithm~. Action 1 displays a text file containing information about the user algorithm or the user's manual. Action 5 displays a text file which discusses this algorithm's performance. It is the programmer's responsbility to create these text files. 12 Action Action Mode Screen Mode INTRODUCTION TEXT SELECTION GRAPH SELECTION TEXT TRACING TEXT & GRAPH INTRODUCTION TEXT (Start 1 : Present the algorithm ::z: Select a graph 3: Select a set of breakpoints 4: Simulate the algorithm ~= Discussion (End Fig. 2.3.1 Overall Design of SCAI 13 Introduction: N>ext G>uit Suppose 6 = IV,El is a connected graph in which each edge (u 1 vl in E has a cost c(u 1 vl attached to it • A spanning tree for 6 is a tree that connects all the vertices in V. The cost of a spanning tree is the sum of the edges in the tree. In this package we shall show how to find a minimal spanning tree for 6. A typical application for minimum-cost spanning trees occurs in the design of communications networ~s. The vertices of a graph represent cities and the edges possible coeeunications links between the cities. The cost associated with an edge represents the cost of selecting that link for the Fig. 2.3.2 A Snapshot of Action 1 in Prim's MST Algorithm 14 In action 2 (select a graph>, the user can select an existing graph. This graph is plotted to a printer and marked 'Original' if a printer is on line. For the user interested in their own graph, it must first be created by using the GRAPH.GEN system. A breakpoint of the algorithm is defined as a separator (semicolon) in its Pascal main program. Fig.2.3.3 shows all the breakpoints and their sequence numbers in a simple algorithm ODDSQUARE 9 , the sequence number of a breakpoint is an integer greater than 1. A set of commands called selection-control commands is available for action 3 command u (select a set of breakpoints>. prompt U>p They are: function move the pointer up to the last breakpoint of the algorithm D D>own move the pointer down to the next breakpoint of the algorithm s S>elect select the current pointer position as a user-breakpoint F F>ree reset the current pointer position free as a breakpoint p P>rint transfer the contents of TEXT 15 BEGIN (f HAIN fl WRITELN!'INPUT N, PLEASE. READLN!N ; 3 ·~~ S:=~ B := Q 5 ------- FORK := 1 TON DO BE6IN --------=:::::: breakpoints s := s + : b _// B:=B+~/ EN , B END. Fig. 2.3.3 The Breakpoints in Algorithm ODDS QUARE 16 screen to a printer if it is available Q Fig. Q)uit stop action 3 2.3.4 shows a snapshot of action 3 in the SCAI of ODDSQUARE algorithm. A prompt which contains the selection-control commands is shown on the bottom line. Because the Apple II+ has a 40 column TEXT screen, the frequently used commands are arranged to appear on the left hand side of the prompt line. The text of the algorithm is shown on the top 22 lines of TEXT screen. The pointer position is indicated by the screen console. The breakpoint is identified with a mark '(-------' when it is selected by the user. Some algorithms are too long to fit onto the screen, so when the pointer reaches the top or the bottom, the algorithm will be scrolled down or up. After action 2, a subset of the breakpoints selected by the user, will be known as the user-breakpoints. Action 4 (simulate the user algorithm> is the main action of SCAI. It simulates each statement throughout the user algorithm and interacts with the user if a user-breakpoint is reached. At all of the user-breakpoints, a set of trace-control commands is available to the user: 17 BESIN (t MAIN +.l WRITELN<~INPUT N, PLEASE. '}; READLN (tll; s := 0; B := 1; <------- FDR K := I TO NDO 3E5IN S : = 5 + B: B := B + 2; <-------1 END; END. Select Breakpoints: U>p D>own S>elert F>ree P>rint Q)uit Fig. 2.3.4 A Snapshot of Action Algorithm ODDSQUARE ~ in 18 command B prompt function B>ackup back up the algorithm one user-breakpoint N N>ext execute the algorithm one user-breakpoint T T>race show the TEXT screen G G>raph show the GRAPH screen p P>rint transfer the contents of the TEXT screen to a printer if it is available E E>nd execute the algorithm to end Q Q)uit execute the algorithm to end and stop action 4 Each of these commands acts independently. For example, the user need not enter 'T' before printing the TEXT screen. The screen format is the same as that from action 3, the bottom line is a prompt and the top 22 lines are the text of the algorithm and trace. Fig. 2.3.5 is a snapshot of action 4 in the SCAI of the ODDSQUARE algorithm. Table 2.3.6 is a summary of prompts for user-interaction commands in a SCAI. the prompt. The command is the first letter of 19 BESIN I* MAIN t) INPUT N, PLEASE •. l; READLNlNl; 9 ~RITEUH' s := 0; INPUT N, PLEASE. (! B : = 1: 1 FOR K := 1 TO NDO BEE IN B := B + 2; 3 I Erm; END. Tracing: N>ext B>ackup T>ext 6}raph E>nd P>rint Q}uit Fig. A Snapshot of Action 4 in Algorithm ODDSQUARE 20 Introductioncontrol Selectioncontrol Tracingcontrol N>e>:t U>p B>ackup Q)uit D>own N>ext S>elect T>race F>ree G>raph P>rint P>rint Q)uit E>nd Q)uit Table 2.3.6 Summary of User-Interaction in A SCAI 21 2.4 Graph Representation A graph (in mathematics> is a finite set of vertices some of which are connected by edges. A weighted graph is a graph in which each edge associates with a cost 3 • The term 'graph' in this report always means weighted graph unless otherwise specified. The best way to show a graph on a computer with a high resolution screen is by drawing the graph's diagram 10 • Fig. 2.4.1 shows an example of a graph's diagram. Each vertex or edge has two representations, state 1 and state 2 as shown in Fig. 2.4.2. One of the two states can be used to stand for the status of ·marked', 'traveled', 'selected' in the graphic algorithms. For instance, MST algorithms use state 2 for the status 'selected', so the original graph and the Minimal Spanning Tree can be shown on the same diagram. 2.4.3) <Fig. 22 ;_A __: .-6- : B l -- -~-~--------s-------- :· C ·:---------5·-------: D ·: :: E::-----------6----------:---F--: Fig. 2.4.1 Representation of a Graph 23 State 1 verte>: F State ..., .L. .r~~ •• ( F 'I ...._~f edge i : Fig. 2.4.2 I I 1. Vertex and Edge Representation 24 (p;--., •• ....__ .-. .-----·-.... .• .• .... -b (_ B )----- 5 -\ _,..Jf. •• ..···· T ·· ... 1. -->- ....• __1,_ \ >--···· ___ .6· ....r--- ---( c ) ....... -s-- ··· .. · t.._ D) .. --___.oe; / ·-' ::3 ··c- •...4 ', / 2I '\-../ iEl··········6··········(F) ~ rlg. 2.4.3 ~~ Representation of a Graph and Its Minimal Spanning Tree 25 2.5 Program Structure The main program of a SCAI contains two parts, the execution part and the simulation part. Fig. 2.5.1 shows the main program of the SCAI for a simple algorithm ODDSQUARE. Execution part performs the algorithm. The simulation part contains the procedure calls of TRACE.STUFF, and has nothing to do with the execution of the algorithm but only with the simulation. parts are merged together. These two Each statement in the original algorithm program becomes two statements, one for the execution and another for simulation. Control is switched between the execution and simulation parts, so the user will feel that execution and simulation are taking place simultaneously. Execution part is written by the CAl programmer, and the simulation part is generated by CAI.WRITER. Next chapter discusses what these simulation statements are and their functions. 26 BEGIN (~ MAIN f.) TRACE\2l; WRITELNl'INPUT N, PLEASE. TRACE{3i; READLN\Nl; s := t); IT RACE \4, Sl; B ! = 1; ITRAC£{5,B); FOR K := 1 TD N DD BEGIN S:=S+B; ITRACE(6,SI; B := B + 2; iTRACE\7,Bl; TRACE\Si; END. / the execution part Fig. 2.5.1 the simulation part The Main Program of the SCAI for Algorithm ODDSQUARE Chapter 3 EXTERNAL PROCEDURES TRACE. STUFF 3.1 Requirements Chapter two defined a CAl model for algorithms. Now the problem is turned to how to produce a CAI package automatically. To achieve this goal, it is necessary to have a set of common procedures <TRACE.STUFF> for most CAl packages. There are three general requirements for TRACE. STUFF: <1> TRACE.STUFF must be easy-to-use <2> TRACE.STUFF must contain all the user-interaction routines and utility procedures which are needed to form a CAI program (3) the structure and organization of TRACE.STUFF must simplify the code generator CAI.WRlTER Although other solutions are possible, the approach taken in this project is to use: * * * meaningful procedure names simple parameter passing pseudo concurrent processing of execution and simulation * pseudo reverse <backup> execution/simulation <using a backup buffer) 27 28 * user-interaction routines, pseudo concurrent processing, and pseudo reverse execution facilities built into appropriate procedures of TRACE.STUFF 29 3.2 Functional Specification TRACE.STUFF contains eleven external procedures that can be classified into the following three categories: Category A: procedures for data loading and data reset Category B: procedures for tracing the user program Category C: procedures to demonstrate the development of a tree Three procedures are in category A: INITTRACE <short for INITializing TRACE>, ENDTRACE and INTRODUCTION. description of each procedure follows: procedure INITTRACE < NAME : string > functions: to load the text file NAME of the algorithm and global data from diskette to set up the selection-mode to prompt user to select a graph data, set up the original graph, and plot this graph on paper to prompt the user to specify the breakpoints of the algorithm to get selection-control commands (see section 2.3) from the user and perform A short 30 them to initialize the tracing-mode procedure ENDTRACE functions: to give the user a last chance to back up the execution of the algorithm to reset the algorithm text file and breakpoints data procedure INTRODUCTION ( NAME : string ) functions: to set up the introduction-mode and display a text file pointed by NAME to get introduction-control commands <see section 2.3> from the user and perform them Procedures TRACE, ITRACE <short for Integer TRACE>, CTRACE (short for Character TRACE> and RTRACE <short for Real TRACE> belong to category B. They take care of the trace of the algorithm, the following describe their functions: procedure TRACE < NUMBER : integer > functions: to move the cursor to the NUMBER-th 31 breakpoint of algorithm on the TEXT screen to get trace-control commands <see section 2.3> from the user and perform them procedure !TRACE < NUMBER : integer; INT : integer > functions: to display the value of integer INT at the NUMBER-th breakpoint of the algorithm on the TEXT screen to get trace-control commands from the user and perform them procedure CTRACE < NUMBER : integer; CH : char ) functions: to display a character CH at the NUMBER-th breakpoint of the algorithm on the TEXT screen to get trace-control commands from the user and perform them procedure RTRACE < NUMBER : integer; REL : real > functions: to display the value of real number REL at the NUMBER-th breakpoint of the algorithm on the TEXT screen 32 to get trace-control commands from the user and perform them Four simple procedures are used to demonstrate the development of a minimal-spanning tree <MST>. By using these procedures the changes between two consecutive snapshots of a MST algorithm can be described. These procedures are VSELECT (short for Vertex SELECTed>, VRELEASE <short for Vertex RELEASEd>, ESELECT <short for Edge SELECT> and ERELEASE <short for Edge RELEASEd>. short description of each procedure follows: procedure VSELECT < Vl : char > functions: to draw a solid circle around vertex Vl on the graph screen to plot the current graph on paper procedure VRELEASE <Vl : char) functions: to draw a dotted circle around vertex Vl on the graph screen to plot the current graph on paper procedure ESELECT <Vl, V2 : char> functions: to draw a solid line to connect vertices A 33 Vl and V2 on the graph screen to plot the current graph on paper procedure ERELEASE <Vl, V2 : char) functions: to draw a dotted line to connect vertices Vl and V2 on the graph screen to plot the current graph on paper 34 3.3 Data Structures This section describes the main data types used in TRACE. STUFF. Because memory is the main concern in developing a large program on an Apple //e 2 , most data types were determined by memory concerns only. The data structure, purpose, and components of each data type are as follows: Data type name VLIST Data structure packed array of record Purpose ----------- describes all the vertices in a graph VLIST contains at most 13 records; the first one contains the total number of vertices, the remainder represent vertices. Each record of VLIST contains the three fields: field NAME meaning identification of this ~ CHAR vertex X x-coordinate on GRAPH INTEGER screen y y-coordinate on GRAPH INTEGER 35 screen Data type name ELIST Data structure packed array of record Purpose ----------- describes all the existed edges in a graph. ELIST contains at most 16 records. As in VLIST the first record is reserved for the total number of edges, the remainder represent edges. Each record of VLIST contains the three fields: field meaning ~ V1 name of the start vertex CHAR V2 name of the end vertex CHAR COST cost of this edge INTEGER / Data type name HALFPAGE Data structure packed array of string Purpose ----------- aid to scrolling the screen contents HALFPAGE contains 11 strings indexed from 1 to 11. In 36 the selection mode, it contains the algorithm text and user-breakpoint mark '<-------·. In the tracing mode it contains the algorithm text and trace. At any instant, there is at most one HALFPAGE in RAM. Data type name BRKTBL Data structure packed array of record Purpose ----------- describes the breakpoints in an algorithm. The size of BRKTBL is defined as a constant. The first record holds the total number of breakpoints and the label of last HALFPAGE. The remainder present breakpoints. Each record of BRKTBL contains the two tields: field PAGENO meaning a label used to access ~ STRING[!] a HALFPAGE LINENO the index of a statement in a HALFPAGE Data type name BUFFER Data structure packed array of record INTEGER 37 Purpose ----------- keeps all the information needed for backing up an algorithm. Each record contains the three fields: field TI meaning the breakpoint number ~ INTEGER indexed by an element of BRKTBL STRI the trace for this STRIN6[10J breakpoint STOP •true· if there will be interaction with the user, 'false· otherwise BOOLEAN 38 3.4 Modular Design Final version of TRACE.STUFF contains eight modules as 3.4.1. shown in Fig. One of them serves as the external interface for the user program; it is implemented as a set of procedures. Five of them serve as the internal modules, and are implemented as segment procedures. The remaining two are the data declaration and common procedures of internal modules, and are implemented as precompiled units. In this section we will discuss the purpose and implementation of each module. Module EASYSTUFF contains all the data declarations and common procedures for the internal modules except those containing file references. intrinsic unit. advantages: It is implemented as an Intrinsic units 4 have two main they are precompiled, and their code is never actually put into the codefile of the user program. So module EASYSTUFF saves both compile time and memory space. These are such great advantages that one might like to put everything into an intrinsic unit. However, intrinsic units do not allow any file references. Module FILESTUFF is implemented as a regular unit. It contains all the file declarations and common procedures which use file references. A regular unit 4 is precompiled but the entire unit's code is actually placed 39 into the user program's codefile. Because it will cause a problem of stack overflow so we did not implement other modules as regular units. FILESTUFF refers to some of the procedures of EASYSTUFF. This is shown in Fig. 3.4.1 where these two modules are drawn with a connecting line. The following four internal modules are implemented as segment procedures: INTRODUC. INITTRAC, GTRACE, PTRACE and The code and data for a segment procedure 4 are in memory only when this procedure is activated. By implementing modules in this way, memory limitation problems can be solved. Module INTERFACE transfers the external procedure call from the user program to an appropriate internal module. The reason for that is to do the code overlay and to make it easier for the user to use this language. Fig. shows a data flow diagram of module INTERFACE. 3.4.2 This module is implemented as a set of short procedures, each of which acts as an external procedure of Tracing Language. It transfers the control and data to an appropriate internal module <segment procedure>. TRACE.STUFF has 11 external procedures but Apple Pascal only allows 7 segment procedures in a program 4 , external procedures must share the same segment so some 40 External Interface Internal Modules Data Declaration i INITTRAC J- -H INTERFACE I t- r-1 EASYSTUFF I 1 _jGTRACE I I 1 I PTRACE _II ~ I NTRODUC Fig. 3.4.1 r- ~ t- HFILESTUFFj t-- Modular Design of TRACE.STUFF 41 ..,.,-~- t' ....... __-.•_-•_e_J''""Io.l INITTRACE ~ p\ II ~i3 -jiNITTRAC '\ 2 --'--'"- __.1_.-l, =NTRODUCTION I I r/ :,;;,e, I 1-iTlWDUC I I I I ·_ ..... ~r l-l•c:ACE I ----- / ../ ........... __ ./ INTERFACE -- / / 42 procedure. For instance, the external procedures VSELECT, VRELEASE, ESELECT and ERELEASE share the internal module GTRACE. 43 3.5 Errors prevention One requirement for TRACE.STUFF is reliability; it is accomplished by introducing variables to insure the appropriate environment status of external procedure calls. The type, function and purpose of each status variable are as follows: Status variable ------- PRINTER Type ------------------ BOOLEAN Function -------------- to indicate whether or not a printer is on line Purpose --------------- to prevent the dead lock problem when the program is requesting a printer value meaning TRUE a printer is available FALSE <default> no printer is available 44 Status variable ------- COMPUTER Type ------------------ CHARACTER Function -------------- to indicate which model of APPLE computer is being used Purpose --------------- to select the proper printing mode before transferring the TEXT screen to a printer 7 • 8 • This is necessary because for different machines the TEXT display is located in different parts of memory11 • 6 (addresses 1024 through 3071 --- page 1 and 2, in an Apple II+, and 1024 through 2047 in an Apple IIe>. meaning value • +. 'E' using APPLE II+ or equivalent (default> using APPLE //e or equivalent 45 Status variable name -- !NIT Type ------------------ BOOLEAN Function -------------- to indicate the status of the tracing mode Purpose --------------- to prevent invalid calls of ENDTRACE, TRACE, !TRACE, CTRACE and RTRACE <as shown in Table 3.5.1) value meaning TRUE in the tracing mode FALSE <default> not in tracing mode Status variable ------- SRAF Type ------------------ BOOLEAN Function -------------- to indicate whether or not a graph is loaded into RAM Purpose --------------- to prevent invalid calls of VSELECT, VRELEASE, ESELECT and ERELEASE (as shown in Table 3.5.1) 46 External Procedure Name Valid Condition Before Called INIT GRAF Condition After Called INIT GRAF INTRODUCTION F - F - INITTRACE - - T * END TRACE T - F - TRACE T - T - I TRACE T - T - RTRACE T - T - CTRACE T - T - VSELECT - T - T VRELEASE - T - T ESELECT - T - T ERELEASE - T - T - * don't care user's option Table 3.5.1 Status Checking of TRACE.STUFF External Procedure Calls 47 value meaning TRUE the data for graph is loaded FALSE (default> the data for graph is not loaded Chapter 4 CAl GENERATOR --- CAI.WRITER 4.1 Hardware/Software Specification CAI.WRITER was developed on an Apple //e microcomputer system, and was tested on the Apple II+ and the Apple 1/e. The hardware environment required for running CAI.WRITER is: • an Apple II microcomputer • two disk drives • a monitor • a dot matrix printer <option> The software needed for CAI.WRITER is a Pascal Operating System, in which the SYSTEM.LIBRARY file must contain the precompiled units TURTLEGRAPHICS, EASYSTUFF and FILESTUFF. The build-in unit TURTLEGRAPHICS provides utility procedures for programming in graphic mode. The units EASYSTUFF and FILESTUFF were implemented by this author. chapter 3. Their design and purpose were discussed in Appendix B shows the program listing of units EASYSTUFF and FILESTUFF. 48 49 4.2 Input/Output Specification Input to CAI.WRITER is a floppy diskette on disk drive 2 containing at least three files, SYSTEM.COMPILER, SYSTEM.LINKER, and an algorithm TEXT file. Algorithm TEXT is a Pascal program with some restrictions. First, it must contain no compiler options and no USES clause. Second, if a semicolon <;> is in a line of the algorithm TEXT, it must be at the end of this line. semicolon in a line is not allowed. of the program is marked by More than one Third, the body part I<* MAIN *>I. Output of CAI.WRITER contains the three input files and the following additional files (as shown in Fig. 4.2.1): (1) TEXT files of TRACE.STUFF declaration (2) DATA files of the graph photographs (3) DATA files of the graph data (4) DATA files of pieces of the algorithm (5) DATA file of the breakpoints of the algorithm (6) TEXT file of CAI The first three types of files are copied from the master diskette on disk driver 1. Other files are generated according to the algorithm TEXT file. Follows describe the files organization and their contents: <1> TEXT files of TRACE.STUFF declaration 50 SYSTEI'!. CGMP I LER SYSTEM. LINKER Algorithm • • • TL Declaration CAl Piece of Algorithm Breakpoints 6raoh Photograph 6raph Data • • • INPUT OUTPUT Fig. 4.2.1 Input/Output of CAI.WRITER 51 Each of these files contains part of the declaration of TRACE.STUFF. The contents of these TEXT files are shown in Appendix B. The design of each file is discussed in chapter 3. <2> DATA files of the graph photographs When one of these files is read into RAM starting at address 8192, it serves as the photograph of a graph on the Apple GRAPH screen. They are binary files with the following structure: RECORD CASE BOOLEAN OF TRUE : <INTPART : INTEGER>; FALSE : <PTRPART : AINTEGER>; END Such files have the advantage that the speed of writing is faster than with other structures. (3) DATA files of the graph data Each graph is saved on two files, file of ELIST and file of VLIST. Arrays ELIST and VLIST are discussed in section 3.3. (4) DATA files of pieces of the algorithm Structure of HALFPAGE is shown in section 3.3. (5) DATA file of the breakpoints of the algorithm 52 Structure of BRKTBL is discussed in section 3.3. (6) TEXT file of SCAI This is a Pascal TEXT file, the content will be discussed in section 4.3. CAI.WRITER will asks the user for the names of the algorithm file and tutorial files, this is the only occurrence of user interaction in CAI.WRITER. ' . 53 4.3 Internal Design CAI.WRITER contains two sub-modules PRODUCE and COPYFILE. They are implemented as segment procedures. Module COPYFILE generates the files of TRACE.STUFF declarations, graph photographs and graph data by copying some files from the master diskette. Module PRODUCE cuts the algorithm file into small pieces, so each piece can fit onto half of the screen. PRODUCE also saves the breakpoints in a file, and generates a CAI text file. The process of generating a CAI text file involves: <1> Inclusion of the compiler option (*$S+*>. This option puts the SYSTEM.COMPILER in the swapping mode, making more memory available for the compile process. (2) Inclusion of the USES clause USES TURTLEGRAPHICS, EASYSTUFF, FILESTUFF; <3> Insertion of the 'include directives' before the first procedure/function declaration of the algorithm: (*$I#5:INITTRACE.TEXT*> (*$I#5:GTRACE.TEXT*> (*$1#5:PTRACE.TEXT*> (*$i#5:INTERFACE.TEXT*> " 54 The compiler option (*$filename*) tells the SYSTEM.COMPILER to include another source file called 'filename· in the compilation. (4) Insertion of a call statement INITTRACE (' filename '); as the first statement of the main program. where 'filename· is the file name of the algorithm. <5> Insertion of one of the call statements TRACE ( ••••• >; ITRACE <••••• >; CTRACE <••••• >; RTRACE <••••• >; after each semicolon in the main program. The statement preceding the semicolon determines which call statement is inserted. For instance, 'ITRACE ( •••• >;'will be inserted if the statement before the breakpoint is an assignment statement for INTEGER. <6> Insertion of a call statement ENDTRACE; before the 'END. · clause. ' Chapter 5 GRAPH EDITOR --- GRAPH.GEN 5.1 Requirements GRAPH.GEN is designed to generate graphic data. As discussed in section 2.4, the graph will be represented as a diagram, so the first requirement is that GRAPH.GEN must be able to represent user input by means of drawing the diagram. As the data is input, part of the graph diagram can be seen by the user. Secondly, GRAPH.GEN must be able to detect allpossible input errors. Thirdly, to avoid redundant code the data structures of graph in GRAPH.GEN must be compatible with those in TRACE. STUFF. 55 56 5.2 Overall Design GRAPH.GEN is a menu driven system; its main menu contains four options: Fig. view, get, create and quit, as shown on 5.2.1. When 'V' or '6' is chosen, GRAPH.GEN displays all the old graph names to the user. This allows the user to select an old graph. The process of 'create a new graph' contains the following steps: (1) locate each vertex (2) enter the cost of each edge <3> enter a name for the new graph In step(1) the user does not need to specify the x-, y-coordinates of each vertex location, instead the arrow keys are used to move the vertex to any position on the screen. This mode saves data entry time and allows the user to make corrections easily. shows this advantage. Fig. 5.2.2 <a>-<e> Some instructions are displayed on top of the screen to inform the user of the proper action. In case of an input error, GRAPH.GEN will give the user a beep and not accept the input. For instance, the user has no way to move a vertex off the screen. 57 GRAPH EDITOP V VIEW LIBRARY GRAPH G GET A LIBRARY GRAPH N EDIT A NEW GRAPH (] G!U IT E!··.J TER Fig. 5.2.1 'T'OUR CHO ICE --- The Main Menu of GRAPH.GEN ENTER A VERTEX, CTRL-C FOR NONE Fig. (a) Locating the Vertices 58 LOCATE VERTEX S BY ARROW KEYS <SPACE> WHEN DONE __ , ·=- Fig. (b) Locating the Vertices LOCATE VERTEX S BY ARROW KEYS <SPACE> WHEN DONE __ c-. Fig. 5.2.2 (c) Locating the Vertices 59 LOCATE VERTEX H BY ARROW KEYS <SPAC~> WHEN DONE H D (d) Fig. Locating The Vertices LOCATE VERTEX H BY ARROW KEYS <SPACE> WHEN DONE c· ·-· D H Fig. (e) Locating the vertices 60 Before going into step<2>, the screen displays all vertices as shown on Fig. 5.2.3. Each vertex represented as a capital letter surrounded by a dotted circle. Step<2> accepts the names of the start vertex and the end vertex for each of the edges, and accepts an integer as the edge cost. The input is checked one character at a time, only a valid input will be accepted and displayed. Fig. entering the edges. 5.2.4 (a)-(d) shows the process of This is done by drawing one edge at a time, so the user can see each edge being drawn on the screen. In the last step, GRAPH.GEN asks the user to input a file name for the graph data, as shown on Fig. 5.2.5. 61 ENTER A VERTEX, CTRL-C FOR NONE B - il . . ..L.' . A H ~ ....... ~-.a: •.'""':""' ..:,: (a) Before Entering the Edges <EDGE)(COST)<SPACE> Et-·-iTER C TRL -C ~-~HEN DONE : B: : G: : D: :A: : H: ..... =-. !-"'!ll ~ ...., ~ .....J. ~- --· (b} Before Entering the Edges 62 Et-iTER <EDGE)(COST)<SPACE> C TPL -C l.-.fHEN DONE . . : B: ·-·-· .:::...1. : D: :A: : H: B~; Fig. = 5.2.4 Entering the Edges and Their Cost (a) <EDGE)<COST)<SPACE> CTRL-C WHEN DONE : B :······ : D: -4··· .... ·=- . ·-' . :A: : H: Fig. 5.2.4 .. ' \OJ Entering the Edges and Their Cost 63 <EIJ13E) <1::t:1E; T) <::=:PA•::E > Ei--iTER CTRL-C WHEN DONE : D: :A: Fio. 5.2.4 (c) Entering the Edges and Their Cost (EDGE)<COST)<SPACE) CTRL-C WHEN DONE .9 -·4:-·-· .. .. --·-··1..0 -· 2 . n ·-·· ..L.." - • :A: -i •-=< ...!...'"-' i-lg. 5.2.4 =~OJ ~ntering the Edges and ~ne1r ~os~ 64 - ·=· .. ·.,...:. ·- .. -· -· -4 ... · :.·::. _·-: .. . s;;, . ?i _.· .. _. • "'!""\ • . .i..J . . H. Fig. (a) Saving the Graph Data .-, .=:. <8 FCF NO .: . ... --- ..--. :=·H··.•t:.. ..! . -.· :A: . . : H: r~g. 5.2.5 (b) Savinq the Graph Data Chapter 6 CONCLUSION 6.1 Discussion Appendix A shows some CAis generated by CAI.WRITER. All of these CAis were tested by SYSTEM.COMPILER, and the subject codes did satisfy the requirements of SCAI <section 2.2>. Part of the output from these CAis is included in Appendix A. Helping the user to learn a subject is the goal of any CAI package. The SCAI model accomplishes this goal by tutorial and simulation. It has some important features: <1> user interaction scheme single letter user interaction commands prompts help the user to use the interaction commands easily error detection mechanism <2> selectable breakpoints any set of algorithm breakpoints can be selected by the user editor type operations allow the user to select the breakpoints easily 65 66 <3> double screens presentation two screens are set by two segments of physical memory, so the contents appearing on the monitor can be switched from one screen to another within 0.1 second 80 column screen to display the algorithm and variable trace 192 x 280 high resolution screen to display the diagram <4> visible and reversible execution algorithm is represented by PASCAL code, it can be understood by junior students the algorithm is automatic scrolling, so the user can see which statement is in execution the execution speed is controlled by the user the execution can be backed up if needed (5) hardcopy of trace • the contents of the two screens can be transferred to a printer Using the preprocessor CAI.WRITER to generate the CAI packages has some advantages: <1> saves time <2> reduces overhead of programming 67 (3) CAI package standardization (4) the space for storing the CAI package can be reduced since SCAI can be regenerated any time The package was developed under the environment of Apple UCSD Pascal, Table 6.1.1 lists some restrictions of Apple Pascal/2/. However, some nice features of UCSD Pascal are employed in solving certain problems: <1> 'Segment procedures' were used to overcome the object code limitation <1200) and the program size limitation. <2> 'Include directive' solved the problem of large text file in the editor. (3) 'Pre-compiled unit' saved the compiler time. Maximum size of Procedure or function Data chunk in a module Characters in a string Elements in a set Segments per program modules within a unit Table 6.1.1 Apple Pascal Limitation 1200 bytes of object P-code 16,383 words per procedure 255 characters 512 elements 7 user segments 127 procedures or functions The Restrictions of Apple Pascal 68 6.2 Further Improvements Several possible improvements should be made: 1> We can improve the CAI.WRITER to release some restriction on the input algorithm. For instance, by employing syntactic analysis, one can easily locate the breakpoints of the algorithm and the beginning of the main program. 2} One can extend TRACE.STUFF to include data types other than graph, such as stack, queue and array. 3} CAI has many different modes, but the CAis generated by CAI.WRITER contain only two modes, simulation and tutorial. One might like to modify TRACE.STUFF and CAI.WRITER, to enable CAI.WRITER to generate a more interesting CAI. REFERENCE 1. Brown, Marc H., 'Progress report: Brown university Instructional Computing Laboratory· ACH SIGCSE Bulletin 16, ~' 91-101 <February 1984> 2. Lewis, T.G., Pascal Prograaaing Tor the Apple, Prentice-Hall, 1981 3. Aho, Alfred V., Hopcroft, John E., Ullman, Jeffrey D., Data Structures and Algorithms, Addison-Wesley, 1983 4. Apple Pascal Reference Hanual, Apple Computer Inc., 1979 5. Apple lie Reference Hanual, Apple Computer Inc., 1982 6. Apple lie Owner's Hanual, Apple Computer Inc., 1983 7. Grappler+ Operators Hanual, Orange Micro Inc., 1983 8. RX Printer Operation Hanual, Epson Co., 1983 9. Motil J., Programming Principles, Ridgeview Publishing Co., 1981 10. Foley, J.D., Dam, A.Van, Fundaaentals of Interactive Computer Graphics, Addison-Wesley, 1982 69 Appendix A EXAMPLES ODDSQUARE Algorithm in Pascal ••.•••••••••••• 71 ODDSQUARE.CAI Program <Generated by CAl. WRITER> 72 Output of ODDSQUARE.CAI 73 Prim's MST Algorithm in Pascal 78 PRIM.CAI Program <Generated by CAI.WRITER> •• 80 PRIM.CAI Program <Modified Version> ••••••••• 82 Output of PRIM.CAI •••.•••••••••••••••••••••• 84 70 71 PROGRAM ODDSG; (ffffttttttttttttttttittifif) (t i) Algoritha ODDSQUARE (t S =N t N (t f) f) f) (f (ififfiftfffiiiffiiftifffiif) VAR N, 51 B, K: INTEGER; BEGIN (t HAIN t) WRITELN('INPUT N, PLEASE. 'l; READLN!Nl; s := 0; B : = 1; FOR K := 1 TO NDO BEGIN S := S + B; B := B + 2; END; END. 72 0 (t$SH) PROGRAM ODD5Q; USES TURTLEGRAPHICS, EASYSTUFF, FILESTUFF; (tftiffffffftffffffiffifffif) t) (f (t (t Algorithm ODDSQUARE S=NtN (I tl tl t) (ttftftttfttftttttftftfttfft) VAR N, 51 B, K: INTEGER; (t$li4:1NTRODUC.TEXTtl (t$li4:INITTRACE.TEXT+l (t$li4:GTRACE.TEXTtl (t$114:PTRACE.TEXTtl (t$li4:INTERFACE. TEXTtl INITTRACE!'t5:0DD5Q'l; BEGIN (t MAIN tl TRACE!2l; WRITELN! 'INPUT N, PLEASE. 'l; TRACE!3l; READLN!Nl; 5 := 0; ITRACEI4,5l; B := 1j ITRACE!5,Bl; FOR K := 1 TO NDO BEGIN 5 : = 5 + B; ITRACE!6,5J; B := B + 2; ITRACEU,Bl; END; TRACE!Bi; END TRACE; END. ' 73 BESIN (* MAIN tl wR!TEUH 'INPUT N, PLEASE. 'i; READLN (N); 3 s := 0; 0 B := 1; FOR K := 1 TO INPUT N, PLEASE. NDO BE SIN S : = S + B~ B : = B + 2; END; END. Tracing: N>ext B>ackup T>ext B>raph E>nd P>rint Q)uit 74 BEGIN (i MAIN f) WRITEUH' INPUT N, PLEASE. 'l; INPUT N, PLEASE. F:EADLN(Nl; 3 : : .= i"i• (1 ••• B := 1; 1 .... & ••• , FOR K := 1 TO N DO BEGIN S:=S+B; B := B + 2; END; END. Tracing: N>ext B>ackup T>ext G>raph E>nd P>rint i;)}uit 75 BE5IN (f ~AIN f) WRITELN('INPUT N, PLEASE. 'J; INPUT N, PLEASE. READLN{N); 3 0 B := !; 1 FuR K := 1 TO s := 0; N DO BEGIN S := S + B; B := B + 2; 3 END: END. Tracing: N>ext B>ackup Dext E>raph E>nd P>rint IDuit 76 4R1TEL~( INPUT : : = ·.·• 5 := B CPT'.~ O...iH.i• ~ND. N~ PLE~SE. ! ! iNPUT ~. PLEASE. 77 BEGIN MAIN \t f) WR!TELN( INPUT N, PLEASE. I F:EADLN!NJ: ): INPUT N. PLEASE. •' B ; :: 1~ ·UK \ := I !D N DD BEC!N S : = 3 + B: 9 B : = E + 2; -: tf-tV; Tracing: N>ext B>acKup T>ext G>rapn E>nd P>r1rt D>uit 78 PROGRAM PRIM; CONST KAXN = 12; INFINITY = 9999.0; TYPE COST= PACKED ARRAY [1 .. 11AXN 1 1.. 11AXNJ OF REAL; VAR TEMP : INTEGER; C : COST; LDWCDST : PACKED ARRAY [1 •• 11AXNJ OF REAL; CLOSEST : PACKED ARRAY [1 •• 11AXNl OF INTEGER; N, I, J 1 K: INTEGER; KIN : REAL; CH : CHAR; PROCEDURE ENTERDATA; ll BUILD THE COST MATRIX C l) VAR 111 1112 : INTEGER; BEGIN N:= V[OJ. Y; FOR I := 1 TO NDO FOR J := 1 TO NDO C[I,Jl := INFINITY-1; FOR I := 1 TO E[OJ.COST DO BEGIN 111 := SEARCH!E[ll.V11; 112 := SEARCH!E[Il.V2l; C[I11,M2J := E[ll.COST; C[l12 1111l := E[IJ.COST; END; END; (l ENTERDATA I) BEGIN (l MAIN ll ENTERDATA; FOR I := 2 TO NDO BEGIN LOWCOST£11 := C£1,11; CLOSEST[!] := 1; END; FOR I := 2 TO NDO BEGIN KIN := LOWCOST£Il; K := I; FOR J := 2 TO NDO IF lLOWCOST[J] < MINI AND <J <> Il THEN BEGIN KIN := LOWCOST[Jl; 79 K := J; END; LOWCOST£Kl := INFINITY; FOR J := 2 TO NDO IF IC£K,Jl < LOWCOST[Jll AND ILOWCOST[JJ < INFINITY> THEN BEGIN LOWCOST[Jl := C£K,Jl; CLOSEST£Jl := K; END; END; END. 80 (i$5ttl PROSRAH PRIM; USES TURTLE6RAPHICS 1 EASYSTUFF, FILESTUFF; CONST I'IAXN = 12; INFINITY = 9999.0; TYPE COST= PACKED ARRAY £1 •• 11AXN,1 •• 11AXNl OF REAL; VAR TEI1P : INTESER; C : COST; LONCOST: PACKED ARRAY [1 •• 11AXNl OF REAL; CLOSEST : PACKED ARRAY £1 •• 11AXNl OF INTESER; N, I, J, K: INTESER; 11IN : REAL; CH : CHAR; (t$134:INTRODUC.TEXTtl (t$li4:INITTRACE.TEXTtl (t$1#4:6TRACE.TEXTtl (t$li4:PTRACE.TEXTfl (t$1#4:INTERFACE.TEXTtl PROCEDURE ENTERDATA; (t BUILD THE COST MATRIX C f) VAR 111,112 : INTESER; BESIN N := V£0]. Y; FOR I := 1 TO NDO FOR J := 1 TO N DO C£I,Jl := INFINITY-1; FOR I := 1 TO E£0l.COST DO BESIN 111 := SEARCH<E£Il.Vll; 112 := SEARCH<E£IJ.V2l; C£111 11121 := E£Il.COST; C£M2,M1l := E£IJ.COST; END; END; (f ENTERDATA f) INITTRACEl'I5:PRIH'l; BESIN (t MAIN t) ENTERDATA; TRACE i2l; FOR I := 2 TO NDO BESIN LOWCOST£11 := C£1 1 11; RTRACE<3,LONCOST£Ill; CLOSEST£!] := 1; ITRACE<4,CLOSEST£Ill; 81 END; TRACE 15l; FOR I := 2 TO NDO BEGIN "IN := LOWCOST[ll; RTRACEib,"IMl; K := I; !TRACE <7 1Kl; FOR J := 2 TO N DO IF ILOWCOST[Jl < "INl AND IJ 0 I> THEM BEGIN "IN := LOWCOST[JJ; RTRACE IB,IHNl; K := J; ITRACEI!f,Kl; END; TRACE <10>; LOWCOST[Kl := INFINITY; RTRACE!ll,LOWCOST[Kll; FOR J := 2 TO NDO IF (C[K,Jl < LOWCOST[Jll AND ILOWCOST[J] < INFINITY> THEN BEGIN LOWCOST[Jl := C£K,Jl; RTRACE!12,LOWCOST£Jll; CLOSEST[Jl := K; ITRACEI13,CLOSEST[Jll; END; TRACE <14>; END; TRACE I 15); END TRACE; END. 82 (t$SH) PROGRAM PRIM; USES TURTLEGRAPHICS, EASYSTUFF, FILESTUFF; CONST HAUl = 12; INFINITY = 9999.0; TYPE COST= PACKED ARRAY [l •• ftAXN,l •• MAlftl Df REAL; VAR TEI'IP : INTEGER; C : COST; LOWCOST : PACKED ARRAY [1 •• MAXNl OF REAL; CLOSEST : PACKED ARRAY [l •• MAXNl OF INTEGER; N, I, J, K: INTEGER; I'IIN : REAL; CH : CHAR; !t$II4:INTRODUC.TEXTt) !t$II4:INITTRACE.TEXTtl !t$li4:STRACE.TEXTtl !t$II4:PTRACE.TEXTtl !t$li4:1NTERFACE.TEXTtl PROCEDURE ENTERDATA; !t BUILD THE COST "ATRIX C t) VAR l'll,M2 : INTEGER; BEGIN N := V[Ol. Y; FOR I := 1 TO N DO FOR J := 1 TO NDO C[I,Jl := INFINITY-1; FOR I := 1 TO E[OJ.COST DO BEGIN Ml := SEARCH!E[IJ.Vll; M2 := SEARCH!E£IJ.V2J; C[Ml,M2J := E£IJ.COST; C£M2,Mll := E£Il.COST; END; END; (t ENTERDATA tJ BEGIN (t MAIN ENTERDATA; INITTRACE!'I5:PRIM'J; t) TRACE!2l; FOR I := 2 TO NDO BEGIN LOWCOST[IJ := C£1,11; RTRACE!3,LOWCOST[IJJ; CLOSEST£!] := 1; ITRACE!4,CLOSEST£IJ>; 83 END; VSELECT!V[lJ.NAKEl; TRACE!Sl; FOR I := 2 TO NDO BESIN KIN := LOWCOST[lJ; RTRACE!b 1 KINl; K := I; FOR J := 2 TO NDO IF !LOWCOST[JJ < KlNl AND !J 0 ll THEN BESIN KIN := LOWCOST[JJ; RTRACE!8 1tliNl; K := J; ITRACE 19 1Kl; END; TRACE !10); ESELECT!V[KJ.NAtlE 1V[CLOSEST£Kll.NAKEl; VSELECT!V[Kl.NAtlEl; LOWCOST[Kl := INFINITY; RTRACE!ll,LOWCOST[KJl; FOR J := 2 TO NDO IF !C[K,JJ < LOWCOST[JJl AND !LOWCOST[Jl < INFINITY! THEN BESIN LOWCOST[Jl := C[K 1JJ; RTRACE!l2 1LOWCOST[Jll; CLOSEST[Jl := K; ITRACE!l3 1 CLOSE5T[Jll; END; TRACE I14l; END; TRACE I15l; END TRACE; END. 84 ; A __: - -- l -- _.-6 :_ -~--~---------s-------- :~ ·c ·:---------s--------==--~ -_: _____ 6-- =~.,. ..... ·c...;- __ _ -~.\_ ;i . _. . ~ . ._ : E :-----------6-·--------: F: Snapshot I 0 -- _.-6 --- Original _.--0-. _ i -----s ..__ :_-~.>-------s--------:- ·c:--------5·-------: =--~ _: . ··-. 'lo .. :- .... ·. _.· : . E . :----·····--6---·-··---: - F:. After selecting vertex A Snapshot I - .....6········7········s. : ---~-----------5--------: -_I_- - . :--------5·-----------~-. --~ ... ·... .. - ~-- ______ 6-- ... . _. · c -4___ _ ._... -·. :. E .. :-----------6----------: . F:.. Snapshot I 2 After selecting edge CA 9 85 _.··., .. ········6 . . . . C?·· . . A. s ........._.·· .. --~.. ---·······5·······-~v-······5·········=--~-- ,__~ .....6....... ' ·'4·... ;/ ' - _. · ·_... -·. : . E . :···········6·········-: . F -: Snapshot I 3 After selecting vertex C .... ·~··· .. rh. . : : : . : · : : D: ----..-{, :: ii:; :: ::::·.·: .. . -..... ....... . . ..... ~- ...6 "'o • "'4 !2 -·~- .._.:·· _ .... : .. E . :···········6·········-: . F .. : Snapshot I 4 After selecting edge FC (A- ···f G .·· ...........6······ . ·······s .......... ··. ....""' : .. .. . . B . :········5········ ~--·····5········. . D . . . -... . ...-- - \3 ...... . ····· ......6 .. -:;;!.... 4~_..... ::~~:·;··········6·········· ~ Snapshot I 5 After selecting vertex F • 86 _ . . G·········d ·········s ...... .n ·: :... :B .-~---~: ..... 5········ c)·······5······:·:: ·. . .. ······~..4 /"" ·-.....3 ·. I ~ ..6 ....~ ..····· ~:E:·-·········6··········® Snapshot I 6 After selecting edge DF .. · . 01 . · . . ···., .......... 6··· 0 6....... . . . . 4 . ·. B . .-········5········ . -.. \~ .·.. .~ ..·· ..·· . ····s ......... ~ c ·······5········CD) /.- 2 I ' >----- ~ :_E_:-··········6··········(~ Snapshot I 7 After selecting vertex D ,..-..... ..... ··J'T)·....... . ~ ·· ........... 6 :~.' 5 ........ ~~ 5~······5····-:;~ ....~ v 6 ..· · · · · · , 4 ··:·~......... z : ..~.:-··········6··········"~ Snapshot I 8 After selecting edge BC 87 ········.s .. .. 6 ___...-·· ~----0-······5··:::::·@ -~- ,4):6I . . _. · .6..... -"' ...·· : E :···········6·········· Snapshot I 9 F After selecting vertex B r;;~ .. ····-~·- ... 1 _..6··· ····s .. ~----0--····5·::::::-0 \ ~ \ ._........ E;.·· ' '4 .·· ~ ~ -,r/ : . E ~ :-··········6·······-··(F"'\ ..;_/ Snapshot I 10 .... 6········· After selecting edge EB C? . ·········s ... ~----0-······5···:::::@ \ :3 F ..··..... 6.· .· , .....4 -. . .>-<I I . ~.'l······ ····6 ······· ···( F I .....~ ........~ Snapshot I 11 After selecting vertex E Appendix B PROGRAM LISTING Program GENERATOR •••••••••••••••••••••••••• 89 Unit EASYSTUFF 91 Unit FILESTUFF 96 Unit GUNIT ••••••••••••••••••••••••••••••••• 101 . . . . .. .. . . . . . . . . . GET GRAPH . . . . . . . . . . . . . . . . . INTRODUC . . . . . . . . . . . . . . . . . INITTRACE . . . . . . . . . . . . . . . . GTRACE . . . . . . . . . . . . . . . . . . . PTRACE . . . . . . . . . . . . . . . . . . . Segment Procedure GET TABLE 102 Segment Procedure 106 Segment Procedure Segment Procedure Segment Procedure Segment Procedure 112 114 118 123 Module INTERFACE .•••••••••••••••••••••••••• 127 88 89 (f$SHl (fffltttfffffflfftffffttffftffftffftftttffffffflflf) (I (I (I (I (I (I (I f) ACAI package generator Apple 1/e Version 3.1 California State University, Northridge Karch 1984 By Shi-Chung Liu f) f) f) f) f) I) (fftllltffllfltttlffflffffflffffflfffffffffffffffff) PROGRAM GENERATOR; USES TURTLESRAPHICS, EASYSTUFF, SUNil, FILESTUFF; VAR PNAI'IE : 11ESG; (1----------------------------------------1) lt$li5:SETTABLE.TEXTtl (1----------------------------------------f) (t$II4:STRACE.TEXTtl (f----------------------------------------1) lt$li5:GETGRAPH.TEXT1l (1---------------------------------------------------------------------------f) BEGIN It GENERATOR tl ONLINE := TRUE; GRAF := TRUE; PAGE !OUTPUT> ; WRITEIATI5,10l, '11 AN U'l; WRITEIATI8,3l, 'P GENERATE TRACING PACKAGE' I; WRITEIATi10,3l, 'G GENERATE GRAPH'l; WRITEIATI20,0l, 'ENTER P or 6 FOR YOUR CHOICE 'l; CASE CHARREAD!['P', 'Q', 'G'll OF 'P' : BEGIN PAGE !OUTPUT> ; WRITEIATI5,10l, 'GENERATE TRACING PACKASE'l; WRITEIATIS,12l, 'WHERE IS YOUR ALGORITHM TEXT FILE ?'l; READLNIPNAKEl; 6ETTABLEIPNAI1El; END; 90 '6' : 6ET6RAPH; END; END. (I GENERATOR I) (t--------------------------------------------------------------------------tl 91 (t$S+tl UNIT EASYSTUFF; INTRINSIC CODE 25 DATA 2b; INTERFACE USES TURTLESRAPHICS; CONST SIZETABLE = 29; SIZEBUFFER = 29; TYPE CHARSET = SET OF CHAR; HESS = STRINS£801; VERTEX = RECORD NAIIE : CHAR; X,Y: INTEGER; END; VLIST =PACKED ARRAY£0 •• 121 OF VERTEX; EDSE = RECORD V1 1 V2 : CHAR; COST : INTEGER; END; ELIST =PACKED ARRAY£0 .• 151 OF EDSE; BRK = RECORD PASENO : STRINS£11; LINENO : INTEGER; END; BRKTBL =PACKED ARRAY [O •• SIZETABLEJ OF BRK; HALFPASE =PACKED ARRAY£1 .• 111 OF IIESS; SHORT = STRINS£101; BUF = RECORD Tl : INTEGER; STRI : SHORT; STOP : BOOLEAN; END; BUFR =PACKED ARRAY£0 •• SIZEBUFFERl OF BUF; VAR PROMPT : INTEGER; BUFFER : BUFR; PASEID,LAST,TOP,BOTTOII : STRINS£11; NONSTOP, ENDINS : BOOLEAN; HEAD,TAIL,CURRENT : INTEGER; NAI'IE : HESS; V: VLIST; E : ELIST; ONLINE, SRAF, INIT : BOOLEAN; COIIPUTER : CHAR; PROCEDURE I'IESSA6E(X 1 92 Y : INTEGER; L : I'IESSl; (I WRITE ASTRING L START AT IX, Y> OF 6RAFI'IODE SCREEN tl PROCEDURE PLOTCHARIX,Y : INTEGER; CH : CHARI; PROCEDURE BELL; FUNCTION ATIX,Y:INTEGERl:CHAR; FUNCTION CLEAR:CHAR; FUNCTION SUBCLEAR:CHAR; FUNCTION CHARREADIVS:CHARSETl:CHAR; FUNCTION SEARCHICH : CHARI : INTEGER; FUNCTION EINDEXIC1,C2 : CHARl:INTEGER; PROCEDURE PI'IT1; PROCEDURE INCIVAR J : INTEGER!; PROCEDURE DECIVAR J : INTEGER!; IHPLEHENTATION (1----------------------------------------f) PROCEDURE BELL; BEGIN WRITE ICHR 17l l; END; (1----------------------------------------f) FUNCTION AT; BEGIN GOTOXYIY,Xl; AT := CHRIOl; END; (1----------------------------------------i) FUNCTION CLEAR; BEGIN CLEAR := CHR126l; END; 93 (i----------------------------------------1) FUNCTION SUBCLEAR; BEGIN SUBCLEAR := CHRI29l; END; (1----------------------------------------f) FUNCTION CHARREAD; VAR C : CHAR; BEGIN READIKEYBOARD,Cl; MHILE NOT (C IN VSl DO BEGIN BELL; READIKEYBOARD,Cl; END; CHARREAD := C; END; (1----------------------------------------f) PROCEDURE HESSASE; BEGIN PENCOLOR!NONE>; HOVETO (X I Yl j PENCOLORIWHITE>; WSTRING!Ll; END; (1----------------------------------------f) PROCEDURE PLOTCHAR; BEGIN PENCOLORINONE>; HOVETOtx,Yl; PENCOLORIWHITEl; WCHARICHl; END; (1----------------------------------------f) FUNCTION SEARCH; VAR I : INTEGER; BEGIN I : = 1; MHILE IJ[IJ.NAHE () CH DO I := I + 1; SEARCH := I; END; 94 (1----------------------------------------t) FUNCTION EINDEX; VAR I : INTESER; FOUND : BOOLEAN; BESIN FOUND := FALSE; I := 1; WHILE II <= E[OJ.COSTl AND INOT FOUNDl DO IF IE[Il.Vl = Cll AND IE£Il.V2 = C2l THEN FOUND := TRUE ELSE I:=l+l; IF FOUND THEN EINDEX := I ELSE EINDEX := 0; END; (1----------------------------------------t) PROCEDURE P11Tl; BESIN WRITEIATI23 10l 1 CLEAR>; IF PROMPT = 1 THEN WRITEI'Select Breakpoints: U>p D>own S>elect F>ree P>rint Q}uit'l ELSE WRITEI'Tracing: N>ext B>ackup T>ext S>raph E>nd P>rint Q)uit'l; END; (1----------------------------------------1) PROCEDURE INC; BE SIN IF J = SIZEBUFFER THEN J := 0 ELSE J:=J+l; END; (1----------------------------------------t) PROCEDURE DEC; BESIN IF J = 0 THEN J : = SIZEBUFFER ELSE J:=J-1; END; (t----------------------------------------1) 95 BESIN NONSTOP := FALSE; ENDINS := FALSE; COMPUTER := 'E'; ONLINE := FALSE; SRAF := FALSE; HHT : = FALSE; END. 96 (I$SHl UNIT FILESTUFF; INTERFACE USES TURTLESRAPHICS, EASYSTUFF; TYPE HFPS = FILE OF HALFPASE; VAR TABLE : FILE OF BRKTBL; H,Hl : HFPG; Z9X22PRINT : TEXT; Z9X22F : FILE; FV : FILE OF VLIST; FE : FILE OF ELIST; SNAP : INTEGER; PROCEDURE PLOT; (I PLOT THE SRAFHODE SCREEN TO THE PRINTER t) PROCEDURE NOTEE <A : HESS; Cl,C2 : CHAR>; PROCEDURE PRINTING; PROCEDURE BLOADIFNAHE:CHARl; It SET THE PICTURE FROM FNAHE PROCEDURE BSAVEIFNAHE:CHARl; (t SAVE THE SRAFHODE SCREEN ON FNAHE tl PROCEDURE GETVEIA : CHAR>; PROCEDURE PRINTIA:HESS; LOC:INTESERl; PROCEDURE CHECKHEHORYII:INTESER>; PROCEDURE CHECKBOTTOHII:INTESER>; PROCEDURE CHECKTOP!l:lNTESERl; IMPLEMENTATION TYPE DISKBLOCK = RECORD CASE BOOLEAN OF TRUE: IINTPART:INTESER>; FALSE: IPTRPART:AINTESERl; END; VAR I) 97 DB : DISKBLOCK; IO : INTEGER; (1----------------------------------------f} PROCEDURE PLOT; BEGIN IF ONLINE THEN BEGIN "ESSASEIO,lBO,' 'l; REWRITEIZ9X22PRINT, 'PRINTER: 'l; WRITELNIZ9X22PRINT,CHRI25) 1 'G' 1 'E'l; CLOSEIZ9X22PRINT>; SNAP := SNAP + 1; nESSAGEIO,lBO, 'Coamand: N B T G E P G'l; END; END; (1----------------------------------------f) PROCEDURE NOTEE; BEGIN IF ONLINE THEN BEGIN CLOSE IOUTPUTl ; REWRITEIOUTPUT, 'PRINTER: 'l; WRITE I' WRITELNISNAP:3,' ',A,Cl,C2l; WRITELN; CLOSEIOUTPUT>; REWRITEIOUTPUT, 'CONSOLE:'); END; END; Snapshot I' l ; (1----------------------------------------1) PROCEDURE PRINTING; BEGIN IF ONLINE THEN BEGIN REWRITEIZ9X22PRINT, 'PRINTER: 'l; IF CO"PUTER = 'E' THEN WRITELNIZ9X22PRINT 1 CHRI25) 1 '30L' 1CHRI25l, 'A' 1 CHRI25l, '8' 1 'S') ELSE WRITELNIZ9X22PRINT,CHRt25) 1 '2', 'S'l; CLOSEtZ9X22PRINTl; END; END; (t----------------------------------------1) PROCEDURE BLOAD; VAR 98 F : STRINSUl; BEGIN DB.INTPART := 8192; F := • '; F£1 l := FNAI1E; RESETIZ9X22F,CONCATt'I5:6RAPH' ,F, '.FOTO'll; IO : = BLOCKREAD IZ9X22F ,DB. PTRPARY, lbl; CLOSEIZ9X22F 1 LOCKl; END; (t----------------------------------------1) PROCEDURE BSAVE; VAR F : STRINS£11; BEGIN DB.INTPART := 8192; .- F ·- ••• ' F[ ll : = FNAI1E; REWRITEIZ9X22F,CONCATI'I5:6RAPH' ,F, '.FOTO'll; 10 := BLOCKWRITEIZ9X22F,DB.PTRPARTA 1 1bl; CLOSEIZ9X22F,LOCKl; END; (t----------------------------------------1) PROCEDURE SETVE; VAR I : INTEGER; F : STRINS[ll; BESIN !t SETVE F ·-- •••' FUl := A; . tl RESETIFV,CONCATI'IS:SRAPH' ,F,' .VLIST'll; SETIFVl; FOR I := 0 TO FVA(OJ.X DO BEGIN V£Il.NAI1E := FVA£Il.NAI1E; V[IJ.X := FVA[Jl.X; V[Il.Y := FVA[ll.Y; END; ClOSEIFV,LOCKl; RESETIFE,CONCATt'IS:SRAPH' ,F, '.ELIST'll; SETtFEl; FOR I := 0 TO FEA[Ol.COST DO BESIN E£Il.V1 := FEA[Jl.Vl; E£Il.V2 := FEA£Il.V2; E£Il.COST := FEA[Il.COST; END; 99 CLOSEIFE,LOCKl; END; (I GETVE I) (1----------------------------------------1} PROCEDURE PRINT; VAR I : INTEGER; BEGIN RESET!Hl,Al; GET!Hll; IF LOC = 3 THEN BEGIN NRITEIATI23,0l,CLEAR,ATI22,0l,CLEARl; FOR I := 1 TO 11 DO NRITELNIH1A[lll; WRITELN; END ELSE FOR I := 0 TO 10 DO NRITEIAT<ILOC-ll1ll+I 10l 1 CLEAR,H1A(l+1Jl; CLOSEIHl,LOCKl; PHTl; END; (t----------------------------------------1) PROCEDURE CHECKHEHORY; BEGIN IF PASEID <> TABLE'·[Il.PAGENO THEN BEGIN CLOSEIH,LOCKl; RENRITElH,CONCATINAHE, '. ',PAGEIDll; PUT!Hl; CLOSEIH,LOCKl; RESETIH,CONCATINAHE, ', ',TABLEA[JJ.PA6ENOll; GET!Hl; PAGEID := TABLEA[Jl.PAGENO; END; END; (t----------------------------------------1) PROCEDURE CHECKBOTTOH; BEGIN WHILE TABLEA£Il.PASENO > BOTTOH DO BEGIN TOP := BOTT011; BOTTOH[1l := SUCCITOP[1Jl; PRINTICONCATINAHE,'.' 1BOTTOHl 13l; END; END; 100 (f----------------------------------------1) PROCEDURE CHECKTOP; BEGIN IF TABLEh[lJ.PAGENO < TOP THEN BEGIN TOP := TABLEh£IJ.PASENO; BOTTOn£1) := SUCC!TOP£1Jl; PRINT!CONCAT!NAnE,'. ',BOTTOnl,2l; PRINT!CONCAT!NAME, ·• ',TOPl,1l; END; END; {f----------------------------------------1) BEGIN SNAP := -1; END. 101 (t$SHI UNIT SUNIT; INTRINSIC CODE 27; INTERFACE USES TURTLESRAPHICS, EASYSTUFF; FUNCTION HELPIVNAHE : HESSI : CHAR; PROCEDURE QUESTIONILOC : INTESER; NORD : HESS; VAR NAME : HESSI; IHPLE11ENTATION (1----------------------------------------t) FUNCTION HELP; BEGIN WRITEIATI0 101 1 CLEAR 1 'Variable ',VNAHE,. is type of'l; WRITE IATll 1 51 1CLEAR 1 'Dnteger ',AT 12,51 ,CLEAR 1 'R)eal 'l; NRITEIATI3 15l,CLEAR,'C)har' 1 ATI4 15l ,CLEAR, 'O>thers'l; WRITEIATI5 1 0l ,CLEAR,'Help 1e 1 please ! 'I; WRITEIATtb,Ol,CLEAR, '--------------------------------------------------'1; WRITE!'-------------------· 1ATI5,1811; HELP:= CHARREAD(['l' ,'R' I '0' I 'C'Jl; WRITEt'Thank you. 'I; END; It HELP t) (1----------------------------------------f) PROCEDURE QUESTION; BE6IN WRITEIATILOC,OI, 'DO YOU WANT SAY SOHETHINS IN THE 'I; WRITEIWORD,' OF THE PACKASE ?'I; WRITEIATILDC+l 1 101 1 'Y/N'l; IF CHARREADt£'Y' 1 'N'JI = ·y· THEN BESIN WRITEIATILOC+2 1 5l, 'WHERE IS THE TEXT FILE ?'l; READLNINAHEI; END ELSE NAME := 'UU'; END; END; (t----------------------------------------1) BE6Hl END. 102 (t IS:SETTABLE.TEXT t) (1-------------------------------------------------------------------------i) SESKENT PROCEDURE SETTABLEINAKE : KESSl; VAR L : STRINS£11; I,J,K 1N: INTEGER; Y: KESS; CAl : TEXT; TAB : KESS; INTROl, INTR02 : KESS; {i----------------------------------------1) SEGMENT PROCEDURE DECLPART; VAR FOUND : BOOLEAN; (1--------------------------i) PROCEDURE INCLUDE; BEGIN WRITELNICAI,CONCATITAB,' l1$li4:1NTRODUC.TEXT1l'll; WRITELNICAI,CONCAT!TAB, '(1$II4:INITTRACE.TEXT1l'll; WRITELNICAI,CONCATITAB, '(1$li4:6TRACE.TEXTtl'll; WRITELNICAI,CONCATITAB, '(t$II4:PTRACE.TEXTtl'll; WRITELN!CAI,CONCATITAB, '(1$II4:1NTERFACE.TEXT1l 'll; END; (1--------------------------1) BEGIN FOUND := FALSE; WRITELNICAI,CONCAT!TAB, '(t$S+Il 'll; READLNIZ9X22PRINT,Yl; WHILE <NOT EOFIZ9X22PRINTll AND <POSI'(I MAIN t)' ,Yl =Ol AND !POSI'!tMAINtl' ,Yl = Ol DO BEGIN WRITELNIATI23 10l 1 Yl; IF IIPOS!'PROCEDURE ' 1 Yl > Ol DR IPDSI'FUNCTIDN ' 1 Yl > Oll AND !NOT FOUND) THEN BEGIN FOUND := TRUE; INCLUDE; END; WRITELN !CAl, Yl; IF I POSI'PROSRAM ',Yl > 0 l AND I POS!'; ' 1 Yl > 0 l THEN WRITELNICAI,CONCATITAB, 'USES TURTLESRAPHICS, EASYSTUFF 1 FILESTUFF; 'll; 103 READLN<Z9X22PRINT,Yl; END; IF NOT FOUND THEN INCLUDE; END; (I DECLPART t) (1----------------------------------------f) SEGMENT PROCEDURE TLCOPY!Y:HESS; I:INTEGERJ; VAR J,K : INTEGER; VNAHE : HESS; CH : CHAR; INT,TL : HESS; DONE : BOOLEAN; BEGIN DONE := FALSE; J := POS!':=' ,Y>; STR!I,INT>; IF J > 0 THEN BE SIN VNAHE := COPY!Y,l,J-1>; NHILE VNAHE[LENSTH!VNAHEJJ = ' ' DO VNAHE := COPY!VNAHE,l,LENSTH!VNAHEJ-ll; NHILE POSt' ',VNAHEJ > 0 DO VNAHE := COPY!VNAHE 1 21 LENSTH!VNAHEJ-ll; CH := HELP!VNAHEJ; IF CH <> '0' THEN BEGIN TL := ' '; TL£11 : = CH; TL := CONCAT!TL, 'TRACE(' ,INT,' 1 ' ,VNAHE, 'J;'l; DONE := TRUE; END; END; IF NOT DONE THEN TL := CONCAT<'TRACE!' 1 INT,'l;'l; NRITELN!CAI,CONCAT!TAB,TL>J; END; (I TLCOPY I) (1----------------------------------------t) SEGMENT PROCEDURE HAINPART; VAR Y2 1 Yl : HESS; MAIN, HAINl : BOOLEAN; BEGIN Yl := Y; IF INTROl <> '$$$$' THEN Y2 := CONCAT< 'INTRODUCTION (' " 1 INTROl," 'l; 'l ELSE 104 Y2 := "; Y:= CONCAT IY, HtB, Y2 1 'IN ITT RACE I'" ,NAI'IE," 'I;' I; HAIN := TRUE; WRITELNIATI23,0I,Yll; WHILE NOT EOF!Z9X22PRINTI DO BEGIN I : = 1; J:=J+l; .- ' L ·- I '• L£11 := CHRIJ+b41; REWRITE!H,CONCAT!NAI'IE, '. ',Lll; WHILE !NOT EOF!Z9X22PRINTll AND II <= 111 DO BEGIN IF !'lAIN THEN BEGIN !'lAIN := FALSE; H''[IJ := Yl; END ELSE H"[IJ := Y; IF POSI';',Yl > 0 THEN BEGIN TABLE'·[Nl. PAGENO := L; TABLE"£Nl.LINENO := I; IF ! POSI'WRITE' ,YI > 0 I OR I POSI'READ' 1 YI > 0 l THEN BEGIN TLCOPYIY,Nl; WRITELN !CAl I Yl; END ELSE BEGIN WRITELN !CAl, Yl; IF POS!'INITTRACE!' 1 ¥1 = 0 THEN TLCOPY!Y,NI; END; N := N + 1; END ELSE BEGIN WRITELN ICAI 1 Yl; I'IAINl := FALSE; END; I := I + 1; READLNIZ9X22PRINT,Yl; WRITELN<ATI23 101 1 Yl; IF POSI'END. ' 1Yl > 0 THEN BEGIN IF INTR02 <> '$$$$' THEN WRITELNICAI,CONCAT!TAB, 'INTRODUCTION{''' 1 INTR02 1 '''l; 'II; WRITELNICAI,CONCATITAB, 'ENDTRACE; 'II; END; END; 105 FOR K:= I TO 11 DO w-m := • ·; PUT!Hl; CLOSEIH,LOCKl; END; END; (t "AINPART t) (1----------------------------------------f) BEGIN (I GETTABLE t) TAB := ' '; PAGE <OUTPUll; QUESTIONI7, 'FRONT' ,INTROll; QUESTION!13 1 'END' ,INTR02l; REWRITEITABLE 1CONCATINAHE, ',TABLE'll; RESET!l9X22PRINT,CONCATINA"E, '.TEXT'll; REWRITEICAI,CDNCATINAHE, '.CAI.TEXT'll; y := ' DECLPART; J := 0; N := 1; MINPART; CLOSEil9X22PRINT,LOCKl; WRITELNICAI,' 'l; CLOSEICAI,LOCK>; TABLEA[Ol.PAGENO := L; TABLEA[Ol.LINENO := N- 1; PUT !TABLE); CLOSEITABLE,LOCKl; END; (t GETTABLE t) (1-------------------------------------------------------------------------l) 106 (I 15:6ET6RAPH.TEXT I) (t-------------------------------------------------------------------------1) SEGMENT PROCEDURE 6ET6RAPH; VAR VS : CHARSET; CH : CHAR; PICTURE : CHAR; (t----------------------------------------1) PROCEDURE PROHPTl; BEGIN HESSA6E!10,1BO, 'ENTER AVERTEX, CTRL-C FOR NONE HESSA6E!10,170,' END; '); 'l; (1----------------------------------------1) PROCEDURE PROHPT2 !CH:CHARl; BEGIN MESSASE!lO,lBO, 'LOCATE VERTEX 'l; WCHAR!CHl; WSTRINS!' BY ARROW KEYS 'l; MESSAGE!10,170,' <SPACE> WHEN DONE END; 'l; (t----------------------------------------1) PROCEDURE PROHPT3; BEGIN MESSA6E!10 1 1BO, 'ENTER !EDSEl !COSTl<SPACE> HESSA6E(10,170,' CTRL-C WHEN DONE END; (1----------------------------------------1) PROCEDURE PROHPT4; BEGIN WSTRINS!' = 'l; END; (1----------------------------------------1) PROCEDURE ADJUST!VAR VS : CHARSETl; VAR N,CH,ACTION : CHAR; X,Y : INTEGER; NUMBER,! : INTEGER; (1--------------------t) 'l; 'l; 107 PROCEDURE BODY; BEGIN PROKPT2 !Nl i PLOTCHARIX,Y,Nl; REPEAT ACTION:= CHARREAD![CHRI114l 1 CHRI82l 1 CHRI21l 1CHRI7bl,CHRIBl, CHRI117l,CHRI108l,CHRIB5l 1CHRI32l,CHRI11l, CHRI100l,CHRibBl,CHRI10lll; PLOTCHARIX,Y,' 'l; CASE ORDIACTIONl OF 114,82,21: IF X<= 2b0 THEN X:= X+ 5 ELSE BELL; 108,7b,B : IF X>= 15 THEN X:= X- 5 ELSE BELL; 117,85,11: IF Y<= 155 THEN y := y + 5 ELSE BELL; 100 1b8,10: IF Y >=15 THEN y := y - s ELSE BELL; END; PLOTCHARIX,Y,Nl; UNTIL ACTION = ' VUJ.NAKE := N; V[IJ.X := X+ 2; V[IJ.Y := Y+ 4; FOR I := 1 TO NUMBER DO PLOTCHARIV[IJ.X-2,V[IJ.Y-4,V[lJ.NAKEl; END; (t BODY tl (1--------------------f) BEGIN It ADJUST tl VS := U; NUMBER := 0; PROMPTl; N := CHARREAD!['A' •• 'Z' ,CHRi3lll; WHILE IORDINl <> 3l DO BEGIN IF N IN VS THEN BEGIN I : = SEARCH INl; X:= V[Il.X- 2; V := V[ll.Y- 4; END ELSE 108 BEGIN VS := VS + [NJ; HUMBER := NUMBER + 1; I := NUIIBER; X := 30; y := 150; END; BODY; PROI1PT1; N:= CHARREAD!['A' •• 'Z' ,CHR!3lll; END; V[OJ.X := NUIIBER; V[OJ.Y :=NUMBER; V[OJ.NAI'IE := 'V'; FOR I := 1 TO NUMBER DO GTRACE!2 1V[IJ.NAIIE,' 'l; END; lt ADJUST t) (1----------------------------------------f) PROCEDURE INSTRING!VAR 5: I'IESGl; VAR 1'1 : 11E5G; CH : CHAR; BEGIN 5 ·, - ''.I CH := CHARREAD![' ·,·o· .. '9'Jl; WHILE CH <> ' ' DO BEGIN WCHAR lCHl; ..n ·=• ' '• I CH; 5 := CONCATl5 111l; CH := CHARREADI[' END; ll[lJ := ·,·o· .. '9'Jl; END; {f----------------------------------------1) FUNCTION CONVERT!WORD:I1ESGl:INTE6ER; VAR I,J : INTEGER; BEGIN I := 0; FOR J := 1 TO LENGTH!WORD> DO I := 10 * I+ ORD!WORD[JJl-ORDl'O'l; CONVERT := I; END; {f----------------------------------------1) PROCEDURE ENTERED6E!V5 : CHARSETl; VAR 109 CHl, CH2 : CHAR; I : INTEGER; 11SG : 11ESG; BEGIN (I ENTEREDGE I) HOJ.COST := 0; PROI1PT3; 11ESSAGEIO,O,' 'l; CHI := CHARREADIVS+£CHRI3lll; IF ORDICHll <> 3 THEN WCHARICHll; WHILE IORDICHll <> 3l DO BEGIN CH2 := CHARREADIVS-£CH1ll; WCHARICH2l; PROI1PT4; INSTRING!I1SGl; I := EINDEXICH1,CH2i + EINDEXICH2,CH1l; IF I = 0 THEN BEGIN E[Ol.COST := E[Ol.COST + 1; I := HOJ.COST; END; Hll.Vl :=CHi; Hll.V2 := CH2; E£Il.COST := CONVERTII1SGl; GTRACEI4 1 CH1,CH2l; CHI := CHARREADIVS+£CHR13lll; 11ESSA6E I0, 0 I • •) ; 11ESSA6E!O,o,· 'l; IF ORD!CHll 0 3 THEN WCHAR ICH 1l ; END; END; (t ENTERED6E tl 11----------------------------------------l) PROCEDURE SAVEVEIA:CHARl; VAR I : INTEGER; F : STRIN6Ul; BEGIN F := , '; HlJ := A; RENRITEIFV,CONCAT!'I5:GRAPH' ,F, '.VLIST'll; FOR I := 0 TO V£0l.X DO BEGIN FVA[Jl.NAI1E := V[ll.NAI1E; FVA[J).X := V£IJ.X; fVA[J).Y := V[ll.Yj END; PUTIFVl; 110 CLOSEIFV,LOCKl; REWRITEIFE,CONCATI'I5:GRAPH' ,F, '.ELIST'll; FOR I := 0 TO E£0l.COST DO BEGIN FEA[ll.Vl := E[JJ.Vl; FEA[Jl.V2 := E£Il.V2; FEA[ll.CDST := E[IJ.COST; END; PUTIFEl; CLOSEIFE,LOCKl; END; If SAVEVE f) (f----------------------------------------1) PROCEDURE NEW; VAR C : CHAR; I : INTEGER; BEGIN If NEW fl INITTURTLE; ADJUSTIVSl; ENTEREDGEIVSl; INITTURTLE; FOR I := 1 TO V[OJ.X DO BEGIN PLOTCHARIV£IJ.X-2,V£IJ.Y-4,V[IJ.NAHEl; GTRACE 12, V£ Il.NAHE,' 'l; END; FOR I := 1 TO E[OJ.COST DO GTRACEI4 1E£Il.Vl,E£IJ.V2l; 10 FOR NO SAVEl 'I; HESSASEI0 1 1BO, 'SAVE AS ? 1 2 3 C := CHARREADI£'0',, '3'Jl; IF C > '0' THEN SAVEVE ICl; .) ; HESSASEIO,lBO,' BSAVEICl; PICTURE := C; END; It NEW tl (f----------------------------------------1) PROCEDURE TOP; BEGIN INITTURTLE; HESSASEIO,lBO, 'GRAPH EDITOR'!; HESSA6EI0 1 110,' V VIEW LIBRARY SRAPH'l; HESSASE!0 1 90 1 ' 6 SET ALIBRARY SRAPH'l; HESSA6EI0,70 1 ' N EDIT ANEW GRAPH' I; HESSASEI0 150 1 ' Q QUIT'l; HESSAGEI0 1 10 1 'ENTER YOUR CHOICE--- 'I; CH := CHARREADI['Q', 'V' 1 'G' ,'N'Jl; END; 111 {1----------------------------------------1) PROCEDURE SETS; VAR C : CHAR; BEGIN INITTURTLE; MESSASEI0 1 180, 'SET WHICH? C := CHARREAD!['l' •• '3'll; BLOADICl; SETVE!Cl; PICTURE := C; END; 1 OR 2 OR 3'1; {l----------------------------------------1) PROCEDURE VIEWS; VAR C : CHAR; BEGIN INITTURTLE; "ESSASEI0,180, 'VIEW WHICH? 1 OR 2 OR 3'1; C := CHARREADI['l · •• '3'll; BLOAD!Cl; MESSASEIO,l80, 'PRESS ANY KEY TO RETURN HENU'l; READ!KEYBOARD,Cl; END; (1----------------------------------------1) BEGIN {I GETSRAPH I) TOP; WHILE ICH <> 'Q'l OR !NOT !PICTURE IN ['0',, '3'1)) DO BEGIN CASE CH OF 'If: NEW; '6': SETS; 'V': VIEWS; 'Q': BEGIN INITTURTLE; BELL; MESSAGEI0 1 180, 'WARNING: NO DATA LOAD'!; END; END; TOP; END; BLOADIPICTUREl; END; I* SETSRAPH I) 112 (t IS:INTRODUC.TEXT t) (1---------------------------------------------------------------------------f) SEGMENT PROCEDURE INTRODUC!TUTO:KESGl; VAR T : TEXT; CH : CHAR; (1----------------------------------------f) PROCEDURE ONERUN; VAR I : INTEGER; Y : 11ES6; BEGIN I := 1; NRITE!AT!23,0ll; NHILE !NOT EOF!Tll AND !I <=51 DO BEGIN READLN !T 1 Yl; NRITELN !Yl; NRITELN; I := I + 1; END; END; It ONERUN tl (1----------------------------------------f) PROCEDURE INTERACT; BEGIN NRITE!AT!l,Ol, '-------------------------------------'); WRITEl'-----------------------------------------'1; NRITElAT!O,Ol, 'Introduction: N>ext P>RINT Q)uit'l; CH := CHARREAD!£'N' ,'Q'll; IF CH = 'P' THEN PRHHINS; END; (1----------------------------------------f) BEGIN (I INTRODUC tl PAGE toUT PUT l ; RESET!T 1CONCAT!TUTO, '.TEXT'll; DNERUN; IF NOT EOF!Tl THEN ONERUN; CH := ' '; NHILE !NOT EOF!Tll AND lCH <> 'Q'l DO BEGIN 9 . 113 INTERACT; IF CH = 'N' THEN ONERUN; END; IF IEOF!Tl> AND ICH <> INTERACT; CLOSE IT, LOCK l ; PAGE !OUTPUT> ; END; (t INTRODUC tl 'Q') THEN (t---------------------------------------------------------------------------•l 114 It 15:INITTRACE.TEXT t) (1---------------------------------------------------------------------------t) SESI'IENT PROCEDURE INITTRACEIA : HESS>; VAR CH : CHAR; (1----------------------------------------t) PROCEDURE SETUPSCREEN; BEGIN (t SETUPSCREEN t) PASE <OUTPUll ; LAST := TABLEA[Ol.PASENO; PRINTICONCATINAME,' .A'l 1 1l; TOP := 'A'; BOTTOM : = 'B'; IF LAST >= 'B' THEN PRINTICONCATINAI'IE, '.B'l 1 2l; RESETIH,CONCAT<NAI'IE, '. ',PASEIDll; SETIHl; END; (t SETUPSCREEN t) (t----------------------------------------1) PROCEDURE SELECTBREAKPOINTS; VAR CH : CHAR; I,X,Y : INTEGER; BEGIN (t SELECTBREAKPOINTS CH := 'D'; t) I := 0; WHILE CH <> 'Q' DO BEGIN CASE CH OF 'D' : IF I ( TABLEA[OJ.LINEND THEN BEGIN I := I t 1; CHECKBOTTOI'I II); END ELSE BELL; 'U' : IF I } 1 THEN BEGIN I := I - 1; CHECKTDP<I>; END ELSE BELL; 'S' : IF POSI' <-------',HA£TABLEA£IJ.LINENDJ) = 0 THEN 115 BESIN HA£TABLEA[IJ.LINENOJ := CONCATIHA£TABLEA[IJ.LINENOJ, . (-------'); WRITEIATIY,Xl, '(-------'!; END ELSE BELL; 'F' : IF POSI' <-------· ,HA[TABLEA[ll.LINENOJl >0 THEN BESIN DELETE!HA[TABLEA(IJ.LINENOJ, POS(. (-------· ,W£TABLEA[lJ.LINENOJ) ,9>; WRITEIAT!Y,X-9!,' ',AT!Y,Xll; END ELSE BELL; 'P' : PRINTING; END; CHECKKEIIORY!Il; X:= LEN6THIHA£TABLEA£IJ.LINENOJJ+l; y := ITABLEA[IJ.LINENO-ll+llt!ORDITABLEA[IJ.PA6EN0[1))-0RDITOP£1J)J; SOTOXY <X, Yl; CH := CHARREADI£'D' 1 'S', 'U', 'F', 'Q', 'P'Jl; END; END; It SELECTBREAKPOINTS t) (t----------------------------------------1) PROCEDURE RESETTEXT; VAR L : STRING£ 1J; I, J : INTEGER; BEGIN (t RESETTEXT t) I := 1; L := TABLEA[lJ.PAGENO; WHILE I <= TABLEA[OJ.LINENO DO BEGIN RESETIH,CONCATINAKE,'. ',Lll; GETIH>; CLOSEIH,LOCKl; REWRITEIH,CONCATINAKE, ·• ',Lll; WHILE II <= TABLE'"EOJ.LINENOl AND ITABLEA[IJ.PAGENO = Ll DO BEGIN J := PQS(' (-------',HA[TABLEA[IJ.LINENOJl; IF J > 0 THEN BEGIN DELETEIHA£TABLEA£IJ.LINENOJ,J,9); TABLEA[IJ.LINENO := TABLEA[IJ.LINENO + 100; END; I:=I+l; END; PUT!Hl; CLOSEIH,LOCKl; 116 IF I <= TABLEA[OJ.LINENO THEN l := TABLEA(IJ.PAGENO; END; END; It RESETTEXT tl (i----------------------------------------1) PROCEDURE SELECTGRAPH; VAR CH : CHAR; BEGIN It SELECTGRAPH t) PROMPT := 1; PAGE !OUTPUT> ; WRITEIATI5,0l, 'TRACE WHICH GRAPH? 2 3 ( 0 FOR NO l'l; CH := CHARREADI£'0'., '3'll; IF CH <> '0' THEN BEGIN GRAF := TRUE; BLOADICHl; GETVEICHl; PLOT; NOTEEI 'Original',· ·, · · l; END; MESSA6EI0 1 180, 'Comaand: N B T 6 E Q'l; PASEID := 'A'; HEAD := 0; TAIL := 0; CURRENT := 0; NONSTOP := FALSE; END; 11 SELECTGRAPH tl (t----------------------------------------1) BEGIN (t INITTRACE tl INITTURTLE; TEXTMDDE; PAGE!DUTPUTl; NRITE!AT!5 1 5l,'HARDWARE ENVIRONMENT: 'l; WRITE!AT!8,7l 1 'IS APRINTER ON LINE? Y/N 'l; IF CHARREAD!['Y', 'N'll = 'Y' THEN BEGIN ONLINE := TRUE; CLOSE !OUTPUT> ; RENRITE!OUTPUT, 'PRINTER:'>; WRITE !CHR (15ll; CLOSE !OUTPUT> ; REWRITE!OUTPUT, 'CDNSOLE:'l; END; WRITEIATI11,7l, 'COMPUTER MODEL ?'l; WRITE!AT!13,7l,' + APPLE II PLUS'!; 117 WRITEIATI15 1 7l 1 ' E APPLE// E'l; IF CHARREADI['+' 1 'E'll = '+' THEN COMPUTER := '+'; NAIIE := A; SELECTSRAPH; RESETITABLE,CONCATINAIIE, '.TABLE'll; SET ITABLEl; CLOSEITABLE,LOCKl; SETUPSCREEN; SELECTBREAKPOINTS; CLOSE IH 1LOCKl; REWRITEIH,CONCAT!NAIIE, '. ',PASEIDll; PUTIHl; CLOSEIH,LOCKl; PAGE IOUTPUT! ; PROIIPT:= 2; WRITEIATI12 1 10l, 'PLEASE WAIT FOR SCREEN SET UP' ,ATIO,Oll; RESETTEXT; PAGEID := 'A'; SETUPSCREEN; INIT := TRUE; PIITl; END; It INITTRACE t) (i-------------------------------------------------------------------------1) 118 (I 15:6TRACE.TEXT I) (f---------------------------------------------------------------------------1) SEGMENT PROCEDURE GTRACEIFLAG:INTEGER; C1,C2:CHARl; VAR I : INTEGER; (1----------------------------------------f) PROCEDURE DEPOINTIX,Y:INTEGERl; BEGIN PENCOLORINONEl; MOVETO IX, Yl; PENCOLORIBLACKl; MOVEIOJ; END; (1----------------------------------------f) PROCEDURE POINTIX,Y:INTEGERl; BEGIN PENCOLORINONEl; 110VETOIX,Vl; PENCOLORIWHITEl; !'lOVE (OJ; END; (t----------------------------------------1) PROCEDURE REDUCEIA1,Bl,A2,B2 : INTEGER; VAR X1,Vl,X2,V2,SS : INTEGER!; VAR X,V,A,S,S1,52 : REAL; I : INTEGER; BEGIN REDUCE +) - A2; V := Bl - B2; 51 := AB5!Xl i 52 := ABS (V); s := 51 + 52; IF Sl < 52 THEN 51 := 52; 52 := S; A := 5QRIXl + SQRIYl; FOR I := 1 TO 10 DO BEGIN S := 151 + S2l I 2; IF SQR15l >= ATHEN 52 := s ELSE (t X : = A1 ~ ' 119 Sl := S; END; X2 Y2 Xl Yl SS END; ROUND!A1-!S-11ltX/Sl; ROUND!B1-lS-11lfY/Sl; ROUND!Al-llfX/Sl; ROUNDIB1-11fY/Sl; ROUND!Sl; (f REDUCE f) := := := := := (i----------------------------------------1) PROCEDURE LINE!Xl,Y1,X2,Y2 : INTEGER; FLAG : INTEGER>; VAR Al,B1,A2,B2 : INTEGER; DIST : INTEGER; (f--------------------1) PROCEDURE DLINE!Xl,Yl,X2,Y2,D,DEL : INTEGER!; VAR X,Y : INTEGER; S,STEP : REAL; BEGIN lt DLINE f) STEP := 4/D; s := 0; X:= X2 - Xl i Y: = Y2 - Yl; IF DEL = 1 THEN WHILE S <= 1 DO BEGIN POINTIROUNDIXl+S+Xl,ROUND!Y1+SfYll; S := S + STEP; END ELSE WHILE S <= 1 DO BEGIN DEPOINT!ROUND!Xl+S+Xl,ROUND!Y1+SfYll; S := S + STEP; END; END; (f DLINE f) (f--------------------1) BEGIN (I LINE i) REDUCE!Xl,Y1,X2 1 Y2,A1,Bl,A2,B2,DISTl; PENCOLORINONEl; I'IOVETO!Al ,Bl l; CASE FLAG OF 1 : BEGIN PENCOLORIWHITEl; I'IOVETO!A2 1 B2l; 120 END; 2 : BEGIN PENCDLORIBLACKl; 110VETDIA2,B2l; DLINE!Al,Bl,A2 1 B2,DIST 10l; END; 3 : DLINEIA1,Bl,A2,B2,DIST,ll; END; END; It LINE t) (1----------------------------------------t) PROCEDURE DDTCIRCLEIX, V: INTEGER>; (f DRAW ADOT CIRCLE NITH RADIUS 10 AND CENTER AT !X,Vl t) BEGIN PDINT!X+10,V+3 l; PDINTIX+7 ,V+7 l; PDINTIX+3 ,V+10l; PDINTIX-3 ,V+lOl; PDINTIX-7 ,V+7 l; PDINTIX-10,Y+3 l; POINTIX-lO,Y-3 l; PDINTIX-7 ,Y-7 l; PDINTIX-3 ,Y-10>; PDINTIX+3 ,Y-101; POINTIX+7 ,Y-7 >; PDINT!X+lO,Y-3 l; END; (t----------------------------------------1) PROCEDURE CRCLIX,Y,J : INTEGER>; BEGIN PENCDLOR!NONE>; 11DVETOIX+lO,Y+3l; IF J = 0 THEN PENCOLORIBLACKl ELSE PENCDLORINHITEl; HDVETD!X+7,Y+7l; 110VETD!X+3,Y+10l; 110VETD!X-3 1 Y+10l; 11DVETDIX-7,Y+7l; HDVETO!X-10,Y+3l; 11DVETOIX-10,Y-3l; 110VETOIX-7,Y-7l; 110VETO!X-3,Y-10l; HOVETD!X+3 1 V-10l; 110VETOIX+7,V-7l; HDVETOIX+lO,Y-31; HDVETOIX+10 1 V+3l; END; 121 (t----------------------------------------1) PROCEDURE EDGE5ELECTIC1,C2 : CHAR; FLAG : INTEGER>; VAR I,J : INTEGER; X,V : INTEGER; 1'1 : I'IESG; BEGIN It EDGESELECT tl I := SEARCHICll; J := 5EARCHIC2l; IF Cl >= C2 THEN BEGIN X:= I; I := J; J := X; END; LINEIV£IJ.X,V£Il.V,V[JJ.X,V[JJ.V,2l; IF FLAB = 1 THEN LINEIV[JJ.X,V[IJ.V,V[JJ.X,V[JJ.V,ll ELSE LINEIV[JJ.X,V£IJ.V,V[JJ.X,V[JJ.Y 13l; X:= IV[l).X t V[Jl.Xl DIV 2; V := IV[Il.V + V[JJ.Vl DIV 2; I'IESSAGEIX-2,Y-4 1 ''l; I := EINDEXIC1 1 C2l; IF I = 0 THEN I := EINDEXIC2 1 Cll; STRIE£Il.COST,I'Il; NSTRIN6111l; END; It EDGESELECT tl (t----------------------------------------1) BEGIN It STRACE IF GRAF THEN BEGIN t) CASE FLAG OF 1: BEGIN If VSELECT tl SEARCHICll; CRCLIV[Il.X 1 V£Il.Y,ll; END; 2: BEGIN It VRELEASE t) I := SEARCHICll; CRCLIV[JJ.X,V£Il.Y,Ol; DOTCIRCLE!V[Il.X,V[Il.Vl; END; 3: EDSESELECTIC1,C2 1 1l; It ESELECT tl I := 122 4: ED6ESELECT!Cl,C2,2l; END; {I ERELEASE tl END; END; (I 6TRACE I} (1---------------------------------------------------------------------------f) 123 (t IS:PTRACE.TEXT tl (1---------------------------------------------------------------------------l) SEGMENT PROCEDURE PTRACEII:INTEGER; ST:SHORTl; VAR Y: INTEGER; L : INTEGER; L1 : INTEGER; (t----------------------------------------1) PROCEDURE LINEUPDATEIL:INTEGER;ST:SHORT;Y:INTEGERl; VAR J,K : INTEGER; BEGIN (t LINEUPDATE tl J := LENGTH!HA(Lll; K :=POSt'; "1 HA[Lll; IF J > KTHEN DELETE!HA[Ll,K+l,J-Kl; HA£Ll := coNCATIHA£Ll,· ·,sT>; WRITE lATtY ,Ol ,CLEAR,H'"£Ll l; END; (1----------------------------------------f) PROCEDURE SCOLLINGII:INTEGERl; BEGIN IF I > 0 THEN BEGIN CHECKI'IEI'IORY II l ; CHECKTOPIIl; CHECKBOTiOI'I (I l; END; END; (1----------------------------------------t) PROCEDURE UPDATESCREENIVAR I:INTEGER;ST:SHORTl; VAR Y,L : INTEGER; BEGIN IF I > 0 THEN BEGIN L := TABLE···riJ.LINENO MOD 100; Y: = IL -1 l +lltiORD!TABLE'··r Il. PAGE NO£ 1l l-ORD !TOP£ 1l l l; LINEUPDATEIL,ST,Yl; END ELSE BEGIN 124 I := 0 - I; IF II HOD 21 = 1 THEN I := I + 1 ELSE I : = I - 1; GTRACE!I,ST£1J 1 ST£2Jl; I := 0 - I; END; END; (1----------------------------------------t) PROCEDURE INBUFFER!I:INTEGER;ST:SHORTl; BEGIN INC!CURRENT>; INCIHEADl; IF HEAD = TAIL THEN INC nAIU; BUFFER£CURRENTl.TI :=I; BUFFER£CURRENTJ.STRI := ST; END; (1----------------------------------------1) PROCEDURE BACKUP; VAR FOUND : BOOLEAN; BEGIN (t BACKUP t) FOUND := FALSE; WHILE NOT FOUND DO BESIN DEC (CURRENT> ; IF TAIL <> CURRENT THEN BEGIN SCOLLIN6!BUFFER£CURRENTl. Til; UPDATESCREEN!BUFFER£CURRENTJ.TI,BUFFER£CURRENTl.STRil; IF BUFFER£CURRENTJ.STDP THEN FOUND := TRUE; END ELSE BEGIN BELL; INC l CURRENT>; FOUND := TRUE; END; END; END; (I BACKUP I) (t----------------------------------------1) PROCEDURE NEXT!VAR ASAIN : BOOLEAN!; VAR 125 FOUND : BOOLEAN; BESIN (t NEXT I) FOUND := FALSE; WHILE NOT FOUND DO IF HEAD <> CURRENT THEN BESIN INC ICURRENT> ; SCOLLI NS (BUFFER [CURRENTJ; Tl l ; UPDATESCREENIBUFFER£CURRENTJ.TI,BUFFER£CURRENTJ.STRil; IF BUFFER£CURRENTJ.STOP THEN FOUND := TRUE; END ELSE BESIN ASAIN := FALSE; FOUND := TRUE; END; END; (I NEXT I) (1----------------------------------------1) PROCEDURE CONTROL; VAR CH : CHAR; ASAIN : BOOLEAN; BESIN lt CONTROL tl ASAIN := TRUE; WHILE ASAIN DO BE SIN cH := cHARREADt£·a·, 'N', ·s· I 'T' I 'E', ·g·, ·p·J>; CASE CH OF 'T' : TEXTIIODE; '6' : SRAFIIODE; 'Q' : BESIN WHILE CURRENT <> HEAD DO NEXT IASAINl; NONSTOP := TRUE; ASAIN := FALSE; END; T : BESIN WHILE CURRENT <> HEAD DO NEXT IASAINl; NONSTOP := TRUE; ENDINS := TRUE; ASAIN := FALSE; END; 'N' : NEXTIASAINl; 'B' : BACKUP; 'P' : PRINTINS; END; END; 126 END; (I CONTROL I) (t----------------------------------------1) BEGIN (I PTRACE I) IF ItHT THEN BEGIN SCOLLING(J); Ll := TABLEA[IJ.LINENO "OD 100; IF LENGTH!H'"[LlJ) = POS!';' ,W'[Llll THEN BEGIN INBUFFER (J I " ) i BUFFER[CURRENTJ.STOP := FALSE; END; UPDATESCREEN!I 1 STl; I NBUFFER (J 1Sll i IF TABLE'·[IJ.LINENO > 100 THEN BEGIN BUFFERICURRENTJ.STOP := TRUE; IF NOT NONSTOP THEN CONTROL; END ELSE BUFFERICURRENTJ.STOP := FALSE; END; END; It PTRACE t) {1---------------------------------------------------------------------------l) 127 (t IS:INTERFACE.TEXT tl (1---------------------------------------------------------------------------f) (f----------------------------------------1) PROCEDURE CLOSING; VAR L : STRINGU J; I,J,K : INTEGER; BEGIN PUT!Hl; CLOSEIH,LOCKl; I := 1; L := TABLEhfll.PA6ENO; WHILE I <= TABLEhfOJ.LINENO DO BEGIN RESETIH,CONCATINAME, '. ',Lll; GET!Hl; CLOSE!H,LOCKl; REWRITEIH,CONCATINAME,'. ',Lll; WHILE II <= TABLE~·[OJ.LINENOJ AND !TABLEh£IJ.PA6ENO = U DO BESIN TABLE"·[ IJ. LINENO := TABLE'·[! J.LINENO IIOD 100; J :=POSt'; ' 1 HAfTABLEAfil.LINENOll; K:= LENGTHIH'HABLE'·[!J.LINENOll; IF IK > Jl AND (J > OJ THEN HAfTABLEA[IJ.LINENOJ := CDPY!HAfTABLEAfiJ.LINENOl,l,Jl; I:=I+l; END; PUT!Hl; CLOSEIH 1 LOCKl; IF I <= TABLEA[(}J.LINENO THEN L := TABLEA[Jl.PAGENO; END; END; (t CLOSING t) (1----------------------------------------f) PROCEDURE INBUF; BEGIN INCICURRENTI; INCIHEADl; IF HEAD = TAIL THEN INCITAIU; BUFFERfCURRENTJ. STRI : = ' '; BUFFERfCURRENTl.STOP := FALSE; END; (1----------------------------------------i} PROCEDURE ITRACEINUMBER:INTEGER; INT:INTE6ER>; VAR 128 ST : SHORT; BEGIN STR (I NT 1STl; PTRACE!NUHBER,STl; END; (1----------------------------------------1} PROCEDURE CTRACEINUHBER:INTEGER; CH:CHARl; VAR ST : SHORT; BEGIN ST := ' '; SHlJ := CH; PTRACEINUHBER,STl; END; (1----------------------------------------1) PROCEDURE RTRACE!NUHBER:INTEGER;X:REALl; VAR ST1,5T2 : SHORT; BEGIN STRITRUNC!Xl,STll; STRITRUNCI!X-TRUNC!Xll1100l,ST2l; STl := CONCATISTl, ', ' 1 ST2l; PTRACEINUHBER,STll; END; (1----------------------------------------1) PROCEDURE TRACE!NUHBER:INTEGERJ; BEGIN PTRACE!NUHBER, ''l; END; (1----------------------------------------1) PROCEDURE VSELECT!Vl:CHAR>; BEGIN GTRACE l1, Vl, ' 'l ; PLOT; NOTEEI'After selecting vertex · ,Vl 1 • 'I; INBUF; BUFFERECURRENTl.TI := -1; BUFFER£CURRENTl.STRI£1J := Vl; END; (1----------------------------------------1) PROCEDURE VRELEASE!Vl:CHARl; BESIN 6TRACEI2 1V1,' 'l; 129 PLOT; NOTEEI'After releasing vertex ',Vl,' 'l; INBUF; BUFFERfCURRENTJ.TI := -2; BUFFERfCURRENTJ.STRI£11 := Vl; END; (f----------------------------------------1) PROCEDURE ESELECTIVl,V2:CHARl; BEGIN 6TRACEI3,Vl,V2l; PLOT; NOTEEI'After selecting edge ',Vl,V2l; INBUF; BUFFER£CURRENTJ.TI := -3; BUFFER£CURRENTJ.STRI£1J := Vl; BUFFERfCURRENTJ.STRif2J := V2; END; (1----------------------------------------1) PROCEDURE ERELEASEIV1 1 V2:CHAR>; BEGIN 6TRACEI4 1 Vl,V2J; PLOT; NDTEEI'After releasing edge ',Vl,V2l; INBUF; BUFFER£CURRENTJ.STRI£1J := Vl; BUFFER£CURRENTJ.STRI£2J := V2; END; (1----------------------------------------f) PROCEDURE ENDTRACE; VAR J : INTEGER; BESIN IF ENDINS THEN BEGIN ENDING := FALSE; NONSTOP := FALSE; J := TABLEAfOJ.LINEND; TABLEA[JJ.LINENO := ITABLEA[JJ.LINENO MOD 100) + 100; PTRACE IJ I") j END; PASE !OUTPUT>; NRITEIATI5 15l, 'Thank you for using this package, 'J; WRITEIATI7 15l,' please wait for resetting data. 'J; CLOSING; PASEIOUTPUTJ; NRITEIATI7,7J, 'THANK Y0 U'J; END;