logo资料库

Fortran自学教材.pdf

第1页 / 共361页
第2页 / 共361页
第3页 / 共361页
第4页 / 共361页
第5页 / 共361页
第6页 / 共361页
第7页 / 共361页
第8页 / 共361页
资料共361页,剩余部分请下载后查看
EGuide to Fortran 2003 Programming E E
Walter S. Brainerd Guide to Fortran 2003 Programming
Walter S. Brainerd Fortran Company 6025 N. Wilmot Road Tucson AZ 85750 USA walt@fortran.com ISBN 978-1-84882-542-0 e-ISBN 978-1-84882-543-7 DOI 10.1007/978-1-84882-543-7 Springer Dordrecht Heidelberg London New York British Library Cataloguing in Publication Data A catalogue record for this book is available from the British Library Library of Congress Control Number: 2009926289 Copyright © Walter S. Brainerd 2009. Published by Springer-Verlag London Ltd. 2009. All Rights Reserverd. Apart from any fair dealing for the purposes of research or private study, or criticism or review, as permitted under the Copyright, Designs and Patents Act. 1988, this publication may only be reproduced, stored or transmitted, in any form or by any means, with the prior permission in writing of the publishers, or in the case of reprographic reproduction in accordance with the terms of licenses issued by the Copyright Licensing Agency. Enquiries concerning reproduction outside those terms should be sent to the publishers. The use of registered names, trademarks, etc., in this publication does not imply, even in the absence of a specific statement, that such names are exempt from the relevant laws and regulations and therefore free for general use. The publisher makes no representation, express or implied, with regard to the accuracy of the information contained in this book and cannot accept any legal responsibility or liability for any errors or omissions that may be made. Cover design: KuenkelLopka GmbH Printed on acid-free paper Springer is part of Springer Science+Business Media (www.springer.com)
Preface Fortran has been the premier language for scientific computing since its introduction in 1957. Fortran originally was designed to allow programmers to evaluate formu- las—FORmula TRANslation—easily on large computers. Fortran compilers are now available on all sizes of machines, from small desktop computers to huge multiproces- sors. The Guide to Fortran 2003 Programming is an informal, tutorial introduction to the most important features of Fortran 2003 (also known as Fortran 03), the latest standard version of Fortran. Fortran has many modern features that will assist the programmer in writing efficient, portable, and maintainable programs that are useful for everything from “hard science” to text processing. Target Audience This book is intended for anyone who wants to learn Fortran 03, including those famil- iar with programming language concepts but unfamiliar with Fortran. Experienced Fortran 95 programmers will be able to use this volume to assimilate quickly those fea- tures in Fortran 03 that are not in Fortran 95 (Fortran 03 contains all of the features of Fortran 95). This guide is not a complete reference work for the entire Fortran lan- guage; it covers the basic features needed to be a good Fortran programmer and an in- troduction to the important new features of Fortran 03. Many older error-prone features have been omitted and some of the more esoteric features that are new to For- tran 03 also are not discussed. To understand some of the features used in old Fortran programs, other sources should be consulted after learning the best basic collection of features for writing new codes or enhancing old ones. Guide to Fortran 2003 Programming is organized so that it may be read from begin- ning to end, but it also is organized so that particular topics may be studied by reading some chapters before previous ones are mastered. To a reasonable extent, all the mate- rial about one topic is presented together, making the book suitable as a reference work, as well as a tutorial. Examples and Case Studies Most of the important features of the Fortran programming language are covered with examples, beginning with the simplest constructs. The book concentrates to some ex- tent on the newer features of the Fortran 03 programming language, because they often provide the best facilities to accomplish a particular programming task. Both the style of the many example programs and the selection of topics discussed in detail guide the
vi Preface reader toward acquiring programming skills to produce Fortran programs that are readable, maintainable, and efficient. Case studies are used to illustrate the practical use of features of Fortran 03 and to show how complete programs are put together. There are also simple problems to en- able the reader to exercise knowledge of the topics learned. Style of the Programming Examples To illustrate the author’s opinion of good Fortran programming style, the program- ming examples do not illustrate the wide variety of styles that might be used. There are certainly many other good programming styles, but it is important to use a style con- sistently within a programming project. The style notes also reflect opinions of the au- thor and reflect but one possible good style to use. Most of the program examples have been run on either the Numerical Algorithms Group NAGWare Fortran 03 compiler (nag.com) or the free g95 compiler (g95.org). Organization of the Content An unusual feature of the book is that the first chapter contains a complete discussion of all the basic features needed to write complete Fortran programs: the form of For- tran programs, data types, simple expressions and assignment, and simple input and output. Subsequent chapters contain detailed discussions of control constructs, mod- ules, procedures, arrays, character strings, data structures and derived types, pointer variables, and object-oriented programming. Module-oriented programming is a very important part of Fortran programming and the topic of modules is introduced earlier to provide the framework for organizing data and procedures for a Fortran program. From the beginning, Fortran has had extensive facilities for input and output; how- ever, this is a topic that is not explored fully in many books because it is a little more difficult than other features and perhaps just not as interesting as some features. The use of these facilities is very important in production programs, so this book contains, in Chapter 11, an extensive discussion of the excellent input/output facilities in Fortran. Appendix A lists the many intrinsic procedures. Appendix B provides a brief infor- mal syntax specification for the language. There still will be occasions when more details about the language must be learned. In these cases it will be necessary to consult the official standard, published by the International Standards Organization or the reference work The Fortran 2003 Hand- book, by Adams, Brainerd, Hendrickson, Maine, Martin, and Smith, Springer, 2009. Many suggestions made by Brian Smith improved the book significantly. Tucson, Arizona, USA Walter S. Brainerd
Contents 1 1 4 11 18 20 24 28 33 41 45 45 45 58 62 77 77 80 81 82 84 86 1 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2 2.1 2.2 2.3 2.4 3 3.1 3.2 3.3 3.4 3.5 3.6 Introduction to Programming in Fortran Programs that Calculate and Print Intrinsic Data Types Variables and Input The Form of a Fortran Program Some Intrinsic Functions Expressions and Assignment Introduction to Formatting Case Study: Quadratic Formula Case Study: Debugging Pendulum Calculations Control Constructs Statement Blocks The if Construct The case Construct The do Construct Modules and Procedures Modules Procedures Subroutines Putting Procedures in a Module Arguments Functions
viii 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16 4 4.1 4.2 4.3 4.4 4.5 4.6 5 5.1 5.2 5.3 6 6.1 6.2 6.3 7 7.1 7.2 7.3 Pure Procedures and Side Effects Argument Passing Using a Function in a Declaration Statement The return Statement Scope The save Attribute Case Study: Numerical Integration Case Study: Calculating Probabilities Recursion Case Study: Adaptive Numerical Integration Arrays Declaring and Using Arrays Searching a List Sorting Selecting Case Study: Solving Linear Equations Case Study: Calculating Probabilities Character Data Use of Character Data in Fortran Programs Text Analysis Case Study: Expression Evaluation Structures and Derived Types Structures Derived Types Declaring and Using Structures IEEE Arithmetic and Exceptions Numerical Representations NaN and Inf Exceptions Contents 89 90 95 96 96 97 97 99 102 109 115 115 131 139 144 147 152 155 155 169 183 191 191 192 195 201 201 202 205
Contents 8 8.1 8.2 8.3 8.4 8.5 8.6 8.7 9 9.1 9.2 9.3 9.4 9.5 9.6 10 10.1 10.2 10.3 10.4 11 11.1 11.2 11.3 11.4 11.5 11.6 11.7 11.8 More about Procedures Date and Time Subroutines Command-Line Arguments Generic Procedures Elemental Procedures More Array Intrinsic Procedures Bit Intrinsic Procedures Calling C Procedures Extending Fortran Extending Assignment Extending Operators User-Defined Operators Extending Intrinsic Functions Derived-Type Editing Computing with Big Integers Pointer Variables The Use of Pointers in Fortran Case Study: Solving a Heat Transfer Problem Linked Lists Trees Input and Output Records Files Data Transfer Statements The open Statement The close Statement The inquire Statement File Positioning Statements Formatting ix 207 207 209 209 212 214 216 218 223 223 225 227 227 229 231 245 245 252 254 262 269 270 272 279 290 293 294 299 301
分享到:
收藏