Codewarrior Programming Practice: Pascal Codewarrior
Total Page:16
File Type:pdf, Size:1020Kb
CD TM CodeWarrior Programming Practice: Pascal CodeWarrior Metrowerks Because of last-minute changes to CodeWarrior, some information in this manual may be out of date. Please read all the Release Notes files that come with CodeWarrior for the latest information. Metrowerks CodeWarrior Copyright ©1993-1995 by Metrowerks Inc. and its Licensors. All rights reserved. Documentation stored on the compact disc may be printed by licensee for personal use. Except for the foregoing, no part of this documentation may be reproduced or transmitted in any form by any means, electronic or mechanical, including photocopying, recording, or any information storage and retrieval system, without permission in writing from Metrowerks Inc. Metrowerks, the Metrowerks logo and Software at Work are registered trademarks of Metrowerks Inc. CodeWarrior, PowerPlant, and PowerPlant Constructor are trademarks of Metrowerks Inc. All other trademarks or registered trademarks are the property of their respective owners. ALL SOFTWARE AND DOCUMENTATION ON THE COMPACT DISC ARE SUBJECT TO THE LICENSE AGREEMENT IN THE CD BOOKLET. Canada and International U.S.A. Metrowerks Inc. Metrowerks Corporation 1500 du College, suite 300 Suite 310 St. Laurent, QC The MCC Building H4L 5G6 Canada 3925 West Braker Lane Austin, TX 78759-5321 voice: (514) 747-5999 voice: 512-305-0400 fax: (514) 747-2822 fax: 512-305-0440 Metrowerks Mail Order voice: (800) 377-5416 or (419) 281-1802 fax: (419) 281-6883 World Wide Web site (Internet): http://www.metrowerks.com Registration information (Internet): [email protected] Technical support (Internet): [email protected] Sales, marketing, & licensing (Internet): [email protected] AppleLink: METROWERKS America OnLine: goto: METROWERKS Compuserve: goto: METROWERKS eWorld: goto: METROWERKS Table of Contents iii Table of Contents Chapter 1 An Overview 1.1 Preview....................................................................14 1.2 Introduction to Programming Practice: Pascal...........15 A Global View.........................................................15 The Road Ahead.......................................................15 Signs Along the Road...............................................16 1.3 Hardware: Computers and Peripherals.....................17 The CPU...................................................................18 Other Components and Packaging............................19 1.4 The World of Programming......................................20 What is Programming?.............................................20 “Real world” and “Abstract World”..........................20 1.5 Pascal.......................................................................21 An example Pascal program......................................22 Running Programs: Compiling, Linking, Executing.23 1.6 Communicating to Computers..................................24 Typing......................................................................24 1.7 Chapter 1 Review.....................................................27 Chapter 2 Computing: A Short Survey of Some Applications 2.1 Preview....................................................................30 2.2 Software and Applications........................................30 2.3 Application Software................................................33 Editor Application....................................................34 Typing Applications.................................................35 Calculator Applications.............................................36 Retrieve Application: A Tiny Database.....................38 Planner-Calendar Application...................................39 Bar Plot Application..................................................41 Drill Application.......................................................42 SSS: Small and Simple Spreadsheet...........................43 2.4 Chapter 2 Review.....................................................47 Chapter 3 Programming Language: Pascal 3.1 Preview....................................................................50 iv Table of Contents 3.2 Languages................................................................50 Syntax and Semantics................................................50 Syntax Diagrams.......................................................53 3.3 Pascal Programs........................................................56 Program Format.......................................................56 Program Presentation................................................59 More Pascal Programming: Data and Actions..........60 Data Items................................................................61 Actions: Arithmetic Operations................................63 3.4 More Example Programs...........................................66 3.5 Chapter 3 Review.....................................................69 3.6 Chapter 3 Problems...................................................70 3.7 Chapter 3 Programming Project................................74 Getting Acquainted...................................................74 Chapter 4 Data and Actions 4.1 Preview....................................................................76 4.2 Programming: Data and Actions...............................76 Declarations: Syntax Diagrams from the Bottom........76 Simple Input and Output in Pascal............................80 4.3 More Programs: A Top View of Pascal....................83 4.4 Programming Style...................................................85 4.5 Layout of Programs..................................................86 4.6 More Programs: Continued.......................................89 Actions: Pre-Defined Standard Functions in Pascal....89 Libraries: Using Units in Pascal.................................92 4.7 A Foretaste of Procedures.........................................94 4.8 Chapter 4 Review.....................................................96 4.9 Chapter 4 Problems...................................................96 4.10 Chapter 4 Programming Projects...............................97 Generate Conversion Tables......................................97 Observing Errors......................................................97 Demilitarize Time.....................................................98 TipTable...................................................................99 STT: Sales Tax Table..................................................100 SSP: Simple Side Plot................................................101 Chapter 5 The Four Fundamental Forms in Pascal 5.1 Preview....................................................................105 Table of Contents v 5.2 The Sequence Form in Pascal....................................105 5.3 Conditions in Pascal..................................................107 5.4 Repetition Form: The WHILE statement....................108 Tracing Loops...........................................................110 5.5 WHILES, REALS, and Errors.....................................114 5.6 Selection Forms in Pascal..........................................117 5.7 More Selections: Combinations of Selection Forms....119 Confusion in Choices................................................120 More Nesting of Choices..........................................120 Alternative Ways to Code Selections.........................121 5.8 Select Form: Handling Many Branches....................124 5.9 Awkward Nests: General Nesting............................127 Mixed Nests: Repetition and Selections.....................128 5.10 Subprograms: Using Subprograms as Black Boxes....130 The ShortSort Library................................................134 Notation for Defined Procedures...............................137 Procedures vs. Functions..........................................140 5.11 Binary Logic Library: BitLib.....................................145 5.12 Chapter 5 Review.....................................................149 5.13 Chapter 5 Problems...................................................150 5.14 Chapter 5 Programming Problems............................152 Sequence Problems...................................................153 Selection Problems....................................................153 Loop Problems.........................................................153 Subprogram Problems..............................................153 Debugging Problems................................................154 Selection Programs...................................................156 Procedures and Repetitions.......................................157 Josephus’ Problem....................................................158 5.15 Chapter 5 Programming Projects...............................159 Project A: Change Change.......................................159 Project B: Payroll.....................................................160 Project C: Quadratic Roots.......................................160 Project D: Digital Circuits.........................................160 Project E: Roll Your Own.........................................161 CRN: Convert Roman Numbers..............................161 GPR: Growing Pay Roll...........................................161 MWM: Many Ways to Mid......................................162 DFP: Data Flow Programming.................................163 Chapter 6 Pascal with Bigger Blocks 6.1 Preview....................................................................169 vi Table of Contents 6.2 Conglomerations......................................................169