home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume8 / cz / part08 / grid.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-10-01  |  3.3 KB  |  131 lines

  1. /*
  2.  * grid - print grid on standard output
  3.  */
  4.  
  5. #ifndef lint
  6. static char _cpyrgt[] = "Copyright 1989 Howard Lee Gayle";
  7. #endif lint
  8.  
  9. /*
  10.  * This program is free software; you can redistribute it and/or modify
  11.  * it under the terms of the GNU General Public License version 1,
  12.  * as published by the Free Software Foundation.
  13.  *
  14.  * This program is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with this program; if not, write to the Free Software
  21.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  */
  23.  
  24. #include <stdio.h>
  25. #include <howard/port.h>
  26. #include <howard/version.h>
  27. #include <howard/usage.h>
  28.  
  29. MAINVER ("@(#)$Header: grid.c,v 1.4 89/08/21 11:03:01 howard Exp $");
  30. USAGE ("number-of-rows number-of-columns");
  31.  
  32. #include <howard/malf.h>
  33. #include <howard/registers.i>
  34.  
  35. #define MIN_ROW    1        /* minimum row number            */
  36. #define MAX_ROW    9999        /* maximum row number            */
  37. #define MIN_COL    11        /* minimum column number        */
  38. #define    MAX_COL    9999        /* maximum column number        */
  39.  
  40. /* decpad - convert integer to string, pad with minus            */
  41.  
  42. PRIVATE void decpad (s, i)
  43. R1 char    *s; /* output string            */
  44. R2 int     i; /* integer                */
  45.  
  46. /* Function:
  47.  *    Convert i to a right-justified string, padded on the left with '-'
  48.  *    characters to exactly 4 positions plus a NUL terminator.
  49.  * Algorithm:
  50.  *    Call sprintf.  Change leading blanks to '-'.
  51.  * Notes:
  52.  *    1) s must point to a buffer of at least 5 bytes.  Overflow is not
  53.  *       checked.
  54.  */
  55.  
  56. {
  57. SPRINTF (s, "%4d", i);
  58. while (*s == ' ')
  59.     *s++ = '-';
  60. }
  61.  
  62. /* main - main function                            */
  63.  
  64. PUBLIC int main (argc, argv)
  65.    int    argc; /* Number of arguments.*/
  66. R6 bStrT *argv; /* Points to array of argument strings.*/
  67.  
  68. /* Function:
  69.  *    
  70.  * Algorithm:
  71.  *    * Initialize rb.  For each line, compute lb, then output lb and rb.
  72.  * Notes:
  73.  *    
  74.  */
  75.  
  76. {
  77. R1     int   i;                  /* general purpose loop variable    */
  78. R2     char *cp;          /* for setting up rb[]            */
  79. R3     int   nr;          /* number of rows            */
  80. R4     int   nc;          /* number of columns            */
  81. R5     int   nc10;          /* highest column number (divided by 10)
  82.                    * to appear on grid.            */
  83. static char  lb[6] = "<";     /* buffer for columns 1 .. 5 of each line */
  84. static char  rb[MAX_COL - 3]; /* buffer for columns 6 .. nc of
  85.                    * output grid.  These do not change from
  86.                    * line to line.            */
  87.  
  88. if (argc != 3) usage();
  89. nr = mra2i (argv[1], NULBSTR, FALSE, S("Number of rows"),    MIN_ROW, MAX_ROW,
  90.             (bStrT *) NULL);
  91. nc = mra2i (argv[2], NULBSTR, FALSE, S("Number of columns"), MIN_COL, MAX_COL,
  92.             (bStrT *) NULL);
  93. cp = rb;
  94. *cp++ = '-';
  95. *cp++ = '-';
  96. *cp++ = '-';
  97. *cp++ = '-';
  98. *cp++ = '1';
  99. nc10 = (nc - 1) / 10;
  100. for (i = 2; i <= nc10; ++i)
  101.    {
  102.    *cp++ = '-';
  103.    *cp++ = '-';
  104.    *cp++ = '-';
  105.    *cp++ = '-';
  106.    *cp++ = '+';
  107.    *cp++ = '-';
  108.    decpad (cp, i);
  109.    cp += 4;
  110.    }
  111. i *= 10;
  112. for (i -= 9; i != nc; ++i)
  113.    *cp++ = ((i % 5) ? '-' : '+');
  114. *cp++ = '>';
  115. *cp++ = '\n';
  116. *cp = 0;
  117. cp = rb;
  118. for (i = 1; i <= nr; ++i)
  119.    {
  120.    decpad (&lb[1], i);
  121.    FPUTS (lb, stdout);
  122.    FPUTS (cp, stdout);
  123.    }
  124. mfflush (stdout, S("Standard Output"));
  125. exit (SUCCESS);
  126.  
  127. #ifdef lint
  128. return (SUCCESS);
  129. #endif
  130. }
  131.