资料内容讲义参考programming via f_第1页
资料内容讲义参考programming via f_第2页
资料内容讲义参考programming via f_第3页
资料内容讲义参考programming via f_第4页
资料内容讲义参考programming via f_第5页
已阅读5页,还剩309页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Object Oriented Program via Fortran 90/95Ed AkinRice UniversityMechanical Engineering and Materials Science Department Houston, TexasMay 29, 2001Draft # 4.2, Copyright c 2001, .iiContentsPrefacevii1 Program Design11.1. Introduction11.2. Problem Definition31.3. Modular Program Design61.4. Program Com

2、position91.4.1. Comments91.4.2. Statements91.4.3. Flow Control111.4.4. Functions131.4.5. Modules151.4.6. Dynamic Memory Management151.5. Program evaluation and testing151.6. Program documentation171.7. Object Oriented Formulations181.8. Exercises212 Data Types232.1. Intrinsic Types232.2. User Define

3、d Data Types252.3. Abstract Data Types272.4. Classes292.5. Exercises313 Object Oriented Program Concepts333.1. Introduction333.2. Encapsulation, Inheritance, and Polymorphism343.2.1. Example Date , and Classes373.3. Object Oriented Numerical Calculations383.3.1. A Rational Number Class and Operator

4、Overloading393.4. Discussion423.5. Exercises484 Features of Program Languages514.1. Comments514.2. Statements and Expressions524.3. Flow Control574.3.1. Explicit Loops584.3.2. Implied Loops604.3.3. Conditionals614.4. Subprograms68 c 2001 J.E. Akiniii4.4.1. Functions and Subroutines684.4.2. Global Va

5、riables724.4.3. Bit Functions744.4.4. Exception Controls744.5. Interface Prototype754.6. Characters and Strings764.7. User Defined Data Types804.7.1. Overloading Operators844.7.2. User Defined Operators864.8. Pointers and Targets864.8.1. Pointer Type Declaration874.8.2. Pointer Assignment884.8.3. Us

6、ing Pointers in Expressions884.8.4. Pointers and Linked Lists884.9. Accessing External Source Files and Functions894.10. Procedural Applications904.10.1. Fitting Curves to Data904.10.2. Sorting924.11. Exercises995 Object Oriented Methods1035.1. Introduction1035.2. The Drill Class1035.3. Global Posit

7、ioning Satellite Distances1065.4. Exercises1186 Inheritance and Polymorphism1196.1. Introduction1196.2. Example Applications of Inheritance1216.2.1. The Professor Class1216.2.2. The Employee and Manager Classes1216.3. Polymorphism1246.3.1. Templates1256.3.2. Subt Objects (Dynamic Dispatching)1306.4.

8、 Exercises1337 OO Data Structures1357.1. Data Structures1357.2. Stacks1357.3. Queues1397.4. Linked Lists1427.4.1. Singly Linked Lists1427.4.2. Doubly Linked Lists1487.5. Direct (Random) Access Files1497.6. Exercises1538 Arrays and Matrices1558.1. Subscripted Variables: Arrays1558.1.1. Initializing A

9、rray Elements1588.1.2. Intrinsic Array Functions1598.1.3. Colon Operations on Arrays (Subscript Triplet)1598.1.4. Array Logical Mask Operators1638.1.5. User Defined Operators1658.1.6. Connectivity Lists and Vector Subscripts166 c 2001 J.E. Akiniv8.1.7. Component Gather and Scatter1688.2. Matrices170

10、8.2.1. Matrix Algebra1728.2.2. Inversion1748.2.3. Factorizations1748.2.4. Determinant of a Matrix1758.2.5. Matrix Calculus1768.2.6. Computation with Matrices1768.3. Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1789 Advanced Topics1819.1. Templates . . .

11、 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1819.2. Subt Objects (Dynamic Dispatching) . . . . . . . . . . . . . . . . . . . . . . . . 1839.3. Non-standard Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184A Bibliography187B Fortran 90 O

12、verview191B.1. List of Language Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191B.2. Alphabetical Table of Fortran 90 Intrinsic Routines . . . . . . . . . . . . . . . . . . . 17B.3. Syntax of Fortran 90 Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . .

