home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l180 / 1.ddi / JUSTIFY.BAS < prev    next >
Encoding:
BASIC Source File  |  1989-02-07  |  5.2 KB  |  159 lines

  1.   ' ************************************************
  2.   ' **  Name:          JUSTIFY                    **
  3.   ' **  Type:          Toolbox                    **
  4.   ' **  Module:        JUSTIFY.BAS                **
  5.   ' **  Language:      Microsoft QuickBASIC 4.00  **
  6.   ' ************************************************
  7.   '
  8.   ' Demonstrates the Justify subprogram.
  9.   '
  10.   ' USAGE:           No command line parameters
  11.   ' .MAK FILE:       JUSTIFY.BAS
  12.   '                  EDIT.BAS
  13.   '                  PARSE.BAS
  14.   '                  KEYS.BAS
  15.   ' PARAMETERS:      (none)
  16.   ' VARIABLES:       a$         String to be justified
  17.   '                  col%       Number of columns for each example of Justify
  18.   '                  x$         Working copy of a$
  19.   '                  y$         Working string space
  20.   
  21.   
  22.     DECLARE SUB Justify (a$, n%)
  23.     DECLARE SUB ParseLine (x$, sep$, a$())
  24.     DECLARE SUB FormatTwo (a$, b$, col%)
  25.   
  26.     CLS
  27.     a$ = ""
  28.     a$ = a$ + "This paragraph is used to demonstrate the Justify "
  29.     a$ = a$ + "subprogram.  First, the entire paragraph is "
  30.     a$ = a$ + "placed in a single string variable.  This string "
  31.     a$ = a$ + "is then split between words into shorter strings, "
  32.     a$ = a$ + "and these shorter strings are then justified in "
  33.     a$ = a$ + "order to align both the left and right edges of "
  34.     a$ = a$ + "the text."
  35.   
  36.     FOR col% = 50 TO 70 STEP 10
  37.         x$ = a$
  38.         DO
  39.             FormatTwo x$, y$, col%
  40.             IF y$ <> "" THEN
  41.                 Justify x$, col%
  42.             END IF
  43.             PRINT x$
  44.             x$ = y$
  45.         LOOP WHILE y$ <> ""
  46.         PRINT
  47.     NEXT col%
  48.   
  49.     END
  50.  
  51.   ' ************************************************
  52.   ' **  Name:          Justify                    **
  53.   ' **  Type:          Subprogram                 **
  54.   ' **  Module:        JUSTIFY.BAS                **
  55.   ' **  Language:      Microsoft QuickBASIC 4.00  **
  56.   ' ************************************************
  57.   '
  58.   ' Spaces words with extra spaces until line
  59.   ' is n% characters long.
  60.   '
  61.   ' EXAMPLE OF USE:  Justify a$, n%
  62.   ' PARAMETERS:      a$         String to be justified
  63.   '                  n%         Desired string length
  64.   ' VARIABLES:       ary$()     Array to store individual words from the string
  65.   '                  cnt%       Count of non-space characters
  66.   '                  i%         Looping index
  67.   '                  j%         Count of words
  68.   '                  each%      Minimum space count to insert between words
  69.   ' MODULE LEVEL
  70.   '   DECLARATIONS:  DECLARE SUB Justify (a$, n%)
  71.   '                  DECLARE SUB ParseLine (x$, sep$, a$())
  72.   '                  DECLARE SUB FormatTwo (a$, b$, col%)
  73.   '
  74.     SUB Justify (a$, n%) STATIC
  75.       
  76.       ' If string is shorter than n%, don't bother
  77.         IF LEN(a$) < n% THEN
  78.             EXIT SUB
  79.         END IF
  80.       
  81.       ' Array for list of words from original string
  82.         REDIM ary$(1 TO n%)
  83.       
  84.       ' Split line up into individual words
  85.         ParseLine a$, " ", ary$()
  86.       
  87.       ' Count the words and total of non-space characters
  88.         cnt% = 0
  89.         FOR i% = n% TO 1 STEP -1
  90.             cnt% = cnt% + LEN(ary$(i%))
  91.             IF ary$(i%) = "" THEN
  92.                 j% = i% - 1
  93.             END IF
  94.         NEXT i%
  95.       
  96.       ' If only one or zero words, there's not much we can do
  97.         IF j% < 2 THEN
  98.             a$ = LEFT$(ary$(1) + SPACE$(n%), n%)
  99.             EXIT SUB
  100.         END IF
  101.       
  102.       ' We want an extra space at the ends of sentences, questions, etc.
  103.         FOR i% = 1 TO j% - 1
  104.             IF INSTR(".!?", RIGHT$(ary$(i%), 1)) THEN
  105.                 ary$(i%) = ary$(i%) + " "
  106.                 cnt% = cnt% + 1
  107.             END IF
  108.         NEXT i%
  109.       
  110.       ' How many spaces minimum to add to each word?
  111.         each% = (n% - cnt%) \ (j% - 1)
  112.       
  113.       ' Tack on the minimum spaces to each word
  114.         FOR i% = 1 TO j% - 1
  115.             ary$(i%) = ary$(i%) + SPACE$(each%)
  116.             cnt% = cnt% + each%
  117.         NEXT i%
  118.       
  119.       ' Which is quicker, adding remaining spaces, or
  120.       ' adding spaces to all and removing a few of them?
  121.         IF (n% - cnt%) < j% \ 2 THEN
  122.           
  123.           ' We'll add a few spaces at random
  124.             DO UNTIL cnt% = n%
  125.                 DO
  126.                     i% = INT(RND * (j% - 1) + 2)
  127.                 LOOP UNTIL LEFT$(ary$(i%), 1) <> " "
  128.                 ary$(i%) = " " + ary$(i%)
  129.                 cnt% = cnt% + 1
  130.             LOOP
  131.           
  132.         ELSE
  133.           
  134.           ' We'll add a space to each, and then remove some at random
  135.             FOR i% = 2 TO j%
  136.                 ary$(i%) = " " + ary$(i%)
  137.                 cnt% = cnt% + 1
  138.             NEXT i%
  139.           
  140.           ' Now we'll take a few away at random
  141.             DO UNTIL cnt% = n%
  142.                 DO
  143.                     i% = INT(RND * (j% - 1) + 2)
  144.                 LOOP UNTIL LEFT$(ary$(i%), 1) = " "
  145.                 ary$(i%) = MID$(ary$(i%), 2)
  146.                 cnt% = cnt% - 1
  147.             LOOP
  148.           
  149.         END IF
  150.       
  151.       ' Glue it all back together
  152.         a$ = ary$(1)
  153.         FOR i% = 2 TO j%
  154.             a$ = a$ + ary$(i%)
  155.         NEXT i%
  156.       
  157.     END SUB
  158.  
  159.