home *** CD-ROM | disk | FTP | other *** search
/ Chip 1995 March / CHIP3.mdf / programm / prog1 / chap16.txt < prev    next >
Encoding:
Text File  |  1991-07-01  |  13.4 KB  |  292 lines

  1.  
  2.  
  3.  
  4.                                                        Chapter 16
  5.                                                  EXAMPLE PROGRAMS
  6.  
  7.  
  8. WHY IS THIS CHAPTER INCLUDED?
  9. _________________________________________________________________
  10.  
  11. It would be a real disservice to you to discuss all of the
  12. constructs available with Ada, then simply drop you in the middle
  13. of a large program and tell you to put it all together.  This
  14. chapter is intended to illustrate to you, with simple examples, how
  15. to put some of these topics together to build a meaningful program.
  16. The examples are larger than any programs we have examined to this
  17. point, but they are still very simple, and should be easy for you
  18. to follow along and understand what they are doing.
  19.  
  20.  
  21. THE CHARACTER STACK
  22. _________________________________________________________________
  23.  
  24. The program named CHARSTAK.ADA illustrates how   ================
  25. you can define your own stack for use in your      CHARSTAK.ADA
  26. programs.  A stack is a list of homogeneous      ================
  27. items that grows and shrinks in such a way that
  28. the last item entered will always be the first
  29. removed, thus it is often referred to as a Last In, First Out
  30. (LIFO) list.  In order to keep it simple, we will only allow the
  31. stack to store CHARACTER type variables, although it could be
  32. defined to store any type of variables we desired, even arrays or
  33. records.
  34.  
  35.  
  36. THE SPECIFICATION PACKAGE
  37. _________________________________________________________________
  38.  
  39. The specification package is defined in lines 2 through 20, and
  40. gives the user everything he needs to know to use the character
  41. stack package.  You will see that the user does not know how the
  42. stack is implemented, or even how big the stack can grow to unless
  43. he looks into the package body, and if we hide the body from him,
  44. he has no way of knowing this information.  All he can do is use
  45. the package the way we tell him it works.  In a real system, we
  46. would have to tell him how big the stack could grow, because that
  47. would probably be valuable information for him when he designed his
  48. software, but for our purposes we are not going to let him know the
  49. maximum size.  Of course, we must then provide a means to be sure
  50. that he cannot force the stack to get larger than its maximum size.
  51. The procedure Push is defined in such a way that if the stack is
  52. full and the character will not fit, it is simply not put on the
  53. list.  The user must check if the stack is full by calling the
  54. function Is_Full which returns a BOOLEAN value of TRUE if there is
  55. no additional room on the stack.
  56.  
  57.  
  58.                                                         Page 16-1
  59.  
  60.                                     Chapter 16 - Example Programs
  61.  
  62. Careful study of the package specification will reveal that enough
  63. functions and procedures are included to allow effective use of the
  64. character stack.  The package body simply defines the actual
  65. implementation required to do the work defined in the
  66. specification.  Once again, you should be able to understand the
  67. package body since there is nothing here which is new or strange
  68. to you.  It should be pointed out to you that this is actually not
  69. a very efficient way to define a stack, because it is limited to
  70. a single type, but it serves our purposes well as an illustration.
  71. When we study generic packages in part 2 of this tutorial, we will
  72. see a much better way to define the stack so that it can be used
  73. with additional types.  Chapter 31 of part 2 of this tutorial
  74. includes this same program rewritten as a generic package which
  75. can be used with virtually any data type.  You should compile this
  76. file in preparation for using it in conjunction with the next
  77. example program.
  78.  
  79.  
  80. HOW DO WE USE THE CHARACTER STACK?
  81. _________________________________________________________________
  82.  
  83. The example program TRYSTAK.ADA uses the          ===============
  84. character stack included in the last program.       TRYSTAK.ADA
  85. Notice the with and the use clause in lines 4     ===============
  86. and 5.  These tell the system to get a copy of
  87. CharStak, and make it available for use in this
  88. program.  Two string constants are defined for use later, and two
  89. procedures are defined to fill the stack, or add a string of
  90. characters to the stack one at a time, and to empty it a character
  91. at a time.  The main program displays each of the string constants
  92. on the monitor, then uses the two procedures to reverse the string
  93. of characters and output the reversed strings on the monitor.  You
  94. should study the program until you understand how it works, even
  95. though the program itself is not nearly as important to understand
  96. as the concept of the separately compiled package.  Be sure to
  97. compile and run TRYSTAK.ADA to see that it really does what you
  98. expect it to do.
  99.  
  100.  
  101. THE DYNAMIC STRING PACKAGE
  102. _________________________________________________________________
  103.  
  104. The next example program, named DYNSTRNG.ADA,    ================
  105. fulfills a promise made when we were studying      DYNSTRNG.ADA
  106. strings.  At that time, we mentioned that Ada    ================
  107. did not have a dynamic string capability.  This
  108. package adds dynamic strings to Ada to make them
  109. much easier to use, and much more flexible.  This package, although
  110. implementing a complete version of a dynamic string package, is not
  111. submitted as the final word in string packages.  In fact, it has
  112. a problem which we will describe later in this chapter.
  113.  
  114. One construct is used which we have not yet studied in this
  115. tutorial although we did mention it in passing in the last chapter.
  116.  
  117.                                                         Page 16-2
  118.  
  119.                                     Chapter 16 - Example Programs
  120.  
  121. Line 33 contains an unconstrained array type declaration.  Its use
  122. is illustrated in lines 11 and 12 of the program named
  123. TRYSTRNG.ADA.  This will be studied in part 2 of this tutorial.
  124. Other than this one construct, DYNSTRNG.ADA uses no portion of Ada
  125. that we have not yet studied in this tutorial, so it does not take
  126. advantage of the advanced constructs of Ada.  It is meant to be a
  127. teaching aid only, but you could use it in a production program,
  128. which you are welcome to do as a registered customer of Coronado
  129. Enterprises.  When you purchased this tutorial, you purchased the
  130. right to use any of the included software for whatever purpose you
  131. deem necessary, except commercialization of the tutorial itself.
  132. You may need to modify it slightly for your own purposes, and you
  133. have permission to do so.
  134.  
  135. The dynamic string, as defined here, has a maximum length of 255
  136. characters, with the dynamic length stored in the first element of
  137. the string which has a subscript of zero.  The string must
  138. therefore be declared as a CHARACTER string with a lower bound of
  139. 0, and an upper bound representing the maximum length that the
  140. string will ever be asked to store.  This definition comes from
  141. Borland International's implementation of Pascal, which they market
  142. as TURBO Pascal.
  143.  
  144.  
  145. THE DYNSTRNG SPECIFICATION
  146. _________________________________________________________________
  147.  
  148. The specification package of DynStrng is defined in lines 29
  149. through 116 of the file and the comments are complete, giving a
  150. thorough definition of the package, and describing how to use it.
  151. The body is given in lines 124 through 384, and give the actual
  152. implementation.  Note that some software developers give you only
  153. the specification part of the package, which is all you really need
  154. to use it, and hide the body from you.  If they provide the
  155. compiled body, and the specification source, you have all you need
  156. to use the package with that specific compiler.  Your compiler came
  157. with the package Text_IO, a standard package, and your compiler
  158. writer almost certainly did not provide you with the source code
  159. to the body, and probably did not give you the source code to the
  160. specification in a file, but only on the printed page.  The Text_IO
  161. package, as defined in the LRM, is only the specification part of
  162. the package Text_IO.  It can be found in section 14.3.10 of the
  163. LRM.
  164.  
  165. You should take notice of the fact that we are overloading the name
  166. Put on line 36 of this package so we can use it to output a dynamic
  167. string to the monitor.  The system knows which one we wish to use
  168. by the type of the actual parameter used.  The careful observer
  169. will notice that we use the Text_IO version of Put in line 132 of
  170. the procedure used to overload the name Put.  Continuing the
  171. discussion of overloading, you will see that we define two
  172. procedures with the same name in lines 60 and 63, once again
  173. depending on the types to select the proper procedure for each
  174. call.
  175.  
  176.                                                         Page 16-3
  177.  
  178.                                     Chapter 16 - Example Programs
  179.  
  180. Study the DynStrng package until you feel you understand it fairly
  181. well, then compile the file to prepare for the next program.
  182.  
  183.  
  184. USING DYNAMIC STRINGS
  185. _________________________________________________________________
  186.  
  187. The example program TRYSTRNG.ADA is designed to  ================
  188. use the dynamic string package in various ways     TRYSTRNG.ADA
  189. by defining strings, inserting characters or     ================
  190. strings, deleting portions of strings, and
  191. displaying the results.  This program was
  192. written to test the DynStrng package so it does a lot of silly
  193. things.  There is nothing new or innovative in this utilitarian
  194. program, so you will be left on your own to understand, compile,
  195. and execute it.
  196.  
  197.  
  198. A PROBLEM WITH DYNSTRNG.ADA
  199. _________________________________________________________________
  200.  
  201. The DynStrng package works just fine the way it is used in the
  202. TryStrng package, but a problem appears when it is used to copy a
  203. string constant into a dynamic string.  The problem is due to
  204. overloading the name Copy in lines 60 and 63 of the DynStrng
  205. specification, and is best illustrated with an example.  Consider
  206. the following line of Ada code;
  207.  
  208.      Copy("Line of text.",Stuff,Result);
  209.  
  210. In this case, the compiler finds that the string constant could be
  211. of type STRING or of type DYNAMIC_STRING, and does not know which
  212. overloading to use, so it gives a compile error saying that it
  213. cannot resolve the type.  The way to use this package to do this
  214. would be to tell the compiler which one to use by qualifying the
  215. string constant as shown in this line of code.
  216.  
  217.      Copy(STRING'("Line of text."),Stuff,Result);
  218.  
  219. This will completely resolve the ambiguity and the program will
  220. then compile and execute properly.  You should include these two
  221. lines in a test program to see for yourself that this does resolve
  222. the ambiguity.
  223.  
  224.  
  225. NOW TO FIX THE PROBLEM
  226. _________________________________________________________________
  227.  
  228. There is an excellent solution to this problem that will render
  229. this dynamic string package flexible and useful but it requires the
  230. use of a discriminated record which we have not yet studied in this
  231. tutorial.  In part 2 of this tutorial, we will revisit this dynamic
  232. string package and offer a much more flexible package for your
  233. information and use.
  234.  
  235.                                                         Page 16-4
  236.  
  237.                                     Chapter 16 - Example Programs
  238.  
  239.  
  240. HOW OLD ARE YOU IN DAYS?
  241. _________________________________________________________________
  242.  
  243. The example program named AGE.ADA, is a silly     ===============
  244. little program, but intended to illustrate how        AGE.ADA
  245. to effectively use the keyboard for input to a    ===============
  246. program.  This program will ask you for today's
  247. date, and your birthday, then calculate your age
  248. in days.  There is no provision for leap year, or even for months
  249. with other than 31 days.  It is intended to illustrate how to put
  250. together an interactive program that could be useful in some way.
  251.  
  252. Once again, since we have not studied the advanced topics of Ada
  253. yet, we have a limited number of constructs to use.  The example
  254. programs at the end of Part 2 of this tutorial repeats this
  255. program, but uses the predefined Ada package Calendar to get
  256. today's date rather than asking the user to supply it.  The
  257. advanced topics will add flexibility to your use of Ada.  Compile
  258. and run this program to get a feel for how to write an interactive
  259. program.
  260.  
  261.  
  262. PROGRAMMING EXERCISES
  263. _________________________________________________________________
  264.  
  265. 1.   Add an additional function to CHARSTAK.ADA to return a value
  266.      indicating how many more characters can be pushed onto the
  267.      stack.
  268.  
  269. 2.   Use the new function in TRYSTAK.ADA to output a message to the
  270.      monitor indicating the amount of space remaining on the stack
  271.      at the end of the Fill_The_Stack procedure.
  272.  
  273. 3.   A MAJOR PROGRAMMING ASSIGNMENT
  274.  
  275.      The best way to learn Ada is to use it, so the following
  276.      programming suggestion is given.  After studying the package
  277.      DYNSTRNG.ADA, put it away and attempt to duplicate it from
  278.      scratch.  You will find that you will use nearly every topic
  279.      covered in part 1 of this tutorial, and if you get completely
  280.      stumped, you will have the supplied version of the package to
  281.      help you over the rough spots.  Your goal should be to
  282.      duplicate the supplied package so closely that the existing
  283.      program named TRYSTRNG.ADA can use your new version.
  284.  
  285.      If you find DYNSTRNG.ADA too big for a first step, you may
  286.      wish to try to duplicate CHARSTAK.ADA in the same manner
  287.      before jumping into the dynamic string effort.
  288.  
  289.  
  290.  
  291.  
  292.                                                         Page 16-5