13、. 29C Selected Exercise Solutions47C.1. Problem 1.8.1 : Checking trigonometric identities. . . . . . . . . . . . . . . . . . . 47C.2. Problem 1.8.2 : Newton-Raphson algorithm. . . . . . . . . . . . . . . . . . . . . . 47C.3. Problem 1.8.3 : Game of life. . . . . . . . . . . . . . . . . . . . . . . .

14、 . . . . . . 48C.4. Problem 2.5.1 : Conversion factors . . . . . . . . . . . . . . . . . . . . . . . . . . . 49C.5. Problem 3.5.3 : Creating a vector class . . . . . . . . . . . . . . . . . . . . . . . . . 50C.6. Problem 3.5.4 : Creating a sparse vector class. . . . . . . . . . . . . . . . . . . . .

15、 56C.7. Problem 4.11.1 : Count the lines in an external file . . . . . . . . . . . . . . . . . . . 61C.8. Problem 4.11.3 : Computing CPU time useage . . . . . . . . . . . . . . . . . . . . . 62C.9. Problem 4.11.4 : Converting a string to upper case . . . . . . . . . . . . . . . . . . . 62C.10. Probl

16、em 4.11.8 : two values from each line of an external file . . . . . . . . . . 63C.11. Problem 4.11.14 : Two line least square fits . . . . . . . . . . . . . . . . . . . . . . . 63C.12. Problem 4.11.15 : Find the next available file unit. . . . . . . . . . . . . . . . . . . 65C.13. Problem 5.4.4 : Po

17、lymorphic interface for the class Position Angle . . . . . . . . . 66C.14. Problem 6.4.1 : Using a function with the same name in two classes . . . . . . . . . . 67C.15. Problem 6.4.3 : Revising the employee-manager classes . . . . . . . . . . . . . . . . 67D Companion C+ Examples69D.1. Introduction

18、 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69EGlossary of Object Oriented Terms77F Subject Index0G Program Index1 c 2001 J.E. AkinvPrefaceThe as been an explosion of interest in, and books on object-oriented program (OOP). Why have yet another book on the subjec

19、t? In the past a basic education was said to master the three rs: ing, riting, and rithmetic. Today a sound education in engineering program leads to producing code that satisfy the four rs: ability, reusability, reliability, and really-efficient. While some object-oriented program languages have so

20、me of these abilities Fortran 90/95 offers all of them for engineering applications. Thus this book is intended to take a different tack by using the Fortran 90/95 language as its main OOP tool. With more than one hundred pure and hybrid object-oriented languages available, one must be selective in

21、deciding which ones merit the effort of learning to utilize them. There are millions of Fortran programmers, so it is logical to present the hybrid object-oriented features of Fortran 90/95 to them to update and expand their program skills. This work provides an introduction to Fortran 90 as well as

22、 to object-oriented program concepts. Even with the current release (Fortran 95) we will demonstrate that Fortran offers essentially all of the tools recommended for object-oriented program techniques. It is expected that Fortran 200X will offer additional object-oriented capabilities, such as decla

23、ring ”extensible” (or virtual) functions. Thus, it is expected that the tools learned here will be of value far into the future.It is commonly agreed that the two decades old F77 standard for the language was missing several useful and important concepts of computer science that evolved and were pop

24、ular after its release, but it also had a large number of powerful and useful features. The following F90 standard included a large number of improvements that have often been overlooked by many programmers. It is fully compatible with all old F77 standard code, but it declared several features of t

25、hat standard as obsolete. That was done to encourage programmers to learn better methods, even though the standard still supports those now obsolete language constructs. The F90 standards committee brought into the language most of the best features of other more recent languages like Ada, C, C+, Ei

26、ffel, etc. Those additions included in part: structures, dynamic memory management, recursion, pointers (references), and abstract data types along with their supporting tools of encapsulation, inheritance, and the overloading of operators and routines. Equally important for those involved in numeri

27、cal analysis the F90 standard added several new features for efficient array operations that are very similar to those of the popular environment. Most of those features include additional options to employ logical filters on arrays. All of the new array features were intended for use on vector or p

28、arallel computers and allow programmers to avoid the bad habit of writing numerous serial loops. The current standard, F95, went on to add more specific parallel array tools, provided “pure” routines for general parallel operations, simplified the use of pointers, and a few user friendly refinements

