Learn Pascal in Three Days, Third Edition
Год: 2001
Автор: Sam Abolrous
Жанр: Программирование
Издательство: Jones & Bartlett Learning
ISBN: 978-1556228056
Язык: Английский
Формат: PDF
Качество: Изначально компьютерное (eBook)
Интерактивное оглавление: Да
Количество страниц: 324
Описание: Learn Pascal in Three Days, Third Edition provides the novice programmer with the fundamentals of the language and is ideal for those wanting a solid foundation in structured programming. This update of one of the best-selling introductions to Pascal is designed for beginning programmers with its chapter summaries, review questions, hands-on examples, and easy-to-follow drills. Readers start with simple programs and end up with useful real-world applications. Among the topics covered are building and evaluating arithmetic expressions, using structures and loops, creating arrays, manipulating text data, writing procedures and functions to make programs more modular, and declaring and using pointers. With this book, learn about the design of structured Pascal programs; problem-solving algorithms; simple and structured data types; variables, operators, and expressions; iteration, branching, and selection statements; pointers and linked lists; manipulating data files; real-life applications and solved drills.
Оглавление
Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . x
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi
Chapter 1: Hello Pascal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1-1 Your First Pascal Program . . . . . . . . . . . . . . . . . . . . . . . . 1
Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Program Heading . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Syntax and Conventions. . . . . . . . . . . . . . . . . . . . . . . . . 2
1-2 Displaying Text: WRITELN, WRITE . . . . . . . . . . . . . . . . . . . 3
1-3 Crunching Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Integers and Real Numbers . . . . . . . . . . . . . . . . . . . . . . . 5
Evaluation of Arithmetic Expressions . . . . . . . . . . . . . . . . . . 7
1-4 Using Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Variable Declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
The Assignment Statement . . . . . . . . . . . . . . . . . . . . . . 10
1-5 Named Constants. . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1-6 Type Conversion: ROUND, TRUNC . . . . . . . . . . . . . . . . . . 13
1-7 Reading from the Keyboard: READLN, READ. . . . . . . . . . . . . 14
1-8 Formatting Output. . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Chapter 2: Language Elements. . . . . . . . . . . . . . . . . . . . . . . . . 19
2-1 Standard Data Types and Functions . . . . . . . . . . . . . . . . . . 19
2-2 Numeric Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Numeric Types in Turbo Pascal . . . . . . . . . . . . . . . . . . . . 20
2-3 Standard Arithmetic Functions . . . . . . . . . . . . . . . . . . . . . 21
Example: The Power Function . . . . . . . . . . . . . . . . . . . . . 23
Example: Grocery Store . . . . . . . . . . . . . . . . . . . . . . . . 23
Turbo Pascal Additional Functions. . . . . . . . . . . . . . . . . . . 25
2-4 The Character Type: CHAR . . . . . . . . . . . . . . . . . . . . . . 26
Standard Functions for Characters . . . . . . . . . . . . . . . . . . 27
Strings in Standard Pascal. . . . . . . . . . . . . . . . . . . . . . . 29
2-5 The STRING Type . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Declaration of a String . . . . . . . . . . . . . . . . . . . . . . . . . 30
The Length of a String . . . . . . . . . . . . . . . . . . . . . . . . . 31
2-6 The BOOLEAN Type . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Simple Boolean Expressions. . . . . . . . . . . . . . . . . . . . . . 32
Compound Boolean Expressions . . . . . . . . . . . . . . . . . . . 34
Turbo Pascal Operators . . . . . . . . . . . . . . . . . . . . . . . . 35
Precedence of Operators . . . . . . . . . . . . . . . . . . . . . . . 35
Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Chapter 3: Decisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
3-1 Making Decisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
3-2 The Simple Decision: IF-THEN. . . . . . . . . . . . . . . . . . . . . 40
Example: Pascal Credit Card . . . . . . . . . . . . . . . . . . . . . 40
Using Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
3-3 The IF-THEN-ELSE Construct . . . . . . . . . . . . . . . . . . . . . 43
3-4 The ELSE-IF Ladders. . . . . . . . . . . . . . . . . . . . . . . . . . 45
Example: A Character Tester. . . . . . . . . . . . . . . . . . . . . . 46
3-5 Nested Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Example: Scores and Grades . . . . . . . . . . . . . . . . . . . . . 47
Tips on the IF-ELSE Puzzles. . . . . . . . . . . . . . . . . . . . . . 50
3-6 The Multiple Choice: CASE. . . . . . . . . . . . . . . . . . . . . . . 51
Example: A Vending Machine . . . . . . . . . . . . . . . . . . . . . 51
Example: Number of Days in a Month . . . . . . . . . . . . . . . . . 52
3-7 Unconditional Branching: GOTO . . . . . . . . . . . . . . . . . . . . 54
Repetition Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
3-8 Turbo Pascal Features: EXIT, CASE-ELSE . . . . . . . . . . . . . . 56
Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Chapter 4: Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
4-1 Looping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
4-2 The FOR Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Example: Powers of Two. . . . . . . . . . . . . . . . . . . . . . . . 64
Example: The Average. . . . . . . . . . . . . . . . . . . . . . . . . 65
4-3 Stepping Up and Stepping Down. . . . . . . . . . . . . . . . . . . . 66
Example: The Factorial. . . . . . . . . . . . . . . . . . . . . . . . . 67
4-4 Nested Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
4-5 The WHILE Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
4-6 The REPEAT Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
Chapter 5: Data Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . 77
5-1 Ordinal Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Enumerations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
Subranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
5-2 The TYPE Section. . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Renaming Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Naming User-Defined Types . . . . . . . . . . . . . . . . . . . . . . 82
5-3 Arrays as Data Structures . . . . . . . . . . . . . . . . . . . . . . . 83
5-4 One-Dimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . 85
Example: Scores of One Student . . . . . . . . . . . . . . . . . . . 85
Displaying Tabulated Results . . . . . . . . . . . . . . . . . . . . . 87
Declaration of Arrays in the TYPE Section . . . . . . . . . . . . . . 90
Example: Sorting an Array . . . . . . . . . . . . . . . . . . . . . . . 90
5-5 Two-Dimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . 93
Example: Scores of Students . . . . . . . . . . . . . . . . . . . . . 94
Array Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Chapter 6: Text Processing . . . . . . . . . . . . . . . . . . . . . . . . . . 101
6-1 Manipulating Text Data . . . . . . . . . . . . . . . . . . . . . . . . 101
6-2 Tips on OUTPUT Statements . . . . . . . . . . . . . . . . . . . . . 101
6-3 Tips on INPUT Statements . . . . . . . . . . . . . . . . . . . . . . 102
Using READLN for Numeric Input . . . . . . . . . . . . . . . . . . 102
Using READ for Numeric Input . . . . . . . . . . . . . . . . . . . . 104
Using READ for Character Input . . . . . . . . . . . . . . . . . . . 105
Using READLN for Character Input. . . . . . . . . . . . . . . . . . 107
Input of Mixed Types . . . . . . . . . . . . . . . . . . . . . . . . . 108
Example: Scrambling Letters . . . . . . . . . . . . . . . . . . . . . 109
6-4 Reading a Line of Text: EOLN. . . . . . . . . . . . . . . . . . . . . 111
Example: Character Counter . . . . . . . . . . . . . . . . . . . . . 111
6-5 Reading a File of Text: EOF . . . . . . . . . . . . . . . . . . . . . . 112
Example: Frequency Counter. . . . . . . . . . . . . . . . . . . . . 112
6-6 String Manipulation . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Tips on String Input/Output . . . . . . . . . . . . . . . . . . . . . . 113
Example: Sorting Names . . . . . . . . . . . . . . . . . . . . . . . 114
6-7 String Functions and Procedures . . . . . . . . . . . . . . . . . . . 116
LENGTH. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
CONCAT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
COPY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
POS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
DELETE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
INSERT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
Answers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
Chapter 7: Program Architecture . . . . . . . . . . . . . . . . . . . . . . . 121
7-1 Programs and Subprograms . . . . . . . . . . . . . . . . . . . . . 121
7-2 Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Procedure Definition . . . . . . . . . . . . . . . . . . . . . . . . . 122
Passing Values to Procedures . . . . . . . . . . . . . . . . . . . . 123
Passing Back Values from Procedures. . . . . . . . . . . . . . . . 126
7-3 Global and Local Variables . . . . . . . . . . . . . . . . . . . . . . 127
Example: Sorting Procedure . . . . . . . . . . . . . . . . . . . . . 127
7-4 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
7-5 Tips on the Scope of Variables . . . . . . . . . . . . . . . . . . . . 131
7-6 Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
Answers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
Chapter 8: Sets and Records . . . . . . . . . . . . . . . . . . . . . . . . . 137
8-1 Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
8-2 Set Declaration and Assignment . . . . . . . . . . . . . . . . . . . 138
Rules and Restrictions . . . . . . . . . . . . . . . . . . . . . . . . 139
8-3 Set Operators and Operations . . . . . . . . . . . . . . . . . . . . 140
Union . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
Intersection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
Difference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
Tips on Using Set Operators . . . . . . . . . . . . . . . . . . . . . 140
Relational Operators . . . . . . . . . . . . . . . . . . . . . . . . . 141
Example: Text Analyzer. . . . . . . . . . . . . . . . . . . . . . . . 142
8-4 Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
Record Declaration . . . . . . . . . . . . . . . . . . . . . . . . . . 144
Accessing Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
The WITH Statement . . . . . . . . . . . . . . . . . . . . . . . . . 146
8-5 Nesting Records. . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152
Answers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
Chapter 9: Files and Applications . . . . . . . . . . . . . . . . . . . . . . . 155
9-1 Data Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
9-2 TEXT Files. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
9-3 Reading a TEXT File . . . . . . . . . . . . . . . . . . . . . . . . . 156
File Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
File Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
Opening a File for Input: RESET . . . . . . . . . . . . . . . . . . . 157
Closing the File . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
File Input Procedures: READ, READLN . . . . . . . . . . . . . . . 158
The EOF and EOLN Functions . . . . . . . . . . . . . . . . . . . . 159
Example: Disk-File Text Analyzer. . . . . . . . . . . . . . . . . . . 159
9-4 Displaying a TEXT File . . . . . . . . . . . . . . . . . . . . . . . . 162
Reading a TEXT File as a Set of Strings . . . . . . . . . . . . . . . 163
Reading Multiple Strings . . . . . . . . . . . . . . . . . . . . . . . 164
9-5 Creating a TEXT File: REWRITE . . . . . . . . . . . . . . . . . . . 165
File Output Procedures: WRITE, WRITELN . . . . . . . . . . . . . 165
Example: Employee File . . . . . . . . . . . . . . . . . . . . . . . 166
Example: Payroll . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
9-6 Non-TEXT Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
Example: Payroll System . . . . . . . . . . . . . . . . . . . . . . . 173
Appending a File . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
9-7 Using the File Buffer Variable . . . . . . . . . . . . . . . . . . . . . 178
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
Answers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
Chapter 10: Using Variant Records. . . . . . . . . . . . . . . . . . . . . . . 183
10-1 Variant Records . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
10-2 Example: Enhanced Payroll System. . . . . . . . . . . . . . . . . 185
10-3 Deleting Records from the File . . . . . . . . . . . . . . . . . . . 192
10-4 Updating Records . . . . . . . . . . . . . . . . . . . . . . . . . . 201
10-5 Enhance the Program Modularity . . . . . . . . . . . . . . . . . . 204
Suggestions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Answers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Chapter 11: Pointers and Linked Lists . . . . . . . . . . . . . . . . . . . . . 217
11-1 Dynamic Memory Allocation . . . . . . . . . . . . . . . . . . . . . 217
11-2 Pointers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
Pointer Operations . . . . . . . . . . . . . . . . . . . . . . . . . . 220
Pointers to Records. . . . . . . . . . . . . . . . . . . . . . . . . . 222
Passing Pointers as Parameters . . . . . . . . . . . . . . . . . . . 224
11-3 Basics of Linked Lists . . . . . . . . . . . . . . . . . . . . . . . . 225
List Declaration . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
Building a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
Reading a List. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
Example: A Linked List Demo . . . . . . . . . . . . . . . . . . . . 229
Storing Lists in Files . . . . . . . . . . . . . . . . . . . . . . . . . 232
Reading Lists from Files . . . . . . . . . . . . . . . . . . . . . . . 233
Example: A List of Records . . . . . . . . . . . . . . . . . . . . . . 234
11-4 Searching Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
11-5 Deleting Nodes from Lists . . . . . . . . . . . . . . . . . . . . . . 243
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
Answers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
The Next Step. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
Appendix A: The ASCII Character Set. . . . . . . . . . . . . . . . . . . . . 257
Appendix B: Reserved Words and Standard Identifiers. . . . . . . . . . . . 261
Appendix C: Answers to Drills . . . . . . . . . . . . . . . . . . . . . . . . . 265
Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317