ebook img

Learn Pascal PDF

360 Pages·2000·3.82 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Learn Pascal

Y L F M A E T Team-Fly® Learn Pascal Sam A. Abolrous Wordware Publishing, Inc. LibraryofCongressCataloging-in-PublicationData Abolrous,SamA. LearnPascal / bySamA.Abolrous. p. cm. ISBN 1-55622-706-X(pb) 1. Pascal. I. Title. QA76.73.P2A2482000 005.13'3--dc21 99-088051 CIP ©2000,WordwarePublishing,Inc. AllRightsReserved 2320LosRiosBoulevard Plano,Texas75074 Nopartofthisbookmaybereproducedinanyformorby anymeanswithoutpermissioninwritingfrom WordwarePublishing,Inc. PrintedintheUnitedStatesofAmerica ISBN1-55622-706-X 10 9 8 7 6 5 4 3 2 1 0002 Productnamesmentionedareusedforidentificationpurposesonlyandmaybetrademarksoftheirrespectivecompanies. AllinquiriesforvolumepurchasesofthisbookshouldbeaddressedtoWordwarePublishing,Inc.,attheabove address.Telephoneinquiriesmaybemadebycalling: (972)423-0090 Contents Chapter1 GettingStartedwithPascal . . . . . . . . . . . . . . . . . . . . 1 1-1ThePascalVersionof“HelloWorld!”. . . . . . . . . . . . . . . . . . . . . . 1 Comments. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 TheProgramHeading. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 SyntaxandConventions . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1-2DisplayingOutput:WRITELN,WRITE . . . . . . . . . . . . . . . . . . . . . 3 1-3WorkingwithNumbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 RealDivision:/ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 IntegerDivision:DIV . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 RemainderoftheIntegerDivision:MOD. . . . . . . . . . . . . . . . . . . 7 OperatorPrecedenceinArithmeticExpressions. . . . . . . . . . . . . . . 7 1-4Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 VariableDeclaration. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 TheAssignmentStatement . . . . . . . . . . . . . . . . . . . . . . . . . 11 1-5Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 1-6TypeConversion:ROUND,TRUNC . . . . . . . . . . . . . . . . . . . . . 14 1-7ReadingfromtheKeyboard:READLN,READ . . . . . . . . . . . . . . . . 15 1-8FormattingOutput. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Chapter2 DataTypes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 2-1OverviewofDataTypes. . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 2-2NumericTypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 NumericTypesinTurboPascal . . . . . . . . . . . . . . . . . . . . . . . 22 2-3ArithmeticFunctions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 ThePowerFunction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Application:GroceryStore. . . . . . . . . . . . . . . . . . . . . . . . . . 26 TurboPascalAdditionalArithmeticFunctions. . . . . . . . . . . . . . . . 27 2-4TheCHARType. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 StandardFunctionsforCharacters . . . . . . . . . . . . . . . . . . . . . 29 StringsinStandardPascal . . . . . . . . . . . . . . . . . . . . . . . . . 32 2-5TheSTRINGType. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 DeclarationofaString. . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 TheLengthofaString. . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 2-6TheBOOLEANType . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 SimpleBooleanExpressions . . . . . . . . . . . . . . . . . . . . . . . . 35 CompoundBooleanExpressions . . . . . . . . . . . . . . . . . . . . . . 37 iii Contents TruthTables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 TurboPascalLogicalOperators. . . . . . . . . . . . . . . . . . . . . . . 38 PrecedenceofPascalOperators . . . . . . . . . . . . . . . . . . . . . . 38 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Chapter3 SelectionStatements. . . . . . . . . . . . . . . . . . . . . . . 43 3-1MakingDecisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 3-2TheSimpleDecision:IF-THEN . . . . . . . . . . . . . . . . . . . . . . . . 44 Application:PascalCreditCard . . . . . . . . . . . . . . . . . . . . . . . 45 UsingBEGIN-ENDBlocks. . . . . . . . . . . . . . . . . . . . . . . . . . 46 3-3TheIF-THEN-ELSEConstruct . . . . . . . . . . . . . . . . . . . . . . . . 48 3-4TheELSE-IFLadder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Application:ACharacterTester . . . . . . . . . . . . . . . . . . . . . . . 50 3-5NestedConditions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Application:ScoresandGrades. . . . . . . . . . . . . . . . . . . . . . . 52 TipsontheIF-ELSEPuzzles . . . . . . . . . . . . . . . . . . . . . . . . 55 3-6TheMultipleChoice:CASE. . . . . . . . . . . . . . . . . . . . . . . . . . 56 Application:AVendingMachine. . . . . . . . . . . . . . . . . . . . . . . 56 Application:NumberofDaysinaMonth . . . . . . . . . . . . . . . . . . 57 3-7UnconditionalBranching:GOTO . . . . . . . . . . . . . . . . . . . . . . . 59 InfiniteLoops. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 3-8TurboPascalAdditionalFeatures:EXIT,CASE-ELSE. . . . . . . . . . . . 61 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Chapter4 IterationStatements . . . . . . . . . . . . . . . . . . . . . . . 67 4-1Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 4-2TheFORLoop. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Application:PowersofTwo . . . . . . . . . . . . . . . . . . . . . . . . . 70 Application:TheAverage . . . . . . . . . . . . . . . . . . . . . . . . . . 72 4-3SteppingUpandSteppingDown . . . . . . . . . . . . . . . . . . . . . . . 73 Application:TheFactorial . . . . . . . . . . . . . . . . . . . . . . . . . . 73 4-4NestedLoops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 4-5TheWHILELoop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 4-6TheREPEATLoop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 Application:PrimeNumbers. . . . . . . . . . . . . . . . . . . . . . . . . 81 Summary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 Exercises. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 Answers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 Chapter5 StructuredandUser-definedTypes . . . . . . . . . . . . . . . 85 5-1ClassificationofDataTypesinPascal . . . . . . . . . . . . . . . . . . . . 85 5-2OrdinalDataTypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 Enumerations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 iv Contents Subranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 RestrictionsonUsingEnumerationsandSubranges. . . . . . . . . . . . 90 5-3TheTYPESection. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 RenamingTypes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 NamingUser-DefinedTypes . . . . . . . . . . . . . . . . . . . . . . . . 91 5-4Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 5-5One-DimensionalArrays . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 Application:ScoresofOneStudent. . . . . . . . . . . . . . . . . . . . . 95 Application:DisplayingTabulatedResults . . . . . . . . . . . . . . . . . 97 Application:PrimeNumbers—Version2 . . . . . . . . . . . . . . . . . . 99 DeclarationofArraysintheTypeSection . . . . . . . . . . . . . . . . . 101 Application:SortinganArray. . . . . . . . . . . . . . . . . . . . . . . . 102 5-6MultidimensionalArrays . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 Application:ScoresofStudents . . . . . . . . . . . . . . . . . . . . . . 105 ArrayInitialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 Answers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 Chapter6 WorkingwithText . . . . . . . . . . . . . . . . . . . . . . . . 111 6-1StandardInputandOutputFiles. . . . . . . . . . . . . . . . . . . . . . . 111 6-2TipsonOutputStatements. . . . . . . . . . . . . . . . . . . . . . . . . . 112 6-3TipsonInputStatements . . . . . . . . . . . . . . . . . . . . . . . . . . 113 UsingREADLNforNumericInput . . . . . . . . . . . . . . . . . . . . . 113 UsingREADforNumericInput. . . . . . . . . . . . . . . . . . . . . . . 115 UsingREADforCharacterInput. . . . . . . . . . . . . . . . . . . . . . 116 UsingREADLNforCharacterInput . . . . . . . . . . . . . . . . . . . . 118 ReadingMixedTypes. . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 Application:ScramblingLetters . . . . . . . . . . . . . . . . . . . . . . 120 6-4ReadingaLineofText:EOLN. . . . . . . . . . . . . . . . . . . . . . . . 122 Application:CharacterCounter . . . . . . . . . . . . . . . . . . . . . . 122 6-5ReadingaFileofText:EOF . . . . . . . . . . . . . . . . . . . . . . . . . 123 Application:FrequencyCounter . . . . . . . . . . . . . . . . . . . . . . 123 6-6StringManipulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 TipsonStringInput/Output. . . . . . . . . . . . . . . . . . . . . . . . . 124 Application:SortingNames . . . . . . . . . . . . . . . . . . . . . . . . 125 6-7StringFunctionsandProcedures . . . . . . . . . . . . . . . . . . . . . . 127 LENGTH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 CONCAT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 COPY. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 POS. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 INSERT. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 DELETE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 Answers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 v Contents Chapter7 ProceduresandFunctions . . . . . . . . . . . . . . . . . . . 133 7-1ProgramsandSubprograms. . . . . . . . . . . . . . . . . . . . . . . . . 133 7-2Procedures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 7-3ProcedureDefinition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 PassingParameterstoProcedures . . . . . . . . . . . . . . . . . . . . 136 ActualandFormalParameters. . . . . . . . . . . . . . . . . . . . . . . 137 PassingParametersbyValueandbyReference . . . . . . . . . . . . . 138 7-4ReturningValuesfromProcedures:VAR . . . . . . . . . . . . . . . . . . 138 7-5GlobalandLocalVariables . . . . . . . . . . . . . . . . . . . . . . . . . 139 Application:SortingProcedure. . . . . . . . . . . . . . . . . . . . . . . 140 7-6Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 Application:TheFibonacciSequence . . . . . . . . . . . . . . . . . . . 143 7-7TipsontheScopeofVariables . . . . . . . . . . . . . . . . . . . . . . . 144 7-8RecursiveFunctionsandProcedures . . . . . . . . . . . . . . . . . . . . 146 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 Answers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 Chapter8 SetsandRecords . . . . . . . . . . . . . . . . . . . . . . . . 151 8-1TheSETDataType . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 8-2DeclarationandAssignment:SETOF. . . . . . . . . . . . . . . . . . . . 152 DeclarationExamples . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 8-3RulesofUsingSets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 8-4SetOperatorsandExpressions . . . . . . . . . . . . . . . . . . . . . . . 154 Union:+. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 Intersection:*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 Difference:– . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 TipsonUsingSetOperators. . . . . . . . . . . . . . . . . . . . . . . . 157 RelationalOperators . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 PrecedenceofPascalOperators . . . . . . . . . . . . . . . . . . . . . 159 Application:TextAnalyzer . . . . . . . . . . . . . . . . . . . . . . . . . 159 8-5Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 RecordDeclaration. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 AccessingRecordFields. . . . . . . . . . . . . . . . . . . . . . . . . . 162 8-6TheWITHStatement . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 8-7NestingRecords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 Answers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170 Chapter9 DataFiles. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 9-1TheFILEType . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 9-2TEXTFiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 9-3ReadingTEXTFiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 TheFileVariable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 FileParameters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 vi Contents OpeningaFileforInput:RESET . . . . . . . . . . . . . . . . . . . . . 173 ClosingtheFile:CLOSE . . . . . . . . . . . . . . . . . . . . . . . . . . 174 FileInputProcedures:READ,READLN. . . . . . . . . . . . . . . . . . 175 TheEOFandEOLNFunctions. . . . . . . . . . . . . . . . . . . . . . . 175 Application:Disk-FileTextAnalyzer . . . . . . . . . . . . . . . . . . . . 176 9-4DisplayingTEXTFiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 ReadingaTEXTFileasaSetofStrings . . . . . . . . . . . . . . . . . 179 ReadingMultipleStrings . . . . . . . . . . . . . . . . . . . . . . . . . . 180 9-5CreatingaFile:REWRITE. . . . . . . . . . . . . . . . . . . . . . . . . . 181 TheOutputProcedures:WRITE,WRITELN . . . . . . . . . . . . . . . 182 Application:EmployeeFile . . . . . . . . . . . . . . . . . . . . . . . . 183 Application:Payroll . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 9-6FilesofOtherTypes:FILEOF. . . . . . . . . . . . . . . . . . . . . . . . 189 Application:PayrollSystem . . . . . . . . . . . . . . . . . . . . . . . . 189 AppendingaFile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 9-7UsingtheFileBufferVariable:GETandPUT. . . . . . . . . . . . . . . . 195 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 Answers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198 Chapter10 UsingVariantRecords . . . . . . . . . . . . . . . . . . . . . 201 10-1VariantRecords. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 10-2Application:EnhancedPayrollSystem. . . . . . . . . . . . . . . . . . . 203 Remarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210 10-3DeletingRecordsfromtheFile. . . . . . . . . . . . . . . . . . . . . . . 211 10-4UpdatingRecords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219 10-5EnhancetheProgramModularity . . . . . . . . . . . . . . . . . . . . . 222 Suggestions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233 Answers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234 Chapter11 PointersandLinkedLists. . . . . . . . . . . . . . . . . . . . 235 11-1DynamicMemoryAllocation . . . . . . . . . . . . . . . . . . . . . . . . 235 11-2Pointers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236 11-3PointerOperations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238 Assignment. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238 Comparison. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239 11-4PointerstoRecords. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241 11-5PassingPointersasParameters . . . . . . . . . . . . . . . . . . . . . . 243 11-6BasicsofLinkedLists. . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 ListDeclaration. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244 BuildingaList. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244 ReadingaList . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246 Application:ALinkedListDemo . . . . . . . . . . . . . . . . . . . . . . 248 11-7StoringListsinFiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 vii Contents 11-8ReadingListsfromFiles . . . . . . . . . . . . . . . . . . . . . . . . . . 252 Application:BuildingaListofRecords. . . . . . . . . . . . . . . . . . . 253 11-9SearchingLists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254 11-10DeletingNodesfromLists. . . . . . . . . . . . . . . . . . . . . . . . . 262 11-11ArrangingNodesinSequentialOrder. . . . . . . . . . . . . . . . . . . 273 InsertingNodes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273 SearchinganOrderedList . . . . . . . . . . . . . . . . . . . . . . . . . 274 Application:TheFinalLinkedListDatabase. . . . . . . . . . . . . . . . 276 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287 Answers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289 Chapter12 AdvancedProgrammingAlgorithms. . . . . . . . . . . . . . 291 12-1SortingAlgorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291 12-2BubbleSort—EnhancedVersion. . . . . . . . . . . . . . . . . . . . . . 292 TheSwapProcedure. . . . . . . . . . . . . . . . . . . . . . . . . . . . 292 TheBubbleSortProcedure . . . . . . . . . . . . . . . . . . . . . . . . 292 TheBubbleSortProgram . . . . . . . . . . . . . . . . . . . . . . . . . 293 12-3QuicksortAlgorithm. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295 TheDivideandConquerProcedure . . . . . . . . . . . . . . . . . . . . 297 TheQuickSortProcedure . . . . . . . . . . . . . . . . . . . . . . . . . 298 TheQuickSortProgram . . . . . . . . . . . . . . . . . . . . . . . . . . 298 12-4SearchingAlgorithms. . . . . . . . . . . . . . . . . . . . . . . . . . . . 301 12-5LinearSearch. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301 12-6BinarySearch. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301 TheBinarySearchProcedure . . . . . . . . . . . . . . . . . . . . . . . 302 TheBinarySearchProgram . . . . . . . . . . . . . . . . . . . . . . . . 303 12-7BinarySearchTrees . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306 TheBinaryTreeStructure . . . . . . . . . . . . . . . . . . . . . . . . . 306 SearchingtheTree. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308 TraversingtheTree. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310 12-8ProgrammingBinaryTrees. . . . . . . . . . . . . . . . . . . . . . . . . 310 TreeDeclaration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310 BuildingaBinarySearchTree . . . . . . . . . . . . . . . . . . . . . . . 311 TestingtheTree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312 12-9Application:BuildingandPrintingaBinaryTree. . . . . . . . . . . . . . 312 12-10Application:SortingDataFilesUsingBinaryTrees. . . . . . . . . . . . . 319 12-11Application:SearchingaDataFileUsingBinaryTrees . . . . . . . . . 322 TheSearchFunction. . . . . . . . . . . . . . . . . . . . . . . . . . . . 322 TheSearchProgram. . . . . . . . . . . . . . . . . . . . . . . . . . . . 323 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327 TheNextStep . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328 AppendixA TheASCIICharacterSet . . . . . . . . . . . . . . . . . . . . 329 viii Contents AppendixB ReservedWordsandStandardIdentifiers. . . . . . . . . . . 335 AppendixC Operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339 ix

Description:
Preface This book is intended for both beginners and advanced users. As a beginner, you can learn the Pascal language through examples and step-by-step procedures. You start with simple programs that crunch numbers and end up with structured programs and advanced problem-solving algorithms. As an ex
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.