home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 2 / crawlyvol2.bin / program / pascal / passrc / strval.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1985-11-18  |  5.5 KB  |  163 lines

  1. { -----------------------------------------------------------------------------
  2.  
  3.                                  NOTICE:
  4.  
  5.       THESE MATERIALS are UNSUPPORTED by OSS!  If you do not understand how to
  6.       use them do not contact OSS for help!  We will not teach you how to 
  7.       program in Pascal.  If you find an error in these materials, feel free
  8.       to SEND US A LETTER explaining the error, and how to fix it.
  9.  
  10.       THE BOTTOM LINE:
  11.  
  12.          Use it, enjoy it, but you are on your own when using these materials!
  13.  
  14.  
  15.                                DISCLAIMER:
  16.  
  17.       OSS makes no representations or warranties with respect to the contents
  18.       hereof and specifically disclaim all warranties of merchantability or
  19.       fitness for any particular purpose.   This document is subject to change
  20.       without notice.
  21.       
  22.       OSS provides these materials for use with Personal Pascal.  Use them in
  23.       any way you wish.
  24.  
  25.    -------------------------------------------------------------------------- }
  26.  
  27.  
  28. { Examples of "str" and "val" routines for Personal Pascal. }
  29.  
  30. PROGRAM str_val;
  31.  
  32.   TYPE
  33.     { Since we aren't including any of the GEM libraries in this demo, we need
  34.       to declare the STRING type we will work with. }
  35.     str255 = STRING[ 255 ];
  36.  
  37.   VAR
  38.     i, n: integer;
  39.     s: str255;
  40.  
  41.  
  42.  
  43.   { str - Convert the integer in the parameter 'n' to a string in 's'.  The
  44.       string may consist of a minus sign ('-'), followed by up to 5 digits of
  45.       the number.  The string will be the minimal length which will hold the
  46.       number (i.e., leading plus signs and leading zeros will NOT appear in
  47.       the final string!). }
  48.  
  49.   PROCEDURE str( n: integer; VAR s: str255 );
  50.  
  51.     VAR
  52.       digit,            { Holds each digit value of 'n' as it is created }
  53.       divisor,          { Division by this is used to find each digit }
  54.       i: integer;       { Index in string at which to put next character }
  55.       leading: boolean; { True, if the next digit will be the leading digit }
  56.  
  57.     { add_char - Add a single character to the string, incrementing the current
  58.         index. }
  59.  
  60.     PROCEDURE add_char( c: char );
  61.  
  62.       BEGIN
  63.         i := i + 1;
  64.         s[i] := c;
  65.       END;
  66.  
  67.     BEGIN { str - main routine }
  68.       i := 0;           { Start at the beginning of the string }
  69.       IF n < 0 THEN     { If the number is negative, add a minus sign }
  70.         BEGIN
  71.           add_char( '-' );
  72.           n := -n;
  73.         END;
  74.       { Now divide the number by decreasing divisors to form each digit-- the
  75.         divisor starts at 10000, since this is the maximum power of 10 which
  76.         will fit into a positive integer. }
  77.       divisor := 10000;
  78.       leading := true;
  79.       WHILE divisor > 0 DO
  80.         BEGIN
  81.           { Get the next digit value.  If the digit is not zero, or the digit
  82.             will not be the leading digit, then add it to the string (this
  83.             inhibits the addition of leading zeros). }
  84.           digit := n DIV divisor;
  85.           IF (digit <> 0) OR NOT( leading ) THEN
  86.             BEGIN
  87.               add_char( chr(digit + ord('0')) );
  88.               leading := false;
  89.             END;
  90.           { Throw away the part of the number just used, and decrease the
  91.             divisor so we will get the next digit next time. }
  92.           n := n MOD divisor;
  93.           divisor := divisor DIV 10;
  94.         END;
  95.       { At this point, if the index is still zero, then we didn't add any
  96.         characters to the string!  The original number must have been zero, so
  97.         just add that single character. }
  98.       IF i = 0 THEN
  99.         add_char( '0' );
  100.       { Finally, set the length of the string to the final index value. }
  101.       s[0] := chr(i);
  102.     END;
  103.  
  104.  
  105.   { val - Convert the number contained in the string 's' to an integer, and
  106.       return that integer as the function result.  We are assuming the caller
  107.       has ensured the string is a valid number, so we we're just going to
  108.       convert characters into the number until a non-digit is encountered, or
  109.       the end of the string is reached.  A zero is returned as the function
  110.       value if the string that was passed has zero length. }
  111.  
  112.   FUNCTION val( s: str255 ): integer;
  113.  
  114.     VAR
  115.       { Flag to indicate the number has a leading minus sign }
  116.       minus: boolean;
  117.  
  118.     BEGIN
  119.       { Start with the first character of the string, but first skip leading
  120.         blanks. }
  121.       i := 1;
  122.       WHILE (i < length(s)) AND (s[i] = ' ') DO
  123.         i := i + 1;
  124.       { If there are characters still in the string, convert it to a number }
  125.       n := 0;
  126.       IF length(s) >= i THEN
  127.         BEGIN
  128.           { If first char is '-', we have to negate the number after we finish
  129.             converting the digits. }
  130.           IF s[i] <> '-' THEN
  131.             minus := false
  132.           ELSE
  133.             BEGIN
  134.               minus := true;
  135.               i := i + 1;
  136.             END;
  137.           { While there are more digits in the string, convert characters. }
  138.           WHILE (i <= length(s)) AND (s[i] IN ['0'..'9']) DO
  139.             BEGIN
  140.               n := (n * 10) + ord(s[i]) - ord('0');
  141.               i := i + 1;
  142.             END;
  143.           { Negate the final result, if necessary. }
  144.           IF minus THEN
  145.             n := -n;
  146.         END;
  147.       { Return the converted number as the result of this function. }
  148.       val := n;
  149.     END;
  150.  
  151.  
  152.  
  153.   BEGIN
  154.     FOR i := -10 TO 10 DO
  155.       BEGIN
  156.         readln( s );
  157.         n := val(s);
  158.         write( n, ' = ' );
  159.         str( n, s );
  160.         writeln( s );
  161.       END;
  162.   END.
  163.