home *** CD-ROM | disk | FTP | other *** search
/ Oakland CPM Archive / oakcpm.iso / sigm / vol132 / getnums.p < prev    next >
Encoding:
Text File  |  1984-04-29  |  3.5 KB  |  77 lines

  1.   PROCEDURE getnums(VAR wrd: wrdarray; nwrds: byte; VAR nmbr: numarray; 
  2.                      VAR n: byte);
  3. {        +------------------------------------------------------------+
  4.          |WRITTEN FOR ZUG USE.                  PASCAL Z, version 4.0 |
  5.          |BY       Clif Kinne                   DATE:  12/15/82       |
  6.          |                                                            |
  7.          |PURPOSE  This procedure will convert an array of numeric    |
  8.          |         strings into an array of integers.  It was designed|
  9.          |         as an adjunct to the procedure, READCMD.           |
  10.          |                                                            |
  11.          |CALL:    Getnums(name,nargs,num,numct);   - typical         |
  12.          |                                                            |
  13.          |ACCEPTS: 1.  An array, WRD, of strings, such as the output  |
  14.          |             array returned by  READCMD.                    |
  15.          |         2.  The number, NWRDS, of non-dummy members of WRD.|
  16.          |                                                            |
  17.          |RETURNS: 1.  A new array, NMBR, of the integer values of all|
  18.          |             input strings which begin with a digit or a    |
  19.          |             minus sign.                                    |
  20.          |         2.  The number, N, of members of this array, NMBR. |
  21.          |                                                            |
  22.          |GLOBALS  CONST maxword  = max digits allowed in WRD[i]      |
  23.          |REQUIRED TYPE  wrdtype  = string maxword;                   |
  24.          |               wrdarray = ARRAY[1..maxargs] OF wrdtype;     |
  25.          |               numarray = ARRAY[1..maxargs] OF INTEGER;     |
  26.          |               byte     = 0..255;                           |
  27.          |         FUNCTION ctoi (buf: wrdtype; i: INTEGER): INTEGER; |
  28.          +------------------------------------------------------------+
  29. .pa}
  30.     VAR   i : byte;
  31.  
  32.     FUNCTION ctoi ( buf: wrdtype; i: INTEGER ): INTEGER;
  33. {$R-}
  34.       LABEL 1;
  35.       VAR    ch : char;
  36.           sign,       { for signed number }
  37.             val :INTEGER;
  38.   
  39.       BEGIN {------------------------------ctoi-------------------------------}
  40.         val := 0;
  41.         sign := 1;
  42.         IF buf[i] = '-' THEN
  43.           BEGIN
  44.             sign := -1; i := i + 1
  45.           END;
  46.         ch := buf[i];
  47.         WHILE ch IN ['0'..'9'] DO
  48.           BEGIN                { CHECK INTEGER WILL BE WITHIN RANGE 0..MAXINT }
  49.             IF ( val<3276 ) OR ((val=3276) AND (ch<'8')) 
  50.               THEN val := val * 10 + ORD(ch) - 48  {ord('0')}
  51.               ELSE 
  52.                 BEGIN
  53.                   val := maxint; { overflow }
  54.                   WRITELN(' ':5,'Numeric arguments may not exceed +/- 32767.');
  55.                   WRITELN;
  56.                   {EXIT} GOTO 1
  57.                 END;
  58.             i := i + 1;
  59.             ch := buf[i]      {THIS CAN GIVE "OUT OF RANGE" ERRORS IF "$R-" IS}
  60.           END{while};         {                                        OMITTED}
  61.   1:    ctoi := val * sign
  62.   {$R+}
  63.       END;  {------------------------------ctoi-------------------------------}
  64.   
  65.     BEGIN {-----------------------------getnums-------------------------------}
  66.  
  67.       n := 0;
  68.       FOR i := 1 TO nwrds DO 
  69.         IF wrd[i,1] IN ['-','0'..'9'] THEN 
  70.           BEGIN
  71.             n := n + 1;
  72.             nmbr[n] := ctoi(wrd[i],1);
  73.           END;
  74.  
  75.     END;  {-----------------------------getnums-------------------------------}
  76.  
  77.