正在加载图片...
§34.3 FORTRAN 1103 A C at the first SUBROUTINE RPUT (X) position on a line REAL X introduces a C CO7m171e71. C PUSH X ON TOP OF REAL STACK COMMON /STREP/TOP,STACK (2000) INTEGER TOP REAL STACK TOP=TOP+1 STACK (TOP)=X RETURN END This version does not have any overflow control;clearly it should be updated to test for TOP going over the array size.(The next version will correct this.)The function to retum the top element is INTEGER FUNCTION RITEM C C TOP ELEMENT OF REAL STACK C COMMON /STREP/TOP,STACK (2000) INTEGER TOP REAL STACK RITEM=STACK(TOP) RETURN END which would similarly need to test for underflow (empty stack).REMOIE and other features will follow the same pattern.What unites the different routines,making sure that they access the same data,is simply the name of the common block,STREP.(It is in fact possible,in different routines,to pretend that the same common block contains data of different types and sizes if the total memory occupied somehow coincides,although in a family-oriented book like this one it is probably preferable to avoid going into details that might not be entirely suitable for the younger members of the audience). The limitations are obvious:this implementation describes one abstract object (one particular stack of reals),not an abstract data type of which the software can create arbitrarily many instances at run time,as with a class.The Fortran world is very static:you must dimension all the arrays(here to 2000,a number picked arbitrarily).Because there is no genericity,you should in principle declare a new set of routines for each type of stack;hence the names RPUT and R/TEM,where the R stands for Real.One can work around some of these problems,but not without considerable effort.§34.3 FORTRAN 1103 SUBROUTINE RPUT (X) REAL X C C PUSH X ON TOP OF REAL STACK C COMMON /STREP/ TOP, STACK (2000) INTEGER TOP REAL STACK C TOP = TOP + 1 STACK (TOP) = X RETURN END This version does not have any overflow control; clearly it should be updated to test for TOP going over the array size. (The next version will correct this.) The function to return the top element is INTEGER FUNCTION RITEM C C TOP ELEMENT OF REAL STACK C COMMON /STREP/ TOP, STACK (2000) INTEGER TOP REAL STACK RITEM = STACK (TOP) RETURN END which would similarly need to test for underflow (empty stack). REMOVE and other features will follow the same pattern. What unites the different routines, making sure that they access the same data, is simply the name of the common block, STREP. (It is in fact possible, in different routines, to pretend that the same common block contains data of different types and sizes if the total memory occupied somehow coincides, although in a family-oriented book like this one it is probably preferable to avoid going into details that might not be entirely suitable for the younger members of the audience). The limitations are obvious: this implementation describes one abstract object (one particular stack of reals), not an abstract data type of which the software can create arbitrarily many instances at run time, as with a class. The Fortran world is very static: you must dimension all the arrays (here to 2000, a number picked arbitrarily). Because there is no genericity, you should in principle declare a new set of routines for each type of stack; hence the names RPUT and RITEM, where the R stands for Real. One can work around some of these problems, but not without considerable effort. A C at the first position on a line introduces a comment
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有