29、 of some F90 features. , at this time one can view F90/95 as the only cross-platform international standard language for parallel computing. Thus Fortran continues to be an important program language that richly rewards the effort of learning to take advantage of its power, clarity, and user friendl

30、iness.We begin that learning process in Chapter 1 with an overview of general program techniques. Primarily the older “procedural” approach is discussed there, but the chapter is d with an outline of the newer “object” approach to program. An experienced programmer may want to skip directly to the l

31、ast section of Chapter 1 where we outline some object-oriented methods. In Chapter 2, we introduce the concept of the abstract data types and their extension to classes. Chapter 3 provides a fairly ed introduction to the concepts and terminology of object-oriented program. A much larger supporting g

32、lossary is provided as an appendix.For the sake of completeness Chapter 4 introduces language specific s of the topics discussed in c 2002 J.E. Akinithe first chapter. The Fortran 90/95 syntax is used there, but in several cases cross-references are to similar constructs in the C+ language and the e

33、nvironment. While some ers may want to skip Chapter 4, it will help others learn the Fortran 90/95 syntax and/or to related publications that use C+ or . All of the syntax of Fortran 90 is also given in an appendix.Since many Fortran applications relate to manipulating arrays or doing numerical matr

34、ix analysis, Chapter 5 presents a very ed coverage of the powerful intrinsic features Fortran 90 has added to provide for more efficient operations with arrays. It has been demonstrated in the literature that object- oriented implementations of scientific projects requiring intensive operations with

35、 arrays execute much faster in Fortran 90 than in C+. Since Fortran 90 was designed for operations on vector and parallel machines that chapter encourages the programmer to avoid unneeded serial loops and to replace them with more efficient intrinsic array functions. ers not needing to use numerical

36、 matrix analysis may skip Chapter 5.Chapter 6 returns to object-oriented methods with a more ed coverage of using object-oriented analysis and object-oriented design to create classes and demonstrates how to implement them as an OOP in Fortran 90. Additional Fortran 90 examples of inheritance and po

37、lymorphism are given in Chapter7. Object-oriented programs often require the objects to be stored in some type of “container” or data structure such as a stack or linked-list. Fortran 90 object-oriented examples of typical containers are given in Chapter 8. Some specialized topics for movanced users

38、 are given in Chapter 9, so beginning programmers could skip it.To summarize the two optional uses of this text; it is recommended that experienced Fortran program- mers wishing to learn to use OOP cover Chapters 2, 3, 6, 7, 8, and 9, while s studying Fortran for the first time should cover Chapters

39、 1, 2, 3, and. Anyone needing to use numerical matrix analysis should also include Chapter 5.A OO glossary is included in an appendix to aid in ing this text and the current literature on OOP. Another appendix on Fortran 90 gives an alphabetical listing on its intrinsic routines, a subject based lis

40、t of them, a ed syntax of all the F90 statements, and a set of example uses of every statement. Selected solutions for most of the assignments are included in another appendix along with comments on those solutions. The final appendix gives the C+ versions of several of the F90 examples in the text.

41、 They are provided as an aid to understanding other OOP literature. Since F90 and are so similar the corresponding versions often directly follow the F90 examples in the text.Ed Akin, Rice University, 2002AcknowledgementsWe are all indebted to the hundreds of programmers that labor on various standa

42、rds committees to con- tinually improve all program languages. Chapter 1 is a modification of introductory program notes developed jointly with Prof. Don Johnson at Rice University. I would like to thank Prof. Tinsley Oden and the Texas Institute for Computational Mathematics for generously hosting

43、my sabbatical leave where most of this work was developed, and Rice University for finng the sabbatical. Special thanks go to my wife, Kimberly, without whose support and infinite patience this book would not have been completed.Source CodesAll of the example programs and selected solutions are incl

44、uded on the CD-ROM provide with the book. To be able on various platforms they have been written with the ISO9660 standard format. Additional files are provided to relate the ISO standard short filenames to the full length program names used in the book. Of course, the source files will have to be p

45、rocessed through a Fortran 90 or 95 or 2000 compiler to form executables. All of the figures are also provided as encapsulated Postscript (tm) files. c 2002 J.E. AkiniiIndex , 53, 56=, 53n, 122*, 10, 56*, 56+, 53, 56/, 10, 56:, 25, 53=, 10=, 143%, 51, 143&, 10, 34, 37, 42/=, 53=, 53=, 121ABS functio

