home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / diverses / leda / man / misc.tex < prev    next >
Encoding:
Text File  |  1991-11-15  |  6.5 KB  |  211 lines

  1. {\magtwo 7. Miscellaneous}
  2.  
  3. This section describes some additional useful data types, functions and
  4. macros of LEDA. They can be used in any program that includes the
  5. $<$LEDA/basic.h$>$ header file.
  6.  
  7. {\magonebf 7.1 Streams}
  8.  
  9. The stream data types described in this section are all derived from
  10. the \CC stream types $istream$ and $ostream$. Some of these types 
  11. may be obsolete in combination with the latest versions of the 
  12. standard \CC I/O library.
  13.  
  14.  
  15. {\magonebf 7.1.1 File input streams (file\_istream)}
  16.  
  17. An instance $I$ of the data type $file\_istream$ is an \CC istream
  18. connected to a file $F$, i.e., all input operations or operators
  19. applied to $I$ read from $F$. 
  20.  
  21. {\bf 1. Creation of a file input stream} 
  22.  
  23. $file\_istream\ \ \ I(string\ s)$;
  24.  
  25. creates an instance $I$ of type file\_istream connected to the file with
  26. name $s$.
  27.  
  28. {\bf 2. Operations} 
  29.  
  30. All operations and operators ($>>$) defined for \CC istreams can
  31. be applied to file input streams as well.
  32.  
  33. \bigskip
  34. \bigskip
  35. {\magonebf 7.1.2 File output streams (file\_ostream)}
  36.  
  37. An instance $O$ of the data type $file\_ostream$ is an \CC ostream
  38. connected to a file $F$, i.e., all output operations or operators
  39. applied to $O$ write to $F$. 
  40.  
  41. {\bf 1. Creation of a file output stream} 
  42.  
  43. $file\_ostream\ \ \ O(string\ s)$;
  44.  
  45. creates an instance $O$ of type file\_ostream connected to the file with
  46. name $s$.
  47.  
  48. {\bf 2. Operations} 
  49.  
  50. All operations and operators ($<<$) defined for \CC ostreams can
  51. be applied to file output streams as well.
  52.  
  53. {\magonebf 7.1.3 String input streams (string\_istream)}
  54.  
  55. An instance $I$ of the data type $string\_istream$ is an \CC istream
  56. connected to a string $s$, i.e., all input operations or operators
  57. applied to $I$ read from $s$. 
  58.  
  59. {\bf 1. Creation of a string input stream} 
  60.  
  61. $string\_istream\ \ \ I(string\ s)$;
  62.  
  63. creates an instance $I$ of type string\_istream connected to the string $s$.
  64.  
  65. {\bf 2. Operations} 
  66.  
  67. All operations and operators ($>>$) defined for \CC istreams can
  68. be applied to string input streams as well.
  69.  
  70. \bigskip
  71. \bigskip
  72. {\magonebf 7.1.4 String output streams (string\_ostream)}
  73.  
  74. An instance $O$ of the data type $string\_ostream$ is an \CC ostream
  75. connected to an internal string buffer, i.e., all output operations 
  76. or operators applied to $O$ write into this internal buffer.  The current
  77. value of the buffer is called the contents of $O$.
  78.  
  79. {\bf 1. Creation of a string output stream} 
  80.  
  81. $string\_ostream\ \ \ O$;
  82.  
  83. creates an instance $O$ of type string\_ostream.
  84.  
  85. {\bf 2. Operations} 
  86. \medskip
  87. \cleartabs
  88. \+\hskip 1.8truecm &\hskip 5truecm &\cr
  89. \medskip
  90. \+string      &O.clear()  &clears the contents of $O$\cr
  91. \medskip
  92. \+string      &O.str()    &returns the current contents of $O$\cr
  93. \smallskip
  94.  
  95. All operations and operators ($<<$) defined for \CC ostreams can
  96. be applied to string output streams as well.
  97.  
  98.  
  99.  
  100. {\magonebf 7.1.5 Command input streams (cmd\_istream)}
  101.  
  102. An instance $I$ of the data type $cmd\_istream$ is an \CC istream
  103. connected to the output of a shell command $cmd$, i.e., all input operations 
  104. or operators applied to $I$ read from the standard output of command $cmd$. 
  105.  
  106. {\bf 1. Creation of a command input stream} 
  107.  
  108. $cmd\_istream\ \ \ I(string\ cmd)$;
  109.  
  110. creates an instance $I$ of type cmd\_istream connected to the output of command $cmd$.
  111.  
  112. {\bf 2. Operations} 
  113.  
  114. All operations and operators ($>>$) defined for \CC istreams can
  115. be applied to command input streams as well.
  116.  
  117. \bigskip
  118. \bigskip
  119. {\magonebf 7.1.6 Command output streams (cmd\_ostream)}
  120.  
  121. An instance $O$ of the data type $cmd\_ostream$ is an \CC ostream
  122. connected to the input of a shell command $cmd$, i.e., all output operations 
  123. or operators applied to $O$ write into the standard input of command $cmd$. 
  124.  
  125. {\bf 1. Creation of a command output stream} 
  126.  
  127. $cmd\_ostream\ \ \ O(string\ cmd)$;
  128.  
  129. creates an instance $O$ of type cmd\_ostream connected to the input of 
  130. command $cmd$. 
  131.  
  132. {\bf 2. Operations} 
  133.  
  134. All operations and operators ($<<$) defined for \CC ostreams can
  135. be applied to command output streams as well.
  136.  
  137.  
  138. \bigskip
  139. \bigskip
  140. {\magonebf 7.2 Some useful functions and macros}
  141. \smallskip
  142. \cleartabs
  143. \+\hskip 1.8truecm &\hskip 5truecm &\cr
  144. \medskip
  145. \+int      &read\_int(string $s$ = ``'')    
  146.                              &prints $s$ and reads an integer\cr
  147. \smallskip
  148. \+char     &read\_char(string $s$ = ``'')   
  149.                              &prints $s$ and reads a character\cr
  150. \smallskip
  151. \+real     &read\_real(string $s$ = ``'')   
  152.                              &prints $s$ and reads a real number\cr
  153. \smallskip
  154. \+string   &read\_string(string $s$ = ``'') 
  155.                              &prints $s$ and reads a line of input\cr
  156. \smallskip
  157. \+bool     &Yes(string $s$ = ``'')          
  158.                              &returns (read\_char($s$) == `y')\cr
  159. \bigskip
  160. \+void &init\_random()       &initializes the random number generator.\cr
  161. \smallskip
  162. \+real &random()             &returns a real valued random number in $[0,1]$\cr
  163. \smallskip
  164. \+int  &random(int a, int b) &returns a random integer in $[a..b]$\cr
  165. \bigskip
  166. \+real &used\_time()         &returns the currently used cpu time in seconds.\cr
  167. \smallskip
  168. \+real &used\_time(real\& $T$)  
  169.                              &returns the cpu time used by the program from\cr
  170. \+      &                    &$T$ up to this moment and assings the current\cr
  171. \+      &                    &time to $T$.\cr
  172. \smallskip
  173. \+void  &print\_statistics() &prints a summary of the currently used memory\cr
  174.  
  175. \bigskip
  176. \cleartabs
  177. \+{\bf newline} \hskip 2truecm   &cout $<<$ ``\n"\cr
  178. \+\cr
  179. \+{\bf forever}                  &for(;;)\cr
  180. \+\cr
  181. \+{\bf loop(a,b,c)}              &for ($a=b;a<=c;a++$)\cr
  182. \+\cr
  183. \+{\bf in\_range(a,b,c)}         &($b<=a\ \&\&\ a<=c$)\cr
  184. \+\cr
  185. \+{\bf Max(a,b)}                 &($(a>b)\ ?\ a\ :\ b$)\cr
  186. \+\cr
  187. \+{\bf Min(a,b)}                 &($(a>b)\ ?\ b\ :\ a$)\cr
  188.  
  189.  
  190. \bigskip
  191. \bigskip
  192. {\magonebf 7.3 Error Handling}
  193.  
  194. LEDA tests the preconditions of many (not all!) operations. Preconditions
  195. are never tested, if the test takes more than constant time. If the test
  196. of a precondition fails an error handling routine is called. It takes 
  197. an integer error number $i$ and a $char*$ error message string $s$ as 
  198. arguments. It writes $s$ to the diagnostic output (cerr) and terminates 
  199. the program abnormally if $i \ne 0$.
  200.  
  201. Users can provide their own error handling function $handler$ by calling 
  202. \smallskip
  203. set\_error\_handler($handler$).
  204. \smallskip 
  205. After this statement $handler$ is used instead of the default error handler. 
  206. $handler$ must be a function of type  $void\ \ handler(int,char*)$. The 
  207. parameters are replaced by the error number and the error message
  208. respectively.
  209.  
  210. \vfill\eject
  211.