home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_01_06 / 1n06032a < prev    next >
Text File  |  1990-07-26  |  5KB  |  207 lines

  1. LISTING TWO
  2.  
  3. '  ╔═══════════════════════════════════════════════════╗
  4. '  ║  Background task algorithm:                       ║
  5. '  ║                                                   ║
  6. '  ║  Entry:                                           ║
  7. '  ║     Using a histogram, display the array          ║
  8. '  ║                                                   ║
  9. '  ║     If a key has been pressed,                    ║
  10. '  ║        Process key press using line input routine ║
  11. '  ║                                                   ║
  12. '  ║  Return to Foreground task                        ║
  13. '  ║                                                   ║
  14. '  ╚═══════════════════════════════════════════════════╝
  15.  
  16.  
  17.  
  18.  
  19.     '--- Constants
  20.     CONST FALSE = 0, TRUE = NOT FALSE
  21.     CONST MaxInt = 32767
  22.  
  23.     CONST ArrayLow = 0, ArrayHigh = 9
  24.  
  25.     CONST BarChar$ = "▄"' character code 220
  26.  
  27.   
  28.  
  29.     '--- The default data type will be integers
  30.     DEFINT A-Z
  31.     '$DYNAMIC
  32.  
  33.  
  34.  
  35.     '--- Procedures and Function declarations
  36.     DECLARE FUNCTION ScreenLoc (i AS INTEGER)
  37.     DECLARE SUB CheckForOverflow ()
  38.     DECLARE SUB ProcessKey (InputLine AS STRING, Ready AS INTEGER)
  39.     DECLARE SUB UpdateScreen ()
  40.   
  41.     DECLARE FUNCTION RandomNumber ()
  42.  
  43.  
  44.  
  45.   
  46.     COMMON SHARED Array() AS INTEGER
  47.     COMMON SHARED Quit
  48.     COMMON SHARED ScreenConst
  49.  
  50. REM $STATIC
  51. '$Page
  52. '
  53. '
  54. SUB BackgroundTask
  55.     '  This is the background task.
  56.  
  57.  
  58.     '--- update the screen
  59.     UpdateScreen
  60.  
  61.  
  62.     '--- check input to see if we need to change
  63.     '---    the screen scale or quit the program
  64.     ProcessKey InputLine$, Ready
  65.     IF Ready THEN
  66.         IF INT(VAL(InputLine$)) = 0 THEN
  67.             Quit = LEFT$(LCASE$(InputLine$), 1) = "q"
  68.         ELSE
  69.             ScreenConst = VAL(InputLine$)
  70.         END IF
  71.     END IF
  72.  
  73.       
  74.     '--- see if the bar is about to run off the
  75.     '---    right side of the screen
  76.     CheckForOverflow
  77.  
  78.  
  79. END SUB' BackgroundTask
  80.  
  81. '$Page
  82. '
  83. '
  84. SUB CheckForOverflow
  85.     '  Checks for screen overflow on the right side of
  86.     '  the screen.  Overflow is where the bar chart
  87.     '  reaches the 70th column.  NOTE: This procedure
  88.     '  affects the variable "Quit"
  89.  
  90.  
  91.     FOR i = ArrayLow TO ArrayHigh
  92.         Quit = Quit OR (ScreenLoc(i) > 70)
  93.     NEXT i
  94.  
  95.                         
  96. END SUB' CheckForOverflow
  97.  
  98. '$Page
  99. '
  100. '
  101. SUB ProcessKey (InputLine AS STRING, Ready AS INTEGER) STATIC
  102.     ' Processes any keyboard inputs; simulates the
  103.     ' LINE INPUT function.  NOTE: The only control key
  104.     ' it processes is [Enter].
  105.     ' STATIC definition is required to maintain the
  106.     ' values of the variables in this procedure.
  107.  
  108.  
  109.     '--- must save cursor position to prevent side
  110.     '       effects of the dual tasks from messing up
  111.     '       the display
  112.     OldX = POS(0)
  113.     OldY = CSRLIN
  114.  
  115.  
  116.     '--- see if a key is ready
  117.     InputChar$ = INKEY$
  118.  
  119.     '--- if not, toggle cursor
  120.     IF InputChar$ = "" THEN
  121.         CursorOn = NOT CursorOn
  122.       
  123.         LOCATE 6, 53 + LEN(TempInput$)
  124.         IF CursorOn THEN
  125.             PRINT "_";
  126.         ELSE
  127.             PRINT " ";
  128.         END IF
  129.  
  130.     '--- otherwise, process the key
  131.     ELSE
  132.         DO
  133.             IF ASC(InputChar$) = 13 THEN
  134.                 InputLine = TempInput$
  135.                 Ready = TRUE
  136.                 TempInput$ = ""
  137.                 LOCATE 6, 53
  138.                 PRINT SPACE$(LEN(InputLine) + 1);
  139.             ELSE
  140.                 LOCATE 6, 53 + LEN(TempInput$)
  141.                 PRINT InputChar$
  142.                 Ready = FALSE
  143.                 TempInput$ = TempInput$ + InputChar$
  144.             END IF
  145.  
  146.             InputChar$ = INKEY$
  147.  
  148.         LOOP UNTIL InputChar$ = ""
  149.       
  150.        
  151.         '--- print the character and move the cursor
  152.         LOCATE 6, 53 + LEN(TempInput$)
  153.         PRINT InputChar$; "_";
  154.  
  155.     END IF
  156.  
  157.  
  158.     '--- restore old cursor position 
  159.     LOCATE OldY, OldX
  160.  
  161.  
  162. END SUB' ProcessKey
  163.  
  164. DEFSNG A-Z
  165. DEFINT A-Z
  166. '$Page
  167. '
  168. FUNCTION ScreenLoc (i AS INTEGER)
  169.  
  170.  
  171.     ScreenLoc = Array(i) / MaxInt * 80 * ScreenConst
  172.  
  173.  
  174. END FUNCTION' ScreenLoc
  175.  
  176. '$Page
  177. '
  178. '
  179. SUB UpdateScreen
  180.  
  181.     '--- must save cursor position to prevent side
  182.     '       effects of the dual tasks from messing up
  183.     '       the display
  184.     OldX = POS(0)
  185.     OldY = CSRLIN
  186.  
  187.  
  188.     LOCATE 6, 15
  189.     PRINT ScreenConst
  190.  
  191.  
  192.     '--- make a bar for each element in the array
  193.     FOR i = ArrayLow TO ArrayHigh
  194.  
  195.         LOCATE 10 + i, 5
  196.         PRINT STRING$(ScreenLoc(i), BarChar$); Array(i)
  197.  
  198.     NEXT i
  199.  
  200.  
  201.     '--- restore old cursor position
  202.     LOCATE OldY, OldX
  203.  
  204.  
  205. END SUB' UpdateScreen
  206.  
  207.