46、n, 56, 162, 250absolute value, 56, 162abstract class, 285abstract data type, 15, 23, 27, 285abstraction, 19, 27, 285access, 36access operation, 142access restriction, 19accessibility, 19accessor, 18, 285ACHAR function, 77, 80ACOS function, 56, 162actual argument, 56Ada, 15, 33addition, 56ADJUSTL fun

47、ction, 77ADJUSTR function, 77 ADT, see abstract data type ADVANCE specifier, 42, 102agent, 18AIMAG function, 56, 162AINT function, 56, 162algorithm, 51ALL function, 162, 255all mask elements true, 162 allocatable array, 156, 157, 285ALLOCATABLE attribute, 183ALLOCATABLE statement, 15allocate, 42ALLO

48、CATE statement, 15, 74, 92, 181, 183ALLOCATED function, 15, 181, 183allocation status, 74, 181, 258AND operand, 42, 63, 104AND operator, 53ANINT function, 162ANY function, 162, 181 any mask element true, 162 arc cosine, 56arc sine, 56arc tangent, 56arccosine, 162arcsine, 162arctangent, 162arctangent

49、 for complex number, 162 area, 34argument, 285inout, 69input, 69interface, 75none, 69number of, 75optional, 75, 76order, 75output, 69rank, 75returned value, 75type, 75array, 26, 60, 66, 82, 135, 149, 285allocatable, 156assumed shape, 76automatic, 89, 156Boolean, 164constant, 156dummy dimension, 156f

50、lip, 166mask, 164, 179of pointers, 135rank, 76, 155, 157, 166rectangular, 166reshape, 155shape, 155shift, 1681size, 155total, 162unknown size, 76variable rank, 156array operations, 159array pointer, 285 array shape vector, 162ASCII character set, 23, 76, 77, 98, 159ASIN function, 56, 162assembly lan

51、guage, 15assignment operator, 10, 39, 189, 285assignment statement, 285ASSOCIATED function, 15, 75, 88, 130, 132,181association, 285associative, 172, 173asterisk (*), 58ATAN function, 56, 162ATAN2 function, 13, 56, 162attribute, 103, 104, 107, 119, 123, 192, 285name, 19private, 27, 123public, 27term

52、inator, 25attribute terminator, 25attributes, 19, 27automatic array, 89, 156, 157, 285automatic deallocation, 29BACKSPACE statement, 75bad style, 158base 10 logarithm, 56, 162base class, 119, 286behavior, 104, 107binary file, 159binary operator, 286binary , 268binary write, 183 bitclear, 74extract,

53、74set, 74shift, 74test, 74 bit functionBIT SIZE, 74BTEST, 74IAND, 74IBCLR, 74IBITS, 74IBSET, 74IEOR, 74IOR, 74ISHFT, 74ISHFTC, 74MVBITS, 74NOT, 74TRANSFER, 74bit manipulation, 74 blanksall, 77leading, 77trailing, 77Boolean type, 53Boolean value, 23bottom-up, 4boundary condition, 192bounds, 155bubble

54、 sort, 92, 94ordered, 95bug, 9C, 1, 33, 52C+, 1, 10, 14, 24, 33, 52, 58, 59, 76, 81, 102,121call by reference, 286 call by value, 286CALL statement, 42, 76, 86, 89, 92, 97, 121,123, 124, 131, 137, 140, 142, 143,149CASE DEFAULT statement, 63, 188CASE statement, 63, 188, 272cases, 62CEILING function,

55、56, 162 central processor unit, 72 CHAR function, 77character, 81case change, 80control, 76from number, 80functions, 77non-print, 76, 102strings, 76to number, 80character set, 23CHARACTER type, 23, 26, 53chemical element, 25chemical element, 128circuits, 166circular shift, 168circular-linked list, 1

56、85class, 15, 19, 33, 286base, 18Date, 118, 121derived, 18Drill, 103Employee, 123Geometric, 118 c 2002 J.E. Akin2Global Position, 112Great Arc, 112hierarchy, 33instance, 33iterator, 192Manager, 123, 133, 118, 121polymorphic, 131Position Angle, 107, 112Professor, 121sparse vector, 258, 118, 121class a

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论